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 $context; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $message |
34
|
|
|
* @param int $code |
35
|
|
|
* @param mixed $value |
36
|
|
|
* @param string|null $pointer |
37
|
|
|
* @param array $context |
38
|
|
|
*/ |
39
|
132 |
|
public function __construct($message, $code, $value, $pointer = null, array $context = []) |
40
|
|
|
{ |
41
|
132 |
|
$this->message = $message; |
42
|
132 |
|
$this->code = $code; |
43
|
132 |
|
$this->pointer = $pointer; |
44
|
132 |
|
$this->value = $value; |
45
|
132 |
|
$this->context = array_map('League\JsonGuard\as_string', $context); |
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->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
|
10 |
|
* |
64
|
|
|
* @return string |
65
|
10 |
|
*/ |
66
|
|
|
private function interpolate($message, array $context = []) |
67
|
|
|
{ |
68
|
|
|
$replace = []; |
69
|
|
|
foreach ($context as $key => $val) { |
70
|
|
|
$replace['{' . $key . '}'] = as_string($val); |
71
|
|
|
} |
72
|
|
|
|
73
|
10 |
|
return strtr($message, $replace); |
74
|
|
|
} |
75
|
10 |
|
|
76
|
|
|
/** |
77
|
|
|
* Get the error code for this error. |
78
|
|
|
* |
79
|
|
|
* @return int |
80
|
|
|
*/ |
81
|
|
|
public function getCode() |
82
|
|
|
{ |
83
|
10 |
|
return $this->code; |
84
|
|
|
} |
85
|
10 |
|
|
86
|
|
|
/** |
87
|
|
|
* Get the path to the property that failed validation. |
88
|
|
|
* |
89
|
|
|
* @return string|null |
90
|
|
|
*/ |
91
|
|
|
public function getPointer() |
92
|
|
|
{ |
93
|
10 |
|
return $this->pointer; |
94
|
|
|
} |
95
|
10 |
|
|
96
|
|
|
/** |
97
|
|
|
* Get the value that caused the assertion to fail. |
98
|
|
|
* |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
10 |
|
public function getValue() |
102
|
|
|
{ |
103
|
|
|
return $this->value; |
104
|
10 |
|
} |
105
|
10 |
|
|
106
|
10 |
|
/** |
107
|
10 |
|
* Get the context that applied to the failed assertion. |
108
|
10 |
|
* |
109
|
10 |
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function getContext() |
112
|
|
|
{ |
113
|
|
|
return $this->context; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
public function toArray() |
120
|
|
|
{ |
121
|
|
|
return [ |
122
|
|
|
'code' => $this->getCode(), |
123
|
|
|
'message' => $this->getMessage(), |
124
|
|
|
'pointer' => $this->getPointer(), |
125
|
|
|
'value' => $this->getValue(), |
126
|
2 |
|
'context' => $this->getContext(), |
127
|
|
|
]; |
128
|
2 |
|
} |
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
|
|
|
public function jsonSerialize() |
138
|
|
|
{ |
139
|
|
|
return $this->toArray(); |
140
|
|
|
} |
141
|
|
|
|
142
|
10 |
|
/** |
143
|
|
|
* Whether a offset exists |
144
|
10 |
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
145
|
10 |
|
* |
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
|
|
|
public function offsetExists($offset) |
157
|
|
|
{ |
158
|
|
|
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
|
|
|
public function offsetGet($offset) |
173
|
|
|
{ |
174
|
|
|
$errorArray = $this->toArray(); |
175
|
|
|
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
|
|
|
|