1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author SignpostMarv |
4
|
|
|
*/ |
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace SignpostMarv\DaftObject; |
8
|
|
|
|
9
|
|
|
use Closure; |
10
|
|
|
|
11
|
|
|
class JsonTypeUtilities |
12
|
|
|
{ |
13
|
|
|
const IS_A_STRINGS = true; |
14
|
|
|
|
15
|
|
|
const INT_TYPE_EXPECT_IS_ARRAY = -2; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @psalm-param class-string<DaftObject> $class |
19
|
|
|
* |
20
|
|
|
* @psalm-return class-string<DaftJson> |
21
|
|
|
*/ |
22
|
460 |
|
public static function ThrowIfNotDaftJson(string $class) : string |
23
|
|
|
{ |
24
|
460 |
|
if ( ! is_a($class, DaftJson::class, self::IS_A_STRINGS)) { |
25
|
356 |
|
throw Exceptions\Factory::DaftObjectNotDaftJsonBadMethodCallException($class); |
26
|
|
|
} |
27
|
|
|
|
28
|
104 |
|
return $class; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @template T as DaftJson |
33
|
|
|
* |
34
|
|
|
* @psalm-param class-string<T> $jsonType |
35
|
|
|
* |
36
|
|
|
* @param array<int|string, scalar|(scalar|array|object|null)[]|object|null> $propVal |
37
|
|
|
* |
38
|
|
|
* @return array<int, DaftJson>|DaftJson |
39
|
|
|
* |
40
|
|
|
* @psalm-return array<int, T>|T |
41
|
|
|
*/ |
42
|
28 |
|
final public static function DaftJsonFromJsonType( |
43
|
|
|
string $jsonType, |
44
|
|
|
array $propVal, |
45
|
|
|
bool $writeAll |
46
|
|
|
) { |
47
|
28 |
|
return JsonTypeUtilities::ArrayToJsonType($jsonType, $propVal, $writeAll); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array<int|string, scalar|(scalar|array|object|null)[]|object|null> $array |
52
|
|
|
* |
53
|
|
|
* @psalm-param class-string<DaftJson> $type |
54
|
|
|
* |
55
|
|
|
* @return array<int|string, scalar|(scalar|array|object|null)[]|object|null> |
56
|
|
|
*/ |
57
|
64 |
|
public static function ThrowIfJsonDefNotValid(string $type, array $array) : array |
58
|
|
|
{ |
59
|
64 |
|
$jsonProps = $type::DaftObjectJsonPropertyNames(); |
60
|
64 |
|
$array = JsonTypeUtilities::FilterThrowIfJsonDefNotValid($type, $jsonProps, $array); |
61
|
60 |
|
$jsonDef = $type::DaftObjectJsonProperties(); |
62
|
|
|
|
63
|
60 |
|
$keys = array_keys($array); |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var array<int|string, scalar|(scalar|array|object|null)[]|object|null> |
67
|
|
|
*/ |
68
|
60 |
|
$out = array_combine($keys, array_map( |
69
|
60 |
|
JsonTypeUtilities::MakeMapperThrowIfJsonDefNotValid($type, $jsonDef, $array), |
70
|
30 |
|
$keys |
71
|
|
|
)); |
72
|
|
|
|
73
|
52 |
|
return $out; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @template T as DaftJson |
78
|
|
|
* |
79
|
|
|
* @psalm-param class-string<DaftObject> $jsonType |
80
|
|
|
* |
81
|
|
|
* @psalm-return class-string<T> |
82
|
|
|
*/ |
83
|
128 |
|
public static function ThrowIfNotJsonType(string $jsonType) : string |
84
|
|
|
{ |
85
|
128 |
|
if ( ! is_a($jsonType, DaftJson::class, DefinitionAssistant::IS_A_STRINGS)) { |
86
|
72 |
|
throw Exceptions\Factory::ClassDoesNotImplementClassException($jsonType, DaftJson::class); |
87
|
|
|
} |
88
|
|
|
|
89
|
56 |
|
return $jsonType; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @template T as DaftJson |
94
|
|
|
* |
95
|
|
|
* @psalm-param class-string<T> $jsonType |
96
|
|
|
* |
97
|
|
|
* @param array<int|string, scalar|(scalar|(scalar|array|object|null)[]|object|null)[]|object|null> $propVal |
98
|
|
|
* |
99
|
|
|
* @return array<int, DaftJson> |
100
|
|
|
* |
101
|
|
|
* @psalm-return array<int, T> |
102
|
|
|
*/ |
103
|
48 |
|
public static function DaftObjectFromJsonTypeArray( |
104
|
|
|
string $jsonType, |
105
|
|
|
string $prop, |
106
|
|
|
array $propVal, |
107
|
|
|
bool $writeAll |
108
|
|
|
) : array { |
109
|
48 |
|
return array_map( |
110
|
|
|
/** |
111
|
|
|
* @param scalar|(scalar|(scalar|array|object|null)[]|object|null)[]|object|null $val |
112
|
|
|
* |
113
|
|
|
* @psalm-return T |
114
|
|
|
* |
115
|
|
|
* @throws Exceptions\PropertyNotJsonDecodableShouldBeArrayException if $val is not an array |
116
|
|
|
*/ |
117
|
|
|
function ($val) use ($jsonType, $writeAll, $prop) : DaftJson { |
118
|
24 |
|
if ( ! is_array($val)) { |
119
|
4 |
|
throw Exceptions\Factory::PropertyNotJsonDecodableShouldBeArrayException($jsonType, $prop); |
120
|
|
|
} |
121
|
|
|
|
122
|
20 |
|
return JsonTypeUtilities::ArrayToJsonType($jsonType, $val, $writeAll); |
123
|
48 |
|
}, |
124
|
36 |
|
array_values($propVal) |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param array<int|string, scalar|array|object|null> $array |
130
|
|
|
* |
131
|
|
|
* @psalm-param class-string<DaftJson> $class_name |
132
|
|
|
*/ |
133
|
52 |
|
public static function DaftJsonClosure( |
134
|
|
|
array $array, |
135
|
|
|
bool $writeAll, |
136
|
|
|
string $class_name |
137
|
|
|
) : Closure { |
138
|
52 |
|
$jsonDef = $class_name::DaftObjectJsonProperties(); |
139
|
|
|
|
140
|
|
|
return |
141
|
|
|
/** |
142
|
|
|
* @return scalar|array|object|null |
143
|
|
|
*/ |
144
|
|
|
function (string $prop) use ($array, $jsonDef, $writeAll) { |
145
|
|
|
/** |
146
|
|
|
* @var string|null |
147
|
|
|
*/ |
148
|
52 |
|
$jsonType = $jsonDef[$prop] ?? null; |
149
|
|
|
|
150
|
52 |
|
if ( ! is_string($jsonType)) { |
151
|
36 |
|
return $array[$prop]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @var array<int|string, scalar|(scalar|(scalar|array|object|null)[]|object|null)[]|object|null> |
156
|
|
|
*/ |
157
|
44 |
|
$propVal = (is_array($array[$prop]) ? $array[$prop] : [$array[$prop]]); |
158
|
|
|
|
159
|
44 |
|
if ('[]' === mb_substr($jsonType, -2)) { |
160
|
|
|
/** |
161
|
|
|
* @psalm-var class-string<DaftObject> |
162
|
|
|
*/ |
163
|
44 |
|
$jsonType = mb_substr($jsonType, 0, -2); |
164
|
|
|
|
165
|
44 |
|
$jsonType = JsonTypeUtilities::ThrowIfNotJsonType($jsonType); |
166
|
|
|
|
167
|
44 |
|
return JsonTypeUtilities::DaftObjectFromJsonTypeArray( |
168
|
33 |
|
$jsonType, |
169
|
33 |
|
$prop, |
170
|
33 |
|
$propVal, |
171
|
33 |
|
$writeAll |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @psalm-var class-string<DaftObject> |
177
|
|
|
*/ |
178
|
28 |
|
$jsonType = $jsonType; |
179
|
|
|
|
180
|
28 |
|
$jsonType = JsonTypeUtilities::ThrowIfNotJsonType($jsonType); |
181
|
|
|
|
182
|
28 |
|
return JsonTypeUtilities::DaftJsonFromJsonType($jsonType, $propVal, $writeAll); |
183
|
52 |
|
}; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param array<string|int, string> $jsonDef |
188
|
|
|
* @param (scalar|array|object|null)[] $array |
189
|
|
|
* |
190
|
|
|
* @psalm-param class-string $class |
191
|
|
|
*/ |
192
|
60 |
|
private static function MakeMapperThrowIfJsonDefNotValid( |
193
|
|
|
string $class, |
194
|
|
|
array $jsonDef, |
195
|
|
|
array $array |
196
|
|
|
) : Closure { |
197
|
|
|
$mapper = |
198
|
|
|
/** |
199
|
|
|
* @return scalar|array|object|null |
200
|
|
|
*/ |
201
|
|
|
function (string $prop) use ($jsonDef, $array, $class) { |
202
|
60 |
|
if (isset($jsonDef[$prop]) && false === is_array($array[$prop])) { |
203
|
8 |
|
static::ThrowBecauseArrayJsonTypeNotValid( |
204
|
6 |
|
$class, |
205
|
8 |
|
$jsonDef[$prop], |
206
|
4 |
|
$prop |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
|
210
|
52 |
|
return $array[$prop]; |
211
|
60 |
|
}; |
212
|
|
|
|
213
|
60 |
|
return $mapper; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @psalm-param class-string<DaftJson> $class |
218
|
|
|
* |
219
|
|
|
* @param (scalar|array|object|null)[] $array |
220
|
|
|
* |
221
|
|
|
* @return (scalar|array|object|null)[] |
222
|
|
|
*/ |
223
|
64 |
|
private static function FilterThrowIfJsonDefNotValid( |
224
|
|
|
string $class, |
225
|
|
|
array $jsonProps, |
226
|
|
|
array $array |
227
|
|
|
) : array { |
228
|
|
|
$filter = function (string $prop) use ($jsonProps, $array, $class) : bool { |
229
|
64 |
|
if ( ! in_array($prop, $jsonProps, DefinitionAssistant::IN_ARRAY_STRICT_MODE)) { |
230
|
4 |
|
throw Exceptions\Factory::PropertyNotJsonDecodableException($class, $prop); |
231
|
|
|
} |
232
|
|
|
|
233
|
60 |
|
return false === is_null($array[$prop]); |
234
|
64 |
|
}; |
235
|
|
|
|
236
|
64 |
|
return array_filter($array, $filter, ARRAY_FILTER_USE_KEY); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @template T as DaftJson |
241
|
|
|
* |
242
|
|
|
* @psalm-param class-string<T> $type |
243
|
|
|
* |
244
|
|
|
* @param array<int|string, scalar|(scalar|array|object|null)[]|object|null> $value |
245
|
|
|
* |
246
|
|
|
* @psalm-return T |
247
|
|
|
*/ |
248
|
28 |
|
private static function ArrayToJsonType(string $type, array $value, bool $writeAll) : DaftJson |
249
|
|
|
{ |
250
|
28 |
|
return $type::DaftObjectFromJsonArray($value, $writeAll); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @psalm-param class-string $class |
255
|
|
|
*/ |
256
|
8 |
|
private static function ThrowBecauseArrayJsonTypeNotValid( |
257
|
|
|
string $class, |
258
|
|
|
string $type, |
259
|
|
|
string $prop |
260
|
|
|
) : void { |
261
|
8 |
|
if ('[]' === mb_substr($type, self::INT_TYPE_EXPECT_IS_ARRAY)) { |
262
|
4 |
|
throw Exceptions\Factory::PropertyNotJsonDecodableShouldBeArrayException($class, $prop); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @psalm-var class-string |
267
|
|
|
*/ |
268
|
4 |
|
$type = $type; |
269
|
|
|
|
270
|
4 |
|
throw Exceptions\Factory::PropertyNotJsonDecodableShouldBeArrayException($type, $prop); |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|