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 boolean values. |
||
22 | * |
||
23 | * Filters collection on equality of boolean properties. The value is specified |
||
24 | * as one of ( "true" | "false" | "1" | "0" ) in the query. |
||
25 | * |
||
26 | * For each property passed, if the resource does not have such property or if |
||
27 | * the value is not one of ( "true" | "false" | "1" | "0" ) the property is ignored. |
||
28 | * |
||
29 | * @author Amrouche Hamza <[email protected]> |
||
30 | * @author Teoh Han Hui <[email protected]> |
||
31 | * @author Alan Poulain <[email protected]> |
||
32 | */ |
||
33 | trait BooleanFilterTrait |
||
34 | { |
||
35 | use PropertyHelperTrait; |
||
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | public function getDescription(string $resourceClass): array |
||
41 | { |
||
42 | $description = []; |
||
43 | |||
44 | $properties = $this->getProperties(); |
||
45 | if (null === $properties) { |
||
46 | $properties = array_fill_keys($this->getClassMetadata($resourceClass)->getFieldNames(), null); |
||
47 | } |
||
48 | |||
49 | foreach ($properties as $property => $unused) { |
||
50 | if (!$this->isPropertyMapped($property, $resourceClass) || !$this->isBooleanField($property, $resourceClass)) { |
||
51 | continue; |
||
52 | } |
||
53 | $propertyName = $this->normalizePropertyName($property); |
||
54 | $description[$propertyName] = [ |
||
55 | 'property' => $propertyName, |
||
56 | 'type' => 'bool', |
||
57 | 'required' => false, |
||
58 | ]; |
||
59 | } |
||
60 | |||
61 | return $description; |
||
62 | } |
||
63 | |||
64 | abstract protected function getProperties(): ?array; |
||
65 | |||
66 | abstract protected function getLogger(): LoggerInterface; |
||
67 | |||
68 | abstract protected function normalizePropertyName($property); |
||
69 | |||
70 | /** |
||
71 | * Determines whether the given property refers to a boolean field. |
||
72 | */ |
||
73 | protected function isBooleanField(string $property, string $resourceClass): bool |
||
74 | { |
||
75 | return isset(self::DOCTRINE_BOOLEAN_TYPES[(string) $this->getDoctrineFieldType($property, $resourceClass)]); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
76 | } |
||
77 | |||
78 | private function normalizeValue($value, string $property): ?bool |
||
79 | { |
||
80 | if (\in_array($value, [true, 'true', '1'], true)) { |
||
81 | return true; |
||
82 | } |
||
83 | |||
84 | if (\in_array($value, [false, 'false', '0'], true)) { |
||
85 | return false; |
||
86 | } |
||
87 | |||
88 | $this->getLogger()->notice('Invalid filter ignored', [ |
||
89 | 'exception' => new InvalidArgumentException(sprintf('Invalid boolean value for "%s" property, expected one of ( "%s" )', $property, implode('" | "', [ |
||
90 | 'true', |
||
91 | 'false', |
||
92 | '1', |
||
93 | '0', |
||
94 | ]))), |
||
95 | ]); |
||
96 | |||
97 | return null; |
||
98 | } |
||
99 | } |
||
100 |