forked from Sylius/Sylius
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[API][Products] Add custom Taxon filter
- Loading branch information
Showing
4 changed files
with
141 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* 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\Bundle\ApiBundle\Filter; | ||
|
||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractContextAwareFilter; | ||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; | ||
use ApiPlatform\Core\Bridge\Symfony\Routing\IriConverter; | ||
use Doctrine\ORM\QueryBuilder; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
|
||
/** | ||
* It should be replaced with generic collection filter after fixed: https://github.com/api-platform/api-platform/issues/1868 | ||
*/ | ||
final class TaxonFilter extends AbstractContextAwareFilter | ||
{ | ||
/** @var IriConverter */ | ||
private $iriConverter; | ||
|
||
/** @var ManagerRegistry */ | ||
protected $managerRegistry; | ||
|
||
public function __construct(IriConverter $iriConverter, ManagerRegistry $managerRegistry) | ||
{ | ||
parent::__construct($managerRegistry); | ||
$this->iriConverter = $iriConverter; | ||
} | ||
|
||
protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null) | ||
{ | ||
if ($property !== 'productTaxons') { | ||
return; | ||
} | ||
|
||
$taxon = $this->iriConverter->getItemFromIri($value); | ||
$alias = $queryBuilder->getRootAliases()[0]; | ||
$valueParameter = $queryNameGenerator->generateParameterName('taxon'); | ||
|
||
$queryBuilder | ||
->join(sprintf('%s.productTaxons', $alias), 'p') | ||
->andWhere(sprintf('p.taxon = :%s', $valueParameter)) | ||
->setParameter($valueParameter, $taxon) | ||
; | ||
} | ||
|
||
public function getDescription(string $resourceClass): array | ||
{ | ||
$description = []; | ||
$description["productTaxons"] = | ||
[ | ||
'type' => 'string', | ||
'required' => false, | ||
'property' => null, | ||
'swagger' => [ | ||
'name' =>'Product taxon filter', | ||
'description' => 'Get a collection of product with chosen taxon' | ||
] | ||
]; | ||
|
||
return $description; | ||
} | ||
} |
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
61 changes: 61 additions & 0 deletions
61
src/Sylius/Bundle/ApiBundle/spec/Filter/TaxonFilterSpec.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,61 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\Bundle\ApiBundle\Filter; | ||
|
||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; | ||
use ApiPlatform\Core\Bridge\Symfony\Routing\IriConverter; | ||
use Doctrine\ORM\QueryBuilder; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
use Sylius\Component\Core\Model\TaxonInterface; | ||
use Symfony\Bridge\Doctrine\ManagerRegistry; | ||
|
||
final class TaxonFilterSpec extends ObjectBehavior | ||
{ | ||
function let(IriConverter $iriConverter, ManagerRegistry $managerRegistry): void | ||
{ | ||
$this->beConstructedWith($iriConverter, $managerRegistry); | ||
} | ||
|
||
function it_adds_taxon_filter_if_property_is_product_taxon( | ||
IriConverter $iriConverter, | ||
TaxonInterface $taxon, | ||
QueryBuilder $queryBuilder, | ||
QueryNameGeneratorInterface $queryNameGenerator | ||
): void { | ||
$iriConverter->getItemFromIri('api/taxon')->willReturn($taxon); | ||
$queryBuilder->getRootAliases()->shouldNotBeCalled(); | ||
$queryNameGenerator->generateParameterName('taxon')->willReturn('taxon1'); | ||
|
||
$queryBuilder | ||
->join('o.productTaxons', 'p') | ||
->shouldBeCalled() | ||
->willReturn($queryBuilder) | ||
; | ||
|
||
$queryBuilder | ||
->andWhere('p.taxon = taxon1') | ||
->shouldBeCalled() | ||
->willReturn($queryBuilder) | ||
; | ||
|
||
$queryBuilder | ||
->setParameter('taxon1', $taxon) | ||
->shouldBeCalled() | ||
->willReturn($queryBuilder) | ||
; | ||
|
||
$this->filterProperty('productTaxon', 'api/taxon', $queryBuilder, $queryNameGenerator, Argument::type('string')); | ||
} | ||
} |