1010use cebe \openapi \exceptions \ReadonlyPropertyException ;
1111use cebe \openapi \exceptions \TypeErrorException ;
1212use cebe \openapi \exceptions \UnknownPropertyException ;
13+ use cebe \openapi \spec \Reference ;
1314use cebe \openapi \spec \Type ;
1415
1516/**
@@ -73,7 +74,6 @@ public function __construct(array $data)
7374 } elseif ($ type [0 ] === Type::ANY || $ type [0 ] === Type::BOOLEAN || $ type [0 ] === Type::INTEGER ) { // TODO simplify handling of scalar types
7475 $ this ->_properties [$ property ][] = $ item ;
7576 } else {
76- // TODO implement reference objects
7777 $ this ->_properties [$ property ][] = $ this ->instantiate ($ type [0 ], $ item );
7878 }
7979 }
@@ -93,7 +93,6 @@ public function __construct(array $data)
9393 } elseif ($ type [1 ] === Type::ANY || $ type [1 ] === Type::BOOLEAN || $ type [1 ] === Type::INTEGER ) { // TODO simplify handling of scalar types
9494 $ this ->_properties [$ property ][$ key ] = $ item ;
9595 } else {
96- // TODO implement reference objects
9796 $ this ->_properties [$ property ][$ key ] = $ this ->instantiate ($ type [1 ], $ item );
9897 }
9998 }
@@ -114,6 +113,9 @@ public function __construct(array $data)
114113 */
115114 private function instantiate ($ type , $ data )
116115 {
116+ if (isset ($ data ['$ref ' ])) {
117+ return new Reference ($ data , $ type );
118+ }
117119 try {
118120 return new $ type ($ data );
119121 } catch (\TypeError $ e ) {
@@ -239,4 +241,27 @@ public function __unset($name)
239241 {
240242 throw new ReadonlyPropertyException ('Unsetting read-only property: ' . \get_class ($ this ) . ':: ' . $ name );
241243 }
244+
245+ /**
246+ * Resolves all Reference Objects in this object and replaces them with their resolution.
247+ * @throws exceptions\UnresolvableReferenceException in case resolving a reference fails.
248+ */
249+ public function resolveReferences (ReferenceContext $ context )
250+ {
251+ foreach ($ this ->_properties as $ property => $ value ) {
252+ if ($ value instanceof Reference) {
253+ $ this ->_properties [$ property ] = $ value ->resolve ($ context );
254+ } elseif ($ value instanceof SpecObjectInterface) {
255+ $ value ->resolveReferences ($ context );
256+ } elseif (is_array ($ value )) {
257+ foreach ($ value as $ k => $ item ) {
258+ if ($ item instanceof Reference) {
259+ $ this ->_properties [$ property ][$ k ] = $ item ->resolve ($ context );
260+ } elseif ($ item instanceof SpecObjectInterface) {
261+ $ item ->resolveReferences ($ context );
262+ }
263+ }
264+ }
265+ }
266+ }
242267}
0 commit comments