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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
<?php
namespace JsonApi\JsonApiIntegration;
use Neomerx\JsonApi\Exceptions\JsonApiException;
use Neomerx\JsonApi\Schema\ErrorCollection;
class QueryChecker
{
/**
* @var bool
*/
private $allowUnrecognized;
/**
* @var array|null
*/
private $includePaths;
/**
* @var array|null
*/
private $fieldSetTypes;
/**
* @var array|null
*/
private $pagingParameters;
/**
* @var array|null
*/
private $sortParameters;
/**
* @var array|null
*/
private $filteringParameters;
public function __construct(
bool $allowUnrecognized = true,
array $includePaths = null,
array $fieldSetTypes = null,
array $sortParameters = null,
array $pagingParameters = null,
array $filteringParameters = null
) {
$this->includePaths = $includePaths;
$this->allowUnrecognized = $allowUnrecognized;
$this->fieldSetTypes = $fieldSetTypes;
$this->sortParameters = $this->flip($sortParameters);
$this->pagingParameters = $this->flip($pagingParameters);
$this->filteringParameters = $this->flip($filteringParameters);
}
public function checkQuery(QueryParserInterface $queryParser): void
{
$errors = new ErrorCollection();
$this->checkIncludePaths($errors, $queryParser);
$this->checkFieldSets($errors, $queryParser);
$this->checkFiltering($errors, $queryParser);
$this->checkSorting($errors, $queryParser);
$this->checkPaging($errors, $queryParser);
$this->checkUnrecognized($errors, $queryParser);
if ($errors->count()) {
throw new JsonApiException($errors, JsonApiException::HTTP_CODE_BAD_REQUEST);
}
}
protected function checkIncludePaths(ErrorCollection $errors, QueryParserInterface $queryParser): void
{
$withinAllowed = $this->valuesWithinAllowed(
iterator_to_array($queryParser->getIncludePaths()),
$this->includePaths
);
if (!$withinAllowed) {
$errors->addQueryParameterError(
QueryParser::PARAM_INCLUDE,
'Include paths should contain only allowed ones.'
);
}
}
protected function checkFieldSets(ErrorCollection $errors, QueryParserInterface $queryParser): void
{
$withinAllowed = $this->isFieldsAllowed(iterator_to_array($queryParser->getFields()));
if (!$withinAllowed) {
$errors->addQueryParameterError(QueryParser::PARAM_FIELDS, 'Field sets should contain only allowed ones.');
}
}
protected function checkFiltering(ErrorCollection $errors, QueryParserInterface $queryParser): void
{
$withinAllowed = $this->keysWithinAllowed(
iterator_to_array($queryParser->getFilters()),
$this->filteringParameters
);
if (!$withinAllowed) {
$errors->addQueryParameterError(QueryParser::PARAM_FILTER, 'Filter should contain only allowed values.');
}
}
protected function checkSorting(ErrorCollection $errors, QueryParserInterface $queryParser): void
{
if (null !== $queryParser->getSorts() && null !== $this->sortParameters) {
foreach ($queryParser->getSorts() as $sortParameter) {
if (!array_key_exists($sortParameter->getField(), $this->sortParameters)) {
$errors->addQueryParameterError(
QueryParser::PARAM_SORT,
sprintf('Sort parameter %s is not allowed.', $sortParameter->getField())
);
}
}
}
}
protected function checkPaging(ErrorCollection $errors, QueryParserInterface $queryParser): void
{
$withinAllowed = $this->keysWithinAllowed(
iterator_to_array($queryParser->getPagination()),
$this->pagingParameters
);
if (!$withinAllowed) {
$errors->addQueryParameterError(
QueryParser::PARAM_PAGE,
'Page parameter should contain only allowed values.'
);
}
}
protected function checkUnrecognized(ErrorCollection $errors, QueryParserInterface $queryParser): void
{
if (!$this->allowUnrecognized && !empty($queryParser->getUnrecognizedParameters())) {
foreach ($queryParser->getUnrecognizedParameters() as $name => $value) {
$errors->addQueryParameterError($name, 'Unrecognized Parameter.');
}
}
}
private function keysWithinAllowed(array $toCheck = null, array $allowed = null): bool
{
return null === $toCheck || null === $allowed || empty(array_diff_key($toCheck, $allowed));
}
private function valuesWithinAllowed(array $toCheck = null, array $allowed = null): bool
{
return null === $toCheck || null === $allowed || empty(array_diff($toCheck, $allowed));
}
/**
* @return array|null
*/
private function flip(array $array = null)
{
return $array === null ? null : array_flip($array);
}
/**
* Check input fields against allowed.
*
* @param array|null $fields
*/
private function isFieldsAllowed(array $fields = null): bool
{
if ($this->fieldSetTypes === null || $fields === null) {
return true;
}
foreach ($fields as $type => $requestedFields) {
if (array_key_exists($type, $this->fieldSetTypes) === false) {
return false;
}
$allowedFields = $this->fieldSetTypes[$type];
// if not all fields are allowed and requested more fields than allowed
if ($allowedFields !== null && empty(array_diff($requestedFields, $allowedFields)) === false) {
return false;
}
}
return true;
}
}
|