Completed
Push — master ( 664dba...f114a2 )
by Freek
9s
created

Fractal::getResource()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 0 Features 3
Metric Value
c 8
b 0
f 3
dl 0
loc 22
rs 8.9197
cc 4
eloc 11
nc 5
nop 0
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
     * @var string
47
     */
48
    protected $dataType;
49
50
    /**
51
     * @var mixed
52
     */
53
    protected $data;
54
55
    /**
56
     * @var string
57
     */
58
    protected $resourceName;
59
60
    /**
61
     * @var array
62
     */
63
    protected $meta = [];
64
65
    /**
66
     * @param \League\Fractal\Manager $manager
67
     */
68
    public function __construct(Manager $manager)
69
    {
70
        $this->manager = $manager;
71
    }
72
73
    /**
74
     * Set the collection data that must be transformed.
75
     *
76
     * @param mixed                                             $data
77
     * @param \League\Fractal\TransformerAbstract|callable|null $transformer
78
     * @param string|null                                       $resourceName
79
     *
80
     * @return $this
81
     */
82
    public function collection($data, $transformer = null, $resourceName = null)
83
    {
84
        $this->resourceName = $resourceName;
85
86
        return $this->data('collection', $data, $transformer);
87
    }
88
89
    /**
90
     * Set the item data that must be transformed.
91
     *
92
     * @param mixed                                             $data
93
     * @param \League\Fractal\TransformerAbstract|callable|null $transformer
94
     * @param string|null                                       $resourceName
95
     *
96
     * @return $this
97
     */
98
    public function item($data, $transformer = null, $resourceName = null)
99
    {
100
        $this->resourceName = $resourceName;
101
102
        return $this->data('item', $data, $transformer);
103
    }
104
105
    /**
106
     * Set the data that must be transformed.
107
     *
108
     * @param string                                            $dataType
109
     * @param mixed                                             $data
110
     * @param \League\Fractal\TransformerAbstract|callable|null $transformer
111
     *
112
     * @return $this
113
     */
114
    protected function data($dataType, $data, $transformer = null)
115
    {
116
        $this->dataType = $dataType;
117
118
        $this->data = $data;
119
120
        if (!is_null($transformer)) {
121
            $this->transformer = $transformer;
122
        }
123
124
        return $this;
125
    }
126
127
    /**
128
     * Set the class or function that will perform the transform.
129
     *
130
     * @param \League\Fractal\TransformerAbstract|callable $transformer
131
     *
132
     * @return $this
133
     */
134
    public function transformWith($transformer)
135
    {
136
        $this->transformer = $transformer;
137
138
        return $this;
139
    }
140
141
    /**
142
     * Set a Fractal paginator for the data.
143
     *
144
     * @param \League\Fractal\Pagination\PaginatorInterface $paginator
145
     *
146
     * @return $this
147
     */
148
    public function paginateWith(PaginatorInterface $paginator)
149
    {
150
        $this->paginator = $paginator;
151
152
        return $this;
153
    }
154
155
    /**
156
     * Set a Fractal cursor for the data
157
     *
158
     * @param  League\Fractal\Pagination\CursorInterface $cursor
159
     *
160
     * @return $this
161
     */
162
    public function withCursor(CursorInterface $cursor)
163
    {
164
        $this->cursor = $cursor;
165
166
        return $this;
167
    }
168
169
    /**
170
     * Specify the includes.
171
     *
172
     * @param array|string $includes Array or csv string of resources to include
173
     *
174
     * @return $this
175
     */
176
    public function parseIncludes($includes)
177
    {
178
        if (is_string($includes)) {
179
            $includes = array_map(function ($value) {
180
                return trim($value);
181
            }, explode(',', $includes));
182
        }
183
184
        $this->includes = array_merge($this->includes, (array) $includes);
185
186
        return $this;
187
    }
188
189
    /**
190
     * Support for magic methods to included data.
191
     *
192
     * @param string $name
193
     * @param array  $arguments
194
     *
195
     * @return $this
196
     */
197
    public function __call($name, array $arguments)
