Skip to content

Commit

Permalink
Change from classId to class name (#34)
Browse files Browse the repository at this point in the history
* Change from classId to class name

* Apply php-cs-fixer changes

---------

Co-authored-by: mattamon <[email protected]>
  • Loading branch information
mattamon and mattamon authored Apr 19, 2024
1 parent 8ed80a3 commit 3ab999e
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 23 deletions.
2 changes: 1 addition & 1 deletion config/filters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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' ]
Original file line number Diff line number Diff line change
Expand Up @@ -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 .= '<br>' . $classDefinition->getId() . ' => ' . $classDefinition->getName();
}
$description .= '<br><br>';

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
),
);
}
}
4 changes: 2 additions & 2 deletions src/Controller/Api/DataObjects/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
6 changes: 3 additions & 3 deletions src/Request/Query/Filter/DataObjectParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -44,8 +44,8 @@ public function __construct(
);
}

public function getClassId(): ?string
public function getClassName(): ?string
{
return $this->classId;
return $this->className;
}
}
2 changes: 1 addition & 1 deletion src/Request/Query/Filter/DataObjectParametersInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
*/
interface DataObjectParametersInterface extends ParametersInterface
{
public function getClassId(): ?string;
public function getClassName(): ?string;
}
8 changes: 6 additions & 2 deletions src/Service/GenericData/V1/DataObjectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 3ab999e

Please sign in to comment.