@@ -31,14 +31,20 @@ final public static function create(mixed ...$args): static
3131 throw DataCreationException::invalidParamsPassed (static ::class);
3232 }
3333
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 ();
4248 } catch (Throwable $ e ) {
4349 throw DataCreationException::unableToCreateInstance (static ::class, $ e );
4450 }
@@ -59,16 +65,22 @@ final public static function from(mixed $value): static
5965 throw DeserializeException::invalidValue ();
6066 }
6167
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 ();
7284 } catch (Throwable $ e ) {
7385 throw DeserializeException::generic ($ e );
7486 }
@@ -93,30 +105,6 @@ protected static function instanceWithoutConstructorFrom(ClassContext $context,
93105 return $ instance ;
94106 }
95107
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-
120108 /**
121109 * @return array<string, mixed>
122110 *
0 commit comments