11<?php
22
3- namespace cebe \openapi \ spec ;
3+ namespace cebe \openapi ;
44
55use cebe \openapi \exceptions \ReadonlyPropertyException ;
66use cebe \openapi \exceptions \UnknownPropertyException ;
77
88/**
9+ * Base class for all spec objects.
910 *
11+ * Implements property management and validation basics.
1012 *
1113 * @author Carsten Brandt <mail@cebe.cc>
1214 */
@@ -45,9 +47,38 @@ public function __construct(array $data)
4547 $ this ->_errors [] = "property ' $ property' must be array, but " . gettype ($ data [$ property ]) . " given. " ;
4648 continue ;
4749 }
48- $ this ->_properties [$ property ] = [];
49- foreach ($ data [$ property ] as $ item ) {
50- $ this ->_properties [$ property ][] = new $ type [0 ]($ item );
50+ switch (count ($ type )) {
51+ case 1 :
52+ // array
53+ $ this ->_properties [$ property ] = [];
54+ foreach ($ data [$ property ] as $ item ) {
55+ if ($ type [0 ] === 'string ' ) {
56+ if (!is_string ($ item )) {
57+ $ this ->_errors [] = "property ' $ property' must be array of strings, but array has " . gettype ($ item ) . " element. " ;
58+ }
59+ $ this ->_properties [$ property ][] = $ item ;
60+ } else {
61+ $ this ->_properties [$ property ][] = new $ type [0 ]($ item );
62+ }
63+ }
64+ break ;
65+ case 2 :
66+ // map
67+ if ($ type [0 ] !== 'string ' ) {
68+ throw new \Exception ('Invalid map key type: ' . $ type [0 ]);
69+ }
70+ $ this ->_properties [$ property ] = [];
71+ foreach ($ data [$ property ] as $ key => $ item ) {
72+ if ($ type [1 ] === 'string ' ) {
73+ if (!is_string ($ item )) {
74+ $ this ->_errors [] = "property ' $ property' must be map<string, string>, but entry ' $ key' is of type " . gettype ($ item ) . ". " ;
75+ }
76+ $ this ->_properties [$ property ][$ key ] = $ item ;
77+ } else {
78+ $ this ->_properties [$ property ][$ key ] = new $ type [1 ]($ item );
79+ }
80+ }
81+ break ;
5182 }
5283 } else {
5384 $ this ->_properties [$ property ] = new $ type ($ data [$ property ]);
@@ -66,7 +97,6 @@ public function __construct(array $data)
6697 */
6798 public function validate (): bool
6899 {
69- $ this ->_errors = [];
70100 foreach ($ this ->_properties as $ k => $ v ) {
71101 if ($ v instanceof self) {
72102 $ v ->performValidation ();
0 commit comments