From 05c5668b21098069fc1779a4da95ddf073810fa1 Mon Sep 17 00:00:00 2001 From: Matthias Schuhmayer <38959016+mattamon@users.noreply.github.com> Date: Thu, 18 Apr 2024 14:44:23 +0200 Subject: [PATCH] Wrap collection response into an object and set total items (#31) * Wrap collection response into an object and set total items * Apply php-cs-fixer changes * Restructure * Rename property * Update minor stuff * CS fix * Remove unecessary brackets * Add DataObjectCollection * Apply php-cs-fixer changes --------- Co-authored-by: mattamon --- config/filters.yaml | 14 ++--- .../Response/Content/CollectionJson.php | 37 ++++++++++++ .../Response/Property/AssetCollection.php | 56 +++++++++++++++++++ .../Property/DataObjectCollection.php | 42 ++++++++++++++ .../Api/Assets/CollectionController.php | 31 +--------- .../Api/DataObjects/CollectionController.php | 7 +-- .../Trait/PaginatedResponseTrait.php | 3 +- src/Response/Asset/Image.php | 2 +- src/Response/Collection.php | 48 ++++++++++++++++ .../Filter/Loader/TaggedIteratorAdapter.php | 8 +-- 10 files changed, 203 insertions(+), 45 deletions(-) create mode 100644 src/Attributes/Response/Content/CollectionJson.php create mode 100644 src/Attributes/Response/Property/AssetCollection.php create mode 100644 src/Attributes/Response/Property/DataObjectCollection.php create mode 100644 src/Response/Collection.php diff --git a/config/filters.yaml b/config/filters.yaml index 7dc94788e..74302b254 100644 --- a/config/filters.yaml +++ b/config/filters.yaml @@ -14,23 +14,23 @@ services: class: Pimcore\Bundle\StudioApiBundle\Service\Filter\FilterService Pimcore\Bundle\StudioApiBundle\Filter\PageFilter: - tags: [ 'pimcore.studio_api.collection.filter' ] + tags: [ 'pimcore.studio_api.filter' ] Pimcore\Bundle\StudioApiBundle\Filter\PageSizeFilter: - tags: [ 'pimcore.studio_api.collection.filter' ] + tags: [ 'pimcore.studio_api.filter' ] Pimcore\Bundle\StudioApiBundle\Filter\ExcludeFolderFilter: - tags: [ 'pimcore.studio_api.collection.filter' ] + tags: [ 'pimcore.studio_api.filter' ] Pimcore\Bundle\StudioApiBundle\Filter\IdSearchFilter: - tags: [ 'pimcore.studio_api.collection.filter' ] + tags: [ 'pimcore.studio_api.filter' ] Pimcore\Bundle\StudioApiBundle\Filter\ParentIdFilter: - tags: [ 'pimcore.studio_api.collection.filter' ] + tags: [ 'pimcore.studio_api.filter' ] Pimcore\Bundle\StudioApiBundle\Filter\PathFilter: - tags: [ 'pimcore.studio_api.collection.filter' ] + tags: [ 'pimcore.studio_api.filter' ] # DataObject Pimcore\Bundle\StudioApiBundle\Filter\DataObject\ClassIdFilter: - tags: [ 'pimcore.studio_api.collection.data_object.filter' ] \ No newline at end of file + tags: [ 'pimcore.studio_api.data_object.filter' ] \ No newline at end of file diff --git a/src/Attributes/Response/Content/CollectionJson.php b/src/Attributes/Response/Content/CollectionJson.php new file mode 100644 index 000000000..11188cfeb --- /dev/null +++ b/src/Attributes/Response/Content/CollectionJson.php @@ -0,0 +1,37 @@ +serialize($data, 'json'); + $serialized = $serializer->serialize(new Collection($totalItems, $data), 'json'); return new JsonResponse($serialized, 200, [self::HEADER_TOTAL_ITEMS => $totalItems], true); } diff --git a/src/Response/Asset/Image.php b/src/Response/Asset/Image.php index f3aef2afe..21766f65a 100644 --- a/src/Response/Asset/Image.php +++ b/src/Response/Asset/Image.php @@ -37,7 +37,7 @@ public function __construct( private readonly bool $isVectorGraphic, #[Property(description: 'is animated', type: 'boolean', example: false)] private readonly bool $isAnimated, - #[Property(description: 'path to thumbnail', type: 'string', example: '/path/to/element/thumbnail.jpg')] + #[Property(description: 'path to thumbnail', type: 'string', example: '/path/to/element/hulk-smash.jpg')] private readonly string $thumbnailPath, string $iconName, bool $hasChildren, diff --git a/src/Response/Collection.php b/src/Response/Collection.php new file mode 100644 index 000000000..9d8c8e85c --- /dev/null +++ b/src/Response/Collection.php @@ -0,0 +1,48 @@ +totalItems; + } + + public function getItems(): array + { + return $this->items; + } +} diff --git a/src/Service/Filter/Loader/TaggedIteratorAdapter.php b/src/Service/Filter/Loader/TaggedIteratorAdapter.php index c863060e2..5572c599a 100644 --- a/src/Service/Filter/Loader/TaggedIteratorAdapter.php +++ b/src/Service/Filter/Loader/TaggedIteratorAdapter.php @@ -25,13 +25,13 @@ */ final class TaggedIteratorAdapter implements FilterLoaderInterface { - public const FILTER_TAG = 'pimcore.studio_api.collection.filter'; + public const FILTER_TAG = 'pimcore.studio_api.filter'; - public const FILTER_ASSET_TAG = 'pimcore.studio_api.collection.asset.filter'; + public const FILTER_ASSET_TAG = 'pimcore.studio_api.asset.filter'; - public const FILTER_DATA_OBJECT_TAG = 'pimcore.studio_api.collection.data_object.filter'; + public const FILTER_DATA_OBJECT_TAG = 'pimcore.studio_api.data_object.filter'; - public const FILTER_DOCUMENT_TAG = 'pimcore.studio_api.collection.document.filter'; + public const FILTER_DOCUMENT_TAG = 'pimcore.studio_api.document.filter'; public function __construct( #[TaggedIterator(self::FILTER_TAG)]