1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chadicus\Marvel\Api\Entities; |
4
|
|
|
|
5
|
|
|
use Chadicus\Marvel\Api\Filterer; |
6
|
|
|
use Chadicus\Spl\Exceptions\UndefinedPropertyException; |
7
|
|
|
use Chadicus\Spl\Exceptions\NotAllowedException; |
8
|
|
|
use DominionEnterprises\Util; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Base entity class. |
12
|
|
|
*/ |
13
|
|
|
abstract class AbstractEntity implements EntityInterface, \ArrayAccess |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The data for this AbstractEntity |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $data = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Create a new AbstractEntity based on the given $input array. |
24
|
|
|
* |
25
|
|
|
* @param array $input The data for the EntityInterface. |
26
|
|
|
*/ |
27
|
|
|
public function __construct(array $input = []) |
28
|
|
|
{ |
29
|
|
|
list($success, $filtered, $error) = Filterer::filter($this->getFilters(), $input, ['allowUnknowns' => true]); |
30
|
|
|
Util::ensure(true, $success, '\InvalidArgumentException', [$error]); |
31
|
|
|
|
32
|
|
|
foreach ($filtered as $key => $value) { |
33
|
|
|
$this->data[$key] = $value; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get an AbstractEntity property. |
39
|
|
|
* |
40
|
|
|
* @param string $name The name of the property. |
41
|
|
|
* |
42
|
|
|
* @return mixed |
43
|
|
|
* |
44
|
|
|
* @throws UndefinedPropertyException Throw if this class does not contain the $name'd property. |
45
|
|
|
*/ |
46
|
|
|
final public function __get(string $name) |
47
|
|
|
{ |
48
|
|
|
if (!array_key_exists($name, $this->data)) { |
49
|
|
|
$class = get_called_class(); |
50
|
|
|
throw new UndefinedPropertyException("Undefined Property {$class}::\${$name}"); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->data[$name]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Allows for getX() method calls. |
58
|
|
|
* |
59
|
|
|
* @param string $name The name of the method being called. |
60
|
|
|
* @param array $arguments The arguments being passed to the method. This parameter is unused. |
61
|
|
|
* |
62
|
|
|
* @return mixed |
63
|
|
|
* |
64
|
|
|
* @throws \BadMethodCallException Thrown if the property being accessed does not exist. |
65
|
|
|
*/ |
66
|
|
|
final public function __call(string $name, array $arguments = []) |
67
|
|
|
{ |
68
|
|
|
if (substr($name, 0, 3) !== 'get') { |
69
|
|
|
$class = get_called_class(); |
70
|
|
|
throw new \BadMethodCallException("Method {$class}::{$name}() does not exist"); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$key = lcfirst(substr($name, 3)); |
74
|
|
|
|
75
|
|
|
if (!array_key_exists($key, $this->data)) { |
76
|
|
|
$class = get_called_class(); |
77
|
|
|
throw new \BadMethodCallException("Method {$class}::{$name}() does not exist"); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->data[$key]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Create an array of new AbstractEntity based on the given $input arrays |
85
|
|
|
* |
86
|
|
|
* @param array[] $inputs The value to be filtered. |
87
|
|
|
* |
88
|
|
|
* @return AbstractEntity[] |
89
|
|
|
*/ |
90
|
|
|
final public static function fromArrays(array $inputs) : array |
91
|
|
|
{ |
92
|
|
|
Util::throwIfNotType(['array' => $inputs]); |
93
|
|
|
|
94
|
|
|
$entities = []; |
95
|
|
|
foreach ($inputs as $key => $input) { |
96
|
|
|
$entities[$key] = new static($input); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $entities; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Create a new AbstractEntity based on the given $input array |
104
|
|
|
* |
105
|
|
|
* @param array $input The data for the AbstractEntity. |
106
|
|
|
* |
107
|
|
|
* @return EntityInterface |
108
|
|
|
*/ |
109
|
|
|
final public static function fromArray(array $input = null) : EntityInterface |
110
|
|
|
{ |
111
|
|
|
return new static($input ?? []); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns whether the requested index exists |
116
|
|
|
* |
117
|
|
|
* @param string $offset The index being checked. |
118
|
|
|
* |
119
|
|
|
* @return boolean |
120
|
|
|
*/ |
121
|
|
|
final public function offsetExists($offset) //@codingStandardsIgnoreLine Ingore missing type-hint |
122
|
|
|
{ |
123
|
|
|
return isset($this->data[$offset]); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns the value at the specified index. |
128
|
|
|
* |
129
|
|
|
* @param string $offset The index with the value. |
130
|
|
|
* |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
|
|
final public function offsetGet($offset) //@codingStandardsIgnoreLine Ingore missing type-hint |
134
|
|
|
{ |
135
|
|
|
return $this->data[$offset]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Sets the value at the specified index to newval |
140
|
|
|
* |
141
|
|
|
* @param string $offset The index being get. |
142
|
|
|
* @param mixed $value The new value for the index. |
143
|
|
|
* |
144
|
|
|
* @return void |
145
|
|
|
* |
146
|
|
|
* @throws NotAllowedException Ensure this object is immutable. |
147
|
|
|
*/ |
148
|
|
|
final public function offsetSet($offset, $value) //@codingStandardsIgnoreLine Ingore missing type-hint |
149
|
|
|
{ |
150
|
|
|
$class = get_called_class(); |
151
|
|
|
throw new NotAllowedException("{$class}::\${$offset} is read-only"); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Unsets the value at the specified index |
156
|
|
|
* |
157
|
|
|
* @param string $offset The index being unset. |
158
|
|
|
* |
159
|
|
|
* @return void |
160
|
|
|
* |
161
|
|
|
* @throws NotAllowedException Ensure this object is immutable. |
162
|
|
|
*/ |
163
|
|
|
final public function offsetUnset($offset) //@codingStandardsIgnoreLine Ingore missing type-hint |
164
|
|
|
{ |
165
|
|
|
$class = get_called_class(); |
166
|
|
|
throw new NotAllowedException("{$class}::\${$offset} is read-only"); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Returns an array of filters suitable for use with \Chadicus\Marvel\Api\Filterer::filter() |
171
|
|
|
* |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
|
|
abstract protected function getFilters() : array; |
175
|
|
|
} |
176
|
|
|
|