@@ -166,7 +166,7 @@ public function map($json, ?string $className = null, ?string $collectionClassNa
166
166
$ collectionClassName = $ this ->getMappedClassName ($ collectionClassName , $ json );
167
167
}
168
168
169
- // Assert that classes exists
169
+ // Assert that the given classes exist
170
170
$ this ->assertClassesExists ($ className , $ collectionClassName );
171
171
172
172
// Handle collections
@@ -195,7 +195,13 @@ public function map($json, ?string $className = null, ?string $collectionClassNa
195
195
$ properties = $ this ->getProperties ($ className );
196
196
$ entity = $ this ->makeInstance ($ className );
197
197
198
+ // Return entity if JSON is not an array or object (is_iterable won't work here)
199
+ if (!is_array ($ json ) && !is_object ($ json )) {
200
+ return $ entity ;
201
+ }
202
+
198
203
// Process all children
204
+
199
205
/** @var string $propertyName */
200
206
foreach ($ json as $ propertyName => $ propertyValue ) {
201
207
// Replaces the property name with another one
@@ -413,7 +419,7 @@ private function hasClassAnnotation(string $className, string $annotationName):
413
419
*
414
420
* @return mixed|null
415
421
*/
416
- private function getDefaultValue (string $ className , string $ propertyName )
422
+ private function getDefaultValue (string $ className , string $ propertyName ): mixed
417
423
{
418
424
$ reflectionProperty = $ this ->getReflectionProperty ($ className , $ propertyName );
419
425
@@ -431,7 +437,7 @@ private function getDefaultValue(string $className, string $propertyName)
431
437
*
432
438
* @return bool
433
439
*/
434
- private function isNumericIndexArray ($ json ): bool
440
+ private function isNumericIndexArray (mixed $ json ): bool
435
441
{
436
442
foreach ($ json as $ propertyName => $ propertyValue ) {
437
443
if (is_int ($ propertyName )) {
@@ -449,8 +455,13 @@ private function isNumericIndexArray($json): bool
449
455
*
450
456
* @return bool
451
457
*/
452
- private function isIterableWithArraysOrObjects ($ json ): bool
458
+ private function isIterableWithArraysOrObjects (mixed $ json ): bool
453
459
{
460
+ // Return false if JSON is not an array or object (is_iterable won't work here)
461
+ if (!is_array ($ json ) && !is_object ($ json )) {
462
+ return false ;
463
+ }
464
+
454
465
foreach ($ json as $ propertyValue ) {
455
466
if (is_array ($ propertyValue )) {
456
467
continue ;
@@ -467,7 +478,7 @@ private function isIterableWithArraysOrObjects($json): bool
467
478
}
468
479
469
480
/**
470
- * Assert that the given classes exists .
481
+ * Assert that the given classes exist .
471
482
*
472
483
* @param class-string $className The class name of the initial element
473
484
* @param class-string|null $collectionClassName The class name of a collection used to
@@ -499,7 +510,7 @@ private function assertClassesExists(string $className, ?string $collectionClass
499
510
* @param string $name
500
511
* @param mixed $value
501
512
*/
502
- private function setProperty (object $ entity , string $ name , $ value ): void
513
+ private function setProperty (object $ entity , string $ name , mixed $ value ): void
503
514
{
504
515
// Handle variadic setters
505
516
if (is_array ($ value )) {
@@ -559,7 +570,7 @@ private function getType(string $className, string $propertyName): Type
559
570
*
560
571
* @throws DomainException
561
572
*/
562
- private function getValue ($ json , Type $ type )
573
+ private function getValue (mixed $ json , Type $ type ): mixed
563
574
{
564
575
if ((is_array ($ json ) || is_object ($ json )) && $ type ->isCollection ()) {
565
576
$ collectionType = $ this ->getCollectionValueType ($ type );
@@ -641,7 +652,7 @@ private function getClassNameFromType(Type $type): string
641
652
*
642
653
* @throws DomainException
643
654
*/
644
- private function getMappedClassName (string $ className , $ json ): string
655
+ private function getMappedClassName (string $ className , mixed $ json ): string
645
656
{
646
657
if (array_key_exists ($ className , $ this ->classMap )) {
647
658
$ classNameOrClosure = $ this ->classMap [$ className ];
@@ -669,7 +680,7 @@ private function getMappedClassName(string $className, $json): string
669
680
*
670
681
* @throws DomainException
671
682
*/
672
- private function getClassName ($ json , Type $ type ): string
683
+ private function getClassName (mixed $ json , Type $ type ): string
673
684
{
674
685
return $ this ->getMappedClassName (
675
686
$ this ->getClassNameFromType ($ type ),
@@ -678,7 +689,7 @@ private function getClassName($json, Type $type): string
678
689
}
679
690
680
691
/**
681
- * Cast node to collection.
692
+ * Cast node to a collection.
682
693
*
683
694
* @param mixed $json
684
695
* @param Type $type
@@ -687,7 +698,7 @@ private function getClassName($json, Type $type): string
687
698
*
688
699
* @throws DomainException
689
700
*/
690
- private function asCollection ($ json , Type $ type ): ?array
701
+ private function asCollection (mixed $ json , Type $ type ): ?array
691
702
{
692
703
if ($ json === null ) {
693
704
return null ;
@@ -712,7 +723,7 @@ private function asCollection($json, Type $type): ?array
712
723
*
713
724
* @throws DomainException
714
725
*/
715
- private function asObject ($ json , Type $ type )
726
+ private function asObject (mixed $ json , Type $ type ): mixed
716
727
{
717
728
/** @var class-string<TEntity> $className */
718
729
$ className = $ this ->getClassName ($ json , $ type );
@@ -748,7 +759,7 @@ private function isCustomType(string $typeClassName): bool
748
759
*
749
760
* @return mixed
750
761
*/
751
- private function callCustomClosure ($ json , string $ typeClassName )
762
+ private function callCustomClosure (mixed $ json , string $ typeClassName ): mixed
752
763
{
753
764
$ callback = $ this ->types [$ typeClassName ];
754
765
0 commit comments