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\Bridge\Doctrine\Common\PropertyHelperTrait; |
||
17 | use ApiPlatform\Core\Exception\InvalidArgumentException; |
||
18 | use Psr\Log\LoggerInterface; |
||
19 | |||
20 | /** |
||
21 | * Trait for filtering the collection by numeric values. |
||
22 | * |
||
23 | * @author Amrouche Hamza <[email protected]> |
||
24 | * @author Teoh Han Hui <[email protected]> |
||
25 | * @author Alan Poulain <[email protected]> |
||
26 | */ |
||
27 | trait NumericFilterTrait |
||
28 | { |
||
29 | use PropertyHelperTrait; |
||
30 | |||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | */ |
||
34 | public function getDescription(string $resourceClass): array |
||
35 | { |
||
36 | $description = []; |
||
37 | |||
38 | $properties = $this->getProperties(); |
||
39 | if (null === $properties) { |
||
40 | $properties = array_fill_keys($this->getClassMetadata($resourceClass)->getFieldNames(), null); |
||
41 | } |
||
42 | |||
43 | foreach ($properties as $property => $unused) { |
||
44 | if (!$this->isPropertyMapped($property, $resourceClass) || !$this->isNumericField($property, $resourceClass)) { |
||
45 | continue; |
||
46 | } |
||
47 | |||
48 | $propertyName = $this->normalizePropertyName($property); |
||
49 | $filterParameterNames = [$propertyName, $propertyName.'[]']; |
||
50 | foreach ($filterParameterNames as $filterParameterName) { |
||
51 | $description[$filterParameterName] = [ |
||
52 | 'property' => $propertyName, |
||
53 | 'type' => $this->getType((string) $this->getDoctrineFieldType($property, $resourceClass)), |
||
54 | 'required' => false, |
||
55 | 'is_collection' => '[]' === substr((string) $filterParameterName, -2), |
||
56 | ]; |
||
57 | } |
||
58 | } |
||
59 | |||
60 | return $description; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Gets the PHP type corresponding to this Doctrine type. |
||
65 | */ |
||
66 | abstract protected function getType(string $doctrineType = null): string; |
||
67 | |||
68 | abstract protected function getProperties(): ?array; |
||
69 | |||
70 | abstract protected function getLogger(): LoggerInterface; |
||
71 | |||
72 | abstract protected function normalizePropertyName($property); |
||
73 | |||
74 | /** |
||
75 | * Determines whether the given property refers to a numeric field. |
||
76 | */ |
||
77 | protected function isNumericField(string $property, string $resourceClass): bool |
||
78 | { |
||
79 | return isset(self::DOCTRINE_NUMERIC_TYPES[(string) $this->getDoctrineFieldType($property, $resourceClass)]); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
80 | } |
||
81 | |||
82 | protected function normalizeValues($value, string $property): ?array |
||
83 | { |
||
84 | if (!is_numeric($value) && (!\is_array($value) || !$this->isNumericArray($value))) { |
||
85 | $this->getLogger()->notice('Invalid filter ignored', [ |
||
86 | 'exception' => new InvalidArgumentException(sprintf('Invalid numeric value for "%s" property', $property)), |
||
87 | ]); |
||
88 | |||
89 | return null; |
||
90 | } |
||
91 | |||
92 | $values = (array) $value; |
||
93 | |||
94 | foreach ($values as $key => $val) { |
||
95 | if (!\is_int($key)) { |
||
96 | unset($values[$key]); |
||
97 | |||
98 | continue; |
||
99 | } |
||
100 | $values[$key] = $val + 0; // coerce $val to the right type. |
||
101 | } |
||
102 | |||
103 | if (empty($values)) { |
||
104 | $this->getLogger()->notice('Invalid filter ignored', [ |
||
105 | 'exception' => new InvalidArgumentException(sprintf('At least one value is required, multiple values should be in "%1$s[]=firstvalue&%1$s[]=secondvalue" format', $property)), |
||
106 | ]); |
||
107 | |||
108 | return null; |
||
109 | } |
||
110 | |||
111 | return array_values($values); |
||
112 | } |
||
113 | |||
114 | protected function isNumericArray(array $values): bool |
||
115 | { |
||
116 | foreach ($values as $value) { |
||
117 | if (!is_numeric($value)) { |
||
118 | return false; |
||
119 | } |
||
120 | } |
||
121 | |||
122 | return true; |
||
123 | } |
||
124 | } |
||
125 |