1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rexlabs\Smokescreen\Resource; |
4
|
|
|
|
5
|
|
|
use Rexlabs\Smokescreen\Exception\InvalidSerializerException; |
6
|
|
|
use Rexlabs\Smokescreen\Exception\InvalidTransformerException; |
7
|
|
|
use Rexlabs\Smokescreen\Serializer\SerializerInterface; |
8
|
|
|
use Rexlabs\Smokescreen\Transformer\TransformerInterface; |
9
|
|
|
|
10
|
|
|
abstract class AbstractResource implements ResourceInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The data to process with the transformer. |
14
|
|
|
* |
15
|
|
|
* @var array|\ArrayIterator|mixed |
16
|
|
|
*/ |
17
|
|
|
protected $data; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Array of meta data. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $meta = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The resource key. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $resourceKey; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* A transformer or callable to process the data attached to this resource. |
35
|
|
|
* |
36
|
|
|
* @var callable|TransformerInterface|null |
37
|
|
|
*/ |
38
|
|
|
protected $transformer; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Provide a custom serializer for this resource. |
42
|
|
|
* |
43
|
|
|
* @var callable|SerializerInterface|false|null |
44
|
|
|
*/ |
45
|
|
|
protected $serializer; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Create a new resource instance. |
49
|
|
|
* |
50
|
|
|
* @param mixed $data |
51
|
|
|
* @param callable|TransformerInterface|null $transformer |
52
|
|
|
* @param string $resourceKey |
53
|
|
|
* |
54
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException |
55
|
|
|
*/ |
56
|
35 |
|
public function __construct($data = null, $transformer = null, $resourceKey = null) |
57
|
|
|
{ |
58
|
35 |
|
$this->setData($data); |
59
|
35 |
|
$this->setTransformer($transformer); |
60
|
34 |
|
$this->setResourceKey($resourceKey); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the data. |
65
|
|
|
* |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
16 |
|
public function getData() |
69
|
|
|
{ |
70
|
16 |
|
return $this->data; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set the data. |
75
|
|
|
* |
76
|
|
|
* @param mixed $data |
77
|
|
|
* |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
35 |
|
public function setData($data) |
81
|
|
|
{ |
82
|
35 |
|
$this->data = $data; |
83
|
|
|
|
84
|
35 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the meta data. |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
1 |
|
public function getMeta() |
93
|
|
|
{ |
94
|
1 |
|
return $this->meta; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set the meta data. |
99
|
|
|
* |
100
|
|
|
* @param array $meta |
101
|
|
|
* |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
1 |
|
public function setMeta(array $meta) |
105
|
|
|
{ |
106
|
1 |
|
$this->meta = $meta; |
107
|
|
|
|
108
|
1 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the meta data. |
113
|
|
|
* |
114
|
|
|
* @param string $metaKey |
115
|
|
|
* |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
1 |
|
public function getMetaValue($metaKey) |
119
|
|
|
{ |
120
|
1 |
|
return $this->meta[$metaKey]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the resource key. |
125
|
|
|
* |
126
|
|
|
* @return string|null |
127
|
|
|
*/ |
128
|
14 |
|
public function getResourceKey() |
129
|
|
|
{ |
130
|
14 |
|
return $this->resourceKey; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set the resource key. |
135
|
|
|
* |
136
|
|
|
* @param string|null $resourceKey |
137
|
|
|
* |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
34 |
|
public function setResourceKey($resourceKey) |
141
|
|
|
{ |
142
|
34 |
|
$this->resourceKey = $resourceKey; |
143
|
|
|
|
144
|
34 |
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set the meta data. |
149
|
|
|
* |
150
|
|
|
* @param string $metaKey |
151
|
|
|
* @param mixed $metaValue |
152
|
|
|
* |
153
|
|
|
* @return $this |
154
|
|
|
*/ |
155
|
1 |
|
public function setMetaValue(string $metaKey, $metaValue) |
156
|
|
|
{ |
157
|
1 |
|
$this->meta[$metaKey] = $metaValue; |
158
|
|
|
|
159
|
1 |
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get a list of relationships from the transformer |
164
|
|
|
* Only applicable when the transformer is an instance of the TransformerInterface (eg. AbstractTransformer). |
165
|
|
|
* |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
1 |
|
public function getRelationships(): array |
169
|
|
|
{ |
170
|
1 |
|
$relationships = []; |
171
|
1 |
|
if ($this->hasTransformer()) { |
172
|
1 |
|
if (($transformer = $this->getTransformer()) instanceof TransformerInterface) { |
173
|
1 |
|
$relationships = $transformer->getRelationships(); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
1 |
|
return $relationships; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return bool |
182
|
|
|
*/ |
183
|
16 |
|
public function hasTransformer(): bool |
184
|
|
|
{ |
185
|
16 |
|
return $this->transformer !== null; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get the transformer. |
190
|
|
|
* |
191
|
|
|
* @return callable|TransformerInterface|null |
192
|
|
|
*/ |
193
|
21 |
|
public function getTransformer() |
194
|
|
|
{ |
195
|
21 |
|
return $this->transformer; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Set the transformer. |
200
|
|
|
* |
201
|
|
|
* @param callable|TransformerInterface|null $transformer |
202
|
|
|
* |
203
|
|
|
* @throws InvalidTransformerException |
204
|
|
|
* |
205
|
|
|
* @return $this |
206
|
|
|
*/ |
207
|
35 |
|
public function setTransformer($transformer) |
208
|
|
|
{ |
209
|
35 |
|
if ($transformer !== null) { |
210
|
14 |
|
if (!$this->isValidTransformer($transformer)) { |
211
|
1 |
|
throw new InvalidTransformerException('Transformer must be a callable or implement TransformerInterface'); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
34 |
|
$this->transformer = $transformer; |
216
|
|
|
|
217
|
34 |
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Determines if the given argument is a valid transformer. |
222
|
|
|
* |
223
|
|
|
* @param mixed $transformer |
224
|
|
|
* |
225
|
|
|
* @return bool |
226
|
|
|
*/ |
227
|
14 |
|
public function isValidTransformer($transformer): bool |
228
|
|
|
{ |
229
|
14 |
|
if ($transformer instanceof TransformerInterface) { |
230
|
11 |
|
return true; |
231
|
|
|
} |
232
|
|
|
|
233
|
5 |
|
if (\is_callable($transformer)) { |
234
|
4 |
|
return true; |
235
|
|
|
} |
236
|
|
|
|
237
|
1 |
|
return false; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @return bool |
242
|
|
|
*/ |
243
|
1 |
|
public function hasSerializer(): bool |
244
|
|
|
{ |
245
|
1 |
|
return $this->serializer !== null; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @return SerializerInterface|callable|false|null |
250
|
|
|
*/ |
251
|
17 |
|
public function getSerializer() |
252
|
|
|
{ |
253
|
17 |
|
return $this->serializer; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Sets a custom serializer to be used for this resource |
258
|
|
|
* - Usually you will set this to an instance of SerializerInterface. |
259
|
|
|
* - Set to false to force no serialization on this resource. |
260
|
|
|
* - When set to null the default serializer will be used. |
261
|
|
|
* - Optionally set a closure/callback to be used for serialization. |
262
|
|
|
* |
263
|
|
|
* @param SerializerInterface|callable|false|null $serializer |
264
|
|
|
* |
265
|
|
|
* @throws InvalidSerializerException |
266
|
|
|
* |
267
|
|
|
* @return $this |
268
|
|
|
*/ |
269
|
8 |
|
public function setSerializer($serializer) |
270
|
|
|
{ |
271
|
8 |
|
if ($serializer !== null) { |
272
|
7 |
|
if (!$this->isValidSerializer($serializer)) { |
273
|
1 |
|
throw new InvalidSerializerException('Serializer must be one of: callable, SerializerInterface, false or null'); |
274
|
|
|
} |
275
|
|
|
} |
276
|
7 |
|
$this->serializer = $serializer; |
277
|
|
|
|
278
|
7 |
|
return $this; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @param mixed $serializer |
283
|
|
|
* |
284
|
|
|
* @return bool |
285
|
|
|
*/ |
286
|
7 |
|
public function isValidSerializer($serializer): bool |
287
|
|
|
{ |
288
|
7 |
|
if ($serializer === false) { |
289
|
|
|
// False is a valid value (forces serialization to be off) |
290
|
1 |
|
return true; |
291
|
|
|
} |
292
|
6 |
|
if ($serializer instanceof SerializerInterface) { |
293
|
2 |
|
return true; |
294
|
|
|
} |
295
|
4 |
|
if (\is_callable($serializer)) { |
296
|
3 |
|
return true; |
297
|
|
|
} |
298
|
|
|
|
299
|
1 |
|
return false; |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|