These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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
|
|||
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 |
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.