Completed
Branch next (29fadd)
by Neomerx
03:15
created

ErrorCollection   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 611
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 4.68%

Importance

Changes 0
Metric Value
dl 0
loc 611
ccs 8
cts 171
cp 0.0468
rs 9.909
c 0
b 0
f 0
wmc 31
lcom 1
cbo 1

30 Methods

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