1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of JSON-API. |
5
|
|
|
* |
6
|
|
|
* (c) Toby Zerner <[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
|
|
|
namespace Tobscure\JsonApi; |
13
|
|
|
|
14
|
|
|
use Tobscure\JsonApi\Exception\InvalidParameterException; |
15
|
|
|
|
16
|
|
|
class Parameters |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $input; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param array $input |
25
|
|
|
*/ |
26
|
42 |
|
public function __construct(array $input) |
27
|
|
|
{ |
28
|
42 |
|
$this->input = $input; |
29
|
42 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get the includes. |
33
|
|
|
* |
34
|
|
|
* @param array $available |
35
|
|
|
* |
36
|
|
|
* @throws \Tobscure\JsonApi\Exception\InvalidParameterException |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
9 |
|
public function getInclude(array $available = []) |
41
|
|
|
{ |
42
|
9 |
|
if ($include = $this->getInput('include')) { |
43
|
6 |
|
$relationships = explode(',', $include); |
44
|
|
|
|
45
|
6 |
|
$invalid = array_diff($relationships, $available); |
46
|
|
|
|
47
|
6 |
View Code Duplication |
if (count($invalid)) { |
|
|
|
|
48
|
3 |
|
throw new InvalidParameterException( |
49
|
3 |
|
'Invalid includes ['.implode(',', $invalid).']', |
50
|
3 |
|
1, |
51
|
3 |
|
null, |
52
|
|
|
'include' |
53
|
3 |
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
3 |
|
return $relationships; |
57
|
|
|
} |
58
|
|
|
|
59
|
3 |
|
return []; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get number of offset. |
64
|
|
|
* |
65
|
|
|
* @param int|null $perPage |
66
|
|
|
* |
67
|
|
|
* @throws \Tobscure\JsonApi\Exception\InvalidParameterException |
68
|
|
|
* |
69
|
|
|
* @return int |
70
|
|
|
*/ |
71
|
9 |
|
public function getOffset($perPage = null) |
72
|
|
|
{ |
73
|
9 |
|
if ($perPage && ($offset = $this->getOffsetFromNumber($perPage))) { |
|
|
|
|
74
|
3 |
|
return $offset; |
75
|
|
|
} |
76
|
|
|
|
77
|
6 |
|
$offset = (int) $this->getPage('offset'); |
78
|
|
|
|
79
|
6 |
|
if ($offset < 0) { |
80
|
3 |
|
throw new InvalidParameterException('page[offset] must be >=0', 2, null, 'page[offset]'); |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
return $offset; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Calculate the offset based on the page[number] parameter. |
88
|
|
|
* |
89
|
|
|
* @param int $perPage |
90
|
|
|
* |
91
|
|
|
* @throws \Tobscure\JsonApi\Exception\InvalidParameterException |
92
|
|
|
* |
93
|
|
|
* @return int |
94
|
|
|
*/ |
95
|
3 |
|
protected function getOffsetFromNumber($perPage) |
96
|
|
|
{ |
97
|
3 |
|
$page = (int) $this->getPage('number'); |
98
|
|
|
|
99
|
3 |
|
if ($page <= 1) { |
100
|
|
|
return 0; |
101
|
|
|
} |
102
|
|
|
|
103
|
3 |
|
return ($page - 1) * $perPage; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the limit. |
108
|
|
|
* |
109
|
|
|
* @param int|null $max |
110
|
|
|
* |
111
|
|
|
* @return int|null |
|
|
|
|
112
|
|
|
*/ |
113
|
6 |
|
public function getLimit($max = null) |
114
|
|
|
{ |
115
|
6 |
|
$limit = $this->getPage('limit') ?: $this->getPage('size') ?: null; |
116
|
|
|
|
117
|
6 |
|
if ($limit && $max) { |
|
|
|
|
118
|
|
|
$limit = min($max, $limit); |
119
|
|
|
} |
120
|
|
|
|
121
|
6 |
|
return $limit; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the sort. |
126
|
|
|
* |
127
|
|
|
* @param array $available |
128
|
|
|
* |
129
|
|
|
* @throws \Tobscure\JsonApi\Exception\InvalidParameterException |
130
|
|
|
* |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
12 |
|
public function getSort(array $available = []) |
134
|
|
|
{ |
135
|
12 |
|
$sort = []; |
136
|
|
|
|
137
|
12 |
|
if ($input = $this->getInput('sort')) { |
138
|
9 |
|
$fields = explode(',', $input); |
139
|
|
|
|
140
|
9 |
|
foreach ($fields as $field) { |
141
|
9 |
|
if (substr($field, 0, 1) === '-') { |
142
|
3 |
|
$field = substr($field, 1); |
143
|
3 |
|
$order = 'desc'; |
144
|
3 |
|
} else { |
145
|
9 |
|
$order = 'asc'; |
146
|
|
|
} |
147
|
|
|
|
148
|
9 |
|
$sort[$field] = $order; |
149
|
9 |
|
} |
150
|
|
|
|
151
|
9 |
|
$invalid = array_diff(array_keys($sort), $available); |
152
|
|
|
|
153
|
9 |
View Code Duplication |
if (count($invalid)) { |
|
|
|
|
154
|
3 |
|
throw new InvalidParameterException( |
155
|
3 |
|
'Invalid sort fields ['.implode(',', $invalid).']', |
156
|
3 |
|
3, |
157
|
3 |
|
null, |
158
|
|
|
'sort' |
159
|
3 |
|
); |
160
|
|
|
} |
161
|
6 |
|
} |
162
|
|
|
|
163
|
9 |
|
return $sort; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get the fields requested for inclusion. |
168
|
|
|
* |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
6 |
|
public function getFields() |
172
|
|
|
{ |
173
|
6 |
|
$fields = $this->getInput('fields'); |
174
|
|
|
|
175
|
6 |
|
if (! is_array($fields)) { |
176
|
3 |
|
return []; |
177
|
|
|
} |
178
|
|
|
|
179
|
3 |
|
return array_map(function ($fields) { |
180
|
3 |
|
return explode(',', $fields); |
181
|
3 |
|
}, $fields); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get a filter item. |
186
|
|
|
* |
187
|
|
|
* @return mixed |
188
|
|
|
*/ |
189
|
|
|
public function getFilter() |
190
|
|
|
{ |
191
|
|
|
return $this->getInput('filter'); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get an input item. |
196
|
|
|
* |
197
|
|
|
* @param string $key |
198
|
|
|
* @param null $default |
199
|
|
|
* |
200
|
|
|
* @return mixed |
201
|
|
|
*/ |
202
|
42 |
|
protected function getInput($key, $default = null) |
203
|
|
|
{ |
204
|
42 |
|
return isset($this->input[$key]) ? $this->input[$key] : $default; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Get the page. |
209
|
|
|
* |
210
|
|
|
* @param string $key |
211
|
|
|
* |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
15 |
|
protected function getPage($key) |
215
|
|
|
{ |
216
|
15 |
|
$page = $this->getInput('page'); |
217
|
|
|
|
218
|
15 |
|
return isset($page[$key]) ? $page[$key] : ''; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
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.