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\Serializer\Filter; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Property filter. |
20
|
|
|
* |
21
|
|
|
* @author Baptiste Meyer <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
final class PropertyFilter implements FilterInterface |
24
|
|
|
{ |
25
|
|
|
private $overrideDefaultProperties; |
26
|
|
|
private $parameterName; |
27
|
|
|
private $whitelist; |
28
|
|
|
|
29
|
|
|
public function __construct(string $parameterName = 'properties', bool $overrideDefaultProperties = false, array $whitelist = null) |
30
|
|
|
{ |
31
|
|
|
$this->overrideDefaultProperties = $overrideDefaultProperties; |
32
|
|
|
$this->parameterName = $parameterName; |
33
|
|
|
$this->whitelist = null === $whitelist ? null : $this->formatWhitelist($whitelist); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function apply(Request $request, bool $normalization, array $attributes, array &$context) |
40
|
|
|
{ |
41
|
|
View Code Duplication |
if (null !== $propertyAttribute = $request->attributes->get('_api_filter_property')) { |
|
|
|
|
42
|
|
|
$properties = $propertyAttribute; |
43
|
|
|
} elseif (array_key_exists($this->parameterName, $commonAttribute = $request->attributes->get('_api_filter_common', []))) { |
44
|
|
|
$properties = $commonAttribute[$this->parameterName]; |
45
|
|
|
} else { |
46
|
|
|
$properties = $request->query->get($this->parameterName); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (!is_array($properties)) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (null !== $this->whitelist) { |
54
|
|
|
$properties = $this->getProperties($properties, $this->whitelist); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (!$this->overrideDefaultProperties && isset($context['attributes'])) { |
58
|
|
|
$properties = array_merge_recursive((array) $context['attributes'], $properties); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$context['attributes'] = $properties; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function getDescription(string $resourceClass): array |
68
|
|
|
{ |
69
|
|
|
return [ |
70
|
|
|
$this->parameterName.'[]' => [ |
71
|
|
|
'property' => null, |
72
|
|
|
'type' => 'string', |
73
|
|
|
'required' => false, |
74
|
|
|
], |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Generate an array of whitelist properties to match the format that properties |
80
|
|
|
* will have in the request. |
81
|
|
|
* |
82
|
|
|
* @param array $whitelist the whitelist to format |
83
|
|
|
* |
84
|
|
|
* @return array An array containing the whitelist ready to match request parameters |
85
|
|
|
*/ |
86
|
|
|
private function formatWhitelist(array $whitelist): array |
87
|
|
|
{ |
88
|
|
|
if (array_values($whitelist) === $whitelist) { |
89
|
|
|
return $whitelist; |
90
|
|
|
} |
91
|
|
|
foreach ($whitelist as $name => $value) { |
92
|
|
|
if (null === $value) { |
93
|
|
|
unset($whitelist[$name]); |
94
|
|
|
$whitelist[] = $name; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $whitelist; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function getProperties(array $properties, array $whitelist = null): array |
102
|
|
|
{ |
103
|
|
|
$whitelist = $whitelist ?? $this->whitelist; |
104
|
|
|
$result = []; |
105
|
|
|
|
106
|
|
|
foreach ($properties as $key => $value) { |
107
|
|
|
if (is_numeric($key)) { |
108
|
|
|
if (in_array($value, $whitelist, true)) { |
109
|
|
|
$result[] = $value; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
continue; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (isset($whitelist[$key]) && is_array($value) && $recursiveResult = $this->getProperties($value, $whitelist[$key])) { |
116
|
|
|
$result[$key] = $recursiveResult; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $result; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
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.