@@ -61,43 +61,47 @@ public function toArray(array $except = [], int $depth = 512)
61
61
62
62
$ depth --;
63
63
$ modelData = [];
64
- array_push ($ except , 'rawModelDataInput ' , 'errorRegistry ' );
64
+ array_push ($ except , 'rawModelDataInput ' , 'errorRegistry ' , ' customSerializer ' );
65
65
66
- foreach (get_object_vars ( $ this ) as $ key => $ value ) {
66
+ foreach (get_class_vars ( get_class ( $ this ) ) as $ key => $ value ) {
67
67
if (in_array ($ key , $ except )) {
68
68
continue ;
69
69
}
70
70
71
71
if ($ customSerializer = $ this ->getCustomSerializerMethod ($ key )) {
72
- $ this ->handleSerializedValue ($ modelData , $ key , $ this ->{$ customSerializer }());
72
+ $ this ->handleSerializedValue ($ modelData , $ key , $ this ->{$ customSerializer }(), $ depth , $ except );
73
73
continue ;
74
74
}
75
75
76
- if (is_array ($ value )) {
77
- $ subData = [];
78
- foreach ($ value as $ subKey => $ element ) {
79
- $ subData [$ subKey ] = $ this ->evaluateAttribute ($ element , $ depth , $ except );
80
- }
81
- $ modelData [$ key ] = $ subData ;
82
- } else {
83
- $ modelData [$ key ] = $ this ->evaluateAttribute ($ value , $ depth , $ except );
84
- }
76
+ $ modelData [$ key ] = $ this ->serializeValue ($ this ->$ key , $ depth , $ except );
85
77
}
86
78
87
79
return $ modelData ;
88
80
}
89
81
90
- private function handleSerializedValue (array &$ data , $ key , $ serializedValue ): void
82
+ private function handleSerializedValue (array &$ data , $ key , $ serializedValue, int $ depth , array $ except ): void
91
83
{
92
84
if ($ serializedValue instanceof SerializedValue &&
93
85
$ serializedValue ->getSerializationStrategy () === SerializedValue::STRATEGY_MERGE_VALUE
94
86
) {
95
- $ data = array_merge ($ data , $ serializedValue ->getSerializedValue ());
87
+ $ data = array_merge ($ data , $ this -> serializeValue ( $ serializedValue ->getSerializedValue (), $ depth , $ except ));
96
88
97
89
return ;
98
90
}
99
91
100
- $ data [$ key ] = $ serializedValue ;
92
+ $ data [$ key ] = $ this ->serializeValue ($ serializedValue , $ depth , $ except );
93
+ }
94
+
95
+ private function serializeValue ($ value , int $ depth , array $ except ) {
96
+ if (is_array ($ value )) {
97
+ $ subData = [];
98
+ foreach ($ value as $ subKey => $ element ) {
99
+ $ subData [$ subKey ] = $ this ->evaluateAttribute ($ element , $ depth , $ except );
100
+ }
101
+ return $ subData ;
102
+ }
103
+
104
+ return $ this ->evaluateAttribute ($ value , $ depth , $ except );
101
105
}
102
106
103
107
private function evaluateAttribute ($ attribute , int $ depth , array $ except )
@@ -113,9 +117,9 @@ private function evaluateAttribute($attribute, int $depth, array $except)
113
117
return (0 >= $ depth )
114
118
? null
115
119
: (
116
- method_exists ($ attribute , 'toArray ' )
117
- ? $ attribute ->toArray ($ except , $ depth - 1 )
118
- : get_object_vars ($ attribute )
120
+ method_exists ($ attribute , 'toArray ' )
121
+ ? $ attribute ->toArray ($ except , $ depth - 1 )
122
+ : get_object_vars ($ attribute )
119
123
);
120
124
}
121
125
0 commit comments