diff --git a/src/Dto/Asset.php b/src/Dto/Asset.php index a51a02f8b..d237d53fb 100644 --- a/src/Dto/Asset.php +++ b/src/Dto/Asset.php @@ -20,22 +20,22 @@ class Asset extends Element { - private ?string $iconName; + private ?string $iconName = null; - private ?bool $hasChildren; + private ?bool $hasChildren = null; - private ?string $type; + private ?string $type = null; private ?string $filename = null; - private ?string $mimeType; + private ?string $mimeType = null; #[ApiProperty(genId: false)] - private ?array $metaData; + private ?array $metaData = null; - private ?bool $workflowWithPermissions; + private ?bool $workflowWithPermissions = null; - private ?string $fullPath; + private ?string $fullPath = null; public function getIconName(): ?string { @@ -94,7 +94,7 @@ public function getMetaData(): ?array public function getHasMetaData(): bool { - return count($this->metaData) > 0; + return $this->metaData && count($this->metaData) > 0; } public function setMetaData(?array $metaData): void diff --git a/src/Dto/Element.php b/src/Dto/Element.php index 083bd509b..9719d0bd6 100644 --- a/src/Dto/Element.php +++ b/src/Dto/Element.php @@ -23,21 +23,21 @@ class Element { private ?int $parentId = null; - private ?string $path; + private ?string $path = null; - private ?int $userOwner; + private ?int $userOwner = null; - private ?int $userModification; + private ?int $userModification = null; - private ?string $locked; + private ?string $locked = null; - private ?bool $isLocked; + private ?bool $isLocked = null; - private ?int $creationDate; + private ?int $creationDate = null; - private ?int $modificationDate; + private ?int $modificationDate = null; - private ?Permissions $permissions; + private ?Permissions $permissions = null; public function __construct( private readonly int $id, @@ -133,12 +133,12 @@ public function setModificationDate(?int $modificationDate): void } #[ApiProperty(genId: false)] - public function getPermissions(): Permissions + public function getPermissions(): ?Permissions { return $this->permissions; } - public function setPermissions(Permissions $permissions): void + public function setPermissions(?Permissions $permissions): void { $this->permissions = $permissions; } diff --git a/src/Service/AssetService.php b/src/Service/AssetService.php index db103e7cf..595cf2a72 100644 --- a/src/Service/AssetService.php +++ b/src/Service/AssetService.php @@ -45,9 +45,25 @@ public function handleAsset(int $id, Asset $data): \Pimcore\Model\Asset */ private function setAssetData(\Pimcore\Model\Asset $asset, Asset $data): \Pimcore\Model\Asset { - $asset->setFilename($data->getFilename() ?: $asset->getFilename()); - $asset->save(); - + $getters = array_filter(get_class_methods($data), static fn($method) => str_starts_with($method, 'get')); + foreach($getters as $getter) { + $property = lcfirst(substr($getter, 3)); + $value = $data->$getter(); + if($value === null) { + continue; + } + $this->propertySetter($asset, $property, $value); + } return $asset; } + + + + private function propertySetter(\Pimcore\Model\Asset $asset, string $property, mixed $value): void + { + $setter = 'set' . ucfirst($property); + if (method_exists($asset, $setter)) { + $asset->$setter($value); + } + } } diff --git a/src/State/Asset/Processor.php b/src/State/Asset/Processor.php index 97bfe069a..ac4905df0 100644 --- a/src/State/Asset/Processor.php +++ b/src/State/Asset/Processor.php @@ -22,6 +22,7 @@ use Pimcore\Bundle\StudioApiBundle\Dto\Asset; use Pimcore\Bundle\StudioApiBundle\Service\AssetServiceInterface; use Pimcore\Bundle\StudioApiBundle\Service\ModelData\V1\Hydrator\AssetHydratorServiceInterface; +use Pimcore\Model\Element\DuplicateFullPathException; final readonly class Processor implements ProcessorInterface { @@ -32,6 +33,9 @@ public function __construct( ) { } + /** + * @throws DuplicateFullPathException + */ public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): Asset { if ( @@ -44,6 +48,8 @@ public function process(mixed $data, Operation $operation, array $uriVariables = $asset = $this->assetService->handleAsset($data->getId(), $data); + $asset->save(); + return $this->assetHydratorService->hydrate($asset); } }