/
RangeFilterTrait.php
154 lines (124 loc) · 5.22 KB
/
RangeFilterTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Core\Bridge\Doctrine\Common\Filter;
use ApiPlatform\Core\Bridge\Doctrine\Common\PropertyHelperTrait;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use Psr\Log\LoggerInterface;
/**
* Trait for filtering the collection by range.
*
* @author Lee Siong Chan <ahlee2326@me.com>
* @author Alan Poulain <contact@alanpoulain.eu>
*/
trait RangeFilterTrait
{
use PropertyHelperTrait;
/**
* {@inheritdoc}
*/
public function getDescription(string $resourceClass): array
{
$description = [];
$properties = $this->getProperties();
if (null === $properties) {
$properties = array_fill_keys($this->getClassMetadata($resourceClass)->getFieldNames(), null);
}
foreach ($properties as $property => $unused) {
if (!$this->isPropertyMapped($property, $resourceClass)) {
continue;
}
$description += $this->getFilterDescription($property, self::PARAMETER_BETWEEN, $resourceClass);
$description += $this->getFilterDescription($property, self::PARAMETER_GREATER_THAN, $resourceClass);
$description += $this->getFilterDescription($property, self::PARAMETER_GREATER_THAN_OR_EQUAL, $resourceClass);
$description += $this->getFilterDescription($property, self::PARAMETER_LESS_THAN, $resourceClass);
$description += $this->getFilterDescription($property, self::PARAMETER_LESS_THAN_OR_EQUAL, $resourceClass);
}
return $description;
}
abstract protected function getProperties(): ?array;
abstract protected function getLogger(): LoggerInterface;
abstract protected function normalizePropertyName($property/*, ?string $resourceClass = null, array $context = []*/);
/**
* Gets filter description.
*/
protected function getFilterDescription(string $property, string $operator/*, string $resourceClass = null*/): array
{
if (\func_num_args() > 2) {
$resourceClass = null === ($arg = func_get_arg(2)) ? $arg : (string) $arg;
} else {
if (__CLASS__ !== static::class) {
$r = new \ReflectionMethod($this, __FUNCTION__);
if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
@trigger_error(sprintf('Method %s() will have a third `$resourceClass` argument in version API Platform 3.0. Not defining it is deprecated since API Platform 2.7.', __FUNCTION__), \E_USER_DEPRECATED);
}
}
$resourceClass = null;
}
$propertyName = $this->normalizePropertyName($property, $resourceClass);
return [
sprintf('%s[%s]', $propertyName, $operator) => [
'property' => $propertyName,
'type' => 'string',
'required' => false,
],
];
}
private function normalizeValues(array $values, string $property): ?array
{
$operators = [self::PARAMETER_BETWEEN, self::PARAMETER_GREATER_THAN, self::PARAMETER_GREATER_THAN_OR_EQUAL, self::PARAMETER_LESS_THAN, self::PARAMETER_LESS_THAN_OR_EQUAL];
foreach ($values as $operator => $value) {
if (!\in_array($operator, $operators, true)) {
unset($values[$operator]);
}
}
if (empty($values)) {
$this->getLogger()->notice('Invalid filter ignored', [
'exception' => new InvalidArgumentException(sprintf('At least one valid operator ("%s") is required for "%s" property', implode('", "', $operators), $property)),
]);
return null;
}
return $values;
}
/**
* Normalize the values array for between operator.
*/
private function normalizeBetweenValues(array $values): ?array
{
if (2 !== \count($values)) {
$this->getLogger()->notice('Invalid filter ignored', [
'exception' => new InvalidArgumentException(sprintf('Invalid format for "[%s]", expected "<min>..<max>"', self::PARAMETER_BETWEEN)),
]);
return null;
}
if (!is_numeric($values[0]) || !is_numeric($values[1])) {
$this->getLogger()->notice('Invalid filter ignored', [
'exception' => new InvalidArgumentException(sprintf('Invalid values for "[%s]" range, expected numbers', self::PARAMETER_BETWEEN)),
]);
return null;
}
return [$values[0] + 0, $values[1] + 0]; // coerce to the right types.
}
/**
* Normalize the value.
*
* @return int|float|null
*/
private function normalizeValue(string $value, string $operator)
{
if (!is_numeric($value)) {
$this->getLogger()->notice('Invalid filter ignored', [
'exception' => new InvalidArgumentException(sprintf('Invalid value for "[%s]", expected number', $operator)),
]);
return null;
}
return $value + 0; // coerce $value to the right type.
}
}