1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\Api; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Exception\InvalidArgumentException; |
17
|
|
|
use Psr\Container\ContainerInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Manipulates filters with a backward compatibility between the new filter locator and the deprecated filter collection. |
21
|
|
|
* |
22
|
|
|
* @author Baptiste Meyer <[email protected]> |
23
|
|
|
* |
24
|
|
|
* @internal |
25
|
|
|
*/ |
26
|
|
|
trait FilterLocatorTrait |
27
|
|
|
{ |
28
|
|
|
private $filterLocator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Sets a filter locator with a backward compatibility. |
32
|
|
|
* |
33
|
|
|
* @param ContainerInterface|FilterCollection|null $filterLocator |
34
|
|
|
* @param bool $allowNull |
35
|
|
|
*/ |
36
|
|
|
private function setFilterLocator($filterLocator = null, bool $allowNull = false) |
37
|
|
|
{ |
38
|
|
|
if ($filterLocator instanceof ContainerInterface || $filterLocator instanceof FilterCollection || null === $filterLocator && $allowNull) { |
39
|
|
|
if ($filterLocator instanceof FilterCollection) { |
40
|
|
|
@trigger_error(sprintf('The %s class is deprecated since version 2.1 and will be removed in 3.0. Provide an implementation of %s instead.', FilterCollection::class, ContainerInterface::class), E_USER_DEPRECATED); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->filterLocator = $filterLocator; |
44
|
|
|
} else { |
45
|
|
|
throw new InvalidArgumentException(sprintf('The "$filterLocator" argument is expected to be an implementation of the "%s" interface%s.', ContainerInterface::class, $allowNull ? ' or null' : '')); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Gets a filter with a backward compatibility. |
51
|
|
|
* |
52
|
|
|
* @param string $filterId |
53
|
|
|
* |
54
|
|
|
* @return FilterInterface|null |
55
|
|
|
*/ |
56
|
|
|
private function getFilter(string $filterId) |
57
|
|
|
{ |
58
|
|
|
if ($this->filterLocator instanceof ContainerInterface && $this->filterLocator->has($filterId)) { |
59
|
|
|
return $this->filterLocator->get($filterId); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($this->filterLocator instanceof FilterCollection && $this->filterLocator->offsetExists($filterId)) { |
63
|
|
|
return $this->filterLocator->offsetGet($filterId); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: