Completed
Pull Request — master (#36)
by Michael
07:34
created

Fractal::normalizeExcludesOrIncludes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 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 array
47
     */
48
    protected $excludes = [];
49
50
    /**
51
     * @var string
52
     */
53
    protected $dataType;
54
55
    /**
56
     * @var mixed
57
     */
58
    protected $data;
59
60
    /**
61
     * @var string
62
     */
63
    protected $resourceName;
64
65
    /**
66
     * @var array
67
     */
68
    protected $meta = [];
69
70
    /**
71
     * @param \League\Fractal\Manager $manager
72
     */
73
    public function __construct(Manager $manager)
74
    {
75
        $this->manager = $manager;
76
    }
77
78
    /**
79
     * Set the collection data that must be transformed.
80
     *
81
     * @param mixed                                             $data
82
     * @param \League\Fractal\TransformerAbstract|callable|null $transformer
83
     * @param string|null                                       $resourceName
84
     *
85
     * @return $this
86
     */
87
    public function collection($data, $transformer = null, $resourceName = null)
88
    {
89
        $this->resourceName = $resourceName;
90
91
        return $this->data('collection', $data, $transformer);
92
    }
93
94
    /**
95
     * Set the item data that must be transformed.
96
     *
97
     * @param mixed                                             $data
98
     * @param \League\Fractal\TransformerAbstract|callable|null $transformer
99
     * @param string|null                                       $resourceName
100
     *
101
     * @return $this
102
     */
103
    public function item($data, $transformer = null, $resourceName = null)
104
    {
105
        $this->resourceName = $resourceName;
106
107
        return $this->data('item', $data, $transformer);
108
    }
109
110
    /**
111
     * Set the data that must be transformed.
112
     *
113
     * @param string                                            $dataType
114
     * @param mixed                                             $data
115
     * @param \League\Fractal\TransformerAbstract|callable|null $transformer
116
     *
117
     * @return $this
118
     */
119
    protected function data($dataType, $data, $transformer = null)
120
    {
121
        $this->dataType = $dataType;
122
123
        $this->data = $data;
124
125
        if (! is_null($transformer)) {
126
            $this->transformer = $transformer;
127
        }
128
129
        return $this;
130
    }
131
132
    /**
133
     * Set the class or function that will perform the transform.
134
     *
135
     * @param \League\Fractal\TransformerAbstract|callable $transformer
136
     *
137
     * @return $this
138
     */
139
    public function transformWith($transformer)
140
    {
141
        $this->transformer = $transformer;
142
143
        return $this;
144
    }
145
146
    /**
147
     * Set a Fractal paginator for the data.
148
     *
149
     * @param \League\Fractal\Pagination\PaginatorInterface $paginator
150
     *
151
     * @return $this
152
     */
153
    public function paginateWith(PaginatorInterface $paginator)
154
    {
155
        $this->paginator = $paginator;
156
157
        return $this;
158
    }
159
160
    /**
161
     * Set a Fractal cursor for the data.
162
     *
163
     * @param \League\Fractal\Pagination\CursorInterface $cursor
164
     *
165
     * @return $this
166
     */
167
    public function withCursor(CursorInterface $cursor)
168
    {
169
        $this->cursor = $cursor;
170
171
        return $this;
172
    }
173
174
    /**
175
     * Specify the includes.
176
     *
177
     * @param array|string $includes Array or csv string of resources to include
178
     *
179
     * @return $this
180
     */
181
    public function parseIncludes($includes)
182
    {
183
        $includes = $this->normalizeExcludesOrIncludes($includes);
184
185
        $this->includes = array_merge($this->includes, (array) $includes);
186
187
        return $this;
188
    }
189
190
    /**
191
     * Specify the excludes.
192
     *
193
     * @param $excludes
194
     * @return $this
195
     */
196
    public function parseExcludes($excludes)
197
    {
198
        $excludes = $this->normalizeExcludesOrIncludes($excludes);
199
200
        $this->excludes = array_merge($this->excludes, (array) $excludes);
201
202
        return $this;
203
    }
204
205
    /**
206
     * Normalize the includes an excludes.
207
     *
208
     * @param $includesOrExcludes
209
     * @return array
210
     */
211
    protected function normalizeExcludesOrIncludes($includesOrExcludes)
212
    {
213
        if (! is_string($includesOrExcludes)) {
214
            return $includesOrExcludes;
215
        }
216
217
        return array_map(function ($value) {
218
            return trim($value);
219
        }, explode(',', $includesOrExcludes));
220
    }
221
222
    /**
223
     * Support for magic methods to included data.
224
     *
225
     * @param string $name
226
     * @param array  $arguments
227
     *
228
     * @return $this
229
     */
230
    public function __call($name, array $arguments)
231
    {
232 View Code Duplication
        if (starts_with($name, ['include'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
233
            $includeName = lcfirst(substr($name, strlen('include')));
234
235
            return $this->parseIncludes($includeName);
236
        }
237
238 View Code Duplication
        if (starts_with($name, ['exclude'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
239
            $excludeName = lcfirst(substr($name, strlen('exclude')));
240
241
            return $this->parseExcludes($excludeName);
242
        }
243
244
        trigger_error('Call to undefined method '.__CLASS__.'::'.$name.'()', E_USER_ERROR);
245
    }
246
247
    /**
248
     * Set the serializer to be used.
249
     *
250
     * @param \League\Fractal\Serializer\SerializerAbstract $serializer
251
     *
252
     * @return $this
253
     */
254
    public function serializeWith(SerializerAbstract $serializer)
255
    {
256
        $this->serializer = $serializer;
257
258
        return $this;
259
    }
260
261
    /**
262
     * Set the meta data.
263
     *
264
     * @param $array,...
265
     *
266
     * @return $this
267
     */
268
    public function addMeta()
269
    {
270
        foreach (func_get_args() as $meta) {
271
            if (is_array($meta)) {
272
                $this->meta += $meta;
273
            }
274
        }
275
276
        return $this;
277
    }
278
279
    /**
280
     * Set the resource name, to replace 'data' as the root of the collection or item.
281
     *
282
     * @param string $resourceName
283
     *
284
     * @return $this
285
     */
286
    public function resourceName($resourceName)
287
    {
288
        $this->resourceName = $resourceName;
289
290
        return $this;
291
    }
292
293
    /**
294
     * Perform the transformation to json.
295
     *
296
     * @return string
297
     */
298
    public function toJson()
299
    {
300
        return $this->transform('toJson');
301
    }
302
303
    /**
304
     * Perform the transformation to array.
305
     *
306
     * @return array
307
     */
308
    public function toArray()
309
    {
310
        return $this->transform('toArray');
311
    }
312
313
    /**
314
     *  Perform the transformation.
315
     *
316
     * @param string $conversionMethod
317
     *
318
     * @return string|array
319
     *
320
     * @throws \Spatie\Fractal\Exceptions\InvalidTransformation
321
     * @throws \Spatie\Fractal\Exceptions\NoTransformerSpecified
322
     */
323
    protected function transform($conversionMethod)
324
    {
325
        $fractalData = $this->createData();
326
327
        return $fractalData->$conversionMethod();
328
    }
329
330
    /**
331
     * Create fractal data.
332
     *
333
     * @return \League\Fractal\Scope
334
     *
335
     * @throws \Spatie\Fractal\Exceptions\InvalidTransformation
336
     * @throws \Spatie\Fractal\Exceptions\NoTransformerSpecified
337
     */
338
    public function createData()
339
    {
340
        if (is_null($this->transformer)) {
341
            throw new NoTransformerSpecified();
342
        }
343
344
        if (! is_null($this->serializer)) {
345
            $this->manager->setSerializer($this->serializer);
346
        }
347
348
        if (! is_null($this->includes)) {
349
            $this->manager->parseIncludes($this->includes);
350
        }
351
352
        if (! is_null($this->excludes)) {
353
            $this->manager->parseExcludes($this->excludes);
354
        }
355
356
357
358
        return $this->manager->createData($this->getResource());
359
    }
360
361
    /**
362
     * Get the resource.
363
     *
364
     * @return \League\Fractal\Resource\ResourceInterface
365
     *
366
     * @throws \Spatie\Fractal\Exceptions\InvalidTransformation
367
     */
368
    public function getResource()
369
    {
370
        $resourceClass = 'League\\Fractal\\Resource\\'.ucfirst($this->dataType);
371
372
        if (! class_exists($resourceClass)) {
373
            throw new InvalidTransformation();
374
        }
375
376
        $resource = new $resourceClass($this->data, $this->transformer, $this->resourceName);
377
378
        $resource->setMeta($this->meta);
379
380
        if (! is_null($this->paginator)) {
381
            $resource->setPaginator($this->paginator);
382
        }
383
384
        if (! is_null($this->cursor)) {
385
            $resource->setCursor($this->cursor);
386
        }
387
388
        return $resource;
389
    }
390
391
    /**
392
     * Convert the object into something JSON serializable.
393
     *
394
     * @return array
395
     */
396
    public function jsonSerialize()
397
    {
398
        return $this->toArray();
399
    }
400
}
401