3
3
* This file contains the DataStructureElement.php
4
4
*
5
5
* @package php-drafter\SOMETHING
6
- * @author Sean Molenaar<[email protected] >
6
+ * @author Sean Molenaar<[email protected] >
7
7
*/
8
8
9
9
namespace PHPDraft \Model ;
10
10
11
11
class DataStructureElement
12
12
{
13
- public $ defaults = ['boolean ' , 'string ' , 'number ' ];
14
-
15
13
/**
16
14
* Object key
17
15
* @var string
18
16
*/
19
17
public $ key ;
20
-
21
18
/**
22
19
* Object JSON type
23
20
* @var string
24
21
*/
25
22
public $ type ;
26
-
27
23
/**
28
24
* Object description
29
25
* @var string
30
26
*/
31
27
public $ description ;
32
-
33
28
/**
34
29
* Type of element
35
30
* @var string
36
31
*/
37
32
public $ element = NULL ;
38
-
39
33
/**
40
34
* Object value
41
35
* @var mixed|DataStructureElement[]
42
36
*/
43
37
public $ value = NULL ;
44
-
45
38
/**
46
39
* Object status (required|optional)
47
40
* @var string
48
41
*/
49
42
public $ status = '' ;
50
43
51
44
/**
52
- * Callback for data types
53
- * @var callable
45
+ * Unreported datatypes
46
+ * @var array
54
47
*/
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 ' ];
67
49
68
50
/**
69
51
* Parse a JSON object to a data structure
70
52
*
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
+ *
72
56
* @return DataStructureElement self reference
73
57
*/
74
- function parse ($ object )
58
+ function parse ($ object, & $ dependencies )
75
59
{
76
- if (empty ($ object )) return $ this ;
60
+ if (empty ($ object ) || !isset ($ object ->content ))
61
+ {
62
+ return $ this ;
63
+ }
77
64
$ this ->element = $ object ->element ;
78
- if (is_array ($ object ->content ))
65
+ if (isset ( $ object -> content ) && is_array ($ object ->content ))
79
66
{
80
67
foreach ($ object ->content as $ value )
81
68
{
82
- $ this ->value [] = new DataStructureElement ($ value , $ this ->callback );
69
+ $ struct = new DataStructureElement ($ this ->callback );
70
+ $ this ->value [] = $ struct ->parse ($ value , $ dependencies );
83
71
}
84
72
85
73
return $ this ;
@@ -88,21 +76,25 @@ function parse($object)
88
76
$ this ->key = $ object ->content ->key ->content ;
89
77
$ this ->type = $ object ->content ->value ->element ;
90
78
$ 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 ;
92
81
93
- if (!is_null ( $ this -> callback ) && ! in_array ($ this ->type , $ this ->defaults ))
82
+ if (!in_array ($ this ->type , $ this ->defaults ))
94
83
{
95
- call_user_func ( $ this -> callback , $ this ->type ) ;
84
+ $ dependencies [] = $ this ->type ;
96
85
}
97
86
98
- if ($ object -> content -> value -> element === 'object ' )
87
+ if ($ this -> type === 'object ' )
99
88
{
100
89
$ 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
+
102
93
return $ this ;
103
94
}
104
95
105
96
$ this ->value = isset ($ object ->content ->value ->content ) ? $ object ->content ->value ->content : NULL ;
97
+
106
98
return $ this ;
107
99
}
108
100
@@ -113,7 +105,7 @@ function parse($object)
113
105
*/
114
106
function __toString ()
115
107
{
116
- if ($ this ->value == NULL && $ this ->key == NULL )
108
+ if ($ this ->value === NULL && $ this ->key = == NULL )
117
109
{
118
110
return '{ ... } ' ;
119
111
}
0 commit comments