-
Notifications
You must be signed in to change notification settings - Fork 12
/
ValidationRule.php
219 lines (196 loc) · 6.34 KB
/
ValidationRule.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
<?php
declare(strict_types=1);
/**
* ValidationRule.
*
* Provides the Model validation logic.
*
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 2.2.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Validation;
use InvalidArgumentException;
/**
* ValidationRule object. Represents a validation method, error message and
* rules for applying such method to a field.
*/
class ValidationRule
{
/**
* The method to be called for a given scope
*
* @var string|callable
*/
protected $_rule;
/**
* The 'on' key
*
* @var string|callable
*/
protected $_on;
/**
* The 'last' key
*
* @var bool
*/
protected $_last = false;
/**
* The 'message' key
*
* @var string
*/
protected $_message;
/**
* Key under which the object or class where the method to be used for
* validation will be found
*
* @var string
*/
protected $_provider = 'default';
/**
* Extra arguments to be passed to the validation method
*
* @var array
*/
protected $_pass = [];
/**
* Constructor
*
* @param array $validator [optional] The validator properties
*/
public function __construct(array $validator = [])
{
$this->_addValidatorProps($validator);
}
/**
* Returns whether this rule should break validation process for associated field
* after it fails
*
* @return bool
*/
public function isLast(): bool
{
return $this->_last;
}
/**
* Dispatches the validation rule to the given validator method and returns
* a boolean indicating whether the rule passed or not. If a string is returned
* it is assumed that the rule failed and the error message was given as a result.
*
* @param mixed $value The data to validate
* @param array $providers associative array with objects or class names that will
* be passed as the last argument for the validation method
* @param array $context A key value list of data that could be used as context
* during validation. Recognized keys are:
* - newRecord: (boolean) whether or not the data to be validated belongs to a
* new record
* - data: The full data that was passed to the validation process
* - field: The name of the field that is being processed
* @return bool|string|array
* @throws \InvalidArgumentException when the supplied rule is not a valid
* callable for the configured scope
*/
public function process($value, array $providers, array $context = [])
{
$context += ['data' => [], 'newRecord' => true, 'providers' => $providers];
if ($this->_skip($context)) {
return true;
}
if (!is_string($this->_rule) && is_callable($this->_rule)) {
$callable = $this->_rule;
$isCallable = true;
} else {
$provider = $providers[$this->_provider];
$callable = [$provider, $this->_rule];
$isCallable = is_callable($callable);
}
if (!$isCallable) {
/** @psalm-suppress PossiblyInvalidArgument */
$message = sprintf(
'Unable to call method "%s" in "%s" provider for field "%s"',
$this->_rule,
$this->_provider,
$context['field']
);
throw new InvalidArgumentException($message);
}
if ($this->_pass) {
$args = array_values(array_merge([$value], $this->_pass, [$context]));
$result = $callable(...$args);
} else {
$result = $callable($value, $context);
}
if ($result === false) {
return $this->_message ?: false;
}
return $result;
}
/**
* Checks if the validation rule should be skipped
*
* @param array $context A key value list of data that could be used as context
* during validation. Recognized keys are:
* - newRecord: (boolean) whether or not the data to be validated belongs to a
* new record
* - data: The full data that was passed to the validation process
* - providers associative array with objects or class names that will
* be passed as the last argument for the validation method
* @return bool True if the ValidationRule should be skipped
*/
protected function _skip(array $context): bool
{
if (!is_string($this->_on) && is_callable($this->_on)) {
$function = $this->_on;
return !$function($context);
}
$newRecord = $context['newRecord'];
if (!empty($this->_on)) {
return ($this->_on === Validator::WHEN_CREATE && !$newRecord)
|| ($this->_on === Validator::WHEN_UPDATE && $newRecord);
}
return false;
}
/**
* Sets the rule properties from the rule entry in validate
*
* @param array $validator [optional]
* @return void
*/
protected function _addValidatorProps(array $validator = []): void
{
foreach ($validator as $key => $value) {
if (!isset($value) || empty($value)) {
continue;
}
if ($key === 'rule' && is_array($value) && !is_callable($value)) {
$this->_pass = array_slice($value, 1);
$value = array_shift($value);
}
if (in_array($key, ['rule', 'on', 'message', 'last', 'provider', 'pass'], true)) {
$this->{"_$key"} = $value;
}
}
}
/**
* Returns the value of a property by name
*
* @param string $property The name of the property to retrieve.
* @return mixed
*/
public function get(string $property)
{
$property = '_' . $property;
if (isset($this->{$property})) {
return $this->{$property};
}
}
}