@@ -16,12 +16,28 @@ final class ObjectDemo implements \JsonSerializable
1616{
1717 public string $ field1 ;
1818 public int $ field2 ;
19+ public ?float $ field3 ;
1920
2021 public function jsonSerialize (): array
2122 {
2223 return [
2324 'field1 ' => $ this ->field1 ,
2425 'field2 ' => $ this ->field2 ,
26+ 'field3 ' => $ this ->field3 ,
27+ ];
28+ }
29+ }
30+
31+ final readonly class ObjectConstructDemo implements \JsonSerializable
32+ {
33+ public function __construct (public string $ field1 , public int $ field2 , public ?float $ field3 ) {}
34+
35+ public function jsonSerialize (): array
36+ {
37+ return [
38+ 'field1 ' => $ this ->field1 ,
39+ 'field2 ' => $ this ->field2 ,
40+ 'field3 ' => $ this ->field3 ,
2541 ];
2642 }
2743}
@@ -35,7 +51,11 @@ final class ObjectSchemaTest extends TestCase
3551{
3652 public function testImmutability (): void
3753 {
38- $ schema = new ObjectSchema (['field1 ' => new StringSchema (), 'field2 ' => new IntSchema ()]);
54+ $ schema = new ObjectSchema ([
55+ 'field1 ' => new StringSchema (),
56+ 'field2 ' => new IntSchema (),
57+ 'field3 ' => new FloatSchema (),
58+ ]);
3959
4060 self ::assertNotSame ($ schema , $ schema ->nullable ());
4161 self ::assertNotSame ($ schema , $ schema ->nullable (false ));
@@ -78,11 +98,15 @@ public function testConstructWithoutFieldSchema(): void
7898
7999 public function testParseSuccess (): void
80100 {
81- $ input = ['field1 ' => 'test ' , 'field2 ' => 1 ];
101+ $ input = ['field1 ' => 'test ' , 'field2 ' => 1 , ' field3 ' => 3.14159 ];
82102
83- $ schema = new ObjectSchema (['field1 ' => new StringSchema (), 'field2 ' => new IntSchema ()]);
103+ $ schema = new ObjectSchema ([
104+ 'field1 ' => new StringSchema (),
105+ 'field2 ' => new IntSchema (),
106+ 'field3 ' => new FloatSchema (),
107+ ]);
84108
85- $ output = $ schema ->parse ([... $ input, ' field3 ' => 1.5 ] );
109+ $ output = $ schema ->parse ($ input );
86110
87111 self ::assertInstanceOf (\stdClass::class, $ output );
88112
@@ -91,9 +115,13 @@ public function testParseSuccess(): void
91115
92116 public function testParseSuccessWithClass (): void
93117 {
94- $ input = ['field1 ' => 'test ' , 'field2 ' => 1 ];
118+ $ input = ['field1 ' => 'test ' , 'field2 ' => 1 , ' field3 ' => 3.14159 ];
95119
96- $ schema = new ObjectSchema (['field1 ' => new StringSchema (), 'field2 ' => new IntSchema ()], ObjectDemo::class);
120+ $ schema = new ObjectSchema ([
121+ 'field1 ' => new StringSchema (),
122+ 'field2 ' => new IntSchema (),
123+ 'field3 ' => new FloatSchema (),
124+ ], ObjectDemo::class);
97125
98126 $ output = $ schema ->parse ($ input );
99127
@@ -102,13 +130,35 @@ public function testParseSuccessWithClass(): void
102130 self ::assertSame ($ input , (array ) $ output );
103131 }
104132
133+ public function testParseSuccessWithConstructClass (): void
134+ {
135+ $ input = ['field1 ' => 'test ' , 'field2 ' => 1 , 'field3 ' => 3.14159 ];
136+
137+ $ schema = new ObjectSchema ([
138+ 'field1 ' => new StringSchema (),
139+ 'field2 ' => new IntSchema (),
140+ 'field3 ' => new FloatSchema (),
141+ ], ObjectConstructDemo::class, true );
142+
143+ $ output = $ schema ->parse ($ input );
144+
145+ self ::assertInstanceOf (ObjectConstructDemo::class, $ output );
146+
147+ self ::assertSame ($ input , (array ) $ output );
148+ }
149+
105150 public function testParseSuccessWithStdClassInput (): void
106151 {
107152 $ input = new \stdClass ();
108153 $ input ->field1 = 'test ' ;
109154 $ input ->field2 = 1 ;
155+ $ input ->field3 = 3.14159 ;
110156
111- $ schema = new ObjectSchema (['field1 ' => new StringSchema (), 'field2 ' => new IntSchema ()]);
157+ $ schema = new ObjectSchema ([
158+ 'field1 ' => new StringSchema (),
159+ 'field2 ' => new IntSchema (),
160+ 'field3 ' => new FloatSchema (),
161+ ]);
112162
113163 $ output = $ schema ->parse ($ input );
114164
@@ -119,9 +169,13 @@ public function testParseSuccessWithStdClassInput(): void
119169
120170 public function testParseSuccessWithIteratorInput (): void
121171 {
122- $ input = new \ArrayIterator (['field1 ' => 'test ' , 'field2 ' => 1 ]);
172+ $ input = new \ArrayIterator (['field1 ' => 'test ' , 'field2 ' => 1 , ' field3 ' => 3.14159 ]);
123173
124- $ schema = new ObjectSchema (['field1 ' => new StringSchema (), 'field2 ' => new IntSchema ()]);
174+ $ schema = new ObjectSchema ([
175+ 'field1 ' => new StringSchema (),
176+ 'field2 ' => new IntSchema (),
177+ 'field3 ' => new FloatSchema (),
178+ ]);
125179
126180 $ output = $ schema ->parse ($ input );
127181
@@ -135,8 +189,13 @@ public function testParseSuccessWithJsonSerialzableObject(): void
135189 $ input = new ObjectDemo ();
136190 $ input ->field1 = 'test ' ;
137191 $ input ->field2 = 1 ;
192+ $ input ->field3 = 3.14159 ;
138193
139- $ schema = new ObjectSchema (['field1 ' => new StringSchema (), 'field2 ' => new IntSchema ()]);
194+ $ schema = new ObjectSchema ([
195+ 'field1 ' => new StringSchema (),
196+ 'field2 ' => new IntSchema (),
197+ 'field3 ' => new FloatSchema (),
198+ ]);
140199
141200 $ output = $ schema ->parse ($ input );
142201
0 commit comments