Skip to content

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
62 changes: 62 additions & 0 deletions src/Doctrine/Orm/Filter/IriFilter.php
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 [];
}
}
Empty file.
7 changes: 7 additions & 0 deletions src/Metadata/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ public function getValue(mixed $default = new ParameterNotFound()): mixed
return $this->extraProperties['_api_values'] ?? $default;
}

public function setValue(mixed $value): static
{
$this->extraProperties['_api_values'] = $value;

return $this;
}

/**
* @return array<string, mixed>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ private function getDefaultParameters(Operation $operation, string $resourceClas
['propertyNames' => $propertyNames, 'properties' => $properties] = $this->getProperties($resourceClass);
$parameters = $operation->getParameters() ?? new Parameters();
foreach ($parameters as $key => $parameter) {
if (null === $parameter->getProvider() && (($filter = $parameter->getFilter()) && $f instanceof ParameterProviderFilterInterface)) {
$parameters->add($key, $parameter->withProvider($filter->getParameterProvider()));
}

if (':property' === $key) {
foreach ($propertyNames as $property) {
$converted = $this->nameConverter?->denormalize($property) ?? $property;
Expand All @@ -131,7 +135,7 @@ private function getDefaultParameters(Operation $operation, string $resourceClas

$key = $parameter->getKey() ?? $key;

if (str_contains($key, ':property') || (($f = $parameter->getFilter()) && is_a($f, PropertiesAwareInterface::class, true)) || $parameter instanceof PropertiesAwareInterface) {
if (str_contains($key, ':property') && ((($f = $parameter->getFilter()) && is_a($f, PropertiesAwareInterface::class, true)) || $parameter instanceof PropertiesAwareInterface)) {
$p = [];
foreach ($propertyNames as $prop) {
$p[$this->nameConverter?->denormalize($prop) ?? $prop] = $prop;
Expand Down
52 changes: 52 additions & 0 deletions src/State/Provider/IriConverterParameterProvider.php
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;
}
}
6 changes: 6 additions & 0 deletions src/Symfony/Bundle/Resources/config/state/provider.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,11 @@
<argument type="service" id="api_platform.state_provider.parameter.inner" />
<argument type="tagged_locator" tag="api_platform.parameter_provider" index-by="key" />
</service>

<service id="api_platform.state_provider.parameter.iri_converter" class="ApiPlatform\State\Provider\IriConverterParameterProvider" public="false">
<argument type="service" id="api_platform.iri_converter"/>

<tag name="api_platform.parameter_provider" key="ApiPlatform\State\Provider\IriConverterParameterProvider" priority="-895" />
</service>
</services>
</container>
60 changes: 60 additions & 0 deletions tests/Fixtures/TestBundle/Document/Chicken.php
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 = null;

#[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;
}
}
75 changes: 75 additions & 0 deletions tests/Fixtures/TestBundle/Document/ChickenCoop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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\Odm\Filter\IriFilter;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\QueryParameter;
use ApiPlatform\Tests\Fixtures\TestBundle\Document\Chicken;
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())]
)]
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);
}

return $this;
}

public function removeChicken(Chicken $chicken): self
{
if ($this->chickens->removeElement($chicken)) {
if ($chicken->getChickenCoop() === $this) {
$chicken->setChickenCoop(null);
}
}

return $this;
}
}
63 changes: 63 additions & 0 deletions tests/Fixtures/TestBundle/Entity/Chicken.php
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]
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;
}
}
Loading