-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathJsonMapper.php
More file actions
353 lines (294 loc) · 11.8 KB
/
JsonMapper.php
File metadata and controls
353 lines (294 loc) · 11.8 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
<?php
declare(strict_types=1);
namespace Brick\JsonMapper;
use Brick\JsonMapper\NameMapper\NullMapper;
use Brick\JsonMapper\Reflection\Reflector;
use Brick\JsonMapper\Reflection\Type\UnionType;
use JsonException;
use LogicException;
use ReflectionClass;
use ReflectionException;
use ReflectionParameter;
use stdClass;
/**
* @psalm-suppress MixedAssignment
*/
final class JsonMapper
{
private readonly Reflector $reflector;
public function __construct(
/**
* Allows untyped arrays, i.e. arrays without a corresponding "@param" docblock or with "@param array".
* When a property declared as such is found, it will be assigned the raw JSON array.
*/
bool $allowUntypedArrays = false,
/**
* Allows untyped objects, i.e. objects declared as "stdClass" or "object".
* When a property declared as such is found, it will be assigned the raw JSON object.
*/
bool $allowUntypedObjects = false,
/**
* Allows "mixed" type.
* When a property declared as such is found, it will be assigned the raw JSON value.
*/
bool $allowMixed = false,
/**
* Controls how extra properties in the JSON object are handled.
* Extra properties are properties of the JSON object that do not have a corresponding constructor parameter in
* the PHP class.
*/
private readonly OnExtraProperties $onExtraProperties = OnExtraProperties::THROW_EXCEPTION,
/**
* Controls how missing properties in the JSON object are handled.
* Missing properties are constructor parameters in the PHP class that do not have a corresponding property in
* the JSON object.
*/
private readonly OnMissingProperties $onMissingProperties = OnMissingProperties::THROW_EXCEPTION,
/**
* Mapper to convert JSON property names to PHP property names.
* By default, no conversion is performed.
*/
private readonly NameMapper $jsonToPhpNameMapper = new NullMapper(),
/**
* Mapper to convert PHP property names to JSON property names.
* By default, no conversion is performed.
*/
private readonly NameMapper $phpToJsonNameMapper = new NullMapper(),
) {
$this->reflector = new Reflector(
$allowUntypedArrays,
$allowUntypedObjects,
$allowMixed,
);
}
/**
* @template T of object
*
* @param class-string<T> $className
*
* @return T
*
* @throws JsonMapperException
*/
public function map(string $json, string $className): object
{
try {
$data = json_decode($json, flags: JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
throw new JsonMapperException('Invalid JSON data: ' . $e->getMessage(), $e);
}
if (! $data instanceof stdClass) {
throw new JsonMapperException(sprintf('Unexpected JSON data: expected object, got %s.', gettype($data)));
}
return $this->mapToObject($data, $className);
}
/**
* @template T of object
*
* @param class-string<T> $className
*
* @return T
*
* @throws JsonMapperException
*/
private function mapToObject(stdClass $jsonData, string $className): object
{
try {
$reflectionClass = new ReflectionClass($className);
} catch (ReflectionException $e) {
throw new JsonMapperException('Invalid class name: ' . $className, $e);
}
$reflectionConstructor = $reflectionClass->getConstructor();
if ($reflectionConstructor === null) {
throw new JsonMapperException('Class ' . $className . ' must have a constructor.');
}
$parameters = [];
$consumedJsonPropertyNames = [];
foreach ($reflectionConstructor->getParameters() as $reflectionParameter) {
$jsonPropertyName = $this->phpToJsonNameMapper->mapName($reflectionParameter->getName());
$consumedJsonPropertyNames[] = $jsonPropertyName;
$parameters[$reflectionParameter->getName()] = $this->getParameterValue(
$jsonData,
$jsonPropertyName,
$reflectionParameter,
);
}
if ($this->onExtraProperties === OnExtraProperties::THROW_EXCEPTION) {
/** @psalm-suppress MixedAssignment, RawObjectIteration */
foreach ($jsonData as $jsonPropertyName => $_) {
/** @var string $jsonPropertyName https://github.com/vimeo/psalm/issues/9108 */
if (! in_array($jsonPropertyName, $consumedJsonPropertyNames, true)) {
throw new JsonMapperException([
sprintf(
'Unexpected property "%s" in JSON data: ' .
'%s::__construct() does not have a corresponding $%s parameter.',
$jsonPropertyName,
$className,
$this->jsonToPhpNameMapper->mapName($jsonPropertyName),
),
'If you want to allow extra properties, change the $onExtraProperties option.',
]);
}
}
}
return $reflectionClass->newInstanceArgs($parameters);
}
/**
* @throws JsonMapperException
*/
private function getParameterValue(
stdClass $jsonData,
string $jsonPropertyName,
ReflectionParameter $reflectionParameter,
): mixed {
$parameterType = $this->reflector->getParameterType($reflectionParameter);
if (!property_exists($jsonData, $jsonPropertyName)) {
if ($this->onMissingProperties === OnMissingProperties::SET_NULL) {
if ($parameterType->allowsNull) {
return null;
}
}
if ($this->onMissingProperties === OnMissingProperties::SET_DEFAULT) {
if ($reflectionParameter->isDefaultValueAvailable()) {
// TODO we should technically check if the default value is compatible with the parameter type,
// as the type declared as @param may be more specific than the PHP type.
return $reflectionParameter->getDefaultValue();
}
}
throw new JsonMapperException([
sprintf('Missing property "%s" in JSON data.', $jsonPropertyName),
match ($this->onMissingProperties) {
OnMissingProperties::SET_NULL => 'The parameter does not allow null.',
OnMissingProperties::SET_DEFAULT => 'The parameter does not have a default value.',
OnMissingProperties::THROW_EXCEPTION => 'If you want to allow missing properties, change the $onMissingProperties option.',
}
]);
}
$jsonValue = $jsonData->{$jsonPropertyName};
return $this->mapValue($jsonValue, $jsonPropertyName, $parameterType);
}
/**
* @throws JsonMapperException
*/
private function mapValue(
mixed $jsonValue,
string $jsonPropertyName,
UnionType $parameterType,
): mixed {
if ($parameterType->allowsMixed) {
return $jsonValue;
}
if ($jsonValue instanceof stdClass) {
return $this->getJsonObjectValue($jsonValue, $jsonPropertyName, $parameterType);
}
if (is_array($jsonValue)) {
if ($parameterType->allowsRawArray) {
return $jsonValue;
}
if ($parameterType->arrayType === null) {
throw new JsonMapperException('Property ' . $jsonPropertyName . ' is an array, but the parameter does not accept arrays.');
}
$type = $parameterType->arrayType->type;
return array_map(
// TODO $jsonPropertyName is wrong here, rework the exception message
fn (mixed $item): mixed => $this->mapValue($item, $jsonPropertyName, $type),
$jsonValue,
);
}
if (is_string($jsonValue)) {
if ($parameterType->allowsString) {
return $jsonValue;
}
foreach ($parameterType->enumTypes as $enumType) {
if ($enumType->isStringBacked) {
return ($enumType->name)::from($jsonValue);
}
}
foreach ($parameterType->types as $type) {
if ($type->name == \DateTime::class) {
return new \DateTime($jsonValue);
}
}
// TODO "Parameter %s of class %s does not accept string" + JSON path
throw new JsonMapperException('Property ' . $jsonPropertyName . ' cannot be a string.');
}
if (is_int($jsonValue)) {
if ($parameterType->allowsInt) {
return $jsonValue;
}
foreach ($parameterType->enumTypes as $enumType) {
if ($enumType->isIntBacked) {
return ($enumType->name)::from($jsonValue);
}
}
throw new JsonMapperException('Property ' . $jsonPropertyName . ' cannot be a string.');
}
if (is_float($jsonValue)) {
if ($parameterType->allowsFloat) {
return $jsonValue;
}
throw new JsonMapperException('Property ' . $jsonPropertyName . ' cannot be a float.');
}
if ($jsonValue === null) {
if ($parameterType->allowsNull) {
return null;
}
throw new JsonMapperException('Property ' . $jsonPropertyName . ' cannot be null.');
}
if ($jsonValue === true) {
if ($parameterType->allowsTrue) {
return true;
}
throw new JsonMapperException('Property ' . $jsonPropertyName . ' cannot be true.');
}
if ($jsonValue === false) {
if ($parameterType->allowsFalse) {
return false;
}
throw new JsonMapperException('Property ' . $jsonPropertyName . ' cannot be false.');
}
throw new LogicException('Unreachable. If you see this, please report a bug.');
}
/**
* @throws JsonMapperException
*/
private function getJsonObjectValue(
stdClass $jsonValue,
string $jsonPropertyName,
UnionType $parameterType,
): object {
if ($parameterType->allowsRawObject) {
return $jsonValue;
}
if (! $parameterType->classTypes) {
throw new JsonMapperException('Property ' . $jsonPropertyName . ' is an object, but the parameter does not accept objects.');
}
if (count($parameterType->classTypes) === 1) {
return $this->mapToObject($jsonValue, $parameterType->classTypes[0]->name);
}
$matches = [];
$errors = [];
foreach ($parameterType->classTypes as $classType) {
try {
$matches[] = $this->mapToObject($jsonValue, $classType->name);
} catch (JsonMapperException $e) {
$errors[] = [$classType->name, $e->getFirstMessage()];
}
}
if (! $matches) {
throw new JsonMapperException(
"JSON object does not match any of the allowed PHP classes:\n" . implode("\n", array_map(
fn (array $error) => sprintf(' - %s: %s', ...$error),
$errors,
),
));
}
if (count($matches) === 1) {
return $matches[0];
}
throw new JsonMapperException(sprintf(
'JSON object matches multiple PHP classes: %s.',
implode(', ', array_map(get_class(...), $matches)),
));
}
}