1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Respect/Validation. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandre Gomes Gaigalas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the "LICENSE.md" |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Respect\Validation\Exceptions; |
15
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
use function in_array; |
18
|
|
|
use function is_numeric; |
19
|
|
|
use function Respect\Stringifier\stringify; |
20
|
|
|
|
21
|
|
|
class ValidationException extends InvalidArgumentException implements ExceptionInterface |
22
|
|
|
{ |
23
|
|
|
const MODE_DEFAULT = 1; |
24
|
|
|
const MODE_NEGATIVE = 2; |
25
|
|
|
const STANDARD = 0; |
26
|
|
|
public static $defaultTemplates = [ |
27
|
|
|
self::MODE_DEFAULT => [ |
28
|
|
|
self::STANDARD => 'Data validation failed for %s', |
29
|
|
|
], |
30
|
|
|
self::MODE_NEGATIVE => [ |
31
|
|
|
self::STANDARD => 'Data validation failed for %s', |
32
|
|
|
], |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
protected $id = 'validation'; |
36
|
|
|
protected $mode = self::MODE_DEFAULT; |
37
|
|
|
protected $name = ''; |
38
|
|
|
protected $template = ''; |
39
|
|
|
protected $params = []; |
40
|
|
|
private $customTemplate = false; |
41
|
|
|
|
42
|
8 |
|
public static function format($template, array $vars = []) |
43
|
|
|
{ |
44
|
8 |
|
return preg_replace_callback( |
45
|
8 |
|
'/{{(\w+)}}/', |
46
|
8 |
|
function ($match) use ($vars) { |
47
|
7 |
|
if (!isset($vars[$match[1]])) { |
48
|
2 |
|
return $match[0]; |
49
|
|
|
} |
50
|
|
|
|
51
|
7 |
|
$value = $vars[$match[1]]; |
52
|
7 |
|
if ('name' == $match[1] && is_string($value)) { |
53
|
4 |
|
return $value; |
54
|
|
|
} |
55
|
|
|
|
56
|
4 |
|
return ValidationException::stringify($value); |
57
|
8 |
|
}, |
58
|
8 |
|
$template |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param mixed $value |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
4 |
|
public static function stringify($value) |
68
|
|
|
{ |
69
|
4 |
|
return stringify($value); |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
public function __toString() |
73
|
|
|
{ |
74
|
2 |
|
return $this->getMainMessage(); |
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
public function chooseTemplate() |
78
|
|
|
{ |
79
|
4 |
|
return key(static::$defaultTemplates[$this->mode]); |
80
|
|
|
} |
81
|
|
|
|
82
|
3 |
|
public function configure($name, array $params = []) |
83
|
|
|
{ |
84
|
3 |
|
$this->setName($name); |
85
|
3 |
|
$this->setId($this->guessId()); |
86
|
3 |
|
$this->setParams($params); |
87
|
|
|
|
88
|
3 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
5 |
|
public function getName() |
92
|
|
|
{ |
93
|
5 |
|
return $this->name; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function hasName(): bool |
97
|
|
|
{ |
98
|
|
|
$name = $this->getName(); |
99
|
|
|
if (is_numeric($name)) { |
100
|
|
|
return (bool) (float) $name; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return !in_array($name, ['`FALSE`', '`NULL`', '`{ }`', '""', '']); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
public function getId() |
107
|
|
|
{ |
108
|
1 |
|
return $this->id; |
109
|
|
|
} |
110
|
|
|
|
111
|
5 |
|
public function getMainMessage() |
112
|
|
|
{ |
113
|
5 |
|
$vars = $this->getParams(); |
114
|
5 |
|
$vars['name'] = $this->getName(); |
115
|
5 |
|
$template = $this->getTemplate(); |
116
|
5 |
|
if (isset($vars['translator']) && is_callable($vars['translator'])) { |
117
|
|
|
$template = call_user_func($vars['translator'], $template); |
118
|
|
|
} |
119
|
|
|
|
120
|
5 |
|
return static::format($template, $vars); |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
public function getParam($name) |
124
|
|
|
{ |
125
|
1 |
|
return $this->hasParam($name) ? $this->params[$name] : false; |
126
|
|
|
} |
127
|
|
|
|
128
|
5 |
|
public function getParams() |
129
|
|
|
{ |
130
|
5 |
|
return $this->params; |
131
|
|
|
} |
132
|
|
|
|
133
|
5 |
|
public function getTemplate() |
134
|
|
|
{ |
135
|
5 |
|
if (!empty($this->template)) { |
136
|
4 |
|
return $this->template; |
137
|
|
|
} |
138
|
|
|
|
139
|
5 |
|
return $this->template = $this->buildTemplate(); |
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
public function hasParam($name) |
143
|
|
|
{ |
144
|
1 |
|
return isset($this->params[$name]); |
145
|
|
|
} |
146
|
|
|
|
147
|
3 |
|
public function setId($id) |
148
|
|
|
{ |
149
|
3 |
|
$this->id = $id; |
150
|
|
|
|
151
|
3 |
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
3 |
|
public function setName($name) |
155
|
|
|
{ |
156
|
3 |
|
$this->name = $name; |
157
|
|
|
|
158
|
3 |
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function setMode($mode) |
162
|
|
|
{ |
163
|
|
|
$this->mode = $mode; |
164
|
|
|
$this->template = $this->buildTemplate(); |
165
|
|
|
|
166
|
|
|
$this->buildMessage(); |
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function setParam($key, $value) |
172
|
|
|
{ |
173
|
|
|
$this->params[$key] = $value; |
174
|
|
|
|
175
|
|
|
$this->buildMessage(); |
176
|
|
|
|
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
3 |
|
public function setParams(array $params) |
181
|
|
|
{ |
182
|
3 |
|
foreach ($params as $key => $value) { |
183
|
1 |
|
$this->params[$key] = $value; |
184
|
|
|
} |
185
|
|
|
|
186
|
3 |
|
$this->buildMessage(); |
187
|
|
|
|
188
|
3 |
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function hasCustomTemplate() |
192
|
|
|
{ |
193
|
|
|
return true === $this->customTemplate; |
194
|
|
|
} |
195
|
|
|
|
196
|
2 |
|
public function setTemplate($template) |
197
|
|
|
{ |
198
|
2 |
|
$this->customTemplate = true; |
199
|
2 |
|
if (isset(static::$defaultTemplates[$this->mode][$template])) { |
200
|
|
|
$template = static::$defaultTemplates[$this->mode][$template]; |
201
|
|
|
} |
202
|
2 |
|
$this->template = $template; |
203
|
|
|
|
204
|
2 |
|
$this->buildMessage(); |
205
|
|
|
|
206
|
2 |
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
3 |
|
private function buildMessage(): void |
210
|
|
|
{ |
211
|
3 |
|
$this->message = $this->getMainMessage(); |
212
|
3 |
|
} |
213
|
|
|
|
214
|
5 |
|
protected function buildTemplate() |
215
|
|
|
{ |
216
|
5 |
|
$templateKey = $this->chooseTemplate(); |
217
|
|
|
|
218
|
5 |
|
return static::$defaultTemplates[$this->mode][$templateKey]; |
219
|
|
|
} |
220
|
|
|
|
221
|
3 |
|
public function guessId() |
222
|
|
|
{ |
223
|
3 |
|
if (!empty($this->id) && 'validation' != $this->id) { |
224
|
|
|
return $this->id; |
225
|
|
|
} |
226
|
|
|
|
227
|
3 |
|
$pieces = explode('\\', get_called_class()); |
228
|
3 |
|
$exceptionClassShortName = end($pieces); |
229
|
3 |
|
$ruleClassShortName = str_replace('Exception', '', $exceptionClassShortName); |
230
|
3 |
|
$ruleName = lcfirst($ruleClassShortName); |
231
|
|
|
|
232
|
3 |
|
return $ruleName; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|