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 Respect\Validation\Message\Formatter; |
18
|
|
|
use function key; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Default exception class for rule validations. |
22
|
|
|
* |
23
|
|
|
* @author Alexandre Gomes Gaigalas <[email protected]> |
24
|
|
|
* @author Henrique Moody <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class ValidationException extends InvalidArgumentException implements Exception |
27
|
|
|
{ |
28
|
|
|
public const MODE_DEFAULT = 'default'; |
29
|
|
|
public const MODE_NEGATIVE = 'negative'; |
30
|
|
|
public const STANDARD = 'standard'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Contains the default templates for exception message. |
34
|
|
|
* |
35
|
|
|
* @var string[][] |
36
|
|
|
*/ |
37
|
|
|
protected $defaultTemplates = [ |
38
|
|
|
self::MODE_DEFAULT => [ |
39
|
|
|
self::STANDARD => '{{name}} must be valid', |
40
|
|
|
], |
41
|
|
|
self::MODE_NEGATIVE => [ |
42
|
|
|
self::STANDARD => '{{name}} must not be valid', |
43
|
|
|
], |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var mixed |
48
|
|
|
*/ |
49
|
|
|
private $input; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
private $id; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
private $mode = self::MODE_DEFAULT; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var mixed[] |
63
|
|
|
*/ |
64
|
|
|
private $params = []; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var Formatter |
68
|
|
|
*/ |
69
|
|
|
private $formatter; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var string |
73
|
|
|
*/ |
74
|
|
|
private $template; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param mixed $input |
78
|
|
|
* @param mixed[] $params |
79
|
|
|
*/ |
80
|
|
|
public function __construct($input, string $id, array $params, Formatter $formatter) |
81
|
|
|
{ |
82
|
|
|
$this->input = $input; |
83
|
192 |
|
$this->id = $id; |
84
|
|
|
$this->params = $params; |
85
|
192 |
|
$this->formatter = $formatter; |
86
|
192 |
|
$this->template = $this->chooseTemplate(); |
87
|
192 |
|
|
88
|
192 |
|
parent::__construct($this->createMessage()); |
89
|
192 |
|
} |
90
|
|
|
|
91
|
192 |
|
public function getId(): string |
92
|
192 |
|
{ |
93
|
|
|
return $this->id; |
94
|
9 |
|
} |
95
|
|
|
|
96
|
9 |
|
/** |
97
|
|
|
* @return mixed[] |
98
|
|
|
*/ |
99
|
|
|
public function getParams(): array |
100
|
|
|
{ |
101
|
|
|
return $this->params; |
102
|
192 |
|
} |
103
|
|
|
|
104
|
192 |
|
/** |
105
|
|
|
* @return mixed|null |
106
|
|
|
*/ |
107
|
|
|
public function getParam(string $name) |
108
|
|
|
{ |
109
|
|
|
return $this->params[$name] ?? null; |
110
|
172 |
|
} |
111
|
|
|
|
112
|
172 |
|
public function updateMode(string $mode): void |
113
|
|
|
{ |
114
|
|
|
$this->mode = $mode; |
115
|
146 |
|
$this->message = $this->createMessage(); |
116
|
|
|
} |
117
|
146 |
|
|
118
|
146 |
|
public function updateTemplate(string $template): void |
119
|
146 |
|
{ |
120
|
|
|
$this->template = $template; |
121
|
16 |
|
$this->message = $this->createMessage(); |
122
|
|
|
} |
123
|
16 |
|
|
124
|
16 |
|
/** |
125
|
16 |
|
* @param mixed[] $params |
126
|
|
|
*/ |
127
|
|
|
public function updateParams(array $params): void |
128
|
|
|
{ |
129
|
|
|
$this->params = $params; |
130
|
3 |
|
$this->message = $this->createMessage(); |
131
|
|
|
} |
132
|
3 |
|
|
133
|
3 |
|
public function hasCustomTemplate(): bool |
134
|
3 |
|
{ |
135
|
|
|
return isset($this->defaultTemplates[$this->mode][$this->template]) === false; |
136
|
155 |
|
} |
137
|
|
|
|
138
|
155 |
|
public function __toString(): string |
139
|
|
|
{ |
140
|
|
|
return $this->getMessage(); |
141
|
1 |
|
} |
142
|
|
|
|
143
|
1 |
|
protected function chooseTemplate(): string |
144
|
|
|
{ |
145
|
|
|
return (string) key($this->defaultTemplates[$this->mode]); |
146
|
161 |
|
} |
147
|
|
|
|
148
|
161 |
|
private function createMessage(): string |
149
|
|
|
{ |
150
|
|
|
return $this->formatter->format( |
151
|
192 |
|
$this->defaultTemplates[$this->mode][$this->template] ?? $this->template, |
152
|
|
|
$this->input, |
153
|
192 |
|
$this->params |
154
|
192 |
|
); |
155
|
192 |
|
} |
156
|
|
|
} |
157
|
|
|
|