22
22
* @author Mathias Arlaud <[email protected] >
23
23
*
24
24
* @internal
25
- *
26
- * @template T of mixed
27
25
*/
28
26
abstract class Deserializer
29
27
{
@@ -43,62 +41,53 @@ public function __construct(
43
41
}
44
42
45
43
/**
46
- * @param T $data
47
- * @param array<string, mixed> $context
48
- */
49
- abstract protected function deserializeScalar (mixed $ data , Type $ type , array $ context ): mixed ;
50
-
51
- /**
52
- * @param T $data
53
44
* @param array<string, mixed> $context
54
45
*/
55
- abstract protected function deserializeEnum (mixed $ data , Type $ type , array $ context ): mixed ;
46
+ abstract protected function deserializeScalar (mixed $ dataOrResource , Type $ type , array $ context ): mixed ;
56
47
57
48
/**
58
- * @param T $data
59
49
* @param array<string, mixed> $context
60
50
*
61
51
* @return \Iterator<mixed>|null
62
52
*/
63
- abstract protected function deserializeList (mixed $ data , Type $ type , array $ context ): ?\Iterator ;
53
+ abstract protected function deserializeList (mixed $ dataOrResource , Type $ type , array $ context ): ?\Iterator ;
64
54
65
55
/**
66
- * @param T $data
67
56
* @param array<string, mixed> $context
68
57
*
69
58
* @return \Iterator<string, mixed>|null
70
59
*/
71
- abstract protected function deserializeDict (mixed $ data , Type $ type , array $ context ): ?\Iterator ;
60
+ abstract protected function deserializeDict (mixed $ dataOrResource , Type $ type , array $ context ): ?\Iterator ;
72
61
73
62
/**
74
- * @param T $data
75
63
* @param array<string, mixed> $context
76
64
*
77
65
* @return \Iterator<string, mixed>|array<string, mixed>|null
78
66
*/
79
- abstract protected function deserializeObjectProperties (mixed $ data , Type $ type , array $ context ): \Iterator |array |null ;
67
+ abstract protected function deserializeObjectProperties (mixed $ dataOrResource , Type $ type , array $ context ): \Iterator |array |null ;
80
68
81
69
/**
82
- * @param T $data
83
70
* @param array<string, mixed> $context
84
- *
85
- * @return T
86
71
*/
87
- abstract protected function deserializeMixed (mixed $ data , Type $ type , array $ context ): mixed ;
72
+ abstract protected function deserializeMixed (mixed $ dataOrResource , array $ context ): mixed ;
88
73
89
74
/**
90
- * @param T $data
91
75
* @param array<string, mixed> $context
92
76
*
93
77
* @return callable(): mixed
94
78
*/
95
- abstract protected function propertyValueCallable (Type |UnionType $ type , mixed $ data , mixed $ value , array $ context ): callable ;
79
+ abstract protected function propertyValueCallable (Type |UnionType $ type , mixed $ dataOrResource , mixed $ value , array $ context ): callable ;
96
80
97
81
/**
98
- * @param T $data
82
+ * @param resource $resource
99
83
* @param array<string, mixed> $context
100
84
*/
101
- final public function deserialize (mixed $ data , Type |UnionType $ type , array $ context ): mixed
85
+ abstract public function deserialize (mixed $ resource , Type |UnionType $ type , array $ context ): mixed ;
86
+
87
+ /**
88
+ * @param array<string, mixed> $context
89
+ */
90
+ protected function doDeserialize (mixed $ dataOrResource , Type |UnionType $ type , array $ context ): mixed
102
91
{
103
92
if ($ type instanceof UnionType) {
104
93
if (!isset ($ context ['union_selector ' ][$ typeString = (string ) $ type ])) {
@@ -110,7 +99,7 @@ final public function deserialize(mixed $data, Type|UnionType $type, array $cont
110
99
}
111
100
112
101
if ($ type ->isScalar ()) {
113
- $ scalar = $ this ->deserializeScalar ($ data , $ type , $ context );
102
+ $ scalar = $ this ->deserializeScalar ($ dataOrResource , $ type , $ context );
114
103
115
104
if (null === $ scalar ) {
116
105
if (!$ type ->isNullable ()) {
@@ -134,7 +123,7 @@ final public function deserialize(mixed $data, Type|UnionType $type, array $cont
134
123
}
135
124
136
125
if ($ type ->isEnum ()) {
137
- $ enum = $ this ->deserializeEnum ( $ data , $ type , $ context );
126
+ $ enum = $ this ->deserializeScalar ( $ dataOrResource , $ type , $ context );
138
127
139
128
if (null === $ enum ) {
140
129
if (!$ type ->isNullable ()) {
@@ -152,13 +141,11 @@ final public function deserialize(mixed $data, Type|UnionType $type, array $cont
152
141
}
153
142
154
143
if ($ type ->isCollection ()) {
155
- if ($ type ->isList ()) {
156
- $ collection = $ this ->deserializeList ($ data , $ type , $ context );
157
- } elseif ($ type ->isDict ()) {
158
- $ collection = $ this ->deserializeDict ($ data , $ type , $ context );
159
- } else {
160
- $ collection = $ this ->deserializeMixed ($ data , $ type , $ context );
161
- }
144
+ $ collection = match (true ) {
145
+ $ type ->isList () => $ this ->deserializeList ($ dataOrResource , $ type , $ context ),
146
+ $ type ->isDict () => $ this ->deserializeDict ($ dataOrResource , $ type , $ context ),
147
+ default => $ this ->deserializeMixed ($ dataOrResource , $ context ),
148
+ };
162
149
163
150
if (null === $ collection ) {
164
151
if (!$ type ->isNullable ()) {
@@ -174,15 +161,15 @@ final public function deserialize(mixed $data, Type|UnionType $type, array $cont
174
161
if ($ type ->isObject ()) {
175
162
if (!$ type ->hasClass ()) {
176
163
$ object = new \stdClass ();
177
- foreach ($ this ->deserializeMixed ($ data , $ type , $ context ) as $ property => $ value ) {
164
+ foreach ($ this ->deserializeMixed ($ dataOrResource , $ context ) as $ property => $ value ) {
178
165
$ object ->{$ property } = $ value ;
179
166
}
180
167
181
168
return $ object ;
182
169
}
183
170
184
171
$ className = $ type ->className ();
185
- $ objectProperties = $ this ->deserializeObjectProperties ($ data , $ type , $ context );
172
+ $ objectProperties = $ this ->deserializeObjectProperties ($ dataOrResource , $ type , $ context );
186
173
187
174
if (null === $ objectProperties ) {
188
175
if (!$ type ->isNullable ()) {
@@ -215,7 +202,7 @@ final public function deserialize(mixed $data, Type|UnionType $type, array $cont
215
202
$ hookResult = $ hook (
216
203
$ reflection ,
217
204
$ name ,
218
- fn (string $ type , array $ context ) => $ this ->propertyValueCallable (self ::$ cache ['type ' ][$ type ] ??= TypeFactory::createFromString ($ type ), $ data , $ value , $ context )(),
205
+ fn (string $ type , array $ context ) => $ this ->propertyValueCallable (self ::$ cache ['type ' ][$ type ] ??= TypeFactory::createFromString ($ type ), $ dataOrResource , $ value , $ context )(),
219
206
$ context ,
220
207
);
221
208
@@ -241,7 +228,7 @@ final public function deserialize(mixed $data, Type|UnionType $type, array $cont
241
228
242
229
self ::$ cache ['property_type ' ][$ identifier ] ??= TypeFactory::createFromString ($ this ->reflectionTypeExtractor ->extractFromProperty ($ reflection ->getProperty ($ name )));
243
230
244
- $ valueCallables [$ name ] = $ this ->propertyValueCallable (self ::$ cache ['property_type ' ][$ identifier ], $ data , $ value , $ context );
231
+ $ valueCallables [$ name ] = $ this ->propertyValueCallable (self ::$ cache ['property_type ' ][$ identifier ], $ dataOrResource , $ value , $ context );
245
232
}
246
233
247
234
if (isset ($ context ['instantiator ' ])) {
@@ -267,6 +254,6 @@ final public function deserialize(mixed $data, Type|UnionType $type, array $cont
267
254
return $ object ;
268
255
}
269
256
270
- return $ this ->deserializeMixed ($ data , $ type , $ context );
257
+ return $ this ->deserializeMixed ($ dataOrResource , $ context );
271
258
}
272
259
}
0 commit comments