|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\json; |
|
4
|
|
|
|
|
5
|
|
|
use DomainException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class JsonDataNode |
|
9
|
|
|
* a DTO (Data Transfer Object) that: |
|
10
|
|
|
* - allows other JsonDataNode objects to be embedded within it to create hierarchical data structures |
|
11
|
|
|
* - correctly converts to JSON upon serialization |
|
12
|
|
|
* - is intended to be written to the DOM for JavaScript use |
|
13
|
|
|
* |
|
14
|
|
|
* @package EventEspresso\core\services\json |
|
15
|
|
|
* @author Brent Christensen |
|
16
|
|
|
* @since $VID:$ |
|
17
|
|
|
*/ |
|
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) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->validator = $validator; |
|
54
|
|
|
} |
|
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) |
|
65
|
|
|
{ |
|
66
|
|
|
if ($this->validator->propertyNotSet($this->data, $key)) { |
|
67
|
|
|
$this->data[ $key ] = $data; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
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) |
|
79
|
|
|
{ |
|
80
|
|
|
if ($this->validator->dataArrayEmpty($this)) { |
|
81
|
|
|
$this->data = $data; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
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) |
|
93
|
|
|
{ |
|
94
|
|
|
if ($data_node->isNotInitialized()) { |
|
95
|
|
|
// $data_node->initialize(); |
|
96
|
|
|
$key = $data_node->nodeName(); |
|
97
|
|
|
$this->addData($key, $data_node); |
|
98
|
|
|
// if the node being added specifies a domain (use case) |
|
99
|
|
|
// and this is the primary data node, then set the domain |
|
100
|
|
|
if ($this instanceof PrimaryJsonDataNode && $data_node->domain() !== null) { |
|
101
|
|
|
$this->setDomain($data_node->domain()); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
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) |
|
114
|
|
|
{ |
|
115
|
|
|
if ($this->domain !== null) { |
|
116
|
|
|
$this->validator->overwriteError($domain, 'domain route'); |
|
117
|
|
|
} |
|
118
|
|
|
$this->domain = $domain; |
|
119
|
|
|
} |
|
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) |
|
128
|
|
|
{ |
|
129
|
|
|
$this->initialized = filter_var($initialized, FILTER_VALIDATE_BOOLEAN); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* self explanatory (i hope) |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $node_name |
|
137
|
|
|
* @throws DomainException |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function setNodeName($node_name) |
|
140
|
|
|
{ |
|
141
|
|
|
$this->validator->validateCriticalProperty($node_name, 'node name'); |
|
142
|
|
|
$this->node_name = $node_name; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* the actual data in key value array format |
|
148
|
|
|
* |
|
149
|
|
|
* @return array |
|
150
|
|
|
*/ |
|
151
|
|
|
public function data() |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->data; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* the domain (use case) that this data node provides data for |
|
159
|
|
|
* |
|
160
|
|
|
* @return string |
|
161
|
|
|
*/ |
|
162
|
|
|
public function domain() |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->domain; |
|
165
|
|
|
} |
|
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() |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->initialized; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* true if the data node has NOT been initialized |
|
182
|
|
|
* |
|
183
|
|
|
* @return bool |
|
184
|
|
|
*/ |
|
185
|
|
|
public function isNotInitialized() |
|
186
|
|
|
{ |
|
187
|
|
|
return ! $this->initialized; |
|
188
|
|
|
} |
|
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() |
|
198
|
|
|
{ |
|
199
|
|
|
return $this->data; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* self explanatory (i hope) |
|
205
|
|
|
* |
|
206
|
|
|
* @return string |
|
207
|
|
|
*/ |
|
208
|
|
|
public function nodeName() |
|
209
|
|
|
{ |
|
210
|
|
|
return $this->node_name; |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|