198
    {
199
        if (!starts_with($name, 'include')) {
200
            trigger_error('Call to undefined method ' . __CLASS__ . '::' . $name . '()', E_USER_ERROR);
201
        }
202
203
        $includeName = lcfirst(substr($name, strlen('include')));
204
205
        return $this->parseIncludes($includeName);
206
    }
207
208
    /**
209
     * Set the serializer to be used.
210
     *
211
     * @param \League\Fractal\Serializer\SerializerAbstract $serializer
212
     *
213
     * @return $this
214
     */
215
    public function serializeWith(SerializerAbstract $serializer)
216
    {
217
        $this->serializer = $serializer;
218
219
        return $this;
220
    }
221
222
    /**
223
     * Set the meta data.
224
     *
225
     * @param $array,...
226
     *
227
     * @return $this
228
     */
229
    public function addMeta()
230
    {
231
        foreach (func_get_args() as $meta) {
232
            if (is_array($meta)) {
233
                $this->meta += $meta;
234
            }
235
        }
236
237
        return $this;
238
    }
239
240
    /**
241
     * Set the resource name, to replace 'data' as the root of the collection or item.
242
     *
243
     * @param string $resourceName
244
     *
245
     * @return $this
246
     */
247
    public function resourceName($resourceName)
248
    {
249
        $this->resourceName = $resourceName;
250
251
        return $this;
252
    }
253
254
    /**
255
     * Perform the transformation to json.
256
     *
257
     * @return string
258
     */
259
    public function toJson()
260
    {
261
        return $this->transform('toJson');
262
    }
263
264
    /**
265
     * Perform the transformation to array.
266
     *
267
     * @return array
268
     */
269
    public function toArray()
270
    {
271
        return $this->transform('toArray');
272
    }
273
274
    /**
275
     *  Perform the transformation.
276
     *
277
     * @param string $conversionMethod
278
     *
279
     * @return string|array
280
     *
281
     * @throws \Spatie\Fractal\Exceptions\InvalidTransformation
282
     * @throws \Spatie\Fractal\Exceptions\NoTransformerSpecified
283
     */
284
    protected function transform($conversionMethod)
285
    {
286
        $fractalData = $this->createData();
287
288
        return $fractalData->$conversionMethod();
289
    }
290
291
    /**
292
     * Create fractal data.
293
     *
294
     * @return \League\Fractal\Scope
295
     *
296
     * @throws \Spatie\Fractal\Exceptions\InvalidTransformation
297
     * @throws \Spatie\Fractal\Exceptions\NoTransformerSpecified
298
     */
299
    public function createData()
300
    {
301
        if (is_null($this->transformer)) {
302
            throw new NoTransformerSpecified();
303
        }
304
305
        if (!is_null($this->serializer)) {
306
            $this->manager->setSerializer($this->serializer);
307
        }
308
309
        if (!is_null($this->includes)) {
310
            $this->manager->parseIncludes($this->includes);
311
        }
312
313
        $resource = $this->getResource();
314
315
        return $this->manager->createData($resource);
316
    }
317
318
    /**
319
     * Get the resource.
320
     *
321
     * @return \League\Fractal\Resource\ResourceInterface
322
     *
323
     * @throws \Spatie\Fractal\Exceptions\InvalidTransformation
324
     */
325
    public function getResource()
326
    {
327
        $resourceClass = 'League\\Fractal\\Resource\\' . ucfirst($this->dataType);
328
329
        if (!class_exists($resourceClass)) {
330
            throw new InvalidTransformation();
331
        }
332
333
        $resource = new $resourceClass($this->data, $this->transformer, $this->resourceName);
334
335
        $resource->setMeta($this->meta);
336
337
        if (!is_null($this->paginator)) {
338
            $resource->setPaginator($this->paginator);
339
        }
340
341
        if (!is_null($this->cursor)) {
342
            $resource->setCursor($this->cursor);
343
        }
344
345
        return $resource;
346
    }
347
348
    /**
349
     * Convert the object into something JSON serializable.
350
     *
351
     * @return array
352
     */
353
    public function jsonSerialize()
354
    {
355
        return $this->toArray();
356
    }
357
}
358