1 | <?php |
||
18 | abstract class JsonDataNode implements JsonDataNodeInterface |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * @var JsonDataNodeValidator $validator |
||
23 | */ |
||
24 | protected $validator; |
||
25 | |||
26 | /** |
||
27 | * @var array $data |
||
28 | */ |
||
29 | private $data = []; |
||
30 | |||
31 | /** |
||
32 | * @var string $domain |
||
33 | */ |
||
34 | private $domain; |
||
35 | |||
36 | /** |
||
37 | * @var boolean $initialized |
||
38 | */ |
||
39 | private $initialized = false; |
||
40 | |||
41 | /** |
||
42 | * @var string $node_name |
||
43 | */ |
||
44 | private $node_name; |
||
45 | |||
46 | |||
47 | /** |
||
48 | * @param JsonDataNodeValidator $validator |
||
49 | * @throws DomainException |
||
50 | */ |
||
51 | public function __construct(JsonDataNodeValidator $validator) |
||
55 | |||
56 | |||
57 | /** |
||
58 | * for adding primitive data like arrays, integers, or strings |
||
59 | * |
||
60 | * @param string $key |
||
61 | * @param mixed $data |
||
62 | * @throws DomainException |
||
63 | */ |
||
64 | protected function addData($key, $data) |
||
70 | |||
71 | |||
72 | /** |
||
73 | * for setting value of the entire data array for the node |
||
74 | * |
||
75 | * @param array $data |
||
76 | * @throws DomainException |
||
77 | */ |
||
78 | protected function setDataArray(array $data) |
||
84 | |||
85 | |||
86 | /** |
||
87 | * for embedding other JsonDataNode objects within this one |
||
88 | * |
||
89 | * @param JsonDataNode $data_node |
||
90 | * @throws DomainException |
||
91 | */ |
||
92 | public function addDataNode(JsonDataNode $data_node) |
||
105 | |||
106 | |||
107 | /** |
||
108 | * sets the domain (use case) that this data node provides data for |
||
109 | * |
||
110 | * @param string $domain |
||
111 | * @throws DomainException |
||
112 | */ |
||
113 | protected function setDomain($domain) |
||
120 | |||
121 | |||
122 | /** |
||
123 | * used to mark the data node as having been processed |
||
124 | * |
||
125 | * @param bool $initialized |
||
126 | */ |
||
127 | protected function setInitialized($initialized) |
||
131 | |||
132 | |||
133 | /** |
||
134 | * self explanatory (i hope) |
||
135 | * |
||
136 | * @param string $node_name |
||
137 | * @throws DomainException |
||
138 | */ |
||
139 | protected function setNodeName($node_name) |
||
144 | |||
145 | |||
146 | /** |
||
147 | * the actual data in key value array format |
||
148 | * |
||
149 | * @return array |
||
150 | */ |
||
151 | public function data() |
||
155 | |||
156 | |||
157 | /** |
||
158 | * the domain (use case) that this data node provides data for |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | public function domain() |
||
166 | |||
167 | |||
168 | /** |
||
169 | * true if the data node has been initialized, |
||
170 | * which entails retrieving the required data and adding it to the data node data array |
||
171 | * |
||
172 | * @return bool |
||
173 | */ |
||
174 | public function isInitialized() |
||
178 | |||
179 | |||
180 | /** |
||
181 | * true if the data node has NOT been initialized |
||
182 | * |
||
183 | * @return bool |
||
184 | */ |
||
185 | public function isNotInitialized() |
||
189 | |||
190 | |||
191 | /** |
||
192 | * Specify data which should be serialized to JSON |
||
193 | * |
||
194 | * @link https://php.net/manual/en/jsonserializable.jsonserialize.php |
||
195 | * @return mixed data which can be serialized by json_encode |
||
196 | */ |
||
197 | public function jsonSerialize() |
||
201 | |||
202 | |||
203 | /** |
||
204 | * self explanatory (i hope) |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | public function nodeName() |
||
212 | } |
||
213 |