Completed
Push — master ( 667739...a330ea )
by Matt
01:57
created

ValidationError::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 4
Code Lines 2

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonGuard;
4
5
class ValidationError implements \ArrayAccess, \JsonSerializable
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 132
    public function __construct($message, $code, $value, $pointer = null, array $constraints = [])
40
    {
41 132
        $this->message     = $message;
42 132
        $this->code        = $code;
43 132
        $this->pointer     = $pointer;
44 132
        $this->value       = $value;
45 132
        $this->constraints = $constraints;
46 132
    }
47
48
    /**
49
     * Get the human readable error message for this error.
50
     *
51
     * @return string
52
     */
53 14
    public function getMessage()
54
    {
55 14
        return $this->message;
56
    }
57
58
    /**
59
     * Get the error code for this error.
60
     *
61
     * @return int
62
     */
63 10
    public function getCode()
64
    {
65 10
        return $this->code;
66
    }
67
68
    /**
69
     * Get the path to the property that failed validation.
70
     *
71
     * @return string|null
72
     */
73 10
    public function getPointer()
74
    {
75 10
        return $this->pointer;
76
    }
77
78
    /**
79
     * Get the value that caused the assertion to fail.
80
     *
81
     * @return mixed
82
     */
83 10
    public function getValue()
84
    {
85 10
        return $this->value;
86
    }
87
88
    /**
89
     * Get the constraints that applied to the failed assertion.
90
     *
91
     * @return array
92
     */
93 10
    public function getConstraints()
94
    {
95 10
        return $this->constraints;
96
    }
97
98
    /**
99
     * @return array
100
     */
101 10
    public function toArray()
102
    {
103
        return [
104 10
            'code'        => $this->getCode(),
105 10
            'message'     => $this->getMessage(),
106 10
            'pointer'     => $this->getPointer(),
107 10
            'value'       => $this->getValue(),
108 10
            'constraints' => $this->getConstraints(),
109 10
        ];
110
    }
111
112
    /**
113
     * Specify data which should be serialized to JSON
114
     * @link  http://php.net/manual/en/jsonserializable.jsonserialize.php
115
     * @return mixed data which can be serialized by <b>json_encode</b>,
116
     * which is a value of any type other than a resource.
117
     * @since 5.4.0
118
     */
119
    public function jsonSerialize()
120
    {
121
        return $this->toArray();
122
    }
123
124
    /**
125
     * Whether a offset exists
126 2
     * @link  http://php.net/manual/en/arrayaccess.offsetexists.php
127
     *
128 2
     * @param mixed $offset <p>
129
     *                      An offset to check for.
130
     *                      </p>
131
     *
132
     * @return boolean true on success or false on failure.
133
     * </p>
134
     * <p>
135
     * The return value will be casted to boolean if non-boolean was returned.
136
     * @since 5.0.0
137
     */
138
    public function offsetExists($offset)
139
    {
140
        return array_key_exists($offset, $this->toArray());
141
    }
142 10
143
    /**
144 10
     * Offset to retrieve
145 10
     * @link  http://php.net/manual/en/arrayaccess.offsetget.php
146
     *
147
     * @param mixed $offset <p>
148
     *                      The offset to retrieve.
149
     *                      </p>
150
     *
151
     * @return mixed Can return all value types.
152
     * @since 5.0.0
153
     */
154
    public function offsetGet($offset)
155
    {
156
        $errorArray = $this->toArray();
157
        return array_key_exists($offset, $errorArray) ? $errorArray[$offset] : null;
158
    }
159
160
    /**
161
     * Offset to set
162
     * @link  http://php.net/manual/en/arrayaccess.offsetset.php
163
     *
164
     * @param mixed $offset <p>
165
     *                      The offset to assign the value to.
166
     *                      </p>
167
     * @param mixed $value  <p>
168
     *                      The value to set.
169
     *                      </p>
170
     *
171
     * @return void
172
     * @since 5.0.0
173
     */
174
    public function offsetSet($offset, $value)
175
    {
176
        // A ValidationError is immutable.
177
        return null;
178
    }
179
180
    /**
181
     * Offset to unset
182
     * @link  http://php.net/manual/en/arrayaccess.offsetunset.php
183
     *
184
     * @param mixed $offset <p>
185
     *                      The offset to unset.
186
     *                      </p>
187
     *
188
     * @return void
189
     * @since 5.0.0
190
     */
191
    public function offsetUnset($offset)
192
    {
193
        // A ValidationError is immutable.
194
        return null;
195
    }
196
}
197