-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLoader.php
213 lines (180 loc) · 4.83 KB
/
Loader.php
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
<?php declare(strict_types=1);
/*
* This file is part of the Json-Works package.
*
* (c) John Stevenson <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace JohnStevenson\JsonWorks;
use \stdClass;
use JohnStevenson\JsonWorks\Helpers\Error;
use JohnStevenson\JsonWorks\Helpers\Utils;
/**
* A class for loading input data.
* @api
*/
class Loader
{
/**
* Processes input to be used as a document
*
* The input can be:
* - a json string, passed to json_decode
* - a .json filename, passed to file_get_contents then json_decode
* - an object, class, array
*
* @param mixed $input
* @return mixed
*/
public function getData($input)
{
$result = $this->processInput($input);
$dataType = gettype($result);
if ($dataType !== 'resource') {
return $result;
}
throw new \RuntimeException($this->getInputError($dataType));
}
/**
* Processes input to be used as a JSON Patch
*
* The input can be:
* - a json string, passed to json_decode
* - a .json filename, passed to file_get_contents then json_decode
* - an array
*
* The resulting data must be an array.
*
* @param mixed $input
* @return array<mixed>
*/
public function getPatch($input): array
{
$result = $this->processInput($input);
if (is_array($result)) {
return $result;
}
$dataType = gettype($result);
throw new \RuntimeException($this->getInputError($dataType));
}
/**
* Processes input to be used as a schema
*
* The input can be:
* - a json string, passed to json_decode
* - a .json filename, passed to file_get_contents then json_decode
* - an object
*
* The resulting data must be an object.
*
* @param mixed $input
* @return stdClass
*/
public function getSchema($input)
{
$result = $this->processInput($input);
if ($result instanceof stdClass) {
return $result;
}
$dataType = gettype($result);
throw new \RuntimeException($this->getInputError($dataType));
}
/**
* The main input processing method
*
* @param mixed $data
* @return mixed
*/
private function processInput($data)
{
if (is_string($data)) {
$data = $this->processStringInput($data);
} else {
$formatter = new Formatter();
$data = $formatter->copy($data);
}
return $data;
}
/**
* Processes a file or raw json
*
* @return mixed
*/
private function processStringInput(string $input)
{
if ($this->isFile($input)) {
$input = $this->getDataFromFile($input);
}
return $this->decodeJson($input);
}
private function isFile(string $input): bool
{
return pathinfo($input, PATHINFO_EXTENSION) === 'json';
}
/**
* Returns the contents of a file
*
* @throws \RuntimeException
*/
private function getDataFromFile(string $filename): string
{
$json = @file_get_contents($filename);
if ($json === false) {
$error = new Error();
throw new \RuntimeException($error->get(Error::ERR_NOT_FOUND, $filename));
}
return $json;
}
/**
* Decodes a json string
*
* This function allows a JSON text as per RFC 8259, except for an empty string
*
* @param string $value
* @return mixed
* @throws \RuntimeException
*/
private function decodeJson(string $value)
{
$result = $value;
$errorMsg = null;
if ($this->checkJson($value, $errorMsg)) {
$result = json_decode($value);
if (json_last_error() > 0) {
$errorMsg = json_last_error_msg();
}
}
if ($errorMsg !== null) {
throw new \RuntimeException($this->getJsonError($errorMsg));
}
return $result;
}
private function checkJson(string &$value, ?string &$errorMsg): bool
{
$value = trim($value);
if (Utils::stringIsJson($value)) {
return true;
}
if (Utils::stringIsEmpty($value)) {
$errorMsg = 'Syntax error';
}
return false;
}
private function getInputError(string $dataType): string
{
$error = new Error();
return $error->get(Error::ERR_BAD_INPUT, $dataType);
}
/**
* Returns a formatted json error message
*
*/
private function getJsonError(string $errorMsg): string
{
$error = new Error();
$msg = sprintf('json error: %s', $errorMsg);
return $error->get(Error::ERR_BAD_INPUT, $msg);
}
}