Completed
Push — master ( a0be20...c3e525 )
by Matt
02:18
created

ValidationError::offsetUnset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 1
rs 9.4285
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 $keyword;
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 $context;
31
32
    /**
33
     * @param string      $message
34
     * @param string      $keyword
35
     * @param mixed       $value
36
     * @param string|null $pointer
37
     * @param array       $context
38
     */
39 142
    public function __construct($message, $keyword, $value, $pointer = null, array $context = [])
40
    {
41 142
        $this->message = $message;
42 142
        $this->keyword = $keyword;
0 ignored issues
show
Documentation Bug introduced by
The property $keyword was declared of type integer, but $keyword is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
43 142
        $this->pointer = $pointer;
44 142
        $this->value   = $value;
45 142
        $this->context = array_map('League\JsonGuard\as_string', $context);
46 142
    }
47
48
    /**
49
     * Get the human readable error message for this error.
50
     *
51
     * @return string
52
     */
53 20
    public function getMessage()
54
    {
55 20
        return $this->interpolate($this->message, $this->context);
56
    }
57
58
    /**
59
     * Interpolate the context values into the message placeholders.
60
     *
61
     * @param  string $message
62
     * @param  array  $context
63
     *
64
     * @return string
65
     */
66 20
    private function interpolate($message, array $context = [])
67
    {
68 20
        $replace = [];
69 20
        foreach ($context as $key => $val) {
70 14
            $replace['{' . $key . '}'] = as_string($val);
71 20
        }
72
73 20
        return strtr($message, $replace);
74
    }
75
76
    /**
77
     * Get the schema keyword for this error.
78
     *
79
     * @return int
80
     */
81 14
    public function getKeyword()
82
    {
83 14
        return $this->keyword;
84
    }
85
86
    /**
87
     * Get the path to the property that failed validation.
88
     *
89
     * @return string|null
90
     */
91 14
    public function getPointer()
92
    {
93 14
        return $this->pointer;
94
    }
95
96
    /**
97
     * Get the value that caused the assertion to fail.
98
     *
99
     * @return mixed
100
     */
101 14
    public function getValue()
102
    {
103 14
        return $this->value;
104
    }
105
106
    /**
107
     * Get the context that applied to the failed assertion.
108
     *
109
     * @return array
110
     */
111 16
    public function getContext()
112
    {
113 16
        return $this->context;
114
    }
115
116
    /**
117
     * @return array
118
     */
119 14
    public function toArray()
120
    {
121
        return [
122 14
            'keyword' => $this->getKeyword(),
123 14
            'message' => $this->getMessage(),
124 14
            'pointer' => $this->getPointer(),
125 14
            'value'   => $this->getValue(),
126 14
            'context' => $this->getContext(),
127 14
        ];
128
    }
129
130
    /**
131
     * Specify data which should be serialized to JSON
132
     * @link  http://php.net/manual/en/jsonserializable.jsonserialize.php
133
     * @return mixed data which can be serialized by <b>json_encode</b>,
134
     * which is a value of any type other than a resource.
135
     * @since 5.4.0
136
     */
137 2
    public function jsonSerialize()
138
    {
139 2
        return $this->toArray();
140
    }
141
142
    /**
143
     * Whether a offset exists
144
     * @link  http://php.net/manual/en/arrayaccess.offsetexists.php
145
     *
146
     * @param mixed $offset <p>
147
     *                      An offset to check for.
148
     *                      </p>
149
     *
150
     * @return boolean true on success or false on failure.
151
     * </p>
152
     * <p>
153
     * The return value will be casted to boolean if non-boolean was returned.
154
     * @since 5.0.0
155
     */
156 6
    public function offsetExists($offset)
157
    {
158 6
        return array_key_exists($offset, $this->toArray());
159
    }
160
161
    /**
162
     * Offset to retrieve
163
     * @link  http://php.net/manual/en/arrayaccess.offsetget.php
164
     *
165
     * @param mixed $offset <p>
166
     *                      The offset to retrieve.
167
     *                      </p>
168
     *
169
     * @return mixed Can return all value types.
170
     * @since 5.0.0
171
     */
172 12
    public function offsetGet($offset)
173
    {
174 12
        $errorArray = $this->toArray();
175 12
        return array_key_exists($offset, $errorArray) ? $errorArray[$offset] : null;
176
    }
177
178
    /**
179
     * Offset to set
180
     * @link  http://php.net/manual/en/arrayaccess.offsetset.php
181
     *
182
     * @param mixed $offset <p>
183
     *                      The offset to assign the value to.
184
     *                      </p>
185
     * @param mixed $value  <p>
186
     *                      The value to set.
187
     *                      </p>
188
     *
189
     * @return void
190
     * @since 5.0.0
191
     */
192 2
    public function offsetSet($offset, $value)
193
    {
194
        // A ValidationError is immutable.
195 2
        return null;
196
    }
197
198
    /**
199
     * Offset to unset
200
     * @link  http://php.net/manual/en/arrayaccess.offsetunset.php
201
     *
202
     * @param mixed $offset <p>
203
     *                      The offset to unset.
204
     *                      </p>
205
     *
206
     * @return void
207
     * @since 5.0.0
208
     */
209 2
    public function offsetUnset($offset)
210
    {
211
        // A ValidationError is immutable.
212 2
        return null;
213
    }
214
}
215