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
Push — master ( 6e0cf4...714232 )
by Freek
02:58
created

Fractal::startsWith()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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