Skip to content

Commit

Permalink
Add classId filter to objects as POC
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed Apr 16, 2024
1 parent 606f20e commit a56365b
Show file tree
Hide file tree
Showing 20 changed files with 259 additions and 40 deletions.
8 changes: 7 additions & 1 deletion config/filters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ services:
public: false

#Filter

#Common
Pimcore\Bundle\StudioApiBundle\Service\Filter\FilterLoaderInterface:
class: Pimcore\Bundle\StudioApiBundle\Service\Filter\Loader\TaggedIteratorAdapter

Expand All @@ -27,4 +29,8 @@ services:
tags: [ 'pimcore.studio_api.collection.filter' ]

Pimcore\Bundle\StudioApiBundle\Filter\PathFilter:
tags: [ 'pimcore.studio_api.collection.filter' ]
tags: [ 'pimcore.studio_api.collection.filter' ]

# DataObject
Pimcore\Bundle\StudioApiBundle\Filter\DataObject\ClassIdFilter:
tags: [ 'pimcore.studio_api.collection.filter' ]
47 changes: 47 additions & 0 deletions src/Attributes/Parameters/Query/ClassIdParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query;

use Attribute;
use OpenApi\Attributes\QueryParameter;
use OpenApi\Attributes\Schema;
use Pimcore\Model\DataObject\ClassDefinition;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class ClassIdParameter 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,
in: 'query',
required: false,
schema: new Schema(type: 'string', enum: $classNames, example: null),
);
}
}
6 changes: 3 additions & 3 deletions src/Controller/Api/Assets/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
use Pimcore\Bundle\StudioApiBundle\Controller\Api\AbstractApiController;
use Pimcore\Bundle\StudioApiBundle\Controller\Trait\PaginatedResponseTrait;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset;
use Pimcore\Bundle\StudioApiBundle\Dto\Collection;
use Pimcore\Bundle\StudioApiBundle\Dto\Filter\Parameters;
use Pimcore\Bundle\StudioApiBundle\Exception\InvalidQueryTypeException;
use Pimcore\Bundle\StudioApiBundle\Service\AssetSearchServiceInterface;
use Pimcore\Bundle\StudioApiBundle\Service\Filter\FilterServiceInterface;
Expand Down Expand Up @@ -83,10 +83,10 @@ public function __construct(
/**
* @throws InvalidQueryTypeException
*/
public function getAssets(#[MapQueryString] Collection $collection): JsonResponse
public function getAssets(#[MapQueryString] Parameters $parameters): JsonResponse
{
/** @var AssetQuery $assetQuery */
$assetQuery = $this->filterService->applyCollectionFilter($collection, 'asset');
$assetQuery = $this->filterService->applyCollectionFilter($parameters, 'asset');

$result = $this->assetSearchService->searchAssets($assetQuery);

Expand Down
8 changes: 5 additions & 3 deletions src/Controller/Api/DataObjects/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use OpenApi\Attributes\Get;
use OpenApi\Attributes\JsonContent;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\ClassIdParameter;
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 All @@ -31,8 +32,8 @@
use Pimcore\Bundle\StudioApiBundle\Config\Tags;
use Pimcore\Bundle\StudioApiBundle\Controller\Api\AbstractApiController;
use Pimcore\Bundle\StudioApiBundle\Controller\Trait\PaginatedResponseTrait;
use Pimcore\Bundle\StudioApiBundle\Dto\Collection;
use Pimcore\Bundle\StudioApiBundle\Dto\DataObject;
use Pimcore\Bundle\StudioApiBundle\Dto\Filter\DataObjectParameters;
use Pimcore\Bundle\StudioApiBundle\Exception\InvalidQueryTypeException;
use Pimcore\Bundle\StudioApiBundle\Service\DataObjectSearchServiceInterface;
use Pimcore\Bundle\StudioApiBundle\Service\Filter\FilterServiceInterface;
Expand Down Expand Up @@ -74,6 +75,7 @@ public function __construct(
#[PathParameter]
#[PathIncludeParentParameter]
#[PathIncludeDescendantsParameter]
#[ClassIdParameter]
#[SuccessResponse(
description: 'Paginated data objects with total count as header param',
content: new JsonContent(ref: DataObject::class)
Expand All @@ -83,11 +85,11 @@ public function __construct(
/**
* @throws InvalidQueryTypeException
*/
public function getDataObjects(#[MapQueryString] Collection $collection): JsonResponse
public function getDataObjects(#[MapQueryString] DataObjectParameters $parameters): JsonResponse
{

/** @var DataObjectQuery $dataObjectQuery */
$dataObjectQuery = $this->filterService->applyCollectionFilter($collection, 'dataObject');
$dataObjectQuery = $this->filterService->applyCollectionFilter($parameters, 'dataObject');

$result = $this->dataObjectSearchService->searchDataObjects($dataObjectQuery);

Expand Down
11 changes: 10 additions & 1 deletion src/Dto/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
{
public function __construct(
#[Property(description: 'ID', type: 'integer', example: 83)]
private int $id
private int $id,
#[Property(description: 'className', type: 'string', example: 'car')]
private string $className
) {

}
Expand All @@ -36,4 +38,11 @@ public function getId(): int
{
return $this->id;
}

public function getClassName(): string
{
return $this->className;
}


}
48 changes: 48 additions & 0 deletions src/Dto/Filter/DataObjectParameters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\Dto\Filter;

final readonly class DataObjectParameters extends Parameters implements DataObjectParametersInterface
{
public function __construct(
int $page = 1,
int $pageSize = 10,
?int $parentId = null,
?string $idSearchTerm = null,
?string $excludeFolders = null,
?string $path = null,
?string $pathIncludeParent = null,
?string $pathIncludeDescendants = null,
private ?string $classId = null
) {
parent::__construct(
$page,
$pageSize,
$parentId,
$idSearchTerm,
$excludeFolders,
$path,
$pathIncludeParent,
$pathIncludeDescendants
);
}

public function getClassId(): ?string
{
return $this->classId;
}
}
22 changes: 22 additions & 0 deletions src/Dto/Filter/DataObjectParametersInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\Dto\Filter;

interface DataObjectParametersInterface extends ParametersInterface
{
public function getClassId(): ?string;
}
4 changes: 2 additions & 2 deletions src/Dto/Collection.php → src/Dto/Filter/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\Dto;
namespace Pimcore\Bundle\StudioApiBundle\Dto\Filter;

use Symfony\Component\Validator\Constraints\NotBlank;

final readonly class Collection
readonly class Parameters implements ParametersInterface
{
public function __construct(
#[NotBlank]
Expand Down
35 changes: 35 additions & 0 deletions src/Dto/Filter/ParametersInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\Dto\Filter;

interface ParametersInterface
{
public function getPage(): int;

public function getPageSize(): int;

public function getParentId(): ?int;

public function getIdSearchTerm(): ?string;
public function getExcludeFolders(): ?bool;

public function getPath(): ?string;

public function getPathIncludeParent(): ?bool;

public function getPathIncludeDescendants(): ?bool;
}
39 changes: 39 additions & 0 deletions src/Filter/DataObject/ClassIdFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\Filter\DataObject;

use Pimcore\Bundle\StudioApiBundle\Dto\Filter\DataObjectParametersInterface;
use Pimcore\Bundle\StudioApiBundle\Dto\Filter\ParametersInterface;
use Pimcore\Bundle\StudioApiBundle\Filter\FilterInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\DataObjectQuery;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\QueryInterface;

final class ClassIdFilter implements FilterInterface
{
public function apply(ParametersInterface $parameters, QueryInterface $query): QueryInterface
{
if(
!$parameters instanceof DataObjectParametersInterface ||
!$query instanceof DataObjectQuery ||
!$parameters->getClassId()
) {
return $query;
}

return $query->setClassDefinitionId($parameters->getClassId());
}
}
7 changes: 4 additions & 3 deletions src/Filter/ExcludeFolderFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@

namespace Pimcore\Bundle\StudioApiBundle\Filter;

use Pimcore\Bundle\StudioApiBundle\Dto\Collection;
use Pimcore\Bundle\StudioApiBundle\Dto\Filter\Parameters;
use Pimcore\Bundle\StudioApiBundle\Dto\Filter\ParametersInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\QueryInterface;

final class ExcludeFolderFilter implements FilterInterface
{
public function apply(Collection $collection, QueryInterface $query): QueryInterface
public function apply(ParametersInterface $parameters, QueryInterface $query): QueryInterface
{
$excludeFolders = $collection->getExcludeFolders();
$excludeFolders = $parameters->getExcludeFolders();
if(!$excludeFolders) {
return $query;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Filter/FilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

namespace Pimcore\Bundle\StudioApiBundle\Filter;

use Pimcore\Bundle\StudioApiBundle\Dto\Collection;
use Pimcore\Bundle\StudioApiBundle\Dto\Filter\Parameters;
use Pimcore\Bundle\StudioApiBundle\Dto\Filter\ParametersInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\QueryInterface;

interface FilterInterface
{
public function apply(Collection $collection, QueryInterface $query): QueryInterface;
public function apply(ParametersInterface $parameters, QueryInterface $query): QueryInterface;
}
7 changes: 4 additions & 3 deletions src/Filter/IdSearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@

namespace Pimcore\Bundle\StudioApiBundle\Filter;

use Pimcore\Bundle\StudioApiBundle\Dto\Collection;
use Pimcore\Bundle\StudioApiBundle\Dto\Filter\Parameters;
use Pimcore\Bundle\StudioApiBundle\Dto\Filter\ParametersInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\QueryInterface;

final class IdSearchFilter implements FilterInterface
{
public function apply(Collection $collection, QueryInterface $query): QueryInterface
public function apply(ParametersInterface $parameters, QueryInterface $query): QueryInterface
{
$idSearchTerm = $collection->getIdSearchTerm();
$idSearchTerm = $parameters->getIdSearchTerm();

if(!$idSearchTerm) {
return $query;
Expand Down
7 changes: 4 additions & 3 deletions src/Filter/PageFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

namespace Pimcore\Bundle\StudioApiBundle\Filter;

use Pimcore\Bundle\StudioApiBundle\Dto\Collection;
use Pimcore\Bundle\StudioApiBundle\Dto\Filter\Parameters;
use Pimcore\Bundle\StudioApiBundle\Dto\Filter\ParametersInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\QueryInterface;

final class PageFilter implements FilterInterface
{
public function apply(Collection $collection, QueryInterface $query): QueryInterface
public function apply(ParametersInterface $parameters, QueryInterface $query): QueryInterface
{
return $query->setPage($collection->getPage());
return $query->setPage($parameters->getPage());
}
}
Loading

0 comments on commit a56365b

Please sign in to comment.