File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed
src/ConstantContact/Definition Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -270,4 +270,27 @@ public function testMinLength() : void
270
270
$ this ->expectException (\PHPFUI \ConstantContact \Exception \InvalidLength::class);
271
271
$ fixture ->string = 'fred ' ;
272
272
}
273
+
274
+ public function testDefaultObjects () : void
275
+ {
276
+ $ address = [];
277
+ $ address ['created_at ' ] = (string )new \PHPFUI \ConstantContact \DateTime ();
278
+ $ address ['permission_to_send ' ] = 'explicit ' ;
279
+
280
+ $ email_address = new \PHPFUI \ConstantContact \Definition \EmailAddressPut ($ address );
281
+ $ contact = ['email_address ' => $ email_address ];
282
+
283
+ $ contactBody = new \PHPFUI \ConstantContact \Definition \ContactPutRequest ($ contact );
284
+ $ contactBody ->update_source = 'Account ' ;
285
+ $ contactBody ->street_addresses = [new \PHPFUI \ConstantContact \Definition \StreetAddressPut ([
286
+ 'kind ' => 'home ' ,
287
+ 'street ' => 'address ' ,
288
+ 'city ' => 'town ' ,
289
+ 'state ' => 'state ' ,
290
+ 'postal_code ' => 'zip ' ,
291
+ 'country ' => 'USA ' , ])];
292
+ $ json = $ contactBody ->getJSON ();
293
+
294
+ $ this ->assertStringContainsString ('created_at ' , $ json );
295
+ }
273
296
}
Original file line number Diff line number Diff line change @@ -33,6 +33,11 @@ abstract class Base
33
33
*/
34
34
protected static array $ requiredFields = [];
35
35
36
+ /**
37
+ * @var bool $constructingFromArray set to true if we are constructing from an array so we don't type check for objects
38
+ */
39
+ private bool $ constructingFromArray = false ;
40
+
36
41
/**
37
42
* $var array<string, mixed> the actual object data by field name.
38
43
*/
@@ -73,7 +78,9 @@ public function __construct(array $initialValues = [])
73
78
74
79
if (! empty (static ::$ fields [$ field ]))
75
80
{
81
+ $ this ->constructingFromArray = true ;
76
82
$ this ->{$ actualField } = $ value ;
83
+ $ this ->constructingFromArray = false ;
77
84
}
78
85
elseif (! \is_array ($ type ) && ! isset (self ::$ scalars [$ type ]))
79
86
{
@@ -172,7 +179,7 @@ public function __set(string $field, mixed $value)
172
179
}
173
180
}
174
181
}
175
- elseif ($ expectedType != $ type )
182
+ elseif ($ expectedType != $ type && ! $ this -> constructingFromArray )
176
183
{
177
184
throw new \PHPFUI \ConstantContact \Exception \InvalidType (static ::class . ":: {$ actualField } is of type {$ type } but should be type {$ expectedType }" );
178
185
}
You can’t perform that action at this time.
0 commit comments