33 * This file contains the DataStructureElement.php
44 *
55 * @package php-drafter\SOMETHING
6- * @author Sean Molenaar<sean@seanmolenaar.eu>
6+ * @author Sean Molenaar<sean@seanmolenaar.eu>
77 */
88
99namespace PHPDraft \Model ;
1010
1111class DataStructureElement
1212{
13- public $ defaults = ['boolean ' , 'string ' , 'number ' ];
14-
1513 /**
1614 * Object key
1715 * @var string
1816 */
1917 public $ key ;
20-
2118 /**
2219 * Object JSON type
2320 * @var string
2421 */
2522 public $ type ;
26-
2723 /**
2824 * Object description
2925 * @var string
3026 */
3127 public $ description ;
32-
3328 /**
3429 * Type of element
3530 * @var string
3631 */
3732 public $ element = NULL ;
38-
3933 /**
4034 * Object value
4135 * @var mixed|DataStructureElement[]
4236 */
4337 public $ value = NULL ;
44-
4538 /**
4639 * Object status (required|optional)
4740 * @var string
4841 */
4942 public $ status = '' ;
5043
5144 /**
52- * Callback for data types
53- * @var callable
45+ * Unreported datatypes
46+ * @var array
5447 */
55- protected $ callback ;
56-
57- /**
58- * DataStructureElement constructor.
59- * @param \stdClass $object Object to parse
60- * @param callable $callback Call on object discovery
61- */
62- public function __construct ($ object = NULL , $ callback = NULL )
63- {
64- $ this ->callback = $ callback ;
65- if ($ object !== NULL ) $ this ->parse ($ object );
66- }
48+ protected $ defaults = ['boolean ' , 'string ' , 'number ' , 'object ' , 'array ' ];
6749
6850 /**
6951 * Parse a JSON object to a data structure
7052 *
71- * @param \stdClass $object An object to parse
53+ * @param \stdClass $object An object to parse
54+ * @param array $dependencies Dependencies of this object
55+ *
7256 * @return DataStructureElement self reference
7357 */
74- function parse ($ object )
58+ function parse ($ object, & $ dependencies )
7559 {
76- if (empty ($ object )) return $ this ;
60+ if (empty ($ object ) || !isset ($ object ->content ))
61+ {
62+ return $ this ;
63+ }
7764 $ this ->element = $ object ->element ;
78- if (is_array ($ object ->content ))
65+ if (isset ( $ object -> content ) && is_array ($ object ->content ))
7966 {
8067 foreach ($ object ->content as $ value )
8168 {
82- $ this ->value [] = new DataStructureElement ($ value , $ this ->callback );
69+ $ struct = new DataStructureElement ($ this ->callback );
70+ $ this ->value [] = $ struct ->parse ($ value , $ dependencies );
8371 }
8472
8573 return $ this ;
@@ -88,21 +76,25 @@ function parse($object)
8876 $ this ->key = $ object ->content ->key ->content ;
8977 $ this ->type = $ object ->content ->value ->element ;
9078 $ this ->description = isset ($ object ->meta ->description ) ? $ object ->meta ->description : NULL ;
91- $ this ->status = isset ($ object ->attributes ->typeAttributes [0 ]) ? $ object ->attributes ->typeAttributes [0 ] : '' ;
79+ $ this ->status =
80+ isset ($ object ->attributes ->typeAttributes [0 ]) ? $ object ->attributes ->typeAttributes [0 ] : NULL ;
9281
93- if (!is_null ( $ this -> callback ) && ! in_array ($ this ->type , $ this ->defaults ))
82+ if (!in_array ($ this ->type , $ this ->defaults ))
9483 {
95- call_user_func ( $ this -> callback , $ this ->type ) ;
84+ $ dependencies [] = $ this ->type ;
9685 }
9786
98- if ($ object -> content -> value -> element === 'object ' )
87+ if ($ this -> type === 'object ' )
9988 {
10089 $ value = isset ($ object ->content ->value ->content ) ? $ object ->content ->value : NULL ;
101- $ this ->value = new DataStructureElement ($ value , $ this ->callback );
90+ $ this ->value = new DataStructureElement ($ this ->callback );
91+ $ this ->value = $ this ->value ->parse ($ value , $ dependencies );
92+
10293 return $ this ;
10394 }
10495
10596 $ this ->value = isset ($ object ->content ->value ->content ) ? $ object ->content ->value ->content : NULL ;
97+
10698 return $ this ;
10799 }
108100
@@ -113,7 +105,7 @@ function parse($object)
113105 */
114106 function __toString ()
115107 {
116- if ($ this ->value == NULL && $ this ->key == NULL )
108+ if ($ this ->value === NULL && $ this ->key = == NULL )
117109 {
118110 return '{ ... } ' ;
119111 }
0 commit comments