|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the reva2/jsonapi. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Sergey Revenko <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace Reva2\JsonApi\Decoders; |
|
13
|
|
|
|
|
14
|
|
|
use Neomerx\JsonApi\Document\Error; |
|
15
|
|
|
use Neomerx\JsonApi\Exceptions\JsonApiException; |
|
16
|
|
|
use Reva2\JsonApi\Contracts\Decoders\DataParserInterface; |
|
17
|
|
|
use Reva2\JsonApi\Contracts\Decoders\DecoderInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* JSON API request decoder |
|
21
|
|
|
* |
|
22
|
|
|
* @package Reva2\JsonApi\Decoders |
|
23
|
|
|
* @author Sergey Revenko <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class RequestDecoder implements DecoderInterface |
|
26
|
|
|
{ |
|
27
|
|
|
const INVALID_JSON_ERROR = 'add83393-3bdc-4042-a9c8-ecd5415e1a04'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* ApiDocument parser |
|
31
|
|
|
* |
|
32
|
|
|
* @var DataParserInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $parser; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Expected document type |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $contentType; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Constructor |
|
45
|
|
|
* |
|
46
|
|
|
* @param DataParserInterface $parser |
|
47
|
|
|
*/ |
|
48
|
2 |
|
public function __construct(DataParserInterface $parser) |
|
49
|
|
|
{ |
|
50
|
2 |
|
$this->parser = $parser; |
|
51
|
2 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @inheritdoc |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function setContentType($type) |
|
57
|
|
|
{ |
|
58
|
1 |
|
$this->contentType = $type; |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @inheritdoc |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public function decode($content) |
|
65
|
|
|
{ |
|
66
|
2 |
|
$data = $this->decodeJson($content); |
|
67
|
|
|
|
|
68
|
1 |
|
return (null !== $this->contentType) ? $this->parser->parseDocument($data, $this->contentType) : $data; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Decode JSON string to object |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $content |
|
75
|
|
|
* @return object |
|
76
|
|
|
*/ |
|
77
|
2 |
|
protected function decodeJson($content) |
|
78
|
|
|
{ |
|
79
|
|
|
$jsonErrors = array( |
|
80
|
2 |
|
JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded', |
|
81
|
2 |
|
JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch', |
|
82
|
2 |
|
JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found', |
|
83
|
2 |
|
JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON', |
|
84
|
2 |
|
JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded' |
|
85
|
2 |
|
); |
|
86
|
|
|
|
|
87
|
|
|
// Can we use JSON_BIGINT_AS_STRING? |
|
88
|
2 |
|
$options = 0; |
|
89
|
2 |
|
if (version_compare(PHP_VERSION, '5.4.0', '>=') && !(defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)) { |
|
90
|
2 |
|
$options = JSON_BIGINT_AS_STRING; |
|
91
|
2 |
|
}; |
|
92
|
|
|
|
|
93
|
2 |
|
$data = json_decode($content, false, 512, $options); |
|
94
|
2 |
|
if (JSON_ERROR_NONE !== json_last_error()) { |
|
95
|
1 |
|
$last = json_last_error(); |
|
96
|
1 |
|
$error = 'Unknown error'; |
|
97
|
|
|
|
|
98
|
1 |
|
if (isset($jsonErrors[$last])) { |
|
99
|
1 |
|
$error = $jsonErrors[$last]; |
|
100
|
1 |
|
} |
|
101
|
|
|
|
|
102
|
1 |
|
$apiError = new Error( |
|
103
|
1 |
|
rand(), |
|
104
|
1 |
|
null, |
|
105
|
1 |
|
400, |
|
106
|
1 |
|
self::INVALID_JSON_ERROR, |
|
107
|
1 |
|
'Unable to parse JSON data', |
|
108
|
|
|
$error |
|
109
|
1 |
|
); |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
1 |
|
throw new JsonApiException($apiError, 400); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
1 |
|
return $data; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|