|
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
|
164 |
|
public static function ThrowIfNotDaftJson(string $class) : void |
|
14
|
|
|
{ |
|
15
|
164 |
|
if (false === is_a($class, DaftJson::class, true)) { |
|
16
|
128 |
|
throw new DaftObjectNotDaftJsonBadMethodCallException($class); |
|
17
|
|
|
} |
|
18
|
36 |
|
} |
|
19
|
|
|
|
|
20
|
10 |
|
public static function ThrowIfNotJsonType(string $jsonType) : void |
|
21
|
|
|
{ |
|
22
|
10 |
|
if (false === is_a($jsonType, DaftJson::class, true)) { |
|
23
|
2 |
|
throw new ClassDoesNotImplementClassException($jsonType, DaftJson::class); |
|
24
|
|
|
} |
|
25
|
8 |
|
} |
|
26
|
|
|
|
|
27
|
22 |
|
public static function MakeMapperThrowIfJsonDefNotValid( |
|
28
|
|
|
string $class, |
|
29
|
|
|
array $jsonDef, |
|
30
|
|
|
array $array |
|
31
|
|
|
) : Closure { |
|
32
|
|
|
$mapper = |
|
33
|
|
|
/** |
|
34
|
|
|
* @return mixed |
|
35
|
|
|
*/ |
|
36
|
|
|
function (string $prop) use ($jsonDef, $array, $class) { |
|
37
|
20 |
|
if (isset($jsonDef[$prop]) && false === is_array($array[$prop])) { |
|
38
|
4 |
|
static::ThrowBecauseArrayJsonTypeNotValid($class, $jsonDef[$prop], $prop); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
16 |
|
return $array[$prop]; |
|
42
|
22 |
|
}; |
|
43
|
|
|
|
|
44
|
22 |
|
return $mapper; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
24 |
|
public static function FilterThrowIfJsonDefNotValid( |
|
48
|
|
|
string $class, |
|
49
|
|
|
array $jsonProps, |
|
50
|
|
|
array $array |
|
51
|
|
|
) : array { |
|
52
|
|
|
$filter = function (string $prop) use ($jsonProps, $array, $class) : bool { |
|
53
|
24 |
|
if (false === in_array($prop, $jsonProps, true)) { |
|
54
|
2 |
|
throw new PropertyNotJsonDecodableException($class, $prop); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
24 |
|
return false === is_null($array[$prop]); |
|
58
|
24 |
|
}; |
|
59
|
|
|
|
|
60
|
24 |
|
return array_filter($array, $filter, ARRAY_FILTER_USE_KEY); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
6 |
|
public static function ArrayToJsonType(string $type, array $value, bool $writeAll) : DaftJson |
|
64
|
|
|
{ |
|
65
|
6 |
|
self::ThrowIfNotJsonType($type); |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var DaftJson $type |
|
69
|
|
|
*/ |
|
70
|
6 |
|
$type = $type; |
|
71
|
|
|
|
|
72
|
6 |
|
return $type::DaftObjectFromJsonArray($value, $writeAll); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return array<int, DaftJson>|DaftJson |
|
77
|
|
|
*/ |
|
78
|
10 |
|
final public static function DaftJsonFromJsonType( |
|
79
|
|
|
string $jsonType, |
|
80
|
|
|
string $prop, |
|
81
|
|
|
array $propVal, |
|
82
|
|
|
bool $writeAll |
|
83
|
|
|
) { |
|
84
|
10 |
|
if ('[]' === mb_substr($jsonType, -2)) { |
|
85
|
6 |
|
$jsonType = mb_substr($jsonType, 0, -2); |
|
86
|
|
|
|
|
87
|
6 |
|
self::ThrowIfNotJsonType($jsonType); |
|
88
|
|
|
|
|
89
|
4 |
|
return self::DaftObjectFromJsonTypeArray($jsonType, $prop, $propVal, $writeAll); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
4 |
|
return JsonTypeUtilities::ArrayToJsonType($jsonType, $propVal, $writeAll); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
56 |
|
public static function ThrowIfJsonDefNotValid(string $type, array $array) : array |
|
96
|
|
|
{ |
|
97
|
56 |
|
self::ThrowIfNotDaftJson($type); |
|
98
|
24 |
|
$jsonProps = $type::DaftObjectJsonPropertyNames(); |
|
99
|
24 |
|
$array = JsonTypeUtilities::FilterThrowIfJsonDefNotValid($type, $jsonProps, $array); |
|
100
|
22 |
|
$jsonDef = $type::DaftObjectJsonProperties(); |
|
101
|
|
|
|
|
102
|
22 |
|
$keys = array_keys($array); |
|
103
|
|
|
|
|
104
|
22 |
|
return array_combine($keys, array_map( |
|
105
|
22 |
|
JsonTypeUtilities::MakeMapperThrowIfJsonDefNotValid($type, $jsonDef, $array), |
|
106
|
22 |
|
$keys |
|
107
|
|
|
)); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return array<int, DaftJson> |
|
112
|
|
|
*/ |
|
113
|
4 |
|
protected static function DaftObjectFromJsonTypeArray( |
|
114
|
|
|
string $jsonType, |
|
115
|
|
|
string $prop, |
|
116
|
|
|
array $propVal, |
|
117
|
|
|
bool $writeAll |
|
118
|
|
|
) : array { |
|
119
|
4 |
|
JsonTypeUtilities::ThrowIfNotJsonType($jsonType); |
|
120
|
|
|
|
|
121
|
4 |
|
$out = []; |
|
122
|
|
|
|
|
123
|
4 |
|
foreach ($propVal as $val) { |
|
124
|
4 |
|
if (false === is_array($val)) { |
|
125
|
2 |
|
throw new PropertyNotJsonDecodableShouldBeArrayException($jsonType, $prop); |
|
126
|
|
|
} |
|
127
|
2 |
|
$out[] = JsonTypeUtilities::ArrayToJsonType($jsonType, $val, $writeAll); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
2 |
|
return $out; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
4 |
|
private static function ThrowBecauseArrayJsonTypeNotValid( |
|
134
|
|
|
string $class, |
|
135
|
|
|
string $type, |
|
136
|
|
|
string $prop |
|
137
|
|
|
) : void { |
|
138
|
4 |
|
if ('[]' === mb_substr($type, -2)) { |
|
139
|
2 |
|
throw new PropertyNotJsonDecodableShouldBeArrayException($class, $prop); |
|
140
|
|
|
} |
|
141
|
2 |
|
throw new PropertyNotJsonDecodableShouldBeArrayException($type, $prop); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|