forked from LearningLocker/StatementFactory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElement.php
executable file
·227 lines (191 loc) · 6.07 KB
/
Element.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php namespace Locker\XApi;
use Locker\XApi\Errors\MissingProperties as MissingProperties;
use Locker\XApi\Errors\UnknownProperties as UnknownProperties;
use Locker\XApi\Errors\Error as Error;
class Element extends Atom {
protected $props = [];
protected $required_props = [];
protected $default_props = [];
protected $allow_unknown_props = null;
protected $known_props = [];
/**
* Constructs a new instance of Element from the $value.
* @param mixed $value
*/
public function __construct($value = null) {
$this->value = new \stdClass();
// Caches props.
$parent_class = get_parent_class(get_called_class());
if ($parent_class && $parent_class !== 'Locker\XApi\Atom') {
$parent_instance = new $parent_class;
$this->props = array_merge($parent_instance->props, $this->props);
$this->required_props = array_unique(
array_merge($parent_instance->required_props, $this->required_props)
);
$this->default_props = array_merge($parent_instance->default_props, $this->default_props);
}
$this->known_props = array_keys($this->props);
// Sets defaults.
foreach ($this->default_props as $prop_key => $prop_value) {
$this->setProp($prop_key, $prop_value);
}
parent::__construct($value);
}
/**
* Gets the known props.
* @return [string]
*/
public function getKnownProps() {
return $this->props;
}
/**
* Adds properties from $new_value.
* @param \stdClass $new_value
* @return Element $this
*/
public function setValue($new_value) {
if (gettype($new_value) !== 'object') {
return parent::setValue($new_value);
}
$new_props = $this->getSetProps($new_value);
foreach ($new_props as $new_prop) {
$this->setProp($new_prop, $new_value->{$new_prop});
}
return $this;
}
/**
* Gets all of the properties set on an $object or ($this->value).
* @param StatementObject
* @return [string]
*/
protected function getSetProps($object = null) {
$object = $object ?: $this->value;
if (gettype($object) !== 'object') {
return [];
}
return array_keys((array) $object);
}
/**
* Outputs the value of $this.
* @return stdClass
*/
public function getValue() {
if (gettype($this->value) !== 'object') {
return parent::getValue();
}
$value = clone($this->value);
$set_props = $this->getSetProps();
// Adds properties to value.
foreach ($set_props as $set_prop) {
if ($value->{$set_prop} instanceof Atom) {
$value->{$set_prop} = $value->{$set_prop}->getValue();
}
}
return $value;
}
/**
* Validates $this.
* @return [Error]
*/
public function validate() {
$value_type = gettype($this->value);
if (gettype($this->value) !== 'object') {
$encoded_value = json_encode($this->value);
return [new Error("`$encoded_value` must be `object` not `$value_type`")];
}
$errors = [];
// Gets properties.
$set_props = $this->getSetProps();
$usable_props = array_intersect($set_props, $this->known_props);
// Finds missing properties.
$missing_props = array_diff($this->required_props, $set_props);
if (!empty($missing_props)) {
$errors[] = new MissingProperties(array_values($missing_props), get_class($this));
}
// Finds unknown properties.
if (!$this->allow_unknown_props) {
$unknown_props = array_diff($set_props, $this->known_props);
if (!empty($unknown_props)) {
$errors[] = new UnknownProperties(array_values($unknown_props), get_class($this));
}
}
// Validates usable properties.
foreach ($set_props as $set_prop) {
if ($this->value->{$set_prop} instanceof Atom) {
$prop_errors = $this->value->{$set_prop}->validate();
$errors = array_merge(array_map(function (Error $error) use ($set_prop) {
return $error->addTrace($set_prop);
}, $prop_errors), $errors);
}
}
return $errors;
}
/**
* Sets $prop_key on $this->value using $prop_value.
* @param string $prop_key
* @param mixed $prop_value
* @return Element $this
*/
public function setProp($prop_key, $prop_value) {
if (gettype($this->value) !== 'object') {
return $this;
}
Helpers::checkType('prop_key', 'string', $prop_key);
if ($prop_value === null) {
if (isset($this->defaults[$prop_key])) {
$this->value->{$prop_key} = null;
}
} else {
$props = $this->props;
// Constructs the $prop_value if it's a known a property and hasn't been constructed.
if (in_array($prop_key, $this->known_props) && !Helpers::isType($prop_value, $props[$prop_key])) {
$this->value->{$prop_key} = new $props[$prop_key]($prop_value);
} else {
$this->value->{$prop_key} = $prop_value;
}
}
return $this;
}
/**
* Gets $prop_key on $this->value.
* @param string $prop_key
* @return mixed
*/
public function getProp($prop_key) {
if (gettype($this->value) !== 'object') {
return null;
}
Helpers::checkType('prop_key', 'string', $prop_key);
return isset($this->value->{$prop_key}) ? $this->value->{$prop_key} : null;
}
/**
* Gets $prop_key on $this->value.
* @param string $prop_key
* @return mixed
*/
public function getPropValue($prop_key) {
if (gettype($this->value) !== 'object') {
return null;
}
Helpers::checkType('prop_key', 'string', $prop_key);
return $this->_getPropValue(explode('.', $prop_key));
}
private function _getPropValue(array $prop_key) {
$prop_value = $this->getProp($prop_key[0]);
if ($prop_value instanceof Element && count($prop_key) > 1) {
return $prop_value->_getPropValue(array_slice($prop_key, 1));
} else if ($prop_value instanceof Atom && count($prop_key) === 1) {
return $prop_value->getValue();
} else {
return null;
}
}
public function unsetProp($prop_key) {
if (gettype($this->value) !== 'object') {
return $this;
}
Helpers::checkType('prop_key', 'string', $prop_key);
unset($this->value->{$prop_key});
return $this;
}
}