These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\Orm\Filter; |
||
15 | |||
16 | use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; |
||
17 | use ApiPlatform\Core\Exception\InvalidArgumentException; |
||
18 | use Doctrine\DBAL\Types\Type as DBALType; |
||
19 | use Doctrine\ORM\QueryBuilder; |
||
20 | |||
21 | /** |
||
22 | * Filters the collection by numeric values. |
||
23 | * |
||
24 | * Filters collection by equality of numeric properties. |
||
25 | * |
||
26 | * For each property passed, if the resource does not have such property or if |
||
27 | * the value is not numeric, the property is ignored. |
||
28 | * |
||
29 | * @author Amrouche Hamza <[email protected]> |
||
30 | * @author Teoh Han Hui <[email protected]> |
||
31 | */ |
||
32 | class NumericFilter extends AbstractFilter |
||
33 | { |
||
34 | /** |
||
35 | * Type of numeric in Doctrine. |
||
36 | * |
||
37 | * @see http://doctrine-orm.readthedocs.org/projects/doctrine-dbal/en/latest/reference/types.html |
||
38 | */ |
||
39 | const DOCTRINE_NUMERIC_TYPES = [ |
||
40 | DBALType::BIGINT => true, |
||
41 | DBALType::DECIMAL => true, |
||
42 | DBALType::FLOAT => true, |
||
43 | DBALType::INTEGER => true, |
||
44 | DBALType::SMALLINT => true, |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | public function getDescription(string $resourceClass): array |
||
51 | { |
||
52 | $description = []; |
||
53 | |||
54 | $properties = $this->properties; |
||
55 | if (null === $properties) { |
||
56 | $properties = array_fill_keys($this->getClassMetadata($resourceClass)->getFieldNames(), null); |
||
57 | } |
||
58 | |||
59 | foreach ($properties as $property => $unused) { |
||
60 | if (!$this->isPropertyMapped($property, $resourceClass) || !$this->isNumericField($property, $resourceClass)) { |
||
61 | continue; |
||
62 | } |
||
63 | $propertyParts = $this->splitPropertyParts($property, $resourceClass); |
||
64 | $metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']); |
||
65 | |||
66 | $description[$property] = [ |
||
67 | 'property' => $property, |
||
68 | 'type' => $this->getType($metadata->getTypeOfField($propertyParts['field'])), |
||
69 | 'required' => false, |
||
70 | ]; |
||
71 | } |
||
72 | |||
73 | return $description; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Gets the PHP type corresponding to this Doctrine type. |
||
78 | * |
||
79 | * @param string $doctrineType |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | private function getType(string $doctrineType): string |
||
84 | { |
||
85 | if (DBALType::DECIMAL === $doctrineType) { |
||
86 | return 'string'; |
||
87 | } |
||
88 | |||
89 | if (DBALType::FLOAT === $doctrineType) { |
||
90 | return 'float'; |
||
91 | } |
||
92 | |||
93 | return 'int'; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null) |
||
100 | { |
||
101 | View Code Duplication | if ( |
|
102 | !$this->isPropertyEnabled($property, $resourceClass) || |
||
103 | !$this->isPropertyMapped($property, $resourceClass) || |
||
104 | !$this->isNumericField($property, $resourceClass) |
||
105 | ) { |
||
106 | return; |
||
107 | } |
||
108 | |||
109 | if (!is_numeric($value)) { |
||
110 | $this->logger->notice('Invalid filter ignored', [ |
||
111 | 'exception' => new InvalidArgumentException(sprintf('Invalid numeric value for "%s" property', $property)), |
||
112 | ]); |
||
113 | |||
114 | return; |
||
115 | } |
||
116 | |||
117 | $alias = 'o'; |
||
118 | $field = $property; |
||
119 | |||
120 | View Code Duplication | if ($this->isPropertyNested($property, $resourceClass)) { |
|
0 ignored issues
–
show
|
|||
121 | list($alias, $field) = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass); |
||
122 | } |
||
123 | $valueParameter = $queryNameGenerator->generateParameterName($field); |
||
124 | |||
125 | $queryBuilder |
||
126 | ->andWhere(sprintf('%s.%s = :%s', $alias, $field, $valueParameter)) |
||
127 | ->setParameter($valueParameter, $value); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Determines whether the given property refers to a numeric field. |
||
132 | * |
||
133 | * @param string $property |
||
134 | * @param string $resourceClass |
||
135 | * |
||
136 | * @return bool |
||
137 | */ |
||
138 | View Code Duplication | protected function isNumericField(string $property, string $resourceClass): bool |
|
139 | { |
||
140 | $propertyParts = $this->splitPropertyParts($property, $resourceClass); |
||
141 | $metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']); |
||
142 | |||
143 | return isset(self::DOCTRINE_NUMERIC_TYPES[$metadata->getTypeOfField($propertyParts['field'])]); |
||
144 | } |
||
145 | } |
||
146 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.