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
|
|
|
* JsonDataNodeHandler constructor. |
49
|
|
|
* |
50
|
|
|
* @param JsonDataNodeValidator $validator |
51
|
|
|
* @throws DomainException |
52
|
|
|
*/ |
53
|
|
|
public function __construct(JsonDataNodeValidator $validator) |
54
|
|
|
{ |
55
|
|
|
$this->validator = $validator; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* for adding primitive data like arrays, integers, or strings |
61
|
|
|
* |
62
|
|
|
* @param string $key |
63
|
|
|
* @param mixed $data |
64
|
|
|
* @throws DomainException |
65
|
|
|
*/ |
66
|
|
|
protected function addData($key, $data) |
67
|
|
|
{ |
68
|
|
|
if ($this->validator->propertyNotSet($this->data, $key)) { |
69
|
|
|
$this->data[ $key ] = $data; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* for embedding other JsonDataNode objects within this one |
76
|
|
|
* |
77
|
|
|
* @param JsonDataNode $data_node |
78
|
|
|
* @throws DomainException |
79
|
|
|
*/ |
80
|
|
|
public function addDataNode(JsonDataNode $data_node) |
81
|
|
|
{ |
82
|
|
|
if ($data_node->isNotInitialized()) { |
83
|
|
|
// $data_node->initialize(); |
84
|
|
|
$key = $data_node->nodeName(); |
85
|
|
|
$this->addData($key, $data_node); |
86
|
|
|
// if the node being added specifies a domain (use case) |
87
|
|
|
// and this is the primary data node, then set the domain |
88
|
|
|
if ($this instanceof PrimaryJsonDataNode && $data_node->domain() !== null) { |
89
|
|
|
$this->setDomain($data_node->domain()); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* sets the domain (use case) that this data node provides data for |
97
|
|
|
* |
98
|
|
|
* @param string $domain |
99
|
|
|
* @throws DomainException |
100
|
|
|
*/ |
101
|
|
|
protected function setDomain($domain) |
102
|
|
|
{ |
103
|
|
|
if ($this->domain !== null) { |
104
|
|
|
$this->validator->overwriteError($domain, 'domain route'); |
105
|
|
|
} |
106
|
|
|
$this->domain = $domain; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* used to mark the data node as having been processed |
112
|
|
|
* |
113
|
|
|
* @param bool $initialized |
114
|
|
|
*/ |
115
|
|
|
protected function setInitialized($initialized) |
116
|
|
|
{ |
117
|
|
|
$this->initialized = filter_var($initialized, FILTER_VALIDATE_BOOLEAN); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* self explanatory (i hope) |
123
|
|
|
* |
124
|
|
|
* @param string $node_name |
125
|
|
|
* @throws DomainException |
126
|
|
|
*/ |
127
|
|
|
protected function setNodeName($node_name) |
128
|
|
|
{ |
129
|
|
|
$this->validator->validateCriticalProperty($node_name, 'node name'); |
130
|
|
|
$this->node_name = $node_name; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* the actual data in key value array format |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
public function data() |
140
|
|
|
{ |
141
|
|
|
return $this->data; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* the domain (use case) that this data node provides data for |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public function domain() |
151
|
|
|
{ |
152
|
|
|
return $this->domain; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* true if the data node has been initialized, |
158
|
|
|
* which entails retrieving the required data and adding it to the data node data array |
159
|
|
|
* |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
public function isInitialized() |
163
|
|
|
{ |
164
|
|
|
return $this->initialized; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* true if the data node has NOT been initialized |
170
|
|
|
* |
171
|
|
|
* @return bool |
172
|
|
|
*/ |
173
|
|
|
public function isNotInitialized() |
174
|
|
|
{ |
175
|
|
|
return ! $this->initialized; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Specify data which should be serialized to JSON |
181
|
|
|
* |
182
|
|
|
* @link https://php.net/manual/en/jsonserializable.jsonserialize.php |
183
|
|
|
* @return mixed data which can be serialized by json_encode |
184
|
|
|
*/ |
185
|
|
|
public function jsonSerialize() |
186
|
|
|
{ |
187
|
|
|
return $this->data; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* self explanatory (i hope) |
193
|
|
|
* |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
public function nodeName() |
197
|
|
|
{ |
198
|
|
|
return $this->node_name; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|