Completed
Branch master (15b45d)
by
unknown
02:59
created

ErrorCollection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Neomerx\JsonApi\Exceptions;
2
3
/**
4
 * Copyright 2015 [email protected] (www.neomerx.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use \Countable;
20
use \ArrayAccess;
21
use \Serializable;
22
use \ArrayIterator;
23
use \IteratorAggregate;
24
use \Neomerx\JsonApi\Document\Error;
25
use \Neomerx\JsonApi\Contracts\Document\LinkInterface;
26
use \Neomerx\JsonApi\Contracts\Document\ErrorInterface;
27
use \Neomerx\JsonApi\Contracts\Document\DocumentInterface;
28
29
/**
30
 * @package Neomerx\JsonApi
31
 */
32
class ErrorCollection implements IteratorAggregate, ArrayAccess, Serializable, Countable
33
{
34
    /**
35
     * @var array
36
     */
37
    private $items = [];
38
39
    /**
40
     * @inheritdoc
41
     */
42 10
    public function getIterator()
43
    {
44 10
        return new ArrayIterator($this->items);
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50 26
    public function count()
51
    {
52 26
        return count($this->items);
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58 1
    public function serialize()
59
    {
60 1
        return serialize($this->items);
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66 1
    public function unserialize($serialized)
67
    {
68 1
        $this->items = unserialize($serialized);
69 1
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74 1
    public function offsetExists($offset)
75
    {
76 1
        return array_key_exists($offset, $this->items);
77
    }
78
79
    /**
80
     * @inheritdoc
81
     *
82
     * @return ErrorInterface
83
     */
84 15
    public function offsetGet($offset)
85
    {
86 15
        return $this->items[$offset];
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92 11
    public function offsetSet($offset, $value)
93
    {
94 11
        $offset === null ? $this->add($value) : $this->items[$offset] = $value;
95 11
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100 1
    public function offsetUnset($offset)
101
    {
102 1
        unset($this->items[$offset]);
103 1
    }
104
105
    /**
106
     * @return ErrorInterface[]
107
     */
108 4
    public function getArrayCopy()
109
    {
110 4
        return $this->items;
111
    }
112
113
    /**
114
     * @param ErrorInterface $error
115
     *
116
     * @return $this
117
     */
118 22
    public function add(ErrorInterface $error)
119
    {
120 22
        $this->items[] =$error;
121
122 22
        return $this;
123
    }
124
125
    /**
126
     * @param string             $title
127
     * @param string|null        $detail
128
     * @param int|string|null    $status
129
     * @param int|string|null    $idx
130
     * @param LinkInterface|null $aboutLink
131
     * @param int|string|null    $code
132
     * @param mixed|null         $meta
133
     *
134
     * @return $this
135
     */
136 2 View Code Duplication
    public function addDataError(
137
        $title,
138
        $detail = null,
139
        $status = null,
140
        $idx = null,
141
        LinkInterface $aboutLink = null,
142
        $code = null,
143
        $meta = null
144
    ) {
145 2
        $pointer = $this->getPathToData();
146
147 2
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
148
    }
149
150
    /**
151
     * @param string             $title
152
     * @param string|null        $detail
153
     * @param int|string|null    $status
154
     * @param int|string|null    $idx
155
     * @param LinkInterface|null $aboutLink
156
     * @param int|string|null    $code
157
     * @param mixed|null         $meta
158
     *
159
     * @return $this
160
     */
161 1 View Code Duplication
    public function addDataTypeError(
162
        $title,
163
        $detail = null,
164
        $status = null,
165
        $idx = null,
166
        LinkInterface $aboutLink = null,
167
        $code = null,
168
        $meta = null
169
    ) {
170 1
        $pointer = $this->getPathToType();
171
172 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
173
    }
174
175
    /**
176
     * @param string             $title
177
     * @param string|null        $detail
178
     * @param int|string|null    $status
179
     * @param int|string|null    $idx
180
     * @param LinkInterface|null $aboutLink
181
     * @param int|string|null    $code
182
     * @param mixed|null         $meta
183
     *
184
     * @return $this
185
     */
186 1 View Code Duplication
    public function addDataIdError(
187
        $title,
188
        $detail = null,
189
        $status = null,
190
        $idx = null,
191
        LinkInterface $aboutLink = null,
192
        $code = null,
193
        $meta = null
194
    ) {
195 1
        $pointer = $this->getPathToId();
196
197 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
198
    }
199
200
    /**
201
     * @param string             $name
202
     * @param string             $title
203
     * @param string|null        $detail
204
     * @param int|string|null    $status
205
     * @param int|string|null    $idx
206
     * @param LinkInterface|null $aboutLink
207
     * @param int|string|null    $code
208
     * @param mixed|null         $meta
209
     *
210
     * @return $this
211
     */
212 1 View Code Duplication
    public function addDataAttributeError(
213
        $name,
214
        $title,
215
        $detail = null,
216
        $status = null,
217
        $idx = null,
218
        LinkInterface $aboutLink = null,
219
        $code = null,
220
        $meta = null
221
    ) {
222 1
        $pointer = $this->getPathToAttribute($name);
223
224 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
225
    }
226
227
    /**
228
     * @param string             $title
229
     * @param string|null        $detail
230
     * @param int|string|null    $status
231
     * @param int|string|null    $idx
232
     * @param LinkInterface|null $aboutLink
233
     * @param int|string|null    $code
234
     * @param mixed|null         $meta
235
     *
236
     * @return $this
237
     */
238 1 View Code Duplication
    public function addRelationshipsError(
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...
239
        $title,
240
        $detail = null,
241
        $status = null,
242
        $idx = null,
243
        LinkInterface $aboutLink = null,
244
        $code = null,
245
        $meta = null
246
    ) {
247 1
        $pointer = $this->getPathToRelationships();
248
249 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
250
    }
251
252
    /**
253
     * @param string             $name
254
     * @param string             $title
255
     * @param string|null        $detail
256
     * @param int|string|null    $status
257
     * @param int|string|null    $idx
258
     * @param LinkInterface|null $aboutLink
259
     * @param int|string|null    $code
260
     * @param mixed|null         $meta
261
     *
262
     * @return $this
263
     */
264 1 View Code Duplication
    public function addRelationshipError(
265
        $name,
266
        $title,
267
        $detail = null,
268
        $status = null,
269
        $idx = null,
270
        LinkInterface $aboutLink = null,
271
        $code = null,
272
        $meta = null
273
    ) {
274 1
        $pointer = $this->getPathToRelationship($name);
275
276 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
277
    }
278
279
    /**
280
     * @param string             $name
281
     * @param string             $title
282
     * @param string|null        $detail
283
     * @param int|string|null    $status
284
     * @param int|string|null    $idx
285
     * @param LinkInterface|null $aboutLink
286
     * @param int|string|null    $code
287
     * @param mixed|null         $meta
288
     *
289
     * @return $this
290
     */
291 1 View Code Duplication
    public function addRelationshipTypeError(
292
        $name,
293
        $title,
294
        $detail = null,
295
        $status = null,
296
        $idx = null,
297
        LinkInterface $aboutLink = null,
298
        $code = null,
299
        $meta = null
300
    ) {
301 1
        $pointer = $this->getPathToRelationshipType($name);
302
303 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
304
    }
305
306
    /**
307
     * @param string             $name
308
     * @param string             $title
309
     * @param string|null        $detail
310
     * @param int|string|null    $status
311
     * @param int|string|null    $idx
312
     * @param LinkInterface|null $aboutLink
313
     * @param int|string|null    $code
314
     * @param mixed|null         $meta
315
     *
316
     * @return $this
317
     */
318 1 View Code Duplication
    public function addRelationshipIdError(
319
        $name,
320
        $title,
321
        $detail = null,
322
        $status = null,
323
        $idx = null,
324
        LinkInterface $aboutLink = null,
325
        $code = null,
326
        $meta = null
327
    ) {
328 1
        $pointer = $this->getPathToRelationshipId($name);
329
330 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
331
    }
332
333
    /**
334
     * @param string             $name
335
     * @param string             $title
336
     * @param string|null        $detail
337
     * @param int|string|null    $status
338
     * @param int|string|null    $idx
339
     * @param LinkInterface|null $aboutLink
340
     * @param int|string|null    $code
341
     * @param mixed|null         $meta
342
     *
343
     * @return $this
344
     */
345 8 View Code Duplication
    public function addQueryParameterError(
346
        $name,
347
        $title,
348
        $detail = null,
349
        $status = null,
350
        $idx = null,
351
        LinkInterface $aboutLink = null,
352
        $code = null,
353
        $meta = null
354
    ) {
355 8
        $source = [Error::SOURCE_PARAMETER => $name];
356 8
        $error  = new Error($idx, $aboutLink, $status, $code, $title, $detail, $source, $meta);
357
358 8
        $this->add($error);
359
360 8
        return $this;
361
    }
362
363
    /** @noinspection PhpTooManyParametersInspection
364
     * @param string             $title
365
     * @param string             $pointer
366
     * @param string|null        $detail
367
     * @param int|string|null    $status
368
     * @param int|string|null    $idx
369
     * @param LinkInterface|null $aboutLink
370
     * @param int|string|null    $code
371
     * @param mixed|null         $meta
372
     *
373
     * @return $this
374
     */
375 9 View Code Duplication
    protected function addResourceError(
376
        $title,
377
        $pointer,
378
        $detail = null,
379
        $status = null,
380
        $idx = null,
381
        LinkInterface $aboutLink = null,
382
        $code = null,
383
        $meta = null
384
    ) {
385 9
        $source = [Error::SOURCE_POINTER => $pointer];
386 9
        $error  = new Error($idx, $aboutLink, $status, $code, $title, $detail, $source, $meta);
387
388 9
        $this->add($error);
389
390 9
        return $this;
391
    }
392
393
    /**
394
     * @return string
395
     */
396 9
    protected function getPathToData()
397
    {
398 9
        return '/' . DocumentInterface::KEYWORD_DATA;
399
    }
400
401
    /**
402
     * @return string
403
     */
404 1
    protected function getPathToType()
405
    {
406 1
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_TYPE;
407
    }
408
409
    /**
410
     * @return string
411
     */
412 1
    protected function getPathToId()
413
    {
414 1
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_ID;
415
    }
416
417
    /**
418
     * @param string $name
419
     *
420
     * @return string
421
     */
422 1
    protected function getPathToAttribute($name)
423
    {
424 1
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_ATTRIBUTES . '/' . $name;
425
    }
426
427
    /**
428
     * @return string
429
     */
430 4
    protected function getPathToRelationships()
431
    {
432 4
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_RELATIONSHIPS;
433
    }
434
435
    /**
436
     * @param string $name
437
     *
438
     * @return string
439
     */
440 3
    protected function getPathToRelationship($name)
441
    {
442 3
        return $this->getPathToRelationships() . '/' . $name;
443
    }
444
445
    /**
446
     * @param string $name
447
     *
448
     * @return string
449
     */
450 1
    protected function getPathToRelationshipType($name)
451
    {
452 1
        return $this->getPathToRelationship($name) . '/' .
453 1
            DocumentInterface::KEYWORD_DATA . '/' . DocumentInterface::KEYWORD_TYPE;
454
    }
455
456
    /**
457
     * @param string $name
458
     *
459
     * @return string
460
     */
461 1
    protected function getPathToRelationshipId($name)
462
    {
463 1
        return $this->getPathToRelationship($name) . '/' .
464 1
            DocumentInterface::KEYWORD_DATA . '/' . DocumentInterface::KEYWORD_ID;
465
    }
466
}
467