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 ( 862be0...4d4fd5 )
by Sergey
02:55
created

DataParser::setPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * This file is part of the reva2/jsonapi.
4
 *
5
 * (c) Sergey Revenko <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Reva2\JsonApi\Decoders;
12
13
use Neomerx\JsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface;
14
use Neomerx\JsonApi\Document\Error;
15
use Neomerx\JsonApi\Exceptions\JsonApiException;
16
use Reva2\JsonApi\Contracts\Decoders\DataParserInterface;
17
use Reva2\JsonApi\Contracts\Decoders\Mapping\DocumentMetadataInterface;
18
use Reva2\JsonApi\Contracts\Decoders\Mapping\Factory\MetadataFactoryInterface;
19
use Reva2\JsonApi\Contracts\Decoders\Mapping\ObjectMetadataInterface;
20
use Reva2\JsonApi\Contracts\Decoders\Mapping\PropertyMetadataInterface;
21
use Reva2\JsonApi\Contracts\Decoders\Mapping\ResourceMetadataInterface;
22
use Symfony\Component\PropertyAccess\PropertyAccess;
23
use Symfony\Component\PropertyAccess\PropertyAccessor;
24
25
/**
26
 * Data parser
27
 *
28
 * @package Reva2\JsonApi\Decoders
29
 * @author Sergey Revenko <[email protected]>
30
 */
