@@ -203,36 +203,49 @@ protected function parseNamedType(
203
203
string $ name
204
204
): mixed
205
205
{
206
+ $ propertyPath = $ path . ". " . $ name ;
207
+
206
208
if ($ type ->isBuiltin ()) {
207
- $ valid = match ($ type ->getName ()) {
208
- "bool " => is_bool ($ value ),
209
- "int " => is_int ($ value ),
210
- "float " => is_float ($ value ) || is_int ($ value ),
211
- "string " => is_string ($ value ),
212
- "mixed " => true ,
213
- "array " => is_array ($ value ),
214
- "object " => is_object ($ value ),
215
- "false " => $ value === false ,
216
- "true " => $ value === true ,
217
- default => throw new UnsupportedTypeException ($ path . ". " . $ name , $ type ->getName ()),
218
- };
219
-
220
- if (!$ valid ) {
221
- throw new IncorrectTypeException ($ path . ". " . $ name , $ type ->getName (), $ value );
209
+ if (!$ this ->isBuiltInTypeValid ($ type ->getName (), $ value , $ propertyPath )) {
210
+ throw new IncorrectTypeException ($ propertyPath , $ type ->getName (), $ value );
222
211
}
223
212
224
213
return $ value ;
225
214
}
226
215
227
216
if (!is_array ($ value )) {
228
- throw new IncorrectTypeException ($ path . " . " . $ name , $ type ->getName (), $ value );
217
+ throw new IncorrectTypeException ($ propertyPath , $ type ->getName (), $ value );
229
218
}
230
219
231
220
if ($ type ->getName () === "self " ) {
232
221
$ deserializer = $ this ;
233
222
} else {
234
223
$ deserializer = new static ($ type ->getName ());
235
224
}
236
- return $ deserializer ->deserialize ($ value , $ path . ". " . $ name );
225
+ return $ deserializer ->deserialize ($ value , $ propertyPath );
226
+ }
227
+
228
+ /**
229
+ * Check if a built-in type is valid
230
+ * @param string $type the type to check
231
+ * @param mixed $value the value to check
232
+ * @param string $path the path to the data in the base data (used for error messages)
233
+ * @return bool if the type is valid
234
+ * @throws UnsupportedTypeException if the type of the property is unsupported
235
+ */
236
+ protected function isBuiltInTypeValid (string $ type , mixed $ value , string $ path ): bool
237
+ {
238
+ return match ($ type ) {
239
+ "bool " => is_bool ($ value ),
240
+ "int " => is_int ($ value ),
241
+ "float " => is_float ($ value ) || is_int ($ value ),
242
+ "string " => is_string ($ value ),
243
+ "mixed " => true ,
244
+ "array " => is_array ($ value ),
245
+ "object " => is_object ($ value ),
246
+ "false " => $ value === false ,
247
+ "true " => $ value === true ,
248
+ default => throw new UnsupportedTypeException ($ path , $ type ),
249
+ };
237
250
}
238
251
}
0 commit comments