@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | /** |
96 | 96 | * Determine if the payload contains a non-empty value for a given key. |
97 | 97 | * |
98 | - * @param string|array $keys |
|
98 | + * @param string|null $keys |
|
99 | 99 | * |
100 | 100 | * @return bool |
101 | 101 | */ |
@@ -356,7 +356,7 @@ discard block |
||
356 | 356 | /** |
357 | 357 | * Return a value from the array identified from the key. |
358 | 358 | * |
359 | - * @param $key |
|
359 | + * @param string|null $key |
|
360 | 360 | * @param $data |
361 | 361 | * @return mixed |
362 | 362 | */ |
@@ -5,11 +5,11 @@ |
||
5 | 5 | use Nathanmac\Utilities\Parser\Formats\BSON; |
6 | 6 | use Nathanmac\Utilities\Parser\Formats\FormatInterface; |
7 | 7 | use Nathanmac\Utilities\Parser\Formats\JSON; |
8 | +use Nathanmac\Utilities\Parser\Formats\MSGPack; |
|
8 | 9 | use Nathanmac\Utilities\Parser\Formats\QueryStr; |
9 | 10 | use Nathanmac\Utilities\Parser\Formats\Serialize; |
10 | 11 | use Nathanmac\Utilities\Parser\Formats\XML; |
11 | 12 | use Nathanmac\Utilities\Parser\Formats\YAML; |
12 | -use Nathanmac\Utilities\Parser\Formats\MSGPack; |
|
13 | 13 | |
14 | 14 | /** |
15 | 15 | * Parser Library, designed to parse payload data from various formats to php array. |
@@ -29,28 +29,28 @@ |
||
29 | 29 | * @var array Supported Formats |
30 | 30 | */ |
31 | 31 | private $supported_formats = array ( |
32 | - // XML |
|
33 | - 'application/xml' => 'xml', |
|
34 | - 'text/xml' => 'xml', |
|
35 | - // JSON |
|
36 | - 'application/json' => 'json', |
|
37 | - 'application/x-javascript' => 'json', |
|
38 | - 'text/javascript' => 'json', |
|
39 | - 'text/x-javascript' => 'json', |
|
40 | - 'text/x-json' => 'json', |
|
41 | - // BSON |
|
32 | + // XML |
|
33 | + 'application/xml' => 'xml', |
|
34 | + 'text/xml' => 'xml', |
|
35 | + // JSON |
|
36 | + 'application/json' => 'json', |
|
37 | + 'application/x-javascript' => 'json', |
|
38 | + 'text/javascript' => 'json', |
|
39 | + 'text/x-javascript' => 'json', |
|
40 | + 'text/x-json' => 'json', |
|
41 | + // BSON |
|
42 | 42 | 'application/bson' => 'bson', |
43 | - // YAML |
|
44 | - 'text/yaml' => 'yaml', |
|
45 | - 'text/x-yaml' => 'yaml', |
|
46 | - 'application/yaml' => 'yaml', |
|
47 | - 'application/x-yaml' => 'yaml', |
|
48 | - // MSGPACK |
|
43 | + // YAML |
|
44 | + 'text/yaml' => 'yaml', |
|
45 | + 'text/x-yaml' => 'yaml', |
|
46 | + 'application/yaml' => 'yaml', |
|
47 | + 'application/x-yaml' => 'yaml', |
|
48 | + // MSGPACK |
|
49 | 49 | 'application/msgpack' => 'msgpack', |
50 | 50 | 'application/x-msgpack' => 'msgpack', |
51 | - // MISC |
|
52 | - 'application/vnd.php.serialized' => 'serialize', |
|
53 | - 'application/x-www-form-urlencoded' => 'querystr' |
|
51 | + // MISC |
|
52 | + 'application/vnd.php.serialized' => 'serialize', |
|
53 | + 'application/x-www-form-urlencoded' => 'querystr' |
|
54 | 54 | ); |
55 | 55 | |
56 | 56 | /* ------------ Access Methods/Helpers ------------ */ |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | /** |
29 | 29 | * @var array Supported Formats |
30 | 30 | */ |
31 | - private $supported_formats = array ( |
|
31 | + private $supported_formats = array( |
|
32 | 32 | // XML |
33 | 33 | 'application/xml' => 'xml', |
34 | 34 | 'text/xml' => 'xml', |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | */ |
191 | 191 | public function getFormat($format = '') |
192 | 192 | { |
193 | - if (! empty($format)) { |
|
193 | + if (!empty($format)) { |
|
194 | 194 | return $this->processContentType($format); |
195 | 195 | } |
196 | 196 | |
@@ -382,13 +382,13 @@ discard block |
||
382 | 382 | } else { |
383 | 383 | $item = array_shift($data); |
384 | 384 | } |
385 | - $data =& $item; |
|
385 | + $data = & $item; |
|
386 | 386 | |
387 | 387 | } else { |
388 | 388 | if (!isset($data[$key]) || !is_array($data[$key])) |
389 | 389 | return false; |
390 | 390 | |
391 | - $data =& $data[$key]; |
|
391 | + $data = & $data[$key]; |
|
392 | 392 | } |
393 | 393 | } |
394 | 394 | |
@@ -439,7 +439,7 @@ discard block |
||
439 | 439 | } else { |
440 | 440 | $item = array_shift($data); |
441 | 441 | } |
442 | - $data =& $item; |
|
442 | + $data = & $item; |
|
443 | 443 | |
444 | 444 | } else { |
445 | 445 | if (!isset($data[$key])) |
@@ -451,7 +451,7 @@ discard block |
||
451 | 451 | if ($data[$key] === '') |
452 | 452 | return false; |
453 | 453 | |
454 | - $data =& $data[$key]; |
|
454 | + $data = & $data[$key]; |
|
455 | 455 | } |
456 | 456 | } |
457 | 457 | return true; |
@@ -489,12 +489,12 @@ discard block |
||
489 | 489 | { |
490 | 490 | $key = array_shift($keys); |
491 | 491 | |
492 | - if ( ! isset($array[$key]) || ! is_array($array[$key])) |
|
492 | + if (!isset($array[$key]) || !is_array($array[$key])) |
|
493 | 493 | { |
494 | 494 | return; |
495 | 495 | } |
496 | 496 | |
497 | - $array =& $array[$key]; |
|
497 | + $array = & $array[$key]; |
|
498 | 498 | } |
499 | 499 | |
500 | 500 | unset($array[array_shift($keys)]); |
@@ -107,8 +107,9 @@ discard block |
||
107 | 107 | |
108 | 108 | foreach ($keys as $value) |
109 | 109 | { |
110 | - if ($this->hasValueAtKey($value, $results) === false) |
|
111 | - return false; |
|
110 | + if ($this->hasValueAtKey($value, $results) === false) { |
|
111 | + return false; |
|
112 | + } |
|
112 | 113 | } |
113 | 114 | return true; |
114 | 115 | } |
@@ -197,13 +198,17 @@ discard block |
||
197 | 198 | if (isset($_SERVER['CONTENT_TYPE'])) |
198 | 199 | { |
199 | 200 | $type = $this->processContentType($_SERVER['CONTENT_TYPE']); |
200 | - if ($type !== false) return $type; |
|
201 | + if ($type !== false) { |
|
202 | + return $type; |
|
203 | + } |
|
201 | 204 | } |
202 | 205 | |
203 | 206 | if (isset($_SERVER['HTTP_CONTENT_TYPE'])) |
204 | 207 | { |
205 | 208 | $type = $this->processContentType($_SERVER['HTTP_CONTENT_TYPE']); |
206 | - if ($type !== false) return $type; |
|
209 | + if ($type !== false) { |
|
210 | + return $type; |
|
211 | + } |
|
207 | 212 | } |
208 | 213 | |
209 | 214 | return 'json'; |
@@ -373,8 +378,9 @@ discard block |
||
373 | 378 | // Shift the first item of the array |
374 | 379 | if (preg_match('/^:(index|item)\[\d+\]$/', $key)) { |
375 | 380 | for ($x = substr($key, 7, -1); $x >= 0; $x--) { |
376 | - if (empty($data)) |
|
377 | - return false; |
|
381 | + if (empty($data)) { |
|
382 | + return false; |
|
383 | + } |
|
378 | 384 | $item = array_shift($data); |
379 | 385 | } |
380 | 386 | } else if ($key == ':last') { |
@@ -385,8 +391,9 @@ discard block |
||
385 | 391 | $data =& $item; |
386 | 392 | |
387 | 393 | } else { |
388 | - if (!isset($data[$key]) || !is_array($data[$key])) |
|
389 | - return false; |
|
394 | + if (!isset($data[$key]) || !is_array($data[$key])) { |
|
395 | + return false; |
|
396 | + } |
|
390 | 397 | |
391 | 398 | $data =& $data[$key]; |
392 | 399 | } |
@@ -397,8 +404,9 @@ discard block |
||
397 | 404 | if (preg_match($this->wildcards, $key)) { |
398 | 405 | if (preg_match('/^:(index|item)\[\d+\]$/', $key)) { |
399 | 406 | for ($x = substr($key, 7, -1); $x >= 0; $x--) { |
400 | - if (empty($data)) |
|
401 | - return false; |
|
407 | + if (empty($data)) { |
|
408 | + return false; |
|
409 | + } |
|
402 | 410 | $item = array_shift($data); |
403 | 411 | } |
404 | 412 | return $item; |
@@ -430,8 +438,9 @@ discard block |
||
430 | 438 | // Shift the first item of the array |
431 | 439 | if (preg_match('/^:(index|item)\[\d+\]$/', $key)) { |
432 | 440 | for ($x = substr($key, 7, -1); $x >= 0; $x--) { |
433 | - if (empty($data)) |
|
434 | - return false; |
|
441 | + if (empty($data)) { |
|
442 | + return false; |
|
443 | + } |
|
435 | 444 | $item = array_shift($data); |
436 | 445 | } |
437 | 446 | } else if ($key == ':last') { |
@@ -442,14 +451,17 @@ discard block |
||
442 | 451 | $data =& $item; |
443 | 452 | |
444 | 453 | } else { |
445 | - if (!isset($data[$key])) |
|
446 | - return false; |
|
454 | + if (!isset($data[$key])) { |
|
455 | + return false; |
|
456 | + } |
|
447 | 457 | |
448 | - if (is_bool($data[$key])) |
|
449 | - return true; |
|
458 | + if (is_bool($data[$key])) { |
|
459 | + return true; |
|
460 | + } |
|
450 | 461 | |
451 | - if ($data[$key] === '') |
|
452 | - return false; |
|
462 | + if ($data[$key] === '') { |
|
463 | + return false; |
|
464 | + } |
|
453 | 465 | |
454 | 466 | $data =& $data[$key]; |
455 | 467 | } |
@@ -35,7 +35,7 @@ |
||
35 | 35 | /** |
36 | 36 | * Get the services provided by the provider. |
37 | 37 | * |
38 | - * @return array |
|
38 | + * @return string[] |
|
39 | 39 | */ |
40 | 40 | public function provides() |
41 | 41 | { |
@@ -27,7 +27,7 @@ |
||
27 | 27 | */ |
28 | 28 | public function register() |
29 | 29 | { |
30 | - $this->app->bind('Parser', function ($app) { |
|
30 | + $this->app->bind('Parser', function($app) { |
|
31 | 31 | return new Parser; |
32 | 32 | }); |
33 | 33 | } |
@@ -27,7 +27,7 @@ |
||
27 | 27 | if (function_exists('bson_decode')) { |
28 | 28 | if ($payload) { |
29 | 29 | $bson = bson_decode(trim($payload)); |
30 | - if (! $bson) |
|
30 | + if (!$bson) |
|
31 | 31 | throw new ParserException('Failed To Parse BSON'); |
32 | 32 | return $bson; |
33 | 33 | } |
@@ -27,8 +27,9 @@ |
||
27 | 27 | if (function_exists('bson_decode')) { |
28 | 28 | if ($payload) { |
29 | 29 | $bson = bson_decode(trim($payload)); |
30 | - if (! $bson) |
|
31 | - throw new ParserException('Failed To Parse BSON'); |
|
30 | + if (! $bson) { |
|
31 | + throw new ParserException('Failed To Parse BSON'); |
|
32 | + } |
|
32 | 33 | return $bson; |
33 | 34 | } |
34 | 35 | return array(); |
@@ -27,7 +27,7 @@ |
||
27 | 27 | if (function_exists('msgpack_unpack')) { |
28 | 28 | if ($payload) { |
29 | 29 | $msg = msgpack_unpack(trim($payload)); |
30 | - if (! $msg) |
|
30 | + if (!$msg) |
|
31 | 31 | throw new ParserException('Failed To Parse MSGPack'); |
32 | 32 | return $msg; |
33 | 33 | } |
@@ -27,8 +27,9 @@ |
||
27 | 27 | if (function_exists('msgpack_unpack')) { |
28 | 28 | if ($payload) { |
29 | 29 | $msg = msgpack_unpack(trim($payload)); |
30 | - if (! $msg) |
|
31 | - throw new ParserException('Failed To Parse MSGPack'); |
|
30 | + if (! $msg) { |
|
31 | + throw new ParserException('Failed To Parse MSGPack'); |
|
32 | + } |
|
32 | 33 | return $msg; |
33 | 34 | } |
34 | 35 | return array(); |
@@ -26,7 +26,7 @@ |
||
26 | 26 | { |
27 | 27 | if ($payload) { |
28 | 28 | $json = json_decode(trim($payload), true); |
29 | - if (! $json) |
|
29 | + if (!$json) |
|
30 | 30 | throw new ParserException('Failed To Parse JSON'); |
31 | 31 | return $json; |
32 | 32 | } |
@@ -26,8 +26,9 @@ |
||
26 | 26 | { |
27 | 27 | if ($payload) { |
28 | 28 | $json = json_decode(trim($payload), true); |
29 | - if (! $json) |
|
30 | - throw new ParserException('Failed To Parse JSON'); |
|
29 | + if (! $json) { |
|
30 | + throw new ParserException('Failed To Parse JSON'); |
|
31 | + } |
|
31 | 32 | return $json; |
32 | 33 | } |
33 | 34 |
@@ -30,9 +30,9 @@ |
||
30 | 30 | |
31 | 31 | // Fix for empty values in XML |
32 | 32 | $json = json_encode((array) $xml); |
33 | - $json = str_replace(':{}',':null', $json); |
|
34 | - $json = str_replace(':[]',':null', $json); |
|
35 | - return json_decode($json, 1); // Work around to accept xml input |
|
33 | + $json = str_replace(':{}', ':null', $json); |
|
34 | + $json = str_replace(':[]', ':null', $json); |
|
35 | + return json_decode($json, 1); // Work around to accept xml input |
|
36 | 36 | } catch (\Exception $ex) { |
37 | 37 | throw new ParserException('Failed To Parse XML'); |
38 | 38 | } |