@@ -35,19 +35,17 @@ public function get($model, string $key, $value, array $attributes): ?DataCollec
35
35
36
36
$ data = json_decode ($ value , true , flags: JSON_THROW_ON_ERROR );
37
37
38
- $ isAbstract = $ this ->isAbstractClassCast ();
39
- $ data = array_map (
40
- function (array $ item ) use ($ isAbstract ) {
41
- if ($ isAbstract ) {
42
- $ dataClass = $ this ->dataConfig ->morphMap ->getMorphedDataClass ($ item ['type ' ]) ?? $ item ['type ' ];
38
+ $ dataClass = $ this ->dataConfig ->getDataClass ($ this ->dataClass );
43
39
44
- return $ dataClass ::from ($ item ['data ' ]);
45
- }
40
+ $ data = array_map (function (array $ item ) use ($ dataClass ) {
41
+ if ($ dataClass ->isAbstract && $ dataClass ->transformable ) {
42
+ $ morphedClass = $ this ->dataConfig ->morphMap ->getMorphedDataClass ($ item ['type ' ]) ?? $ item ['type ' ];
46
43
47
- return ($ this ->dataClass )::from ($ item );
48
- },
49
- $ data
50
- );
44
+ return $ morphedClass ::from ($ item ['data ' ]);
45
+ }
46
+
47
+ return ($ this ->dataClass )::from ($ item );
48
+ }, $ data );
51
49
52
50
return new ($ this ->dataCollectionClass )($ this ->dataClass , $ data );
53
51
}
@@ -70,26 +68,24 @@ public function set($model, string $key, $value, array $attributes): ?string
70
68
throw CannotCastData::shouldBeArray ($ model ::class, $ key );
71
69
}
72
70
73
- $ isAbstract = $ this ->isAbstractClassCast ();
74
- $ data = array_map (
75
- function (array | BaseData $ item ) use ($ isAbstract ) {
76
- if ($ isAbstract ) {
77
- $ class = get_class ($ item );
78
-
79
- return [
80
- 'type ' => $ this ->dataConfig ->morphMap ->getDataClassAlias ($ class ) ?? $ class ,
81
- 'data ' => json_decode (json: $ item ->toJson (), associative: true , flags: JSON_THROW_ON_ERROR ),
82
- ];
83
- }
84
-
85
- return is_array ($ item )
86
- ? ($ this ->dataClass )::from ($ item )
87
- : $ item ;
88
- },
89
- $ value
90
- );
91
-
92
- if ($ isAbstract ) {
71
+ $ dataClass = $ this ->dataConfig ->getDataClass ($ this ->dataClass );
72
+
73
+ $ data = array_map (function (array |BaseData $ item ) use ($ dataClass ) {
74
+ if ($ dataClass ->isAbstract && $ item instanceof TransformableData) {
75
+ $ class = get_class ($ item );
76
+
77
+ return [
78
+ 'type ' => $ this ->dataConfig ->morphMap ->getDataClassAlias ($ class ) ?? $ class ,
79
+ 'data ' => json_decode (json: $ item ->toJson (), associative: true , flags: JSON_THROW_ON_ERROR ),
80
+ ];
81
+ }
82
+
83
+ return is_array ($ item )
84
+ ? ($ this ->dataClass )::from ($ item )
85
+ : $ item ;
86
+ }, $ value );
87
+
88
+ if ($ dataClass ->isAbstract ) {
93
89
return json_encode ($ data );
94
90
}
95
91
0 commit comments