@@ -86,22 +86,22 @@ public function __construct(array $data)
8686 } else {
8787 // array
8888 $ this ->_properties [$ property ] = [];
89- foreach ($ data [$ property ] as $ item ) {
89+ foreach ($ data [$ property ] as $ key => $ item ) {
9090 if ($ type [0 ] === Type::STRING ) {
9191 if (!is_string ($ item )) {
9292 $ this ->_errors [] = "property ' $ property' must be array of strings, but array has " . gettype ($ item ) . " element. " ;
9393 }
94- $ this ->_properties [$ property ][] = $ item ;
94+ $ this ->_properties [$ property ][$ key ] = $ item ;
9595 } elseif (Type::isScalar ($ type [0 ])) {
96- $ this ->_properties [$ property ][] = $ item ;
96+ $ this ->_properties [$ property ][$ key ] = $ item ;
9797 } elseif ($ type [0 ] === Type::ANY ) {
9898 if (is_array ($ item ) && isset ($ item ['$ref ' ])) {
99- $ this ->_properties [$ property ][] = new Reference ($ item , null );
99+ $ this ->_properties [$ property ][$ key ] = new Reference ($ item , null );
100100 } else {
101- $ this ->_properties [$ property ][] = $ item ;
101+ $ this ->_properties [$ property ][$ key ] = $ item ;
102102 }
103103 } else {
104- $ this ->_properties [$ property ][] = $ this ->instantiate ($ type [0 ], $ item );
104+ $ this ->_properties [$ property ][$ key ] = $ this ->instantiate ($ type [0 ], $ item );
105105 }
106106 }
107107 }
@@ -190,15 +190,23 @@ public function getSerializableData()
190190 if ($ v instanceof SpecObjectInterface) {
191191 $ data [$ k ] = $ v ->getSerializableData ();
192192 } elseif (is_array ($ v )) {
193+ // test if php arrays should be represented as object in YAML/JSON
193194 $ 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+ }
201206 }
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 ;
202210 }
203211 if ($ toObject ) {
204212 $ data [$ k ] = (object ) $ data [$ k ];
@@ -297,11 +305,25 @@ protected function addError(string $error, $class = '')
297305 $ this ->_errors [] = end ($ shortName ).$ error ;
298306 }
299307
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+ */
300313 protected function hasProperty (string $ name ): bool
301314 {
302315 return isset ($ this ->_properties [$ name ]) || isset ($ this ->attributes ()[$ name ]);
303316 }
304317
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+
305327 protected function requireProperties (array $ names , array $ atLeastOne = [])
306328 {
307329 foreach ($ names as $ name ) {
0 commit comments