-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilterLocatorTrait.php
56 lines (48 loc) · 1.61 KB
/
FilterLocatorTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?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\ParameterValidator;
use ApiPlatform\Exception\InvalidArgumentException;
use ApiPlatform\Metadata\FilterInterface;
use Psr\Container\ContainerInterface;
/**
* Manipulates filters with a backward compatibility between the new filter locator and the deprecated filter collection.
*
* @author Baptiste Meyer <[email protected]>
*
* @deprecated
*
* @internal
*/
trait FilterLocatorTrait
{
private ?ContainerInterface $filterLocator = null;
/**
* Sets a filter locator with a backward compatibility.
*/
private function setFilterLocator(?ContainerInterface $filterLocator, bool $allowNull = false): void
{
if ($filterLocator instanceof ContainerInterface || (null === $filterLocator && $allowNull)) {
$this->filterLocator = $filterLocator;
} else {
throw new InvalidArgumentException(sprintf('The "$filterLocator" argument is expected to be an implementation of the "%s" interface%s.', ContainerInterface::class, $allowNull ? ' or null' : ''));
}
}
/**
* Gets a filter with a backward compatibility.
*/
private function getFilter(string $filterId): null|FilterInterface
{
if ($this->filterLocator && $this->filterLocator->has($filterId)) {
return $this->filterLocator->get($filterId);
}
return null;
}
}