Completed
Pull Request — master (#17)
by Matt
02:30
created

ValidationError::getCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace League\JsonGuard;
4
5
class ValidationError implements \ArrayAccess
6
{
7
    /**
8
     * @var string
9
     */
10
    private $message;
11
12
    /**
13
     * @var int
14
     */
15
    private $code;
16
17
    /**
18
     * @var string|null
19
     */
20
    private $pointer;
21
22
    /**
23
     * @var mixed
24
     */
25
    private $value;
26
27
    /**
28
     * @var array
29
     */
30
    private $constraints;
31
32
    /**
33
     * @param string      $message
34
     * @param int         $code
35
     * @param mixed       $value
36
     * @param string|null $pointer
37
     * @param array       $constraints
38
     */
39 34
    public function __construct($message, $code, $value, $pointer = null, array $constraints = [])
40
    {
41 34
        $this->message     = $message;
42 34
        $this->code        = $code;
43 34
        $this->pointer     = $pointer;
44 34
        $this->value       = $value;
45 34
        $this->constraints = $constraints;
46 34
    }
47
48
    /**
49
     * Get the human readable error message for this error.
50
     *
51
     * @return string
52
     */
53 4
    public function getMessage()
54
    {
55 4
        return $this->message;
56
    }
57
58
    /**
59
     * Get the error code for this error.
60
     *
61
     * @return int
62
     */
63 4
    public function getCode()
64
    {
65 4
        return $this->code;
66
    }
67
68
    /**
69
     * Get the path to the property that failed validation.
70
     *
71
     * @return string|null
72
     */
73 4
    public function getPointer()
74
    {
75 4
        return $this->pointer;
76
    }
77
78
    /**
79
     * Get the value that caused the assertion to fail.
80
     *
81
     * @return mixed
82
     */
83 4
    public function getValue()
84
    {
85 4
        return $this->value;
86
    }
87
88
    /**
89
     * Get the constraints that applied to the failed assertion.
90
     *
91
     * @return array
92
     */
93 4
    public function getConstraints()
94
    {
95 4
        return $this->constraints;
96
    }
97
98
    /**
99
     * @return array
100
     */
101 4
    public function toArray()
102
    {
103
        return [
104 4
            'code'        => $this->getCode(),
105 4
            'message'     => $this->getMessage(),
106 4
            'pointer'     => $this->getPointer(),
107 4
            'value'       => $this->getValue(),
108 4
            'constraints' => $this->getConstraints(),
109 4
        ];
110
    }
111
112
    /**
113
     * Whether a offset exists
114
     * @link  http://php.net/manual/en/arrayaccess.offsetexists.php
115
     *
116
     * @param mixed $offset <p>
117
     *                      An offset to check for.
118
     *                      </p>
119
     *
120
     * @return boolean true on success or false on failure.
121
     * </p>
122
     * <p>
123
     * The return value will be casted to boolean if non-boolean was returned.
124
     * @since 5.0.0
125
     */
126 1
    public function offsetExists($offset)
127
    {
128 1
        return array_key_exists($offset, $this->toArray());
129
    }
130
131
    /**
132
     * Offset to retrieve
133
     * @link  http://php.net/manual/en/arrayaccess.offsetget.php
134
     *
135
     * @param mixed $offset <p>
136
     *                      The offset to retrieve.
137
     *                      </p>
138
     *
139
     * @return mixed Can return all value types.
140
     * @since 5.0.0
141
     */
142 4
    public function offsetGet($offset)
143
    {
144 4
        $errorArray = $this->toArray();
145 4
        return array_key_exists($offset, $errorArray) ? $errorArray[$offset] : null;
146
    }
147
148
    /**
149
     * Offset to set
150
     * @link  http://php.net/manual/en/arrayaccess.offsetset.php
151
     *
152
     * @param mixed $offset <p>
153
     *                      The offset to assign the value to.
154
     *                      </p>
155
     * @param mixed $value  <p>
156
     *                      The value to set.
157
     *                      </p>
158
     *
159
     * @return void
160
     * @since 5.0.0
161
     */
162
    public function offsetSet($offset, $value)
163
    {
164
        // A ValidationError is immutable.
165
        return null;
166
    }
167
168
    /**
169
     * Offset to unset
170
     * @link  http://php.net/manual/en/arrayaccess.offsetunset.php
171
     *
172
     * @param mixed $offset <p>
173
     *                      The offset to unset.
174
     *                      </p>
175
     *
176
     * @return void
177
     * @since 5.0.0
178
     */
179
    public function offsetUnset($offset)
180
    {
181
        // A ValidationError is immutable.
182
        return null;
183
    }
184
}
185