88
99namespace PHPDraft \Model ;
1010
11+ use PHPDraft \Model \Elements \ArrayStructureElement ;
12+
1113class DataStructureElement
1214{
15+ /**
16+ * Default datatypes
17+ * @var array
18+ */
19+ const DEFAULTS = ['boolean ' , 'string ' , 'number ' , 'object ' , 'array ' ];
1320 /**
1421 * Object key
1522 * @var string
1623 */
1724 public $ key ;
18-
1925 /**
2026 * Object JSON type
21- * @var string
27+ * @var mixed
2228 */
2329 public $ type ;
24-
2530 /**
2631 * Object description
2732 * @var string
2833 */
2934 public $ description ;
30-
3135 /**
3236 * Type of element
3337 * @var string
3438 */
3539 public $ element = NULL ;
36-
3740 /**
3841 * Object value
3942 * @var mixed|DataStructureElement[]
4043 */
4144 public $ value = NULL ;
42-
4345 /**
4446 * Object status (required|optional)
4547 * @var string
4648 */
4749 public $ status = '' ;
48-
4950 /**
5051 * List of object dependencies
5152 * @var string[]
5253 */
5354 public $ deps ;
5455
55- /**
56- * Default datatypes
57- * @var array
58- */
59- protected $ defaults = ['boolean ' , 'string ' , 'number ' , 'object ' , 'array ' ];
60-
6156 /**
6257 * Parse a JSON object to a data structure
6358 *
@@ -88,17 +83,24 @@ public function parse($object, &$dependencies)
8883 $ this ->type = $ object ->content ->value ->element ;
8984 $ this ->description = isset ($ object ->meta ->description ) ? $ object ->meta ->description : NULL ;
9085 $ this ->status =
91- isset ($ object ->attributes ->typeAttributes [ 0 ] ) ? $ object ->attributes ->typeAttributes [ 0 ] : NULL ;
86+ isset ($ object ->attributes ->typeAttributes ) ? join ( ' , ' , $ object ->attributes ->typeAttributes ) : NULL ;
9287
93- if (!in_array ($ this ->type , $ this -> defaults ))
88+ if (!in_array ($ this ->type , self :: DEFAULTS ))
9489 {
9590 $ dependencies [] = $ this ->type ;
9691 }
9792
98- if ($ this ->type === 'object ' )
93+ if ($ this ->type === 'object ' || $ this -> type === ' array ' )
9994 {
100- $ value = isset ($ object ->content ->value ->content ) ? $ object ->content ->value : NULL ;
101- $ this ->value = new DataStructureElement ();
95+ $ value = isset ($ object ->content ->value ->content ) ? $ object ->content ->value : NULL ;
96+ if ($ this ->type === 'array ' )
97+ {
98+ $ this ->value = new ArrayStructureElement ();
99+ }
100+ else
101+ {
102+ $ this ->value = new DataStructureElement ();
103+ }
102104 $ this ->value = $ this ->value ->parse ($ value , $ dependencies );
103105
104106 return $ this ;
@@ -118,49 +120,56 @@ function __toString()
118120 {
119121 if ($ this ->value === NULL && $ this ->key === NULL )
120122 {
121- return '{ ... } ' ;
123+ return '<span class="example-value pull-right"> { ... }</span> ' ;
122124 }
123125
124126 if (is_array ($ this ->value ))
125127 {
126- $ return = '<dl class="dl-horizontal "> ' ;
128+ $ return = '<table class="table table-striped "> ' ;
127129 foreach ($ this ->value as $ object )
128130 {
129- if (get_class ($ object ) === get_class ($ this ) )
131+ if (get_class ($ object ) === self ::class || get_class ($ object ) === ArrayStructureElement::class )
130132 {
131133 $ return .= $ object ;
132134 }
133135 }
134136
135- $ return .= '</dl > ' ;
137+ $ return .= '</table > ' ;
136138
137139 return $ return ;
138140 }
139141
140- $ type = (!in_array ($ this ->type , $ this -> defaults )) ?
141- '<a class="code" href="#object- ' . $ this ->type . '"> ' . $ this ->type . '</a> ' : '<code> ' . $ this ->type . '</code> ' ;
142+ $ type = (!in_array ($ this ->type , self :: DEFAULTS )) ?
143+ '<a class="code" href="#object- ' . $ this ->type . '"> ' . $ this ->type . '</a> ' : '<code> ' . $ this ->type . '</code> ' ;
142144
143145 if (empty ($ this ->value ))
144146 {
145- $ value = '<s class="example-value pull-right">no example</s> ' ;
147+ $ value = '' ;
146148 }
147- else if ( is_object ( $ this -> value ) && self ::class === get_class ( $ this -> value ))
149+ else
148150 {
149- $ value = '<div class="sub-struct"> ' .$ this ->value .'</div> ' ;
150- }
151- else {
152- $ value = '<span class="example-value pull-right">Example: ' . $ this ->value . '</span> ' ;
151+ if (is_object ($ this ->value ) && self ::class === get_class ($ this ->value ))
152+ {
153+ $ value = '<div class="sub-struct"> ' . $ this ->value . '</div> ' ;
154+ }
155+ elseif (is_object ($ this ->value ) && (ArrayStructureElement::class === get_class ($ this ->value )))
156+ {
157+ $ value = '<div class="array-struct"> ' . $ this ->value . '</div> ' ;
158+ }
159+ else
160+ {
161+ $ value = '<span class="example-value pull-right"> ' . $ this ->value . '</span> ' ;
162+ }
153163 }
154164
155165 $ return =
156- '<dt> ' .
157- '<span> ' . $ this ->key . "</span> " .
158- "</dt> \t" .
159- '<dd> ' .
160- $ type .
161- '<span> ' . $ this ->description . '</span> ' .
162- $ value .
163- '</dd> ' ;
166+ '<tr> ' .
167+ '<td> ' . '<span> ' . $ this ->key . "</span> " . '</td> ' .
168+ '<td> ' . $ type . '</td> ' .
169+ '<td> <span class="status"> ' . $ this ->status . '</span></td> ' .
170+ '<td><span> ' . $ this ->description . '</span></td> ' .
171+ '<td> ' . $ value . '</td> ' .
172+ '</tr> ' ;
164173
165174 return $ return ;
166175 }
0 commit comments