@@ -155,28 +155,35 @@ public static function getHydrator($class)
155
155
156
156
public static function getSimpleHydrator ($ class )
157
157
{
158
- $ baseHydrator = self ::$ simpleHydrators ['stdClass ' ] ??= static function ($ properties , $ object ) {
158
+ $ baseHydrator = self ::$ simpleHydrators ['stdClass ' ] ??= (function ($ properties , $ object ) {
159
+ $ readonly = (array ) $ this ;
160
+
159
161
foreach ($ properties as $ name => &$ value ) {
160
- $ object ->$ name = &$ value ;
162
+ $ object ->$ name = $ value ;
163
+
164
+ if (!($ readonly [$ name ] ?? false )) {
165
+ $ object ->$ name = &$ value ;
166
+ }
161
167
}
162
- };
168
+ })-> bindTo ( new \ stdClass ()) ;
163
169
164
170
switch ($ class ) {
165
171
case 'stdClass ' :
166
172
return $ baseHydrator ;
167
173
168
174
case 'ErrorException ' :
169
- return $ baseHydrator ->bindTo (null , new class () extends \ErrorException {
175
+ return $ baseHydrator ->bindTo (new \ stdClass () , new class () extends \ErrorException {
170
176
});
171
177
172
178
case 'TypeError ' :
173
- return $ baseHydrator ->bindTo (null , new class () extends \Error {
179
+ return $ baseHydrator ->bindTo (new \ stdClass () , new class () extends \Error {
174
180
});
175
181
176
182
case 'SplObjectStorage ' :
177
183
return static function ($ properties , $ object ) {
178
184
foreach ($ properties as $ name => &$ value ) {
179
185
if ("\0" !== $ name ) {
186
+ $ object ->$ name = $ value ;
180
187
$ object ->$ name = &$ value ;
181
188
continue ;
182
189
}
@@ -202,14 +209,22 @@ public static function getSimpleHydrator($class)
202
209
if ("\0" === $ name ) {
203
210
$ constructor ($ object , $ value );
204
211
} else {
212
+ $ object ->$ name = $ value ;
205
213
$ object ->$ name = &$ value ;
206
214
}
207
215
}
208
216
};
209
217
}
210
218
211
219
if (!$ classReflector ->isInternal ()) {
212
- return $ baseHydrator ->bindTo (null , $ class );
220
+ $ readonly = new \stdClass ();
221
+ foreach ($ classReflector ->getProperties (\ReflectionProperty::IS_READONLY ) as $ propertyReflector ) {
222
+ if ($ class === $ propertyReflector ->class ) {
223
+ $ readonly ->{$ propertyReflector ->name } = true ;
224
+ }
225
+ }
226
+
227
+ return $ baseHydrator ->bindTo ($ readonly , $ class );
213
228
}
214
229
215
230
if ($ classReflector ->name !== $ class ) {
@@ -232,6 +247,7 @@ public static function getSimpleHydrator($class)
232
247
if ($ setValue = $ propertySetters [$ name ] ?? null ) {
233
248
$ setValue ($ object , $ value );
234
249
} else {
250
+ $ object ->$ name = $ value ;
235
251
$ object ->$ name = &$ value ;
236
252
}
237
253
}
@@ -252,10 +268,10 @@ public static function getPropertyScopes($class)
252
268
$ name = $ property ->name ;
253
269
254
270
if (\ReflectionProperty::IS_PRIVATE & $ flags ) {
255
- $ propertyScopes ["\0$ class \0$ name " ] = $ propertyScopes [$ name ] = [$ class , $ name ];
271
+ $ propertyScopes ["\0$ class \0$ name " ] = $ propertyScopes [$ name ] = [$ class , $ name, $ flags & \ReflectionProperty:: IS_READONLY ? $ class : null ];
256
272
continue ;
257
273
}
258
- $ propertyScopes [$ name ] = [$ flags & \ReflectionProperty::IS_READONLY ? $ property ->class : $ class , $ name ];
274
+ $ propertyScopes [$ name ] = [$ class , $ name , $ flags & \ReflectionProperty::IS_READONLY ? $ property ->class : null ];
259
275
260
276
if (\ReflectionProperty::IS_PROTECTED & $ flags ) {
261
277
$ propertyScopes ["\0* \0$ name " ] = $ propertyScopes [$ name ];
@@ -268,7 +284,9 @@ public static function getPropertyScopes($class)
268
284
foreach ($ r ->getProperties (\ReflectionProperty::IS_PRIVATE ) as $ property ) {
269
285
if (!$ property ->isStatic ()) {
270
286
$ name = $ property ->name ;
271
- $ propertyScopes ["\0$ class \0$ name " ] = [$ class , $ name ];
287
+ $ readonlyScope = $ property ->isReadOnly () ? $ class : null ;
288
+ $ propertyScopes ["\0$ class \0$ name " ] = [$ class , $ name , $ readonlyScope ];
289
+ $ propertyScopes [$ name ] ??= [$ class , $ name , $ readonlyScope ];
272
290
}
273
291
}
274
292
}
0 commit comments