8
8
9
9
namespace PHPDraft \Model ;
10
10
11
+ use PHPDraft \Model \Elements \ArrayStructureElement ;
12
+
11
13
class DataStructureElement
12
14
{
15
+ /**
16
+ * Default datatypes
17
+ * @var array
18
+ */
19
+ const DEFAULTS = ['boolean ' , 'string ' , 'number ' , 'object ' , 'array ' ];
13
20
/**
14
21
* Object key
15
22
* @var string
16
23
*/
17
24
public $ key ;
18
-
19
25
/**
20
26
* Object JSON type
21
- * @var string
27
+ * @var mixed
22
28
*/
23
29
public $ type ;
24
-
25
30
/**
26
31
* Object description
27
32
* @var string
28
33
*/
29
34
public $ description ;
30
-
31
35
/**
32
36
* Type of element
33
37
* @var string
34
38
*/
35
39
public $ element = NULL ;
36
-
37
40
/**
38
41
* Object value
39
42
* @var mixed|DataStructureElement[]
40
43
*/
41
44
public $ value = NULL ;
42
-
43
45
/**
44
46
* Object status (required|optional)
45
47
* @var string
46
48
*/
47
49
public $ status = '' ;
48
-
49
50
/**
50
51
* List of object dependencies
51
52
* @var string[]
52
53
*/
53
54
public $ deps ;
54
55
55
- /**
56
- * Default datatypes
57
- * @var array
58
- */
59
- protected $ defaults = ['boolean ' , 'string ' , 'number ' , 'object ' , 'array ' ];
60
-
61
56
/**
62
57
* Parse a JSON object to a data structure
63
58
*
@@ -88,17 +83,24 @@ public function parse($object, &$dependencies)
88
83
$ this ->type = $ object ->content ->value ->element ;
89
84
$ this ->description = isset ($ object ->meta ->description ) ? $ object ->meta ->description : NULL ;
90
85
$ this ->status =
91
- isset ($ object ->attributes ->typeAttributes [ 0 ] ) ? $ object ->attributes ->typeAttributes [ 0 ] : NULL ;
86
+ isset ($ object ->attributes ->typeAttributes ) ? join ( ' , ' , $ object ->attributes ->typeAttributes ) : NULL ;
92
87
93
- if (!in_array ($ this ->type , $ this -> defaults ))
88
+ if (!in_array ($ this ->type , self :: DEFAULTS ))
94
89
{
95
90
$ dependencies [] = $ this ->type ;
96
91
}
97
92
98
- if ($ this ->type === 'object ' )
93
+ if ($ this ->type === 'object ' || $ this -> type === ' array ' )
99
94
{
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
+ }
102
104
$ this ->value = $ this ->value ->parse ($ value , $ dependencies );
103
105
104
106
return $ this ;
@@ -118,49 +120,56 @@ function __toString()
118
120
{
119
121
if ($ this ->value === NULL && $ this ->key === NULL )
120
122
{
121
- return '{ ... } ' ;
123
+ return '<span class="example-value pull-right"> { ... }</span> ' ;
122
124
}
123
125
124
126
if (is_array ($ this ->value ))
125
127
{
126
- $ return = '<dl class="dl-horizontal "> ' ;
128
+ $ return = '<table class="table table-striped "> ' ;
127
129
foreach ($ this ->value as $ object )
128
130
{
129
- if (get_class ($ object ) === get_class ($ this ) )
131
+ if (get_class ($ object ) === self ::class || get_class ($ object ) === ArrayStructureElement::class )
130
132
{
131
133
$ return .= $ object ;
132
134
}
133
135
}
134
136
135
- $ return .= '</dl > ' ;
137
+ $ return .= '</table > ' ;
136
138
137
139
return $ return ;
138
140
}
139
141
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> ' ;
142
144
143
145
if (empty ($ this ->value ))
144
146
{
145
- $ value = '<s class="example-value pull-right">no example</s> ' ;
147
+ $ value = '' ;
146
148
}
147
- else if ( is_object ( $ this -> value ) && self ::class === get_class ( $ this -> value ))
149
+ else
148
150
{
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
+ }
153
163
}
154
164
155
165
$ 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> ' ;
164
173
165
174
return $ return ;
166
175
}
0 commit comments