1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Fractal; |
4
|
|
|
|
5
|
|
|
use JsonSerializable; |
6
|
|
|
use League\Fractal\Manager; |
7
|
|
|
use League\Fractal\Pagination\CursorInterface; |
8
|
|
|
use League\Fractal\Pagination\PaginatorInterface; |
9
|
|
|
use League\Fractal\Serializer\SerializerAbstract; |
10
|
|
|
use Spatie\Fractal\Exceptions\InvalidTransformation; |
11
|
|
|
use Spatie\Fractal\Exceptions\NoTransformerSpecified; |
12
|
|
|
|
13
|
|
|
class Fractal implements JsonSerializable |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var \League\Fractal\Manager |
17
|
|
|
*/ |
18
|
|
|
protected $manager; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \League\Fractal\Serializer\SerializerAbstract |
22
|
|
|
*/ |
23
|
|
|
protected $serializer; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \League\Fractal\TransformerAbstract|callable |
27
|
|
|
*/ |
28
|
|
|
protected $transformer; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \League\Fractal\Pagination\PaginatorInterface |
32
|
|
|
*/ |
33
|
|
|
protected $paginator; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var \League\Fractal\Pagination\CursorInterface |
37
|
|
|
*/ |
38
|
|
|
protected $cursor; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $includes = []; |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
protected $excludes = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $dataType; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var mixed |
55
|
|
|
*/ |
56
|
|
|
protected $data; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $resourceName; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
protected $meta = []; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param \League\Fractal\Manager $manager |
70
|
|
|
*/ |
71
|
|
|
public function __construct(Manager $manager) |
72
|
|
|
{ |
73
|
|
|
$this->manager = $manager; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set the collection data that must be transformed. |
78
|
|
|
* |
79
|
|
|
* @param mixed $data |
80
|
|
|
* @param \League\Fractal\TransformerAbstract|callable|null $transformer |
81
|
|
|
* @param string|null $resourceName |
82
|
|
|
* |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public function collection($data, $transformer = null, $resourceName = null) |
86
|
|
|
{ |
87
|
|
|
$this->resourceName = $resourceName; |
88
|
|
|
|
89
|
|
|
return $this->data('collection', $data, $transformer); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set the item data that must be transformed. |
94
|
|
|
* |
95
|
|
|
* @param mixed $data |
96
|
|
|
* @param \League\Fractal\TransformerAbstract|callable|null $transformer |
97
|
|
|
* @param string|null $resourceName |
98
|
|
|
* |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function item($data, $transformer = null, $resourceName = null) |
102
|
|
|
{ |
103
|
|
|
$this->resourceName = $resourceName; |
104
|
|
|
|
105
|
|
|
return $this->data('item', $data, $transformer); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Set the data that must be transformed. |
110
|
|
|
* |
111
|
|
|
* @param string $dataType |
112
|
|
|
* @param mixed $data |
113
|
|
|
* @param \League\Fractal\TransformerAbstract|callable|null $transformer |
114
|
|
|
* |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
|
|
protected function data($dataType, $data, $transformer = null) |
118
|
|
|
{ |
119
|
|
|
$this->dataType = $dataType; |
120
|
|
|
|
121
|
|
|
$this->data = $data; |
122
|
|
|
|
123
|
|
|
if (! is_null($transformer)) { |
124
|
|
|
$this->transformer = $transformer; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set the class or function that will perform the transform. |
132
|
|
|
* |
133
|
|
|
* @param \League\Fractal\TransformerAbstract|callable $transformer |
134
|
|
|
* |
135
|
|
|
* @return $this |
136
|
|
|
*/ |
137
|
|
|
public function transformWith($transformer) |
138
|
|
|
{ |
139
|
|
|
$this->transformer = $transformer; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set a Fractal paginator for the data. |
146
|
|
|
* |
147
|
|
|
* @param \League\Fractal\Pagination\PaginatorInterface $paginator |
148
|
|
|
* |
149
|
|
|
* @return $this |
150
|
|
|
*/ |
151
|
|
|
public function paginateWith(PaginatorInterface $paginator) |
152
|
|
|
{ |
153
|
|
|
$this->paginator = $paginator; |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Set a Fractal cursor for the data. |
160
|
|
|
* |
161
|
|
|
* @param \League\Fractal\Pagination\CursorInterface $cursor |
162
|
|
|
* |
163
|
|
|
* @return $this |
164
|
|
|
*/ |
165
|
|
|
public function withCursor(CursorInterface $cursor) |
166
|
|
|
{ |
167
|
|
|
$this->cursor = $cursor; |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Specify the includes. |
174
|
|
|
* |
175
|
|
|
* @param array|string $includes Array or csv string of resources to include |
176
|
|
|
* |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
|
|
public function parseIncludes($includes) |
180
|
|
|
{ |
181
|
|
|
$includes = $this->normalizeExcludesOrIncludes($includes); |
182
|
|
|
|
183
|
|
|
$this->includes = array_merge($this->includes, (array) $includes); |
184
|
|
|
|
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function parseExcludes($excludes) |
189
|
|
|
{ |
190
|
|
|
$excludes = $this->normalizeExcludesOrIncludes($excludes); |
191
|
|
|
|
192
|
|
|
$this->excludes = array_merge($this->excludes, (array) $excludes); |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
protected function normalizeExcludesOrIncludes($includesOrExcludes) |
198
|
|
|
{ |
199
|
|
|
if (! is_string($includesOrExcludes)) { |
200
|
|
|
return $includesOrExcludes; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return array_map(function ($value) { |
204
|
|
|
return trim($value); |
205
|
|
|
}, explode(',', $includesOrExcludes)); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Support for magic methods to included data. |
210
|
|
|
* |
211
|
|
|
* @param string $name |
212
|
|
|
* @param array $arguments |
213
|
|
|
* |
214
|
|
|
* @return $this |
215
|
|
|
*/ |
216
|
|
|
public function __call($name, array $arguments) |
217
|
|
|
{ |
218
|
|
View Code Duplication |
if(starts_with($name, ['include'])) { |
|
|
|
|
219
|
|
|
$includeName = lcfirst(substr($name, strlen('include'))); |
220
|
|
|
return $this->parseIncludes($includeName); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
View Code Duplication |
if(starts_with($name, ['exclude'])) { |
|
|
|
|
224
|
|
|
$excludeName = lcfirst(substr($name, strlen('exclude'))); |
225
|
|
|
return $this->parseExcludes($excludeName); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
trigger_error('Call to undefined method '.__CLASS__.'::'.$name.'()', E_USER_ERROR); |
229
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Set the serializer to be used. |
234
|
|
|
* |
235
|
|
|
* @param \League\Fractal\Serializer\SerializerAbstract $serializer |
236
|
|
|
* |
237
|
|
|
* @return $this |
238
|
|
|
*/ |
239
|
|
|
public function serializeWith(SerializerAbstract $serializer) |
240
|
|
|
{ |
241
|
|
|
$this->serializer = $serializer; |
242
|
|
|
|
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Set the meta data. |
248
|
|
|
* |
249
|
|
|
* @param $array,... |
250
|
|
|
* |
251
|
|
|
* @return $this |
252
|
|
|
*/ |
253
|
|
|
public function addMeta() |
254
|
|
|
{ |
255
|
|
|
foreach (func_get_args() as $meta) { |
256
|
|
|
if (is_array($meta)) { |
257
|
|
|
$this->meta += $meta; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return $this; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Set the resource name, to replace 'data' as the root of the collection or item. |
266
|
|
|
* |
267
|
|
|
* @param string $resourceName |
268
|
|
|
* |
269
|
|
|
* @return $this |
270
|
|
|
*/ |
271
|
|
|
public function resourceName($resourceName) |
272
|
|
|
{ |
273
|
|
|
$this->resourceName = $resourceName; |
274
|
|
|
|
275
|
|
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Perform the transformation to json. |
280
|
|
|
* |
281
|
|
|
* @return string |
282
|
|
|
*/ |
283
|
|
|
public function toJson() |
284
|
|
|
{ |
285
|
|
|
return $this->transform('toJson'); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Perform the transformation to array. |
290
|
|
|
* |
291
|
|
|
* @return array |
292
|
|
|
*/ |
293
|
|
|
public function toArray() |
294
|
|
|
{ |
295
|
|
|
return $this->transform('toArray'); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Perform the transformation. |
300
|
|
|
* |
301
|
|
|
* @param string $conversionMethod |
302
|
|
|
* |
303
|
|
|
* @return string|array |
304
|
|
|
* |
305
|
|
|
* @throws \Spatie\Fractal\Exceptions\InvalidTransformation |
306
|
|
|
* @throws \Spatie\Fractal\Exceptions\NoTransformerSpecified |
307
|
|
|
*/ |
308
|
|
|
protected function transform($conversionMethod) |
309
|
|
|
{ |
310
|
|
|
$fractalData = $this->createData(); |
311
|
|
|
|
312
|
|
|
return $fractalData->$conversionMethod(); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Create fractal data. |
317
|
|
|
* |
318
|
|
|
* @return \League\Fractal\Scope |
319
|
|
|
* |
320
|
|
|
* @throws \Spatie\Fractal\Exceptions\InvalidTransformation |
321
|
|
|
* @throws \Spatie\Fractal\Exceptions\NoTransformerSpecified |
322
|
|
|
*/ |
323
|
|
|
public function createData() |
324
|
|
|
{ |
325
|
|
|
if (is_null($this->transformer)) { |
326
|
|
|
throw new NoTransformerSpecified(); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
if (! is_null($this->serializer)) { |
330
|
|
|
$this->manager->setSerializer($this->serializer); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
if (! is_null($this->includes)) { |
334
|
|
|
$this->manager->parseIncludes($this->includes); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
if (! is_null($this->excludes)) { |
338
|
|
|
$this->manager->parseExcludes($this->excludes); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
return $this->manager->createData($this->getResource()); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Get the resource. |
346
|
|
|
* |
347
|
|
|
* @return \League\Fractal\Resource\ResourceInterface |
348
|
|
|
* |
349
|
|
|
* @throws \Spatie\Fractal\Exceptions\InvalidTransformation |
350
|
|
|
*/ |
351
|
|
|
public function getResource() |
352
|
|
|
{ |
353
|
|
|
$resourceClass = 'League\\Fractal\\Resource\\'.ucfirst($this->dataType); |
354
|
|
|
|
355
|
|
|
if (! class_exists($resourceClass)) { |
356
|
|
|
throw new InvalidTransformation(); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
$resource = new $resourceClass($this->data, $this->transformer, $this->resourceName); |
360
|
|
|
|
361
|
|
|
$resource->setMeta($this->meta); |
362
|
|
|
|
363
|
|
|
if (! is_null($this->paginator)) { |
364
|
|
|
$resource->setPaginator($this->paginator); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
if (! is_null($this->cursor)) { |
368
|
|
|
$resource->setCursor($this->cursor); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
return $resource; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Convert the object into something JSON serializable. |
376
|
|
|
* |
377
|
|
|
* @return array |
378
|
|
|
*/ |
379
|
|
|
public function jsonSerialize() |
380
|
|
|
{ |
381
|
|
|
return $this->toArray(); |
382
|
|
|
} |
383
|
|
|
} |
384
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.