forked from particle-php/Validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Validator.php
254 lines (226 loc) · 6.27 KB
/
Validator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
/**
* Particle.
*
* @link http://github.com/particle-php for the canonical source repository
* @copyright Copyright (c) 2005-2015 Particle (http://particle-php.com)
* @license https://github.com/particle-php/validator/blob/master/LICENSE New BSD License
*/
namespace Particle\Validator;
use Particle\Validator\Value\Container;
class Validator
{
/**
* The default context (if no context is currently active).
*/
const DEFAULT_CONTEXT = 'default';
/**
* Contains an array of context => Chain objects.
*
* @var array
*/
protected $chains = [];
/**
* Contains an array of context => messages.
*
* @var MessageStack
*/
protected $messageStack;
/**
* Message overwrites.
*
* @var array
*/
protected $messageOverwrites = [];
/**
* An array containing all the default messages.
*
* @var array
*/
protected $defaultMessages = [];
/**
* Contains a Value\Container to keep track of all values we *have* validated.
*
* @var Container
*/
protected $output;
/**
* Contains the name of the current context.
*
* @var string
*/
protected $context;
/**
* Construct the validator.
*/
public function __construct()
{
$this->context = self::DEFAULT_CONTEXT;
}
/**
* Creates a new required Validation Chain for the key $key.
*
* @param string $key
* @param string|null $name
* @param bool $allowEmpty
* @return Chain
*/
public function required($key, $name = null, $allowEmpty = false)
{
return $this->getChain($key, $name, true, $allowEmpty);
}
/**
* Creates a new optional Validation Chain for the key $key.
*
* @param string $key
* @param string|null $name
* @param bool $allowEmpty
* @return Chain
*/
public function optional($key, $name = null, $allowEmpty = true)
{
return $this->getChain($key, $name, false, $allowEmpty);
}
/**
* Validates the values in the $values array and returns the result as a bool.
*
* @param array $values
* @param string $context
* @return bool
*/
public function validate(array $values, $context = self::DEFAULT_CONTEXT)
{
$valid = true;
$messageStack = $this->buildMessageStack($context);
$this->output = new Container();
$input = new Container($values);
foreach ($this->chains[$context] as $chain) {
/** @var Chain $chain */
$valid = $chain->validate($messageStack, $input, $this->output) && $valid;
}
return $valid;
}
/**
* @return array
*/
public function getValues()
{
if (!$this->output instanceof Container) {
return [];
}
return $this->output->getArrayCopy();
}
/**
* Returns an array of all validation failures.
*
* @return array
*/
public function getMessages()
{
return $this->messageStack->getMessages();
}
/**
* Copy the rules and messages of the context $otherContext to the current context.
*
* @param string $otherContext
* @param callable|null $callback
* @return $this
*/
public function copyContext($otherContext, callable $callback = null)
{
if (isset($this->messageOverwrites[$otherContext])) {
$this->messageOverwrites[$this->context] = $this->messageOverwrites[$otherContext];
}
if (isset($this->chains[$otherContext])) {
$clonedChains = [];
foreach ($this->chains[$otherContext] as $key => $chain) {
$clonedChains[$key] = clone $chain;
}
if ($callback !== null) {
$callback($clonedChains);
}
$this->chains[$this->context] = $clonedChains;
}
return $this;
}
/**
* Create a new validation context with the closure $closure.
*
* @param string $name
* @param \Closure $closure
*/
public function context($name, \Closure $closure)
{
$this->context = $name;
$closure($this);
$this->context = self::DEFAULT_CONTEXT;
}
/**
* Overwrite the messages for specific keys.
*
* @param array $messages
* @return $this
*/
public function overwriteMessages(array $messages)
{
$this->messageOverwrites[$this->context] = $messages;
return $this;
}
/**
* Overwrite the default messages with custom messages.
*
* @param array $messages
* @return $this
*/
public function overwriteDefaultMessages(array $messages)
{
$this->defaultMessages = $messages;
return $this;
}
/**
* Retrieves a Chain object, or builds one if it doesn't exist yet.
*
* @param string $key
* @param string $name
* @param bool $required
* @param bool $allowEmpty
* @return Chain
*/
protected function getChain($key, $name, $required, $allowEmpty)
{
if (isset($this->chains[$this->context][$key])) {
return $this->chains[$this->context][$key];
}
return $this->chains[$this->context][$key] = $this->buildChain($key, $name, $required, $allowEmpty);
}
/**
* Build a new Chain object and return it.
*
* @param string $key
* @param string $name
* @param bool $required
* @param bool $allowEmpty
* @return Chain
*/
protected function buildChain($key, $name, $required, $allowEmpty)
{
return new Chain($key, $name, $required, $allowEmpty);
}
/**
* Build a new MessageStack, set the message overwrites and return it.
*
* @param string $context
* @return MessageStack
*/
protected function buildMessageStack($context)
{
$this->messageStack = new MessageStack();
$this->messageStack->overwriteDefaultMessages($this->defaultMessages);
foreach ([self::DEFAULT_CONTEXT, $context] as $currentContext) {
if (isset($this->messageOverwrites[$currentContext])) {
$this->messageStack->overwriteMessages($this->messageOverwrites[$currentContext]);
}
}
return $this->messageStack;
}
}