-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathRequestParser.php
More file actions
395 lines (337 loc) · 9.24 KB
/
RequestParser.php
File metadata and controls
395 lines (337 loc) · 9.24 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
namespace alsvanzelf\jsonapi\helpers;
use alsvanzelf\jsonapi\Document;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;
class RequestParser {
const SORT_ASCENDING = 'ascending';
const SORT_DESCENDING = 'descending';
/** @var array */
protected static $defaults = [
/**
* reformat the include query parameter paths to nested arrays
* this allows easier processing on each step of the chain
*/
'useNestedIncludePaths' => true,
/**
* reformat the sort query parameter paths to separate the sort order
* this allows easier processing of sort orders and field names
*/
'useAnnotatedSortFields' => true,
];
/** @var string */
protected $selfLink = '';
/** @var array */
protected $queryParameters = [];
/** @var array */
protected $document = [];
/**
* @param string $selfLink the uri used to make this request {@see getSelfLink()}
* @param array $queryParameters all query parameters defined by the specification
* @param array $document the request jsonapi document
*/
public function __construct($selfLink='', array $queryParameters=[], array $document=[]) {
$this->selfLink = $selfLink;
$this->queryParameters = $queryParameters;
$this->document = $document;
}
/**
* @return self
*/
public static function fromSuperglobals() {
$selfLink = '';
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
$selfLink .= $_SERVER['HTTP_X_FORWARDED_PROTO'].'://';
}
elseif (isset($_SERVER['REQUEST_SCHEME'])) {
$selfLink .= $_SERVER['REQUEST_SCHEME'].'://';
}
else {
$selfLink .= 'http://';
}
if (isset($_SERVER['HTTP_HOST'])) {
$selfLink .= $_SERVER['HTTP_HOST'];
}
elseif (isset($_SERVER['SCRIPT_URI'])) {
$startOfDomain = strpos($_SERVER['SCRIPT_URI'], '://') + strlen('://');
$endOfDomain = strpos($_SERVER['SCRIPT_URI'], '/', $startOfDomain);
$lengthOfDomain = ($endOfDomain - $startOfDomain);
$selfLink .= substr($_SERVER['SCRIPT_URI'], $startOfDomain, $lengthOfDomain);
}
if (isset($_SERVER['REQUEST_URI'])) {
$selfLink .= $_SERVER['REQUEST_URI'];
}
elseif (isset($_SERVER['PATH_INFO']) && isset($_SERVER['QUERY_STRING'])) {
$selfLink .= $_SERVER['PATH_INFO'];
$selfLink .= ($_SERVER['QUERY_STRING'] !== '') ? '?'.$_SERVER['QUERY_STRING'] : '';
}
$queryParameters = $_GET;
$document = $_POST;
if ($document === [] && isset($_SERVER['CONTENT_TYPE'])) {
$documentIsJsonapi = (strpos($_SERVER['CONTENT_TYPE'], Document::CONTENT_TYPE_OFFICIAL) !== false);
$documentIsJson = (strpos($_SERVER['CONTENT_TYPE'], Document::CONTENT_TYPE_DEBUG) !== false);
if ($documentIsJsonapi || $documentIsJson) {
$document = json_decode(file_get_contents('php://input'), true);
if ($document === null) {
$document = [];
}
}
}
return new self($selfLink, $queryParameters, $document);
}
/**
* @param ServerRequestInterface|RequestInterface $request
* @return self
*/
public static function fromPsrRequest(RequestInterface $request) {
$selfLink = (string) $request->getUri();
if ($request instanceof ServerRequestInterface) {
$queryParameters = $request->getQueryParams();
}
else {
$queryParameters = [];
parse_str($request->getUri()->getQuery(), $queryParameters);
}
if ($request->getBody()->getContents() === '') {
$document = [];
}
else {
$document = json_decode($request->getBody()->getContents(), true);
if ($document === null) {
$document = [];
}
}
return new self($selfLink, $queryParameters, $document);
}
/**
* the full link used to make this request
*
* this is not a bare self link of a resource and includes query parameters if used
*
* @return string
*/
public function getSelfLink() {
return $this->selfLink;
}
/**
* @return boolean
*/
public function hasIncludePaths() {
return isset($this->queryParameters['include']);
}
/**
* returns a nested array based on the path, or the raw paths
*
* the nested format allows easier processing on each step of the chain
* the raw format allows for custom processing
*
* @param array $options optional {@see RequestParser::$defaults}
* @return string[]|array
*/
public function getIncludePaths(array $options=[]) {
$includePaths = explode(',', $this->queryParameters['include']);
$options = array_merge(self::$defaults, $options);
if ($options['useNestedIncludePaths'] === false) {
return $includePaths;
}
$restructured = [];
foreach ($includePaths as $path) {
$steps = explode('.', $path);
$wrapped = [];
while ($steps !== []) {
$lastStep = array_pop($steps);
$wrapped = [$lastStep => $wrapped];
}
$restructured = array_merge_recursive($restructured, $wrapped);
}
return $restructured;
}
/**
* @return boolean
*/
public function hasAnySparseFieldset() {
return (isset($this->queryParameters['fields']));
}
/**
* @param string $type
* @return boolean
*/
public function hasSparseFieldset($type) {
return isset($this->queryParameters['fields'][$type]);
}
/**
* @param string $type
* @return string[]
*/
public function getSparseFieldset($type) {
if ($this->queryParameters['fields'][$type] === '') {
return [];
}
return explode(',', $this->queryParameters['fields'][$type]);
}
/**
* @return boolean
*/
public function hasSortFields() {
return isset($this->queryParameters['sort']);
}
/**
* returns an array with sort order annotations, or the raw sort fields with minus signs
*
* the annotated format allows easier processing of sort orders and field names
* the raw format allows for custom processing
*
* @todo return some kind of SortFieldObject
*
* @param array $options optional {@see RequestParser::$defaults}
* @return string[]|array[] {
* @var string $field the sort field, without any minus sign for descending sort order
* @var string $order one of the RequestParser::SORT_* constants
* }
*/
public function getSortFields(array $options=[]) {
$fields = explode(',', $this->queryParameters['sort']);
$options = array_merge(self::$defaults, $options);
if ($options['useAnnotatedSortFields'] === false) {
return $fields;
}
$sort = [];
foreach ($fields as $field) {
$order = RequestParser::SORT_ASCENDING;
if (strpos($field, '-') === 0) {
$field = substr($field, 1);
$order = RequestParser::SORT_DESCENDING;
}
$sort[] = [
'field' => $field,
'order' => $order,
];
}
return $sort;
}
/**
* @return boolean
*/
public function hasPagination() {
return isset($this->queryParameters['page']);
}
/**
* @todo return some kind of PaginatorObject which recognizes the strategy of pagination used
* e.g. page-based, offset-based, cursor-based, or unknown
*
* @return array
*/
public function getPagination() {
return $this->queryParameters['page'];
}
/**
* @return boolean
*/
public function hasFilter() {
return isset($this->queryParameters['filter']);
}
/**
* @return array
*/
public function getFilter() {
return $this->queryParameters['filter'];
}
/**
* @return boolean
*/
public function hasLocalId() {
return (isset($this->document['data']['lid']));
}
/**
* @return string
*/
public function getLocalId() {
return $this->document['data']['lid'];
}
/**
* @param string $attributeName
* @return boolean
*/
public function hasAttribute($attributeName) {
if (isset($this->document['data']['attributes']) === false) {
return false;
}
if (array_key_exists($attributeName, $this->document['data']['attributes']) === false) {
return false;
}
return true;
}
/**
* @param string $attributeName
* @return mixed
*/
public function getAttribute($attributeName) {
return $this->document['data']['attributes'][$attributeName];
}
/**
* @param string $relationshipName
* @return boolean
*/
public function hasRelationship($relationshipName) {
if (isset($this->document['data']['relationships']) === false) {
return false;
}
if (array_key_exists($relationshipName, $this->document['data']['relationships']) === false) {
return false;
}
return true;
}
/**
* @todo return some kind of read-only ResourceIdentifierObject
*
* @param string $relationshipName
* @return array
*/
public function getRelationship($relationshipName) {
return $this->document['data']['relationships'][$relationshipName];
}
/**
* @param string $metaKey
* @return boolean
*/
public function hasMeta($metaKey) {
if (isset($this->document['meta']) === false) {
return false;
}
if (array_key_exists($metaKey, $this->document['meta']) === false) {
return false;
}
return true;
}
/**
* @param string $metaKey
* @return mixed
*/
public function getMeta($metaKey) {
return $this->document['meta'][$metaKey];
}
/**
* @return boolean
*/
public function hasQueryParameters() {
return ($this->queryParameters !== []);
}
/**
* @return array
*/
public function getQueryParameters() {
return $this->queryParameters;
}
/**
* @return boolean
*/
public function hasDocument() {
return ($this->document !== []);
}
/**
* @return array
*/
public function getDocument() {
return $this->document;
}
}