Completed
Push — master ( e24db7...5dd27b )
by Matt
02:58
created

ValidationError::getKeyword()   A

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 4
Code Lines 2

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
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 $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 134
    public function __construct($message, $keyword, $value, $pointer = null, array $context = [])
40
    {
41 134
        $this->message = $message;
42 134
        $this->keyword = $keyword;
0 ignored issues
show
Documentation Bug introduced by Matt Allan
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 134
        $this->pointer = $pointer;
44 134
        $this->value   = $value;
45 134
        $this->context = array_map('League\JsonGuard\as_string', $context);
46 134
    }
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->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 14
    private function interpolate($message, array $context = [])
67
    {
68 14
        $replace = [];
69 14
        foreach ($context as $key => $val) {
70 10
            $replace['{' . $key . '}'] = as_string($val);
71 14
        }
72
73 14
        return strtr($message, $replace);
74
    }
75
76
    /**
77
     * Get the schema keyword for this error.
78
     *
79
     * @return int
80
     */
81 10
    public function getKeyword()
82
    {
83 10
        return $this->keyword;
84
    }
85
86
    /**
87
     * Get the path to the property that failed validation.
88
     *
89
     * @return string|null
90
     */
91 10
    public function getPointer()
92
    {
93 10
        return $this->pointer;
94
    }
95
96
    /**
97
     * Get the value that caused the assertion to fail.
98
     *
99
     * @return mixed
100
     */
101 10
    public function getValue()
102
    {
103 10
        return $this->value;
104
    }
105
106
    /**
107
     * Get the context that applied to the failed assertion.
108
     *
109
     * @return array
110
     */
111 10
    public function getContext()
112
    {
113 10
        return $this->context;
114
    }
115
116
    /**
117
     * @return array
118
     */
119 10
    public function toArray()
120
    {
121
        return [
122 10
            'keyword' => $this->getKeyword(),
123 10
            'message' => $this->getMessage(),
124 10
            'pointer' => $this->getPointer(),
125 10
            'value'   => $this->getValue(),
126 10
            'context' => $this->getContext(),
127 10
        ];
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 2
    public function offsetExists($offset)
157
    {
158 2
        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 10
    public function offsetGet($offset)
173
    {
174 10
        $errorArray = $this->toArray();
175 10
        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
    public function offsetSet($offset, $value)
193
    {
194
        // A ValidationError is immutable.
195
        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
    public function offsetUnset($offset)
210
    {
211
        // A ValidationError is immutable.
212
        return null;
213
    }
214
}
215