ErrorCollection::unserialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Neomerx\JsonApi\Schema;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use ArrayAccess;
22
use ArrayIterator;
23
use Countable;
24
use IteratorAggregate;
25
use Neomerx\JsonApi\Contracts\Schema\DocumentInterface;
26
use Neomerx\JsonApi\Contracts\Schema\ErrorInterface;
27
use Neomerx\JsonApi\Contracts\Schema\LinkInterface;
28
use Serializable;
29
30
/**
31
 * @package Neomerx\JsonApi
32
 *
33
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
34
 * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
35
 */
36
class ErrorCollection implements IteratorAggregate, ArrayAccess, Serializable, Countable
37
{
38
    /**
39
     * @var array
40
     */
41
    private $items = [];
42
43
    /**
44
     * @inheritdoc
45
     */
46 2
    public function getIterator()
47
    {
48 2
        return new ArrayIterator($this->items);
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54 14
    public function count()
55
    {
56 14
        return \count($this->items);
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62 1
    public function serialize()
63
    {
64 1
        return \serialize($this->items);
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70 1
    public function unserialize($serialized)
71
    {
72 1
        $this->items = \unserialize($serialized);
73 1
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78 1
    public function offsetExists($offset)
79
    {
80 1
        return isset($this->items[$offset]);
81
    }
82
83
    /**
84
     * @inheritdoc
85
     *
86
     * @return ErrorInterface
87
     */
88 11
    public function offsetGet($offset)
89
    {
90 11
        return $this->items[$offset];
91
    }
92
93
    /**
94
     * @inheritdoc
95
     */
96 9
    public function offsetSet($offset, $value)
97
    {
98 9
        $offset === null ? $this->add($value) : $this->items[$offset] = $value;
99 9
    }
100
101
    /**
102
     * @inheritdoc
103
     */
104 1
    public function offsetUnset($offset)
105
    {
106 1
        unset($this->items[$offset]);
107 1
    }
108
109
    /**
110
     * @return ErrorInterface[]
111
     */
112 4
    public function getArrayCopy(): array
113
    {
114 4
        return $this->items;
115
    }
116
117
    /**
118
     * @param ErrorInterface $error
119
     *
120
     * @return self
121
     */
122 22
    public function add(ErrorInterface $error): self
123
    {
124 22
        $this->items[] = $error;
125
126 22
        return $this;
127
    }
128
129
    /**
130
     * @param string             $title
131
     * @param string|null        $detail
132
     * @param string|null        $status
133
     * @param int|string|null    $idx
134
     * @param LinkInterface|null $aboutLink
135
     * @param iterable|null      $typeLinks
136
     * @param string|null        $code
137
     * @param bool               $hasMeta
138
     * @param mixed              $meta
139
     *
140
     * @return self
141
     */
142 2
    public function addDataError(
143
        string $title,
144
        string $detail = null,
145
        string $status = null,
146
        $idx = null,
147
        LinkInterface $aboutLink = null,
148
        ?iterable $typeLinks = null,
149
        string $code = null,
150
        bool $hasMeta = false,
151
        $meta = null
152
    ): self {
153 2
        $pointer = $this->getPathToData();
154
155 2
        return $this->addResourceError(
156 2
            $title,
157 2
            $pointer,
158 2
            $detail,
159 2
            $status,
160 2
            $idx,
161 2
            $aboutLink,
162 2
            $typeLinks,
163 2
            $code,
164 2
            $hasMeta,
165 2
            $meta
166
        );
167
    }
168
169
    /**
170
     * @param string             $title
171
     * @param string|null        $detail
172
     * @param string|null        $status
173
     * @param int|string|null    $idx
174
     * @param LinkInterface|null $aboutLink
175
     * @param iterable|null      $typeLinks
176
     * @param string|null        $code
177
     * @param bool               $hasMeta
178
     * @param mixed              $meta
179
     *
180
     * @return self
181
     */
182 1
    public function addDataTypeError(
183
        string $title,
184
        string $detail = null,
185
        string $status = null,
186
        $idx = null,
187
        LinkInterface $aboutLink = null,
188
        ?iterable $typeLinks = null,
189
        string $code = null,
190
        bool $hasMeta = false,
191
        $meta = null
192
    ): self {
193 1
        $pointer = $this->getPathToType();
194
195 1
        return $this->addResourceError(
196 1
            $title,
197 1
            $pointer,
198 1
            $detail,
199 1
            $status,
200 1
            $idx,
201 1
            $aboutLink,
202 1
            $typeLinks,
203 1
            $code,
204 1
            $hasMeta,
205 1
            $meta
206
        );
207
    }
208
209
    /**
210
     * @param string             $title
211
     * @param string|null        $detail
212
     * @param string|null        $status
213
     * @param int|string|null    $idx
214
     * @param LinkInterface|null $aboutLink
215
     * @param iterable|null      $typeLinks
216
     * @param string|null        $code
217
     * @param bool               $hasMeta
218
     * @param mixed              $meta
219
     *
220
     * @return self
221
     */
222 1
    public function addDataIdError(
223
        string $title,
224
        string $detail = null,
225
        string $status = null,
226
        $idx = null,
227
        LinkInterface $aboutLink = null,
228
        ?iterable $typeLinks = null,
229
        string $code = null,
230
        bool $hasMeta = false,
231
        $meta = null
232
    ): self {
233 1
        $pointer = $this->getPathToId();
234
235 1
        return $this->addResourceError(
236 1
            $title,
237 1
            $pointer,
238 1
            $detail,
239 1
            $status,
240 1
            $idx,
241 1
            $aboutLink,
242 1
            $typeLinks,
243 1
            $code,
244 1
            $hasMeta,
245 1
            $meta
246
        );
247
    }
248
249
    /**
250
     * @param string             $title
251
     * @param string|null        $detail
252
     * @param string|null        $status
253
     * @param int|string|null    $idx
254
     * @param LinkInterface|null $aboutLink
255
     * @param iterable|null      $typeLinks
256
     * @param string|null        $code
257
     * @param bool               $hasMeta
258
     * @param mixed              $meta
259
     *
260
     * @return self
261
     */
262 1
    public function addAttributesError(
263
        string $title,
264
        string $detail = null,
265
        string $status = null,
266
        $idx = null,
267
        LinkInterface $aboutLink = null,
268
        ?iterable $typeLinks = null,
269
        string $code = null,
270
        bool $hasMeta = false,
271
        $meta = null
272
    ): self {
273 1
        $pointer = $this->getPathToAttributes();
274
275 1
        return $this->addResourceError(
276 1
            $title,
277 1
            $pointer,
278 1
            $detail,
279 1
            $status,
280 1
            $idx,
281 1
            $aboutLink,
282 1
            $typeLinks,
283 1
            $code,
284 1
            $hasMeta,
285 1
            $meta
286
        );
287
    }
288
289
    /**
290
     * @param string             $name
291
     * @param string             $title
292
     * @param string|null        $detail
293
     * @param string|null        $status
294
     * @param int|string|null    $idx
295
     * @param LinkInterface|null $aboutLink
296
     * @param iterable|null      $typeLinks
297
     * @param string|null        $code
298
     * @param bool               $hasMeta
299
     * @param mixed              $meta
300
     *
301
     * @return self
302
     *
303
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
304
     */
305 1
    public function addDataAttributeError(
306
        $name,
307
        string $title,
308
        string $detail = null,
309
        string $status = null,
310
        $idx = null,
311
        LinkInterface $aboutLink = null,
312
        ?iterable $typeLinks = null,
313
        string $code = null,
314
        bool $hasMeta = false,
315
        $meta = null
316
    ): self {
317 1
        $pointer = $this->getPathToAttribute($name);
318
319 1
        return $this->addResourceError(
320 1
            $title,
321 1
            $pointer,
322 1
            $detail,
323 1
            $status,
324 1
            $idx,
325 1
            $aboutLink,
326 1
            $typeLinks,
327 1
            $code,
328 1
            $hasMeta,
329 1
            $meta
330
        );
331
    }
332
333
    /**
334
     * @param string             $title
335
     * @param string|null        $detail
336
     * @param string|null        $status
337
     * @param int|string|null    $idx
338
     * @param LinkInterface|null $aboutLink
339
     * @param iterable|null      $typeLinks
340
     * @param string|null        $code
341
     * @param bool               $hasMeta
342
     * @param mixed              $meta
343
     *
344
     * @return self
345
     */
346 1
    public function addRelationshipsError(
347
        string $title,
348
        string $detail = null,
349
        string $status = null,
350
        $idx = null,
351
        LinkInterface $aboutLink = null,
352
        ?iterable $typeLinks = null,
353
        string $code = null,
354
        bool $hasMeta = false,
355
        $meta = null
356
    ): self {
357 1
        $pointer = $this->getPathToRelationships();
358
359 1
        return $this->addResourceError(
360 1
            $title,
361 1
            $pointer,
362 1
            $detail,
363 1
            $status,
364 1
            $idx,
365 1
            $aboutLink,
366 1
            $typeLinks,
367 1
            $code,
368 1
            $hasMeta,
369 1
            $meta
370
        );
371
    }
372
373
    /**
374
     * @param string             $name
375
     * @param string             $title
376
     * @param string|null        $detail
377
     * @param string|null        $status
378
     * @param int|string|null    $idx
379
     * @param LinkInterface|null $aboutLink
380
     * @param iterable|null      $typeLinks
381
     * @param string|null        $code
382
     * @param bool               $hasMeta
383
     * @param mixed              $meta
384
     *
385
     * @return self
386
     *
387
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
388
     */
389 1
    public function addRelationshipError(
390
        $name,
391
        string $title,
392
        string $detail = null,
393
        string $status = null,
394
        $idx = null,
395
        LinkInterface $aboutLink = null,
396
        ?iterable $typeLinks = null,
397
        string $code = null,
398
        bool $hasMeta = false,
399
        $meta = null
400
    ): self {
401 1
        $pointer = $this->getPathToRelationship($name);
402
403 1
        return $this->addResourceError(
404 1
            $title,
405 1
            $pointer,
406 1
            $detail,
407 1
            $status,
408 1
            $idx,
409 1
            $aboutLink,
410 1
            $typeLinks,
411 1
            $code,
412 1
            $hasMeta,
413 1
            $meta
414
        );
415
    }
416
417
    /**
418
     * @param string             $name
419
     * @param string             $title
420
     * @param string|null        $detail
421
     * @param string|null        $status
422
     * @param int|string|null    $idx
423
     * @param LinkInterface|null $aboutLink
424
     * @param iterable|null      $typeLinks
425
     * @param string|null        $code
426
     * @param bool               $hasMeta
427
     * @param mixed              $meta
428
     *
429
     * @return self
430
     *
431
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
432
     */
433 1
    public function addRelationshipTypeError(
434
        $name,
435
        string $title,
436
        string $detail = null,
437
        string $status = null,
438
        $idx = null,
439
        LinkInterface $aboutLink = null,
440
        ?iterable $typeLinks = null,
441
        string $code = null,
442
        bool $hasMeta = false,
443
        $meta = null
444
    ): self {
445 1
        $pointer = $this->getPathToRelationshipType($name);
446
447 1
        return $this->addResourceError(
448 1
            $title,
449 1
            $pointer,
450 1
            $detail,
451 1
            $status,
452 1
            $idx,
453 1
            $aboutLink,
454 1
            $typeLinks,
455 1
            $code,
456 1
            $hasMeta,
457 1
            $meta
458
        );
459
    }
460
461
    /**
462
     * @param string             $name
463
     * @param string             $title
464
     * @param string|null        $detail
465
     * @param string|null        $status
466
     * @param int|string|null    $idx
467
     * @param LinkInterface|null $aboutLink
468
     * @param iterable|null      $typeLinks
469
     * @param string|null        $code
470
     * @param bool               $hasMeta
471
     * @param mixed              $meta
472
     *
473
     * @return self
474
     *
475
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
476
     */
477 1
    public function addRelationshipIdError(
478
        $name,
479
        string $title,
480
        string $detail = null,
481
        string $status = null,
482
        $idx = null,
483
        LinkInterface $aboutLink = null,
484
        ?iterable $typeLinks = null,
485
        string $code = null,
486
        bool $hasMeta = false,
487
        $meta = null
488
    ): self {
489 1
        $pointer = $this->getPathToRelationshipId($name);
490
491 1
        return $this->addResourceError(
492 1
            $title,
493 1
            $pointer,
494 1
            $detail,
495 1
            $status,
496 1
            $idx,
497 1
            $aboutLink,
498 1
            $typeLinks,
499 1
            $code,
500 1
            $hasMeta,
501 1
            $meta
502
        );
503
    }
504
505
    /**
506
     * @param string             $name
507
     * @param string             $title
508
     * @param string|null        $detail
509
     * @param string|null        $status
510
     * @param int|string|null    $idx
511
     * @param LinkInterface|null $aboutLink
512
     * @param iterable|null      $typeLinks
513
     * @param string|null        $code
514
     * @param bool               $hasMeta
515
     * @param mixed              $meta
516
     *
517
     * @return self
518
     *
519
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
520
     */
521 1
    public function addQueryParameterError(
522
        string $name,
523
        string $title,
524
        string $detail = null,
525
        string $status = null,
526
        $idx = null,
527
        LinkInterface $aboutLink = null,
528
        ?iterable $typeLinks = null,
529
        string $code = null,
530
        bool $hasMeta = false,
531
        $meta = null
532
    ): self {
533 1
        $source = [ErrorInterface::SOURCE_PARAMETER => $name];
534 1
        $error  = new Error($idx, $aboutLink, $typeLinks, $status, $code, $title, $detail, $source, $hasMeta, $meta);
535
536 1
        $this->add($error);
537
538 1
        return $this;
539
    }
540
541
    /** @noinspection PhpTooManyParametersInspection
542
     * @param string             $title
543
     * @param string             $pointer
544
     * @param string|null        $detail
545
     * @param string|null        $status
546
     * @param null               $idx
547
     * @param LinkInterface|null $aboutLink
548
     * @param iterable|null      $typeLinks
549
     * @param string|null        $code
550
     * @param bool               $hasMeta
551
     * @param mixed              $meta
552
     *
553
     * @return self
554
     *
555
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
556
     */
557 10
    protected function addResourceError(
558
        string $title,
559
        string $pointer,
560
        string $detail = null,
561
        string $status = null,
562
        $idx = null,
563
        LinkInterface $aboutLink = null,
564
        ?iterable $typeLinks = null,
565
        string $code = null,
566
        bool $hasMeta = false,
567
        $meta = null
568
    ): self {
569 10
        $source = [ErrorInterface::SOURCE_POINTER => $pointer];
570 10
        $error  = new Error($idx, $aboutLink, $typeLinks, $status, $code, $title, $detail, $source, $hasMeta, $meta);
571
572 10
        $this->add($error);
573
574 10
        return $this;
575
    }
576
577
    /**
578
     * @return string
579
     */
580 10
    protected function getPathToData(): string
581
    {
582 10
        return '/' . DocumentInterface::KEYWORD_DATA;
583
    }
584
585
    /**
586
     * @return string
587
     */
588 1
    protected function getPathToType(): string
589
    {
590 1
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_TYPE;
591
    }
592
593
    /**
594
     * @return string
595
     */
596 1
    protected function getPathToId(): string
597
    {
598 1
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_ID;
599
    }
600
601
    /**
602
     * @return string
603
     */
604 1
    protected function getPathToAttributes(): string
605
    {
606 1
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_ATTRIBUTES;
607
    }
608
609
    /**
610
     * @param string $name
611
     *
612
     * @return string
613
     */
614 1
    protected function getPathToAttribute(string $name): string
615
    {
616 1
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_ATTRIBUTES . '/' . $name;
617
    }
618
619
    /**
620
     * @return string
621
     */
622 4
    protected function getPathToRelationships(): string
623
    {
624 4
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_RELATIONSHIPS;
625
    }
626
627
    /**
628
     * @param string $name
629
     *
630
     * @return string
631
     */
632 3
    protected function getPathToRelationship(string $name): string
633
    {
634 3
        return $this->getPathToRelationships() . '/' . $name;
635
    }
636
637
    /**
638
     * @param string $name
639
     *
640
     * @return string
641
     */
642 1
    protected function getPathToRelationshipType(string $name): string
643
    {
644 1
        return $this->getPathToRelationship($name) . '/' .
645 1
            DocumentInterface::KEYWORD_DATA . '/' . DocumentInterface::KEYWORD_TYPE;
646
    }
647
648
    /**
649
     * @param string $name
650
     *
651
     * @return string
652
     */
653 1
    protected function getPathToRelationshipId(string $name): string
654
    {
655 1
        return $this->getPathToRelationship($name) . '/' .
656 1
            DocumentInterface::KEYWORD_DATA . '/' . DocumentInterface::KEYWORD_ID;
657
    }
658
}
659