12
12
namespace Symfony \UX \LiveComponent \Util ;
13
13
14
14
use Symfony \Component \HttpFoundation \Request ;
15
+ use Symfony \UX \LiveComponent \Exception \HydrationException ;
15
16
use Symfony \UX \LiveComponent \LiveComponentHydrator ;
16
17
use Symfony \UX \LiveComponent \Metadata \LiveComponentMetadata ;
18
+ use Symfony \UX \LiveComponent \Metadata \LivePropMetadata ;
17
19
18
20
/**
19
21
* @author Nicolas Rigaud <[email protected] >
@@ -46,12 +48,21 @@ public function extract(Request $request, LiveComponentMetadata $metadata, objec
46
48
if (\is_array ($ value ) && $ this ->isNumericIndexedArray ($ value )) {
47
49
// Sort numeric array
48
50
ksort ($ value );
49
- } elseif (( '' === $ value ) && (!$ livePropMetadata ->isBuiltIn () || 'array ' === $ livePropMetadata ->getType ())) {
51
+ } elseif ('' === $ value && null !== $ livePropMetadata -> getType ( ) && (!$ livePropMetadata ->isBuiltIn () || 'array ' === $ livePropMetadata ->getType ())) {
50
52
// Cast empty string to empty array for objects and arrays
51
53
$ value = [];
52
54
}
53
55
54
- $ data [$ livePropMetadata ->getName ()] = $ this ->hydrator ->hydrateValue ($ value , $ livePropMetadata , $ component );
56
+ try {
57
+ $ hydratedValue = $ this ->hydrator ->hydrateValue ($ value , $ livePropMetadata , $ component );
58
+
59
+ if ($ this ->isValueTypeConsistent ($ hydratedValue , $ livePropMetadata )) {
60
+ // Only set data if hydrated value type is consistent with prop metadata type
61
+ $ data [$ livePropMetadata ->getName ()] = $ hydratedValue ;
62
+ }
63
+ } catch (HydrationException ) {
64
+ // Skip hydration errors (e.g. with objects)
65
+ }
55
66
}
56
67
}
57
68
}
@@ -63,4 +74,14 @@ private function isNumericIndexedArray(array $array): bool
63
74
{
64
75
return 0 === \count (array_filter (array_keys ($ array ), 'is_string ' ));
65
76
}
77
+
78
+ private function isValueTypeConsistent (mixed $ value , LivePropMetadata $ livePropMetadata ): bool
79
+ {
80
+ $ propType = $ livePropMetadata ->getType ();
81
+
82
+ return
83
+ \in_array ($ propType , [null , 'mixed ' ])
84
+ || $ livePropMetadata ->isBuiltIn () && ('\is_ ' .$ propType )($ value )
85
+ || !$ livePropMetadata ->isBuiltIn () && $ value instanceof $ propType ;
86
+ }
66
87
}
0 commit comments