31
class DataParser implements DataParserInterface
32
{
33
    const ERROR_CODE = 'ee2c1d49-ba40-4077-a6bb-b06baceb3e97';
34
35
    /**
36
     * Current path
37
     *
38
     * @var \SplStack
39
     */
40
    protected $path;
41
42
    /**
43
     * Resource decoders factory
44
     *
45
     * @var MetadataFactoryInterface
46
     */
47
    protected $factory;
48
49
    /**
50
     * @var PropertyAccessor
51
     */
52
    protected $accessor;
53
54
    /**
55
     * Constructor
56
     *
57
     * @param MetadataFactoryInterface $factory
58
     */
59 11
    public function __construct(MetadataFactoryInterface $factory)
60
    {
61 11
        $this->factory = $factory;
62 11
        $this->accessor = PropertyAccess::createPropertyAccessor();
63
        
64 11
        $this->initPathStack();
65 11
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70 11
    public function setPath($path)
71
    {
72 11
        $this->path->push($this->preparePathSegment($path));
73
74 11
        return $this;
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80 11
    public function restorePath()
81
    {
82 11
        $this->path->pop();
83
84 11
        return $this;
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90 1
    public function getPath()
91
    {
92 1
        $segments = [];
93 1
        foreach ($this->path as $segment) {
94 1
            $segments[] = $segment;
95
        }
96
97 1
        return '/' . implode('/', array_reverse($segments));
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103 11
    public function hasValue($data, $path)
104
    {
105 11
        return $this->accessor->isReadable($data, $path);
106
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111 11
    public function getValue($data, $path)
112
    {
113 11
        return $this->accessor->getValue($data, $path);
114
    }
115
116
    /**
117
     * @inheritdoc
118
     */
119 6
    public function parseString($data, $path)
120
    {
121 6
        $this->setPath($path);
122
123 6
        $pathValue = null;
124 6
        if ($this->hasValue($data, $path)) {
125 6
            $value = $this->getValue($data, $path);
126 6
            if ((null === $value) || (is_string($value))) {
127 6
                $pathValue = $value;
128
            } else {
129 1
                throw new \InvalidArgumentException(
130 1
                    sprintf("Value expected to be a string, but %s given", gettype($value)),
131 1
                    400
132
                );
133
            }
134
        }
135 6
        $this->restorePath();
136
137 6
        return $pathValue;
138
    }
139
140
    /**
141
     * @inheritdoc
142
     */
143 3
    public function parseInt($data, $path)
144
    {
145 3
        return $this->parseNumeric($data, $path, 'int');
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151 2
    public function parseFloat($data, $path)
152
    {
153 2
        return $this->parseNumeric($data, $path, 'float');
154
    }
155
156
    /**
157
     * @inheritdoc
158
     */
159 1 View Code Duplication
    public function parseRaw($data, $path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
160
    {
161 1
        $this->setPath($path);
162
163 1
        $pathValue = null;
164 1
        if ($this->hasValue($data, $path)) {
165 1
            $pathValue = $this->getValue($data, $path);
166
        }
167
168 1
        $this->restorePath();
169
170 1
        return $pathValue;
171
    }
172
173
    /**
174
     * @inheritdoc
175
     */
176 2 View Code Duplication
    public function parseCallback($data, $path, $callback)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
177
    {
178 2
        $this->setPath($path);
179
180 2
        $pathValue = null;
181 2
        if ($this->hasValue($data, $path)) {
182 2
            $pathValue = call_user_func($callback, $this->getValue($data, $path));
183
        }
184
185 2
        $this->restorePath();
186
187 2
        return $pathValue;
188
    }
189
190
    /**
191
     * @inheritdoc
192
     */
193 2
    public function parseBool($data, $path)
194
    {
195 2
        $this->setPath($path);
196
197 2
        $pathValue = null;
198 2
        if ($this->hasValue($data, $path)) {
199 2
            $value = $this->getValue($data, $path);
200 2
            if ((null === $value) || (is_bool($value))) {
201 1
                $pathValue = $value;
202 2
            } elseif (is_string($value)) {
203 2
                $pathValue = (in_array($value, ['true', 'yes', 'y', 'on', 'enabled'])) ? true : false;
204 1
            } elseif (is_numeric($value)) {
205 1
                $pathValue = (bool) $value;
206
            } else {
207 1
                throw new \InvalidArgumentException(
208 1
                    sprintf("Value expected to be a boolean, but %s given", gettype($value)),
209 1
                    400
210
                );
211
            }
212
        }
213
214 2
        $this->restorePath();
215
216 2
        return $pathValue;
217
    }
218
219
    /**
220
     * @inheritdoc
221
     */
222 2
    public function parseDateTime($data, $path, $format = 'Y-m-d')
223
    {
224 2
        $this->setPath($path);
225
226 2
        $pathValue = null;
227 2
        if ($this->hasValue($data, $path)) {
228 2
            $value = $this->getValue($data, $path);
229 2
            if (null !== $value) {
230 2
                if (is_string($value)) {
231 2
                    $pathValue = \DateTimeImmutable::createFromFormat($format, $value);
232
                }
233
234 2
                if (!$pathValue instanceof \DateTimeImmutable) {
235 1
                    throw new \InvalidArgumentException(
236 1
                        sprintf("Value expected to be a date/time string in '%s' format", $format),
237 1
                        400
238
                    );
239
                }
240
            }
241
        }
242
243 2
        $this->restorePath();
244
245 2
        return $pathValue;
246
    }
247
248
    /**
249
     * @inheritdoc
250
     */
251 4
    public function parseArray($data, $path, \Closure $itemsParser)
252
    {
253 4
        $this->setPath($path);
254
255 4
        $pathValue = null;
256 4
        if ($this->hasValue($data, $path)) {
257 4
            $value = $this->getValue($data, $path);
258 4
            if ((null !== $value) && (false === is_array($value))) {
259 1
                throw new \InvalidArgumentException(
260 1
                    sprintf("Value expected to be an array, but %s given", gettype($value)),
261 1
                    400
262
                );
263 4
            } elseif (is_array($value)) {
264 4
                $pathValue = [];
265 4
                $keys = array_keys($value);
266 4
                foreach ($keys as $key) {
267 4
                    $arrayPath = sprintf("[%s]", $key);
268
269 4
                    $pathValue[$key] = $itemsParser($value, $arrayPath, $this);
270
                }
271
            }
272
        }
273
274 3
        $this->restorePath();
275
276 3
        return $pathValue;
277
    }
278
279
    /**
280
     * Parse data object value at specified path as object of specified class
281
     *
282
     * @param array|object $data
283
     * @param string $path
284
     * @param string $objType
285
     * @return null
286
     */
287 1
    public function parseObject($data, $path, $objType)
288
    {
289 1
        $this->setPath($path);
290
291 1
        $pathValue = null;
292 1
        if ((true === $this->hasValue($data, $path)) &&
293 1
            (null !== ($value = $this->getValue($data, $path)))
294
        ) {
295 1
            $this->restorePath();
296
297 1
            $pathValue = $this->parseObjectValue($value, $objType);
298
        }
299
300 1
        return $pathValue;
301
    }
302
303
    /**
304
     * @inheritdoc
305
     */
306 3
    public function parseResource($data, $path, $resType)
307
    {
308 3
        $this->setPath($path);
309
310 3
        $pathValue = null;
311 3
        if ((true === $this->hasValue($data, $path)) &&
312 3
            (null !== ($value = $this->getValue($data, $path)))
313
        ) {
314 3
            $metadata = $this->factory->getMetadataFor($resType);
315
            /* @var $metadata ResourceMetadataInterface */
316
317 3 View Code Duplication
            if ((null !== ($discField = $metadata->getDiscriminatorField()))) {
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...
318 3
                $discValue = $this->parseString($value, $discField->getDataPath());
319 3
                $discClass = $metadata->getDiscriminatorClass($discValue);
320 3
                if ($discClass !== $resType) {
321 3
                    $this->restorePath();
322
323 3
                    return $this->parseResource($data, $path, $discClass);
324
                }
325
            }
326
327 3
            $name = $this->parseString($value, 'type');
328 3
            if ($name !== $metadata->getName()) {
329 1
                throw new \InvalidArgumentException(
330 1
                    sprintf("Value must contain resource of type '%s'", $metadata->getName()),
331 1
                    409
332
                );
333
            }
334
335 3
            $objClass = $metadata->getClassName();
336 3
            $pathValue = new $objClass();
337
338 3
            $this->parseProperty($value, $pathValue, $metadata->getIdMetadata());
339
340 3
            foreach ($metadata->getAttributes() as $attribute) {
341 3
                $this->parseProperty($value, $pathValue, $attribute);
342
            }
343
344 3
            foreach ($metadata->getRelationships() as $relationship) {
345 3
                $this->parseProperty($value, $pathValue, $relationship);
346
            }
347
        }
348
349 2
        $this->restorePath();
350
351 2
        return $pathValue;
352
    }
353
354
    /**
355
     * @inheritdoc
356
     */
357 2
    public function parseDocument($data, $docType)
358
    {
359
        try {
360 2
            $this->initPathStack();
361
362 2
            $metadata = $this->factory->getMetadataFor($docType);
363
            /* @var $metadata DocumentMetadataInterface */
364
365 2
            $docClass = $metadata->getClassName();
366 2
            $doc = new $docClass();
367
368 2
            $this->parseProperty($data, $doc, $metadata->getContentMetadata());
369
370 1
            return $doc;
371 1
        } catch (JsonApiException $e) {
372
            throw $e;
373 1
        } catch (\Exception $e) {
374 1
            throw  $this->convertToApiException($e, 'document');
375
        }
376
    }
377
378
    /**
379
     * @inheritdoc
380
     */
381 1
    public function parseQueryParams($data, $paramsType)
382
    {
383
        try {
384 1
            $this->initPathStack();
385
386 1
            $query = $this->parseObjectValue($data, $paramsType);
387 1
            if (!$query instanceof EncodingParametersInterface) {
388
                throw new \InvalidArgumentException(sprintf(
389
                    "Query parameters object must implement interface %s",
390
                    EncodingParametersInterface::class
391
                ));
392
            }
393
394 1
            return $query;
395
        } catch (JsonApiException $e) {
396
            throw $e;
397
        } catch (\Exception $e) {
398
            throw  $this->convertToApiException($e, 'query');
399
        }
400
    }
401
402
    /**
403
     * Prepare path segment
404
     *
405
     * @param string $path
406
     * @return string
407
     */
408 11
    protected function preparePathSegment($path)
409
    {
410 11
        return trim(preg_replace('~[\/]+~si', '/', str_replace(['.', '[', ']'], '/', (string) $path)), '/');
411
    }
412
413
    /**
414
     * Initialize stack that store current path
415
     */
416 11
    protected function initPathStack()
417
    {
418 11
        $this->path = new \SplStack();
419 11
    }
420
421
    /**
422
     * Parse numeric value
423
     *
424
     * @param mixed $data
425
     * @param string $path
426
     * @param string $type
427
     * @return float|int|null
428
     */
429 4
    protected function parseNumeric($data, $path, $type)
430
    {
431 4
        $this->setPath($path);
432
433 4
        $pathValue = null;
434 4
        if ($this->hasValue($data, $path)) {
435 4
            $value = $this->getValue($data, $path);
436 4
            $rightType = ('int' === $type) ? is_int($value) : is_float($value);
437 4
            if ($rightType) {
438 3
                $pathValue = $value;
439 4
            } elseif (is_numeric($value)) {
440 4
                $pathValue = ('int' === $type) ? (int) $value : (float) $value;
441 2
            } elseif (null !== $value) {
442 2
                throw new \InvalidArgumentException(
443 2
                    sprintf("Value expected to be %s, but %s given", $type, gettype($value)),
444 2
                    400
445
                );
446
            }
447
        }
448
449 4
        $this->restorePath();
450
451 4
        return $pathValue;
452
    }
453
454
    /**
455
     * Parse property of specified object
456
     *
457
     * @param object|array $data
458
     * @param object $obj
459
     * @param PropertyMetadataInterface $metadata
460
     */
461 5
    private function parseProperty($data, $obj, PropertyMetadataInterface $metadata)
462
    {
463 5
        $path = $metadata->getDataPath();
464
465 5
        if (false === $this->hasValue($data, $path)) {
466 2
            return;
467
        }
468
469 5
        if ('custom' === $metadata->getDataType()) {
470 2
            $value = $this->parseCallback($data, $path, [$obj, $metadata->getDataTypeParams()]);
471
        } else {
472 5
            $value = $this->parsePropertyValue($data, $path, $metadata);
473
        }
474
475 5
        $setter = $metadata->getSetter();
476 5
        if (null !== $setter) {
477 4
            $obj->{$setter}($value);
478
        } else {
479 4
            $setter = $metadata->getPropertyName();
480 4
            $obj->{$setter} = $value;
481
        }
482 5
    }
483
484
    /**
485
     * Parse value of specified property
486
     *
487
     * @param object|array $data
488
     * @param string $path
489
     * @param PropertyMetadataInterface $metadata
490
     * @return mixed|null
491
     */
492 5
    private function parsePropertyValue($data, $path, PropertyMetadataInterface $metadata)
493
    {
494 5
        switch ($metadata->getDataType()) {
495 5
            case 'scalar':
496 5
                return $this->parseScalarValue($data, $path, $metadata->getDataTypeParams());
0 ignored issues
show
Bug introduced by
It seems like $metadata->getDataTypeParams() targeting Reva2\JsonApi\Contracts\...ce::getDataTypeParams() can also be of type array; however, Reva2\JsonApi\Decoders\D...ser::parseScalarValue() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
497
498 4
            case 'datetime':
499 1
                $format = $metadata->getDataTypeParams();
500 1
                if (empty($format)) {
501 1
                    $format = 'Y-m-d';
502
                }
503
504 1
                return $this->parseDateTime($data, $path, $format);
0 ignored issues
show
Bug introduced by
It seems like $format defined by $metadata->getDataTypeParams() on line 499 can also be of type array; however, Reva2\JsonApi\Decoders\DataParser::parseDateTime() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
505
506 4
            case 'array':
507 3
                return $this->parseArrayValue($data, $path, $metadata->getDataTypeParams());
0 ignored issues
show
Bug introduced by
It seems like $metadata->getDataTypeParams() targeting Reva2\JsonApi\Contracts\...ce::getDataTypeParams() can also be of type string; however, Reva2\JsonApi\Decoders\D...rser::parseArrayValue() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
508
509 4
            case 'object':
510 4
                return $this->parseResourceOrObject($data, $path, $metadata->getDataTypeParams());
0 ignored issues
show
Bug introduced by
It seems like $metadata->getDataTypeParams() targeting Reva2\JsonApi\Contracts\...ce::getDataTypeParams() can also be of type array; however, Reva2\JsonApi\Decoders\D...parseResourceOrObject() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
511
512 1
            case 'raw':
513 1
                return $this->parseRaw($data, $path);
514
515
            default:
516
                throw new \InvalidArgumentException(sprintf(
517
                    "Unsupported property data type '%s'",
518
                    $metadata->getDataType()
519
                ));
520
        }
521
    }
522
523
    /**
524
     * Parse value as JSON API resource or object
525
     *
526
     * @param object|array $data
527
     * @param string $path
528
     * @param string $objClass
529
     * @return mixed|null
530
     */
531 4
    public function parseResourceOrObject($data, $path, $objClass)
532
    {
533 4
        $metadata = $this->factory->getMetadataFor($objClass);
534
535 4
        if ($metadata instanceof ResourceMetadataInterface) {
536 3
            return $this->parseResource($data, $path, $objClass);
537
        } else {
538 1
            return $this->parseObject($data, $path, $objClass);
539
        }
540
    }
541
542
    /**
543
     * Parse value that contains JSON API object
544
     *
545
     * @param object|array $data
546
     * @param string $objType
547
     * @return mixed
548
     */
549 2
    public function parseObjectValue($data, $objType)
550
    {
551 2
        $metadata = $this->factory->getMetadataFor($objType);
552 2
        if (!$metadata instanceof ObjectMetadataInterface) {
553
            throw new \InvalidArgumentException('Invalid object metadata');
554
        }
555
556 2 View Code Duplication
        if (null !== ($discField = $metadata->getDiscriminatorField())) {
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...
557 1
            $discValue = $this->parseString($data, $discField->getDataPath());
558 1
            $discClass = $metadata->getDiscriminatorClass($discValue);
559 1
            if ($discClass !== $objType) {
560 1
                return $this->parseObjectValue($data, $discClass);
561
            }
562
        }
563
564 2
        $objClass = $metadata->getClassName();
565 2
        $obj = new $objClass();
566
567 2
        $properties = $metadata->getProperties();
568 2
        foreach ($properties as $property) {
569 2
            $this->parseProperty($data, $obj, $property);
570
        }
571
572 2
        return $obj;
573
    }
574
575
    /**
576
     * Parse value that contains array
577
     *
578
     * @param object|array $data
579
     * @param string $path
580
     * @param array $params
581
     * @return array|null
582
     */
583 3
    public function parseArrayValue($data, $path, array $params)
584
    {
585 3
        $type = $params[0];
586 3
        $typeParams = $params[1];
587
588
        switch ($type) {
589 3
            case 'scalar':
590 1
                return $this->parseArray(
591
                    $data,
592
                    $path,
593
                    function ($data, $path, DataParser $parser) use ($typeParams) {
594 1
                        return $parser->parseScalarValue($data, $path, $typeParams);
595 1
                    }
596
                );
597
598 3
            case 'datetime':
599 1
                $format = (!empty($typeParams)) ? $typeParams : 'Y-m-d';
600 1
                return $this->parseArray(
601
                    $data,
602
                    $path,
603
                    function ($data, $path, DataParser $parser) use ($format) {
604 1
                        return $parser->parseDateTime($data, $path, $format);
605 1
                    }
606
                );
607
608 3
            case 'object':
609 3
                return $this->parseArray(
610
                    $data,
611
                    $path,
612
                    function ($data, $path, DataParser $parser) use ($typeParams) {
613 3
                        return $parser->parseResourceOrObject($data, $path, $typeParams);
614 3
                    }
615
                );
616
617 1
            case 'array':
618 1
                return $this->parseArray(
619
                    $data,
620
                    $path,
621
                    function ($data, $path, DataParser $parser) use ($typeParams) {
622 1
                        return $parser->parseArrayValue($data, $path, $typeParams);
623 1
                    }
624
                );
625
626 1
            case 'raw':
627 1
                return $this->parseArray(
628
                    $data,
629
                    $path,
630 1
                    function ($data, $path, DataParser $parser) {
631 1
                        return $parser->parseRaw($data, $path);
632 1
                    }
633
                );
634
635
            default:
636
                throw new \InvalidArgumentException(sprintf(
637
                    "Unsupported array item type '%s' specified",
638
                    $type
639
                ));
640
        }
641
    }
642
643
    /**
644
     * Parse scalar value
645
     *
646
     * @param object|array $data
647
     * @param string $path
648
     * @param string $type
649
     * @return bool|float|int|null|string
650
     */
651 5
    public function parseScalarValue($data, $path, $type)
652
    {
653
        switch ($type) {
654 5
            case 'string':
655 4
                return $this->parseString($data, $path);
656
657 2
            case 'bool':
658 2
            case 'boolean':
659 1
                return $this->parseBool($data, $path);
660
661 2
            case 'int':
662 2
            case 'integer':
663 2
                return $this->parseInt($data, $path);
664
665 1
            case 'float':
666 1
            case 'double':
667 1
                return $this->parseFloat($data, $path);
668
669
            default:
670
                throw new \InvalidArgumentException(sprintf("Unsupported scalar type '%s' specified", $type));
671
        }
672
    }
673
674
    /**
675
     * Convert any exception to JSON API exception
676
     *
677
     * @param \Exception $e
678
     * @param string $objType
679
     * @return JsonApiException
680
     */
681 1
    private function convertToApiException(\Exception $e, $objType)
682
    {
683 1
        $status = $e->getCode();
684 1
        $message = 'Failed to parse document';
685 1
        if (empty($status)) {
686
            $message = 'Internal server error';
687
            $status = 500;
688
        }
689
690 1
        $source = null;
691
        switch ($objType) {
692 1
            case 'document':
693 1
                $source = ['pointer' => $this->getPath()];
694 1
                break;
695
696
            case 'query':
697
                $source = ['parameter' => $this->getPath()];
698
                break;
699
        }
700
701 1
        $error = new Error(rand(), null, $status, self::ERROR_CODE, $message, $e->getMessage(), $source);
702
703 1
        return new JsonApiException($error, $status, $e);
704
    }
705
}
706