ErrorBag   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 15
c 3
b 0
f 0
lcom 1
cbo 0
dl 0
loc 110
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setHttpCode() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A count() 0 4 1
A jsonSerialize() 0 12 4
A getIterator() 0 4 1
A get() 0 4 2
A toArray() 0 4 1
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 11/20/15
5
 * Time: 7:22 PM.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace NilPortugues\Api\JsonApi\Server\Errors;
11
12
use ArrayAccess;
13
use ArrayIterator;
14
use Countable;
15
use IteratorAggregate;
16
use JsonSerializable;
17
18
/**
19
 * Class ErrorBag.
20
 */
21
class ErrorBag implements JsonSerializable, ArrayAccess, Countable, IteratorAggregate
22
{
23
    /**
24
     * @var array
25
     */
26
    protected $errors = [];
27
28
    /**
29
     * @var
30
     */
31
    protected $httpCode;
32
33
    /**
34
     * @param array $errors
35
     */
36
    public function __construct(array $errors = [])
37
    {
38
        $this->errors = $errors;
39
    }
40
41
    /**
42
     * @param $httpCode
43
     */
44
    public function setHttpCode($httpCode)
45
    {
46
        $this->httpCode = $httpCode;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function offsetExists($offset)
53
    {
54
        return array_key_exists($offset, $this->errors);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function offsetGet($offset)
61
    {
62
        return $this->get($offset);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function offsetSet($offset, $value)
69
    {
70
        $this->errors[] = $value;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function offsetUnset($offset)
77
    {
78
        unset($this->errors[$offset]);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function count()
85
    {
86
        return count($this->errors);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function jsonSerialize()
93
    {
94
        /** @var Error $error */
95
        foreach ($this->errors as $error) {
96
            $status = $error->getStatus();
97
            if (empty($status) && !empty($this->httpCode)) {
98
                $error->setStatus($this->httpCode);
99
            }
100
        }
101
102
        return ['errors' => array_values($this->errors)];
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function getIterator()
109
    {
110
        return new ArrayIterator($this->errors);
111
    }
112
113
    /**
114
     * @param $key
115
     *
116
     * @return null|mixed
117
     */
118
    protected function get($key)
119
    {
120
        return isset($this->errors[$key]) ? $this->errors[$key] : null;
121
    }
122
123
    /**
124
     * @return array
125
     */
126
    public function toArray()
127
    {
128
        return array_values($this->errors);
129
    }
130
}
131