@@ -86,22 +86,22 @@ public function __construct(array $data)
86
86
} else {
87
87
// array
88
88
$ this ->_properties [$ property ] = [];
89
- foreach ($ data [$ property ] as $ item ) {
89
+ foreach ($ data [$ property ] as $ key => $ item ) {
90
90
if ($ type [0 ] === Type::STRING ) {
91
91
if (!is_string ($ item )) {
92
92
$ this ->_errors [] = "property ' $ property' must be array of strings, but array has " . gettype ($ item ) . " element. " ;
93
93
}
94
- $ this ->_properties [$ property ][] = $ item ;
94
+ $ this ->_properties [$ property ][$ key ] = $ item ;
95
95
} elseif (Type::isScalar ($ type [0 ])) {
96
- $ this ->_properties [$ property ][] = $ item ;
96
+ $ this ->_properties [$ property ][$ key ] = $ item ;
97
97
} elseif ($ type [0 ] === Type::ANY ) {
98
98
if (is_array ($ item ) && isset ($ item ['$ref ' ])) {
99
- $ this ->_properties [$ property ][] = new Reference ($ item , null );
99
+ $ this ->_properties [$ property ][$ key ] = new Reference ($ item , null );
100
100
} else {
101
- $ this ->_properties [$ property ][] = $ item ;
101
+ $ this ->_properties [$ property ][$ key ] = $ item ;
102
102
}
103
103
} else {
104
- $ this ->_properties [$ property ][] = $ this ->instantiate ($ type [0 ], $ item );
104
+ $ this ->_properties [$ property ][$ key ] = $ this ->instantiate ($ type [0 ], $ item );
105
105
}
106
106
}
107
107
}
@@ -190,15 +190,23 @@ public function getSerializableData()
190
190
if ($ v instanceof SpecObjectInterface) {
191
191
$ data [$ k ] = $ v ->getSerializableData ();
192
192
} elseif (is_array ($ v )) {
193
+ // test if php arrays should be represented as object in YAML/JSON
193
194
$ toObject = false ;
194
- $ j = 0 ;
195
- foreach ($ v as $ i => $ d ) {
196
- if ($ j ++ !== $ i ) {
197
- $ toObject = true ;
198
- }
199
- if ($ d instanceof SpecObjectInterface) {
200
- $ data [$ k ][$ i ] = $ d ->getSerializableData ();
195
+ if (!empty ($ v )) {
196
+ // case 1: non-empty array should be an object if it does not contain
197
+ // consecutive numeric keys
198
+ $ j = 0 ;
199
+ foreach ($ v as $ i => $ d ) {
200
+ if ($ j ++ !== $ i ) {
201
+ $ toObject = true ;
202
+ }
203
+ if ($ d instanceof SpecObjectInterface) {
204
+ $ data [$ k ][$ i ] = $ d ->getSerializableData ();
205
+ }
201
206
}
207
+ } elseif (isset ($ this ->attributes ()[$ k ]) && is_array ($ this ->attributes ()[$ k ]) && 2 === count ($ this ->attributes ()[$ k ])) {
208
+ // case 2: Attribute type is an object (specified in attributes() by an array which specifies two items (key and value type)
209
+ $ toObject = true ;
202
210
}
203
211
if ($ toObject ) {
204
212
$ data [$ k ] = (object ) $ data [$ k ];
@@ -297,11 +305,25 @@ protected function addError(string $error, $class = '')
297
305
$ this ->_errors [] = end ($ shortName ).$ error ;
298
306
}
299
307
308
+ /**
309
+ * @param string $name property name.
310
+ * @return bool true when this object has a property with a non-null value or the property is defined in the OpenAPI spec.
311
+ * @deprecated since 1.6.0, will be removed in 2.0.0
312
+ */
300
313
protected function hasProperty (string $ name ): bool
301
314
{
302
315
return isset ($ this ->_properties [$ name ]) || isset ($ this ->attributes ()[$ name ]);
303
316
}
304
317
318
+ /**
319
+ * @param string $name property name.
320
+ * @return bool true, when a property has a non-null value (does not check for default values)
321
+ */
322
+ protected function hasPropertyValue (string $ name ): bool
323
+ {
324
+ return isset ($ this ->_properties [$ name ]);
325
+ }
326
+
305
327
protected function requireProperties (array $ names , array $ atLeastOne = [])
306
328
{
307
329
foreach ($ names as $ name ) {
0 commit comments