From 3ab999e356cf32d892aaad2999460431c513a3f6 Mon Sep 17 00:00:00 2001 From: Matthias Schuhmayer <38959016+mattamon@users.noreply.github.com> Date: Fri, 19 Apr 2024 11:03:48 +0200 Subject: [PATCH] Change from classId to class name (#34) * Change from classId to class name * Apply php-cs-fixer changes --------- Co-authored-by: mattamon --- config/filters.yaml | 2 +- ...IdParameter.php => ClassNameParameter.php} | 19 ++++++++----------- .../Api/DataObjects/CollectionController.php | 4 ++-- ...{ClassIdFilter.php => ClassNameFilter.php} | 6 +++--- .../Query/Filter/DataObjectParameters.php | 6 +++--- .../Filter/DataObjectParametersInterface.php | 2 +- .../GenericData/V1/DataObjectQuery.php | 8 ++++++-- 7 files changed, 24 insertions(+), 23 deletions(-) rename src/Attributes/Parameters/Query/{ClassIdParameter.php => ClassNameParameter.php} (64%) rename src/Filter/DataObject/{ClassIdFilter.php => ClassNameFilter.php} (87%) diff --git a/config/filters.yaml b/config/filters.yaml index 74302b254..f925a6b3d 100644 --- a/config/filters.yaml +++ b/config/filters.yaml @@ -32,5 +32,5 @@ services: tags: [ 'pimcore.studio_api.filter' ] # DataObject - Pimcore\Bundle\StudioApiBundle\Filter\DataObject\ClassIdFilter: + Pimcore\Bundle\StudioApiBundle\Filter\DataObject\ClassNameFilter: tags: [ 'pimcore.studio_api.data_object.filter' ] \ No newline at end of file diff --git a/src/Attributes/Parameters/Query/ClassIdParameter.php b/src/Attributes/Parameters/Query/ClassNameParameter.php similarity index 64% rename from src/Attributes/Parameters/Query/ClassIdParameter.php rename to src/Attributes/Parameters/Query/ClassNameParameter.php index c71816cc4..1d7cc8a15 100644 --- a/src/Attributes/Parameters/Query/ClassIdParameter.php +++ b/src/Attributes/Parameters/Query/ClassNameParameter.php @@ -22,26 +22,23 @@ use Pimcore\Model\DataObject\ClassDefinition; #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)] -final class ClassIdParameter extends QueryParameter +final class ClassNameParameter extends QueryParameter { public function __construct() { // TODO Find better concept for this $classDefinitions = new ClassDefinition\Listing(); - $classNames = []; - $description = 'Filter by class.'; - foreach ($classDefinitions->load() as $classDefinition) { - $classNames[] = $classDefinition->getId(); - $description .= '
' . $classDefinition->getId() . ' => ' . $classDefinition->getName(); - } - $description .= '

'; parent::__construct( - name: 'classId', - description: $description, + name: 'className', + description: 'Filter by class.', in: 'query', required: false, - schema: new Schema(type: 'string', enum: $classNames, example: null), + schema: new Schema( + type: 'string', + enum: array_map(static fn (ClassDefinition $def) => $def->getName(), $classDefinitions->load()), + example: null + ), ); } } diff --git a/src/Controller/Api/DataObjects/CollectionController.php b/src/Controller/Api/DataObjects/CollectionController.php index 0d8ca614b..e73d89372 100644 --- a/src/Controller/Api/DataObjects/CollectionController.php +++ b/src/Controller/Api/DataObjects/CollectionController.php @@ -17,7 +17,7 @@ namespace Pimcore\Bundle\StudioApiBundle\Controller\Api\DataObjects; use OpenApi\Attributes\Get; -use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\ClassIdParameter; +use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\ClassNameParameter; use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\ExcludeFoldersParameter; use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\IdSearchTermParameter; use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\PageParameter; @@ -75,7 +75,7 @@ public function __construct( #[PathParameter] #[PathIncludeParentParameter] #[PathIncludeDescendantsParameter] - #[ClassIdParameter] + #[ClassNameParameter] #[SuccessResponse( description: 'Paginated data objects with total count as header param', content: new CollectionJson(new DataObjectCollection()) diff --git a/src/Filter/DataObject/ClassIdFilter.php b/src/Filter/DataObject/ClassNameFilter.php similarity index 87% rename from src/Filter/DataObject/ClassIdFilter.php rename to src/Filter/DataObject/ClassNameFilter.php index 13642abed..63408e725 100644 --- a/src/Filter/DataObject/ClassIdFilter.php +++ b/src/Filter/DataObject/ClassNameFilter.php @@ -25,18 +25,18 @@ /** * @internal */ -final class ClassIdFilter implements FilterInterface +final class ClassNameFilter implements FilterInterface { public function apply(ParametersInterface $parameters, QueryInterface $query): QueryInterface { if( !$parameters instanceof DataObjectParametersInterface || !$query instanceof DataObjectQuery || - !$parameters->getClassId() + !$parameters->getClassName() ) { return $query; } - return $query->setClassDefinitionId($parameters->getClassId()); + return $query->setClassDefinitionName($parameters->getClassName()); } } diff --git a/src/Request/Query/Filter/DataObjectParameters.php b/src/Request/Query/Filter/DataObjectParameters.php index b652a3dde..b007fbca9 100644 --- a/src/Request/Query/Filter/DataObjectParameters.php +++ b/src/Request/Query/Filter/DataObjectParameters.php @@ -30,7 +30,7 @@ public function __construct( ?string $path = null, ?string $pathIncludeParent = null, ?string $pathIncludeDescendants = null, - private ?string $classId = null + private ?string $className = null ) { parent::__construct( $page, @@ -44,8 +44,8 @@ public function __construct( ); } - public function getClassId(): ?string + public function getClassName(): ?string { - return $this->classId; + return $this->className; } } diff --git a/src/Request/Query/Filter/DataObjectParametersInterface.php b/src/Request/Query/Filter/DataObjectParametersInterface.php index 0992e83ab..61b8c2c64 100644 --- a/src/Request/Query/Filter/DataObjectParametersInterface.php +++ b/src/Request/Query/Filter/DataObjectParametersInterface.php @@ -21,5 +21,5 @@ */ interface DataObjectParametersInterface extends ParametersInterface { - public function getClassId(): ?string; + public function getClassName(): ?string; } diff --git a/src/Service/GenericData/V1/DataObjectQuery.php b/src/Service/GenericData/V1/DataObjectQuery.php index 20b380b07..9e0b7213a 100644 --- a/src/Service/GenericData/V1/DataObjectQuery.php +++ b/src/Service/GenericData/V1/DataObjectQuery.php @@ -16,6 +16,7 @@ namespace Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1; +use Exception; use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\DataObject\DataObjectSearch; use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Basic\ExcludeFoldersFilter; use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Tree\ParentIdFilter; @@ -79,9 +80,12 @@ public function excludeFolders(): self return $this; } - public function setClassDefinitionId(string $classDefinitionId): self + /** + * @throws Exception + */ + public function setClassDefinitionName(string $classDefinitionId): self { - $classDefinition = $this->classDefinitionResolver->getById($classDefinitionId); + $classDefinition = $this->classDefinitionResolver->getByName($classDefinitionId); $this->search->setClassDefinition($classDefinition);