Skip to content

Commit

Permalink
feat(attributes): AsFilter attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
vasilvestre committed Oct 21, 2024
1 parent d654fe3 commit 4b58c75
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public function process(ContainerBuilder $container): void
}

foreach ($attributes as $attribute) {
if (isset($attribute['type'], $attribute['form_type'])) {
return;
}

if (null === $type && null === ($attribute['type'] ?? null)) {
throw new InvalidArgumentException(sprintf('Tagged grid filters needs to have "type" attributes or implements "%s".', TypeAwareFilterInterface::class));
}
Expand Down
10 changes: 10 additions & 0 deletions src/Bundle/DependencyInjection/SyliusGridExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
use Sylius\Bundle\GridBundle\SyliusGridBundle;
use Sylius\Component\Grid\Data\DataProviderInterface;
use Sylius\Component\Grid\Filtering\FilterInterface;
use Sylius\Component\Grid\Metadata\AsFilter;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
Expand Down Expand Up @@ -60,6 +62,14 @@ public function load(array $configs, ContainerBuilder $container): void
->addTag('sylius.grid')
;

$container->registerAttributeForAutoconfiguration(AsFilter::class, static function (ChildDefinition $definition, AsFilter $attribute, \ReflectionClass $reflector) {
$tagAttributes = [
'type' => $attribute->type,
'form_type' => $attribute->formType,
];
$definition->addTag('sylius.grid_filter', $tagAttributes);
});

$container->registerForAutoconfiguration(FilterInterface::class)
->addTag('sylius.grid_filter')
;
Expand Down
24 changes: 24 additions & 0 deletions src/Component/Metadata/AsFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Component\Grid\Metadata;

#[\Attribute(\Attribute::TARGET_CLASS)]
final class AsFilter
{
public function __construct(
public ?string $type = null,
public ?string $formType = null,
) {
}
}
6 changes: 6 additions & 0 deletions tests/Application/config/sylius/grids/book.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use App\Entity\Author;
use App\Entity\Book;
use App\Grid\Builder\AttributeNationalityFilter;
use App\Grid\Builder\NationalityFilter;
use Sylius\Bundle\GridBundle\Builder\Field\StringField;
use Sylius\Bundle\GridBundle\Builder\Filter\EntityFilter;
Expand All @@ -31,6 +32,11 @@
null,
['author.nationality'],
))
->addFilter(AttributeNationalityFilter::create(
'attribute_nationality',
null,
['author.nationality'],
))
->addFilter(StringFilter::create(
'currencyCode',
['price.currencyCode'],
Expand Down
36 changes: 36 additions & 0 deletions tests/Application/src/Filter/AttributeNationalityFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Filter;

use App\Grid\Type\NationalityFilterType;
use Sylius\Component\Grid\Data\DataSourceInterface;
use Sylius\Component\Grid\Filter\EntityFilter;
use Sylius\Component\Grid\Filtering\FilterInterface;
use Sylius\Component\Grid\Metadata\AsFilter;

#[AsFilter(
type: 'attribute_nationality',
formType: NationalityFilterType::class,
)]
final class AttributeNationalityFilter implements FilterInterface
{
public function __construct(private EntityFilter $decorated)
{
}

public function apply(DataSourceInterface $dataSource, string $name, $data, array $options): void
{
$this->decorated->apply($dataSource, $name, $data, $options);
}
}
39 changes: 39 additions & 0 deletions tests/Application/src/Grid/Builder/AttributeNationalityFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Grid\Builder;

use App\Entity\Nationality;
use App\Grid\Type\NationalityFilterType;
use Sylius\Bundle\GridBundle\Builder\Filter\Filter;
use Sylius\Bundle\GridBundle\Builder\Filter\FilterInterface;

final class AttributeNationalityFilter
{
public static function create(string $name, ?bool $multiple = null, ?array $fields = null): FilterInterface
{
$filter = Filter::create($name, $name);

$filter->setFormOptions(['class' => Nationality::class]);

if (null !== $fields) {
$filter->setOptions(['fields' => $fields]);
}

if (null !== $multiple) {
$filter->addFormOption('multiple', $multiple);
}

return $filter;
}
}

0 comments on commit 4b58c75

Please sign in to comment.