@@ -31,14 +31,20 @@ final public static function create(mixed ...$args): static
31
31
throw DataCreationException::invalidParamsPassed (static ::class);
32
32
}
33
33
34
- $ data = DeserializePipeline::createFromArray ()
35
- ->sendThenReturn (new DeserializePipelinePassable (
36
- classContext: $ context ,
37
- data: $ value
38
- ))
39
- ->data ;
40
-
41
- return static ::instanceWithConstructorCallFrom ($ context , $ data );
34
+ $ dataCreationClosure = static function () use ($ context , $ value ): static {
35
+ $ data = DeserializePipeline::createFromArray ()
36
+ ->sendThenReturn (new DeserializePipelinePassable (
37
+ classContext: $ context ,
38
+ data: $ value
39
+ ))
40
+ ->data ;
41
+
42
+ return $ context ->constructFromArray ($ data );
43
+ };
44
+
45
+ return $ context ->isLazy
46
+ ? $ context ->newLazyProxy ($ dataCreationClosure )
47
+ : $ dataCreationClosure ();
42
48
} catch (Throwable $ e ) {
43
49
throw DataCreationException::unableToCreateInstance (static ::class, $ e );
44
50
}
@@ -59,16 +65,22 @@ final public static function from(mixed $value): static
59
65
throw DeserializeException::invalidValue ();
60
66
}
61
67
62
- $ data = DeserializePipeline::hydrateFromArray ()
63
- ->sendThenReturn (new DeserializePipelinePassable (
64
- classContext: $ context ,
65
- data: $ value
66
- ))
67
- ->data ;
68
-
69
- return $ context ->hasComputedProperties
70
- ? static ::instanceWithConstructorCallFrom ($ context , $ data )
71
- : static ::instanceWithoutConstructorFrom ($ context , $ data );
68
+ $ dataCreationClosure = static function () use ($ context , $ value ): static {
69
+ $ data = DeserializePipeline::hydrateFromArray ()
70
+ ->sendThenReturn (new DeserializePipelinePassable (
71
+ classContext: $ context ,
72
+ data: $ value
73
+ ))
74
+ ->data ;
75
+
76
+ return $ context ->hasComputedProperties
77
+ ? $ context ->constructFromArray ($ data )
78
+ : static ::instanceWithoutConstructorFrom ($ context , $ data );
79
+ };
80
+
81
+ return $ context ->isLazy
82
+ ? $ context ->newLazyProxy ($ dataCreationClosure )
83
+ : $ dataCreationClosure ();
72
84
} catch (Throwable $ e ) {
73
85
throw DeserializeException::generic ($ e );
74
86
}
@@ -93,30 +105,6 @@ protected static function instanceWithoutConstructorFrom(ClassContext $context,
93
105
return $ instance ;
94
106
}
95
107
96
- /**
97
- * @param ClassContext<static> $context
98
- * @param array<string, mixed> $value
99
- *
100
- * @throws Throwable
101
- */
102
- protected static function instanceWithConstructorCallFrom (ClassContext $ context , array $ value ): static
103
- {
104
- /** @var array<string, mixed> $args */
105
- $ args = [];
106
-
107
- foreach ($ context ->constructorParams as $ paramName ) {
108
- $ propertyContext = $ context ->properties [$ paramName ] ?? null ;
109
-
110
- if (!$ propertyContext ) {
111
- throw DeserializeException::invalidParamsPassed ();
112
- }
113
-
114
- $ args [$ paramName ] = $ propertyContext ->deserializeFrom ($ value );
115
- }
116
-
117
- return $ context ->newInstanceWithConstructorCall (...$ args );
118
- }
119
-
120
108
/**
121
109
* @return array<string, mixed>
122
110
*
0 commit comments