GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#12)
by
unknown
01:45
created

Fractal::serializeWith()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Fractalistic;
4
5
use Traversable;
6
use JsonSerializable;
7
use League\Fractal\Manager;
8
use League\Fractal\Pagination\CursorInterface;
9
use League\Fractal\Pagination\PaginatorInterface;
10
use League\Fractal\Serializer\SerializerAbstract;
11
use Spatie\Fractalistic\Exceptions\InvalidTransformation;
12
use Spatie\Fractalistic\Exceptions\NoTransformerSpecified;
13
14
class Fractal implements JsonSerializable
15
{
16
    /** @var \League\Fractal\Manager */
17
    protected $manager;
18
19
    /** @var \League\Fractal\Serializer\SerializerAbstract */
20
    protected $serializer;
21
22
    /** @var \League\Fractal\TransformerAbstract|callable */
23
    protected $transformer;
24
25
    /** @var \League\Fractal\Pagination\PaginatorInterface */
26
    protected $paginator;
27
28
    /** @var \League\Fractal\Pagination\CursorInterface */
29
    protected $cursor;
30
31
    /** @var array */
32
    protected $includes = [];
33
34
    /** @var array */
35
    protected $excludes = [];
36
37
    /** @var string */
38
    protected $dataType;
39
40
    /** @var mixed */
41
    protected $data;
42
43
    /** @var string */
44
    protected $resourceName;
45
46
    /** @var array */
47
    protected $meta = [];
48
49
    /**
50
     * @param null|mixed $data
51
     * @param null|callable|\League\Fractal\TransformerAbstract $transformer
52
     * @param null|\League\Fractal\Serializer\SerializerAbstract $serializer
53
     *
54
     * @return \Spatie\Fractalistic\Fractal
55
     */
56
    public static function create($data = null, $transformer = null, $serializer = null)
57
    {
58
        $instance = new static(new Manager());
59
60
        $instance->data = $data ?: null;
61
        $instance->dataType = $data ? $instance->determineDataType($data) : null;
62
        $instance->transformer = $transformer ?: null;
63
        $instance->serializer = $serializer ?: null;
64
65
        return $instance;
66
    }
67
68
    /** @param \League\Fractal\Manager $manager */
69
    public function __construct(Manager $manager)
70
    {
71
        $this->manager = $manager;
72
    }
73
74
    /**
75
     * Set the collection data that must be transformed.
76
     *
77
     * @param mixed                                             $data
78
     * @param \League\Fractal\TransformerAbstract|callable|null $transformer
79
     * @param string|null                                       $resourceName
80
     *
81
     * @return $this
82
     */
83
    public function collection($data, $transformer = null, $resourceName = null)
84
    {
85
        $this->resourceName = $resourceName;
86
87
        return $this->data('collection', $data, $transformer);
88
    }
89
90
    /**
91
     * Set the item data that must be transformed.
92
     *
93
     * @param mixed                                             $data
94
     * @param \League\Fractal\TransformerAbstract|callable|null $transformer
95
     * @param string|null                                       $resourceName
96
     *
97
     * @return $this
98
     */
99
    public function item($data, $transformer = null, $resourceName = null)
100
    {
101
        $this->resourceName = $resourceName;
102
103
        return $this->data('item', $data, $transformer);
104
    }
105
106
    /**
107
     * Set the data that must be transformed.
108
     *
109
     * @param string                                            $dataType
110
     * @param mixed                                             $data
111
     * @param \League\Fractal\TransformerAbstract|callable|null $transformer
112
     *
113
     * @return $this
114
     */
115
    public function data($dataType, $data, $transformer = null)
116
    {
117
        $this->dataType = $dataType;
118
119
        $this->data = $data;
120
121
        if (! is_null($transformer)) {
122
            $this->transformer = $transformer;
123
        }
124
125
        return $this;
126
    }
127
128
    /**
129
     * @param mixed $data
130
     *
131
     * @return string
132
     */
133
    protected function determineDataType($data)
134
    {
135
        if (is_array($data)) {
136
            return 'collection';
137
        }
138
139
        if ($data instanceof Traversable) {
140
            return 'collection';
141
        }
142
143
        return 'item';
144
    }
145
146
    /**
147
     * Set the class or function that will perform the transform.
148
     *
149
     * @param \League\Fractal\TransformerAbstract|callable $transformer
150
     *
151
     * @return $this
152
     */
153
    public function transformWith($transformer)
154
    {
155
        if (is_string($transformer) && class_exists($transformer)) {
156
            $transformer = new $transformer;
157
        }
158
159
        $this->transformer = $transformer;
160
161
        return $this;
162
    }
163
164
    /**
165
     * Set the serializer to be used.
166
     *
167
     * @param \League\Fractal\Serializer\SerializerAbstract $serializer
168
     *
169
     * @return $this
170
     */
171
    public function serializeWith(SerializerAbstract $serializer)
172
    {
173
        $this->serializer = $serializer;
174
175
        return $this;
176
    }
177
178
    /**
179
     * Set a Fractal paginator for the data.
180
     *
181
     * @param \League\Fractal\Pagination\PaginatorInterface $paginator
182
     *
183
     * @return $this
184
     */
185
    public function paginateWith(PaginatorInterface $paginator)
186
    {
187
        $this->paginator = $paginator;
188
189
        return $this;
190
    }
191
192
    /**
193
     * Set a Fractal cursor for the data.
194
     *
195
     * @param \League\Fractal\Pagination\CursorInterface $cursor
196
     *
197
     * @return $this
198
     */
199
    public function withCursor(CursorInterface $cursor)
200
    {
201
        $this->cursor = $cursor;
202
203
        return $this;
204
    }
205
206
    /**
207
     * Specify the includes.
208
     *
209
     * @param array|string $includes Array or string of resources to include.
210
     *
211
     * @return $this
212
     */
213
    public function parseIncludes($includes)
214
    {
215
        $includes = $this->normalizeIncludesOrExcludes($includes);
216
217
        $this->includes = array_merge($this->includes, (array) $includes);
218
219
        return $this;
220
    }
221
222
    /**
223
     * Specify the excludes.
224
     *
225
     * @param array|string $excludes Array or string of resources to exclude.
226
     * @return $this
227
     */
228
    public function parseExcludes($excludes)
229
    {
230
        $excludes = $this->normalizeIncludesOrExcludes($excludes);
231
232
        $this->excludes = array_merge($this->excludes, (array) $excludes);
233
234
        return $this;
235
    }
236
237
    /**
238
     * Normalize the includes an excludes.
239
     *
240
     * @param array|string $includesOrExcludes
241
     *
242
     * @return array|string
243
     */
244
    protected function normalizeIncludesOrExcludes($includesOrExcludes = '')
245
    {
246
        if (! is_string($includesOrExcludes)) {
247
            return $includesOrExcludes;
248
        }
249
250
        return array_map(function ($value) {
251
            return trim($value);
252
        }, explode(',', $includesOrExcludes));
253
    }
254
255
    /**
256
     * Set the meta data.
257
     *
258
     * @param $array,...
259
     *
260
     * @return $this
261
     */
262
    public function addMeta()
263
    {
264
        foreach (func_get_args() as $meta) {
265
            if (is_array($meta)) {
266
                $this->meta += $meta;
267
            }
268
        }
269
270
        return $this;
271
    }
272
273
    /**
274
     * Set the resource name, to replace 'data' as the root of the collection or item.
275
     *
276
     * @param string $resourceName
277
     *
278
     * @return $this
279
     */
280
    public function withResourceName($resourceName)
281
    {
282
        $this->resourceName = $resourceName;
283
284
        return $this;
285
    }
286
287
    /**
288
     * Perform the transformation to json.
289
     *
290
     * @return string
291
     */
292
    public function toJson()
293
    {
294
        return $this->createData()->toJson();
295
    }
296
297
    /**
298
     * Perform the transformation to array.
299
     *
300
     * @return array
301
     */
302
    public function toArray()
303
    {
304
        return $this->createData()->toArray();
305
    }
306
307
    /**
308
     * Create fractal data.
309
     *
310
     * @return \League\Fractal\Scope
311
     *
312
     * @throws \Spatie\Fractalistic\Exceptions\InvalidTransformation
313
     * @throws \Spatie\Fractalistic\Exceptions\NoTransformerSpecified
314
     */
315
    public function createData()
316
    {
317
        if (is_null($this->transformer)) {
318
            throw new NoTransformerSpecified();
319
        }
320
321
        if (! is_null($this->serializer)) {
322
            $this->manager->setSerializer($this->serializer);
323
        }
324
325
        if (! is_null($this->includes)) {
326
            $this->manager->parseIncludes($this->includes);
327
        }
328
329
        if (! is_null($this->excludes)) {
330
            $this->manager->parseExcludes($this->excludes);
331
        }
332
333
        return $this->manager->createData($this->getResource());
334
    }
335
336
    /**
337
     * Get the resource.
338
     *
339
     * @return \League\Fractal\Resource\ResourceInterface
340
     *
341
     * @throws \Spatie\Fractalistic\Exceptions\InvalidTransformation
342
     */
343
    public function getResource()
344
    {
345
        $resourceClass = 'League\\Fractal\\Resource\\'.ucfirst($this->dataType);
346
    
347
        if (! class_exists($resourceClass)) {
348
            throw new InvalidTransformation();
349
        }
350
351
        $resource = new $resourceClass($this->data, $this->transformer, $this->resourceName);
352
353
        $resource->setMeta($this->meta);
354
355
        if (! is_null($this->paginator)) {
356
            $resource->setPaginator($this->paginator);
357
        }
358
359
        if (! is_null($this->cursor)) {
360
            $resource->setCursor($this->cursor);
361
        }
362
363
        return $resource;
364
    }
365
366
    /**
367
     * Convert the object into something JSON serializable.
368
     */
369
    public function jsonSerialize()
370
    {
371
        return $this->toArray();
372
    }
373
374
    /**
375
     * Support for magic methods to included data.
376
     *
377
     * @param string $name
378
     * @param array  $arguments
379
     *
380
     * @return $this
381
     */
382
    public function __call($name, array $arguments)
383
    {
384 View Code Duplication
        if ($this->startsWith($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...
385
            $includeName = lcfirst(substr($name, strlen('include')));
386
387
            return $this->parseIncludes($includeName);
388
        }
389
390 View Code Duplication
        if ($this->startsWith($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...
391
            $excludeName = lcfirst(substr($name, strlen('exclude')));
392
393
            return $this->parseExcludes($excludeName);
394
        }
395
396
        trigger_error('Call to undefined method '.__CLASS__.'::'.$name.'()', E_USER_ERROR);
397
    }
398
399
    /**
400
     * Determine if a given string starts with a given substring.
401
     *
402
     * @param  string  $haystack
403
     * @param  string|array  $needles
404
     * @return bool
405
     */
406
    protected function startsWith($haystack, $needles)
407
    {
408
        foreach ((array) $needles as $needle) {
409
            if ($needle != '' && substr($haystack, 0, strlen($needle)) === (string) $needle) {
410
                return true;
411
            }
412
        }
413
414
        return false;
415
    }
416
417
    /**
418
    * Locale Tr problem, change item to Item *
419
    */
420
421
    private function mb_ucfirst($dataType){
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
422
        return $dataType == 'item' ? 'Item' : ucfirst($dataType);
423
    }
424
}
425