-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(attributes): AsFilter attribute
- Loading branch information
1 parent
d654fe3
commit a106327
Showing
6 changed files
with
141 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 Sylius\Component\Grid\Attribute; | ||
|
||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
final class AsFilter | ||
{ | ||
public const SERVICE_TAG = 'sylius.grid.filter'; | ||
|
||
/** | ||
* @param class-string $formType The form type class name to use for filter rendering | ||
*/ | ||
public function __construct( | ||
private readonly string $formType, | ||
private readonly ?string $type = null, | ||
) { | ||
} | ||
|
||
public function getFormType(): string | ||
{ | ||
return $this->formType; | ||
} | ||
|
||
public function getType(): ?string | ||
{ | ||
return $this->type; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
tests/Application/src/Filter/AttributeNationalityFilter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?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\Attribute\AsFilter; | ||
use Sylius\Component\Grid\Data\DataSourceInterface; | ||
use Sylius\Component\Grid\Filter\EntityFilter; | ||
use Sylius\Component\Grid\Filtering\FilterInterface; | ||
|
||
#[AsFilter( | ||
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
39
tests/Application/src/Grid/Builder/AttributeNationalityFilter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\Filter\AttributeNationalityFilter as GridNationalityFilter; | ||
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, GridNationalityFilter::class); | ||
|
||
$filter->setFormOptions(['class' => Nationality::class]); | ||
|
||
if (null !== $fields) { | ||
$filter->setOptions(['fields' => $fields]); | ||
} | ||
|
||
if (null !== $multiple) { | ||
$filter->addFormOption('multiple', $multiple); | ||
} | ||
|
||
return $filter; | ||
} | ||
} |