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\Bridge\Doctrine\Common\Filter; |
||
15 | |||
16 | use ApiPlatform\Core\Api\IriConverterInterface; |
||
17 | use ApiPlatform\Core\Bridge\Doctrine\Common\PropertyHelperTrait; |
||
18 | use ApiPlatform\Core\Exception\InvalidArgumentException; |
||
19 | use Psr\Log\LoggerInterface; |
||
20 | use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
||
21 | |||
22 | /** |
||
23 | * Trait for filtering the collection by given properties. |
||
24 | * |
||
25 | * @author Kévin Dunglas <[email protected]> |
||
26 | * @author Alan Poulain <[email protected]> |
||
27 | */ |
||
28 | trait SearchFilterTrait |
||
29 | { |
||
30 | use PropertyHelperTrait; |
||
31 | |||
32 | protected $iriConverter; |
||
33 | protected $propertyAccessor; |
||
34 | protected $identifiersExtractor; |
||
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | public function getDescription(string $resourceClass): array |
||
40 | { |
||
41 | $description = []; |
||
42 | |||
43 | $properties = $this->getProperties(); |
||
44 | if (null === $properties) { |
||
45 | $properties = array_fill_keys($this->getClassMetadata($resourceClass)->getFieldNames(), null); |
||
46 | } |
||
47 | |||
48 | foreach ($properties as $property => $strategy) { |
||
49 | if (!$this->isPropertyMapped($property, $resourceClass, true)) { |
||
50 | continue; |
||
51 | } |
||
52 | |||
53 | if ($this->isPropertyNested($property, $resourceClass)) { |
||
54 | $propertyParts = $this->splitPropertyParts($property, $resourceClass); |
||
55 | $field = $propertyParts['field']; |
||
56 | $metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']); |
||
57 | } else { |
||
58 | $field = $property; |
||
59 | $metadata = $this->getClassMetadata($resourceClass); |
||
60 | } |
||
61 | |||
62 | $propertyName = $this->normalizePropertyName($property); |
||
63 | if ($metadata->hasField($field)) { |
||
64 | $typeOfField = $this->getType($metadata->getTypeOfField($field)); |
||
65 | $strategy = $this->getProperties()[$property] ?? self::STRATEGY_EXACT; |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
66 | $filterParameterNames = [$propertyName]; |
||
67 | |||
68 | if (self::STRATEGY_EXACT === $strategy) { |
||
69 | $filterParameterNames[] = $propertyName.'[]'; |
||
70 | } |
||
71 | |||
72 | foreach ($filterParameterNames as $filterParameterName) { |
||
73 | $description[$filterParameterName] = [ |
||
74 | 'property' => $propertyName, |
||
75 | 'type' => $typeOfField, |
||
76 | 'required' => false, |
||
77 | 'strategy' => $strategy, |
||
78 | 'is_collection' => '[]' === substr((string) $filterParameterName, -2), |
||
79 | ]; |
||
80 | } |
||
81 | } elseif ($metadata->hasAssociation($field)) { |
||
82 | $filterParameterNames = [ |
||
83 | $propertyName, |
||
84 | $propertyName.'[]', |
||
85 | ]; |
||
86 | |||
87 | foreach ($filterParameterNames as $filterParameterName) { |
||
88 | $description[$filterParameterName] = [ |
||
89 | 'property' => $propertyName, |
||
90 | 'type' => 'string', |
||
91 | 'required' => false, |
||
92 | 'strategy' => self::STRATEGY_EXACT, |
||
93 | 'is_collection' => '[]' === substr((string) $filterParameterName, -2), |
||
94 | ]; |
||
95 | } |
||
96 | } |
||
97 | } |
||
98 | |||
99 | return $description; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Converts a Doctrine type in PHP type. |
||
104 | */ |
||
105 | abstract protected function getType(string $doctrineType): string; |
||
106 | |||
107 | abstract protected function getProperties(): ?array; |
||
108 | |||
109 | abstract protected function getLogger(): LoggerInterface; |
||
110 | |||
111 | abstract protected function getIriConverter(): IriConverterInterface; |
||
112 | |||
113 | abstract protected function getPropertyAccessor(): PropertyAccessorInterface; |
||
114 | |||
115 | abstract protected function normalizePropertyName($property); |
||
116 | |||
117 | /** |
||
118 | * Gets the ID from an IRI or a raw ID. |
||
119 | */ |
||
120 | protected function getIdFromValue(string $value) |
||
121 | { |
||
122 | try { |
||
123 | $item = $this->getIriConverter()->getItemFromIri($value, ['fetch_data' => false]); |
||
124 | |||
125 | return $this->getPropertyAccessor()->getValue($item, 'id'); |
||
126 | } catch (InvalidArgumentException $e) { |
||
127 | // Do nothing, return the raw value |
||
128 | } |
||
129 | |||
130 | return $value; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Normalize the values array. |
||
135 | */ |
||
136 | protected function normalizeValues(array $values, string $property): ?array |
||
137 | { |
||
138 | foreach ($values as $key => $value) { |
||
139 | if (!\is_int($key) || !\is_string($value)) { |
||
140 | unset($values[$key]); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | if (empty($values)) { |
||
145 | $this->getLogger()->notice('Invalid filter ignored', [ |
||
146 | 'exception' => new InvalidArgumentException(sprintf('At least one value is required, multiple values should be in "%1$s[]=firstvalue&%1$s[]=secondvalue" format', $property)), |
||
147 | ]); |
||
148 | |||
149 | return null; |
||
150 | } |
||
151 | |||
152 | return array_values($values); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * When the field should be an integer, check that the given value is a valid one. |
||
157 | * |
||
158 | * @param mixed|null $type |
||
159 | */ |
||
160 | protected function hasValidValues(array $values, $type = null): bool |
||
161 | { |
||
162 | foreach ($values as $key => $value) { |
||
163 | if (self::DOCTRINE_INTEGER_TYPE === $type && null !== $value && false === filter_var($value, FILTER_VALIDATE_INT)) { |
||
0 ignored issues
–
show
|
|||
164 | return false; |
||
165 | } |
||
166 | } |
||
167 | |||
168 | return true; |
||
169 | } |
||
170 | } |
||
171 |