1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Percy\Entity; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use RuntimeException; |
7
|
|
|
use Percy\Exception\ScopeException; |
8
|
|
|
use Percy\Store\StoreInterface; |
9
|
|
|
|
10
|
|
|
abstract class AbstractEntity implements EntityInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $data = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $mapping = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $relationshipMap = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $relationships = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $decorators = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $validator; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $readScope; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $writeScope; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
1 |
|
public function toArray(array $scopes = []) |
56
|
|
|
{ |
57
|
|
|
$data = [ |
58
|
1 |
|
'_relationships' => [] |
59
|
1 |
|
]; |
60
|
|
|
|
61
|
1 |
|
foreach ($this->getRelationships() as $key => $value) { |
62
|
|
|
$data['_relationships'][$key] = $value->toArray($scopes); |
63
|
1 |
|
} |
64
|
|
|
|
65
|
1 |
|
return array_merge($this->getData($scopes, false), $data); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
2 |
|
public function getData(array $scopes = [], $toPersist = false) |
72
|
|
|
{ |
73
|
2 |
|
if (! is_null($this->getReadScope()) && ! in_array($this->getReadScope(), $scopes)) { |
74
|
|
|
throw new ScopeException(sprintf( |
75
|
|
|
'(%s) scope needed to read (%s) resource', $this->getReadScope(), get_called_class() |
76
|
|
|
)); |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
if (! is_null($this->getWriteScope()) && ! in_array($this->getWriteScope(), $scopes) && $toPersist === true) { |
80
|
|
|
throw new ScopeException(sprintf( |
81
|
|
|
'(%s) scope needed to write (%s) resource', $this->getReadScope(), get_called_class() |
82
|
|
|
)); |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
$data = []; |
86
|
|
|
|
87
|
2 |
|
foreach ($this->mapping as $prop => $options) { |
88
|
2 |
|
if (array_key_exists('persist', $options) && $options['persist'] === false && $toPersist === true) { |
89
|
|
|
continue; |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
if (array_key_exists('read', $options) && ! in_array($options['read'], $scopes)) { |
93
|
|
|
continue; |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
if (array_key_exists('write', $options) && ! in_array($options['write'], $scopes) && $toPersist === true) { |
97
|
|
|
continue; |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
if (array_key_exists($prop, $this->data)) { |
101
|
2 |
|
$data[$prop] = $this->data[$prop]; |
102
|
2 |
|
} |
103
|
2 |
|
} |
104
|
|
|
|
105
|
2 |
|
return $data; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
public function getMapping() |
112
|
|
|
{ |
113
|
6 |
|
return array_combine(array_keys($this->mapping), array_map(function ($value) { |
114
|
6 |
|
return (array_key_exists('type', $value)) ? $value['type'] : null; |
115
|
6 |
|
}, $this->mapping)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
1 |
|
public function getRelationshipMap() |
122
|
|
|
{ |
123
|
1 |
|
return $this->relationshipMap; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
2 |
|
public function getRelationships() |
130
|
|
|
{ |
131
|
2 |
|
return $this->relationships; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
1 |
|
public function addRelationship($relationship, Collection $collection) |
138
|
|
|
{ |
139
|
1 |
|
$this->relationships[$relationship] = $collection; |
140
|
|
|
|
141
|
1 |
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
3 |
|
public function getDecorators($action = null) |
148
|
|
|
{ |
149
|
3 |
|
$decorators = array_replace([ |
150
|
3 |
|
StoreInterface::ON_CREATE => [], |
151
|
3 |
|
StoreInterface::ON_READ => [], |
152
|
3 |
|
StoreInterface::ON_UPDATE => [], |
153
|
3 |
|
StoreInterface::ON_DELETE => [] |
154
|
3 |
|
], $this->decorators); |
155
|
|
|
|
156
|
3 |
|
return (is_null($action)) ? $decorators : $decorators[$action]; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
1 |
|
public function getValidator() |
163
|
|
|
{ |
164
|
1 |
|
return $this->validator; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* {@inheritdoc} |
169
|
|
|
*/ |
170
|
4 |
|
public function hydrate(array $data) |
171
|
|
|
{ |
172
|
4 |
|
foreach ($data as $key => $value) { |
173
|
2 |
|
$this[$key] = $value; |
174
|
4 |
|
} |
175
|
|
|
|
176
|
4 |
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritdoc} |
181
|
|
|
*/ |
182
|
3 |
|
public function offsetSet($offset, $value) |
183
|
|
|
{ |
184
|
3 |
|
$mapping = $this->getMapping(); |
185
|
|
|
|
186
|
3 |
|
if (! array_key_exists($offset, $mapping)) { |
187
|
1 |
|
throw new InvalidArgumentException( |
188
|
1 |
|
sprintf('(%s) is not an accepted field for (%s)', $offset, get_class($this)) |
189
|
1 |
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
2 |
|
if (array_key_exists($offset, $mapping) && ! is_null($mapping[$offset]) && ! is_null($value)) { |
193
|
2 |
|
settype($value, $mapping[$offset]); |
194
|
2 |
|
} |
195
|
|
|
|
196
|
2 |
|
$this->data[$offset] = (! isset($value)) ? null : $value; |
197
|
2 |
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* {@inheritdoc} |
201
|
|
|
*/ |
202
|
2 |
|
public function offsetGet($offset) |
203
|
|
|
{ |
204
|
2 |
|
if (isset($this->data[$offset])) { |
205
|
1 |
|
return $this->data[$offset]; |
206
|
|
|
} |
207
|
|
|
|
208
|
1 |
|
throw new InvalidArgumentException( |
209
|
1 |
|
sprintf('Undefined offset (%s) on (%s)', $offset, get_class($this)) |
210
|
1 |
|
); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* {@inheritdoc} |
215
|
|
|
*/ |
216
|
1 |
|
public function offsetExists($offset) |
217
|
|
|
{ |
218
|
1 |
|
return isset($this->data); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* {@inheritdoc} |
223
|
|
|
*/ |
224
|
1 |
|
public function offsetUnset($offset) |
225
|
|
|
{ |
226
|
1 |
|
unset($this->data[$offset]); |
227
|
1 |
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* {@inheritdoc} |
231
|
|
|
*/ |
232
|
2 |
|
public function getReadScope() |
233
|
|
|
{ |
234
|
2 |
|
return $this->readScope; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* {@inheritdoc} |
239
|
|
|
*/ |
240
|
2 |
|
public function getWriteScope() |
241
|
|
|
{ |
242
|
2 |
|
return $this->writeScope; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|