1 | <?php |
||
5 | final class ValidationError implements \JsonSerializable |
||
6 | { |
||
7 | const KEYWORD = 'keyword'; |
||
8 | const PARAMETER = 'parameter'; |
||
9 | const DATA = 'data'; |
||
10 | const DATA_PATH = 'data_path'; |
||
11 | const SCHEMA = 'schema'; |
||
12 | const SCHEMA_PATH = 'schema_path'; |
||
13 | const CAUSE = 'cause'; |
||
14 | const MESSAGE = 'message'; |
||
15 | |||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | private $message; |
||
20 | |||
21 | /** |
||
22 | * @var string|null |
||
23 | */ |
||
24 | private $interpolatedMessage; |
||
25 | |||
26 | /** |
||
27 | * @var mixed |
||
28 | */ |
||
29 | private $cause; |
||
30 | |||
31 | /** |
||
32 | * @var string[]|null |
||
33 | */ |
||
34 | private $context; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $keyword; |
||
40 | |||
41 | /** |
||
42 | * @var mixed |
||
43 | */ |
||
44 | private $parameter; |
||
45 | |||
46 | /** |
||
47 | * @var mixed |
||
48 | */ |
||
49 | private $data; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $dataPath; |
||
55 | |||
56 | /** |
||
57 | * @var object |
||
58 | */ |
||
59 | private $schema; |
||
60 | |||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | private $schemaPath; |
||
65 | |||
66 | 94 | public function __construct( |
|
67 | $message, |
||
68 | $keyword, |
||
69 | $parameter, |
||
70 | $data, |
||
71 | $dataPath, |
||
72 | $schema, |
||
73 | $schemaPath |
||
74 | ) { |
||
75 | 94 | $this->message = $message; |
|
76 | 94 | $this->keyword = $keyword; |
|
77 | 94 | $this->parameter = $parameter; |
|
78 | 94 | $this->data = $data; |
|
79 | 94 | $this->dataPath = $dataPath; |
|
80 | 94 | $this->schema = $schema; |
|
81 | 94 | $this->schemaPath = $schemaPath; |
|
82 | 94 | } |
|
83 | |||
84 | /** |
||
85 | * Get the human readable error message for this error. |
||
86 | * |
||
87 | * @return string |
||
88 | */ |
||
89 | 6 | public function getMessage() |
|
90 | { |
||
91 | 6 | if ($this->interpolatedMessage === null) { |
|
92 | 6 | $this->interpolatedMessage = $this->interpolate($this->message, $this->getContext()); |
|
93 | } |
||
94 | |||
95 | 6 | return $this->interpolatedMessage; |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * @return string |
||
100 | */ |
||
101 | 8 | public function getKeyword() |
|
105 | |||
106 | /** |
||
107 | * @return mixed |
||
108 | */ |
||
109 | 2 | public function getParameter() |
|
110 | { |
||
111 | 2 | return $this->parameter; |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * @return mixed |
||
116 | */ |
||
117 | 2 | public function getData() |
|
118 | { |
||
119 | 2 | return $this->data; |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * @return string |
||
124 | */ |
||
125 | 6 | public function getDataPath() |
|
126 | { |
||
127 | 6 | return $this->dataPath; |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * @return object |
||
132 | */ |
||
133 | 2 | public function getSchema() |
|
134 | { |
||
135 | 2 | return $this->schema; |
|
136 | } |
||
137 | |||
138 | /** |
||
139 | * @return string |
||
140 | */ |
||
141 | 2 | public function getSchemaPath() |
|
142 | { |
||
143 | 2 | return $this->schemaPath; |
|
144 | } |
||
145 | |||
146 | /** |
||
147 | * Get the cause of the error. The cause is either the the value itself |
||
148 | * or the subset of the value that failed validation. For example, the |
||
149 | * cause of a failed minimum constraint would be the number itself, while |
||
150 | * the cause of a failed additionalProperties constraint would be the |
||
151 | * additional properties in the value that are not allowed. |
||
152 | * |
||
153 | * @return mixed |
||
154 | */ |
||
155 | 6 | public function getCause() |
|
159 | |||
160 | /** |
||
161 | * @param $cause |
||
162 | * |
||
163 | * @return \League\JsonGuard\ValidationError |
||
164 | */ |
||
165 | 20 | public function withCause($cause) |
|
172 | |||
173 | /** |
||
174 | * Get the context that applied to the failed assertion. |
||
175 | * |
||
176 | * @return string[] |
||
177 | */ |
||
178 | 6 | public function getContext() |
|
179 | { |
||
180 | 6 | if ($this->context === null) { |
|
181 | 6 | $this->context = array_map( |
|
182 | 6 | 'League\JsonGuard\as_string', |
|
183 | [ |
||
184 | 6 | self::KEYWORD => $this->keyword, |
|
185 | 6 | self::PARAMETER => $this->parameter, |
|
186 | 6 | self::DATA => $this->data, |
|
187 | 6 | self::DATA_PATH => $this->dataPath, |
|
188 | 6 | self::SCHEMA => $this->schema, |
|
189 | 6 | self::SCHEMA_PATH => $this->schemaPath, |
|
190 | 6 | self::CAUSE => $this->getCause(), |
|
191 | ] |
||
192 | ); |
||
193 | } |
||
194 | |||
195 | 6 | return $this->context; |
|
196 | } |
||
197 | |||
198 | /** |
||
199 | * @return array |
||
200 | */ |
||
201 | 2 | public function toArray() |
|
202 | { |
||
203 | return [ |
||
204 | 2 | self::MESSAGE => $this->getMessage(), |
|
205 | 2 | self::KEYWORD => $this->keyword, |
|
206 | 2 | self::PARAMETER => $this->parameter, |
|
207 | 2 | self::DATA => $this->data, |
|
208 | 2 | self::DATA_PATH => $this->dataPath, |
|
209 | 2 | self::SCHEMA => $this->schema, |
|
210 | 2 | self::SCHEMA_PATH => $this->schemaPath, |
|
211 | 2 | self::CAUSE => $this->getCause(), |
|
212 | ]; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * @inheritdoc |
||
217 | */ |
||
218 | 2 | public function jsonSerialize() |
|
222 | |||
223 | /** |
||
224 | * Interpolate the context values into the message placeholders. |
||
225 | * |
||
226 | * @param string $message |
||
227 | * @param array $context |
||
228 | * |
||
229 | * @return string |
||
230 | */ |
||
231 | 6 | private function interpolate($message, array $context = []) |
|
240 | } |
||
241 |