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