-
-
Notifications
You must be signed in to change notification settings - Fork 901
feat: iri search filter #7079
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: iri search filter #7079
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Doctrine\Orm\Filter; | ||
|
||
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface; | ||
use ApiPlatform\Metadata\OpenApiParameterFilterInterface; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\Metadata\Parameter; | ||
use ApiPlatform\Metadata\ParameterProviderFilterInterface; | ||
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter; | ||
use ApiPlatform\State\Provider\IriConverterParameterProvider; | ||
use Doctrine\ORM\QueryBuilder; | ||
|
||
class IriFilter implements FilterInterface, OpenApiParameterFilterInterface, ParameterProviderFilterInterface | ||
{ | ||
public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void | ||
{ | ||
if (!$parameter = $context['parameter'] ?? null) { | ||
return; | ||
} | ||
|
||
$value = $parameter->getValue(); | ||
if (!\is_array($value)) { | ||
$value = [$value]; | ||
} | ||
|
||
$property = $parameter->getProperty(); | ||
$alias = $queryBuilder->getRootAliases()[0]; | ||
$parameterName = $queryNameGenerator->generateParameterName($property); | ||
|
||
$queryBuilder | ||
->join(\sprintf('%s.%s', $alias, $property), $parameterName) | ||
->andWhere(\sprintf('%s IN(:%s)', $parameterName, $parameterName)) | ||
->setParameter($parameterName, $value); | ||
} | ||
|
||
public static function getParameterProvider(): string | ||
{ | ||
return IriConverterParameterProvider::class; | ||
} | ||
|
||
public function getOpenApiParameters(Parameter $parameter): OpenApiParameter|array|null | ||
{ | ||
return new OpenApiParameter(name: $parameter->getKey().'[]', in: 'query', style: 'deepObject', explode: true); | ||
} | ||
|
||
public function getDescription(string $resourceClass): array | ||
{ | ||
return []; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\State\Provider; | ||
|
||
use ApiPlatform\Metadata\IriConverterInterface; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\Metadata\Parameter; | ||
use ApiPlatform\State\ParameterNotFound; | ||
use ApiPlatform\State\ParameterProviderInterface; | ||
|
||
/** | ||
* @author Vincent Amstoutz | ||
*/ | ||
final readonly class IriConverterParameterProvider implements ParameterProviderInterface | ||
{ | ||
public function __construct( | ||
private IriConverterInterface $iriConverter, | ||
) { | ||
} | ||
|
||
public function provide(Parameter $parameter, array $parameters = [], array $context = []): ?Operation | ||
{ | ||
$operation = $context['operation'] ?? null; | ||
if (!($value = $parameter->getValue()) || $value instanceof ParameterNotFound) { | ||
return $operation; | ||
} | ||
|
||
if (!\is_array($value)) { | ||
$value = [$value]; | ||
} | ||
|
||
$entities = []; | ||
foreach ($value as $v) { | ||
$entities[] = $this->iriConverter->getResourceFromIri($v, ['fetch_data' => false]); | ||
} | ||
|
||
$parameter->setValue($entities); | ||
|
||
return $operation; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,60 @@ | ||||||
<?php | ||||||
|
||||||
/* | ||||||
* This file is part of the API Platform project. | ||||||
* | ||||||
* (c) Kévin Dunglas <[email protected]> | ||||||
* | ||||||
* For the full copyright and license information, please view the LICENSE | ||||||
* file that was distributed with this source code. | ||||||
*/ | ||||||
|
||||||
declare(strict_types=1); | ||||||
|
||||||
namespace ApiPlatform\Tests\Fixtures\TestBundle\Document; | ||||||
|
||||||
use ApiPlatform\Metadata\Get; | ||||||
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; | ||||||
|
||||||
#[ODM\Document] | ||||||
#[Get] | ||||||
class Chicken | ||||||
{ | ||||||
#[ODM\Id] | ||||||
private string $id; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
#[ODM\Field(type: 'string')] | ||||||
private string $name; | ||||||
|
||||||
#[ODM\ReferenceOne(targetDocument: ChickenCoop::class, inversedBy: 'chickens')] | ||||||
private ?ChickenCoop $chickenCoop = null; | ||||||
|
||||||
public function getId(): ?string | ||||||
{ | ||||||
return $this->id; | ||||||
} | ||||||
|
||||||
public function getName(): ?string | ||||||
{ | ||||||
return $this->name; | ||||||
} | ||||||
|
||||||
public function setName(string $name): self | ||||||
{ | ||||||
$this->name = $name; | ||||||
|
||||||
return $this; | ||||||
} | ||||||
|
||||||
public function getChickenCoop(): ?ChickenCoop | ||||||
{ | ||||||
return $this->chickenCoop; | ||||||
} | ||||||
|
||||||
public function setChickenCoop(?ChickenCoop $chickenCoop): self | ||||||
{ | ||||||
$this->chickenCoop = $chickenCoop; | ||||||
|
||||||
return $this; | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,72 @@ | ||||||||||||
<?php | ||||||||||||
|
||||||||||||
/* | ||||||||||||
* This file is part of the API Platform project. | ||||||||||||
* | ||||||||||||
* (c) Kévin Dunglas <[email protected]> | ||||||||||||
* | ||||||||||||
* For the full copyright and license information, please view the LICENSE | ||||||||||||
* file that was distributed with this source code. | ||||||||||||
*/ | ||||||||||||
|
||||||||||||
declare(strict_types=1); | ||||||||||||
|
||||||||||||
namespace ApiPlatform\Tests\Fixtures\TestBundle\Document; | ||||||||||||
|
||||||||||||
use ApiPlatform\Doctrine\Orm\Filter\IriFilter; | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
use ApiPlatform\Metadata\GetCollection; | ||||||||||||
use ApiPlatform\Metadata\QueryParameter; | ||||||||||||
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Chicken; | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
use Doctrine\Common\Collections\ArrayCollection; | ||||||||||||
use Doctrine\Common\Collections\Collection; | ||||||||||||
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; | ||||||||||||
|
||||||||||||
#[ODM\Document] | ||||||||||||
#[GetCollection(normalizationContext: ['hydra_prefix' => false], parameters: ['chickens' => new QueryParameter(filter: new IriFilter())])] | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
class ChickenCoop | ||||||||||||
{ | ||||||||||||
#[ODM\Id] | ||||||||||||
private ?string $id = null; | ||||||||||||
|
||||||||||||
#[ODM\ReferenceMany(targetDocument: Chicken::class, mappedBy: 'chickenCoop')] | ||||||||||||
private Collection $chickens; | ||||||||||||
|
||||||||||||
public function __construct() | ||||||||||||
{ | ||||||||||||
$this->chickens = new ArrayCollection(); | ||||||||||||
} | ||||||||||||
|
||||||||||||
public function getId(): ?string | ||||||||||||
{ | ||||||||||||
return $this->id; | ||||||||||||
} | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* @return Collection<int, Chicken> | ||||||||||||
*/ | ||||||||||||
public function getChickens(): Collection | ||||||||||||
{ | ||||||||||||
return $this->chickens; | ||||||||||||
} | ||||||||||||
|
||||||||||||
public function addChicken(Chicken $chicken): self | ||||||||||||
{ | ||||||||||||
if (!$this->chickens->contains($chicken)) { | ||||||||||||
$this->chickens[] = $chicken; | ||||||||||||
$chicken->setChickenCoop($this); | ||||||||||||
Check failure on line 56 in tests/Fixtures/TestBundle/Document/ChickenCoop.php
|
||||||||||||
} | ||||||||||||
|
||||||||||||
return $this; | ||||||||||||
} | ||||||||||||
|
||||||||||||
public function removeChicken(Chicken $chicken): self | ||||||||||||
{ | ||||||||||||
if ($this->chickens->removeElement($chicken)) { | ||||||||||||
if ($chicken->getChickenCoop() === $this) { | ||||||||||||
Check failure on line 65 in tests/Fixtures/TestBundle/Document/ChickenCoop.php
|
||||||||||||
$chicken->setChickenCoop(null); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
return $this; | ||||||||||||
} | ||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -22,7 +22,7 @@ | |||||
use Symfony\Component\Serializer\Annotation\Groups; | ||||||
|
||||||
#[ApiResource] | ||||||
#[GetCollection] | ||||||
#[GetCollection()] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
#[Get] | ||||||
#[Post] | ||||||
#[ApiResource(uriTemplate: '/employees/{employeeId}/rooms/{roomId}/company/{companyId}', uriVariables: ['employeeId' => ['from_class' => Employee::class, 'from_property' => 'company']])] | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,63 @@ | ||||||
<?php | ||||||
|
||||||
/* | ||||||
* This file is part of the API Platform project. | ||||||
* | ||||||
* (c) Kévin Dunglas <[email protected]> | ||||||
* | ||||||
* For the full copyright and license information, please view the LICENSE | ||||||
* file that was distributed with this source code. | ||||||
*/ | ||||||
|
||||||
declare(strict_types=1); | ||||||
|
||||||
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity; | ||||||
|
||||||
use ApiPlatform\Metadata\Get; | ||||||
use Doctrine\ORM\Mapping as ORM; | ||||||
|
||||||
#[ORM\Entity] | ||||||
#[Get()] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
class Chicken | ||||||
{ | ||||||
#[ORM\Id] | ||||||
#[ORM\GeneratedValue] | ||||||
#[ORM\Column(type: 'integer')] | ||||||
private ?int $id = null; | ||||||
|
||||||
#[ORM\Column(type: 'string', length: 255)] | ||||||
private string $name; | ||||||
|
||||||
#[ORM\ManyToOne(targetEntity: ChickenCoop::class, inversedBy: 'chickens')] | ||||||
#[ORM\JoinColumn(nullable: false)] | ||||||
private ChickenCoop $chickenCoop; | ||||||
|
||||||
public function getId(): ?int | ||||||
{ | ||||||
return $this->id; | ||||||
} | ||||||
|
||||||
public function getName(): ?string | ||||||
{ | ||||||
return $this->name; | ||||||
} | ||||||
|
||||||
public function setName(string $name): self | ||||||
{ | ||||||
$this->name = $name; | ||||||
|
||||||
return $this; | ||||||
} | ||||||
|
||||||
public function getChickenCoop(): ?ChickenCoop | ||||||
{ | ||||||
return $this->chickenCoop; | ||||||
} | ||||||
|
||||||
public function setChickenCoop(?ChickenCoop $chickenCoop): self | ||||||
{ | ||||||
$this->chickenCoop = $chickenCoop; | ||||||
|
||||||
return $this; | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.