Skip to content

Commit

Permalink
Add filters based on type
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed Apr 17, 2024
1 parent 6a3cfc8 commit f2920f9
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 12 deletions.
2 changes: 1 addition & 1 deletion config/filters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ services:

# DataObject
Pimcore\Bundle\StudioApiBundle\Filter\DataObject\ClassIdFilter:
tags: [ 'pimcore.studio_api.collection.filter' ]
tags: [ 'pimcore.studio_api.collection.data_object.filter' ]
10 changes: 7 additions & 3 deletions src/DependencyInjection/CompilerPass/FilterPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ final class FilterPass implements CompilerPassInterface
public function process(ContainerBuilder $container): void
{
$taggedServices = array_keys(
$container->findTaggedServiceIds(
TaggedIteratorAdapter::FILTER_TAG
)
[
... $container->findTaggedServiceIds(TaggedIteratorAdapter::FILTER_TAG),
... $container->findTaggedServiceIds(TaggedIteratorAdapter::FILTER_ASSET_TAG),
... $container->findTaggedServiceIds(TaggedIteratorAdapter::FILTER_DATA_OBJECT_TAG),
... $container->findTaggedServiceIds(TaggedIteratorAdapter::FILTER_DOCUMENT_TAG)

]
);

foreach ($taggedServices as $environmentType) {
Expand Down
2 changes: 1 addition & 1 deletion src/Service/Filter/FilterLoaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
*/
interface FilterLoaderInterface
{
public function loadFilters(): array;
public function loadFilters(): Filters;
}
24 changes: 21 additions & 3 deletions src/Service/Filter/FilterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,31 @@ public function __construct(
/**
* @throws InvalidQueryTypeException
*/
public function applyCollectionFilter(Parameters $collection, string $type): QueryInterface
public function applyCollectionFilter(Parameters $parameters, string $type): QueryInterface
{
$query = $this->queryFactory->create($type);
foreach ($this->filterLoader->loadFilters() as $filter) {
$query = $filter->apply($collection, $query);
// apply default filters
$filters = $this->filterLoader->loadFilters();

foreach($filters->getFilters() as $filter) {
$query = $filter->apply($parameters, $query);
}

// apply type specific filters

foreach ($this->getTypeFilters($filters, $type) as $filter) {
$query = $filter->apply($parameters, $query);
}

return $query;
}

private function getTypeFilters(Filters $filters, string $type): array
{
return match($type) {

Check failure on line 56 in src/Service/Filter/FilterService.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, highest, false)

Match expression does not handle remaining value: string

Check failure on line 56 in src/Service/Filter/FilterService.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, highest, 11.x-dev as 11.99.9, true)

Match expression does not handle remaining value: string
'asset' => $filters->getAssetFilters(),
'dataObject' => $filters->getDataObjectFilters(),
'document' => $filters->getDocumentFilters()
};
}
}
2 changes: 1 addition & 1 deletion src/Service/Filter/FilterServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ interface FilterServiceInterface
/**
* @throws InvalidQueryTypeException
*/
public function applyCollectionFilter(Parameters $collection, string $type): QueryInterface;
public function applyCollectionFilter(Parameters $parameters, string $type): QueryInterface;
}
48 changes: 48 additions & 0 deletions src/Service/Filter/Filters.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\Service\Filter;

final readonly class Filters
{
public function __construct(
private array $filters = [],
private array $assetFilters = [],
private array $dataObjectFilters = [],
private array $documentFilters = []
) {
}

public function getFilters(): array
{
return $this->filters;
}

public function getAssetFilters(): array
{
return $this->assetFilters;
}

public function getDataObjectFilters(): array
{
return $this->dataObjectFilters;
}

public function getDocumentFilters(): array
{
return $this->documentFilters;
}
}
22 changes: 19 additions & 3 deletions src/Service/Filter/Loader/TaggedIteratorAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace Pimcore\Bundle\StudioApiBundle\Service\Filter\Loader;

use Pimcore\Bundle\StudioApiBundle\Service\Filter\FilterLoaderInterface;
use Pimcore\Bundle\StudioApiBundle\Service\Filter\Filters;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;

/**
Expand All @@ -26,14 +27,29 @@ final class TaggedIteratorAdapter implements FilterLoaderInterface
{
public const FILTER_TAG = 'pimcore.studio_api.collection.filter';

public const FILTER_ASSET_TAG = 'pimcore.studio_api.collection.asset.filter';
public const FILTER_DATA_OBJECT_TAG = 'pimcore.studio_api.collection.data_object.filter';
public const FILTER_DOCUMENT_TAG = 'pimcore.studio_api.collection.document.filter';

public function __construct(
#[TaggedIterator(self::FILTER_TAG)]
private readonly iterable $taggedServices
private readonly iterable $taggedFilters,
#[TaggedIterator(self::FILTER_ASSET_TAG)]
private readonly iterable $taggedAssetFilters,
#[TaggedIterator(self::FILTER_DATA_OBJECT_TAG)]
private readonly iterable $taggedDataObjectFilters,
#[TaggedIterator(self::FILTER_DOCUMENT_TAG)]
private readonly iterable $taggedDocumentFilters
) {
}

public function loadFilters(): array
public function loadFilters(): Filters
{
return [... $this->taggedServices];
return new Filters(
[... $this->taggedFilters],
[... $this->taggedAssetFilters],
[... $this->taggedDataObjectFilters],
[... $this->taggedDocumentFilters]
);
}
}

0 comments on commit f2920f9

Please sign in to comment.