1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\contents\validation; |
4
|
|
|
|
5
|
|
|
use Ubiquity\cache\CacheManager; |
6
|
|
|
use Ubiquity\cache\objects\SessionCache; |
7
|
|
|
use Ubiquity\contents\validation\validators\basic\IsBooleanValidator; |
8
|
|
|
use Ubiquity\contents\validation\validators\basic\IsEmptyValidator; |
9
|
|
|
use Ubiquity\contents\validation\validators\basic\IsFalseValidator; |
10
|
|
|
use Ubiquity\contents\validation\validators\basic\IsNullValidator; |
11
|
|
|
use Ubiquity\contents\validation\validators\basic\IsTrueValidator; |
12
|
|
|
use Ubiquity\contents\validation\validators\basic\NotEmptyValidator; |
13
|
|
|
use Ubiquity\contents\validation\validators\basic\NotNullValidator; |
14
|
|
|
use Ubiquity\contents\validation\validators\basic\TypeValidator; |
15
|
|
|
use Ubiquity\contents\validation\validators\comparison\EqualsValidator; |
16
|
|
|
use Ubiquity\contents\validation\validators\comparison\GreaterThanOrEqualValidator; |
17
|
|
|
use Ubiquity\contents\validation\validators\comparison\GreaterThanValidator; |
18
|
|
|
use Ubiquity\contents\validation\validators\comparison\LessThanOrEqualValidator; |
19
|
|
|
use Ubiquity\contents\validation\validators\comparison\LessThanValidator; |
20
|
|
|
use Ubiquity\contents\validation\validators\comparison\RangeValidator; |
21
|
|
|
use Ubiquity\contents\validation\validators\dates\DateTimeValidator; |
22
|
|
|
use Ubiquity\contents\validation\validators\dates\DateValidator; |
23
|
|
|
use Ubiquity\contents\validation\validators\dates\TimeValidator; |
24
|
|
|
use Ubiquity\contents\validation\validators\multiples\IdValidator; |
25
|
|
|
use Ubiquity\contents\validation\validators\multiples\LengthValidator; |
26
|
|
|
use Ubiquity\contents\validation\validators\strings\EmailValidator; |
27
|
|
|
use Ubiquity\contents\validation\validators\strings\IpValidator; |
28
|
|
|
use Ubiquity\contents\validation\validators\strings\RegexValidator; |
29
|
|
|
use Ubiquity\contents\validation\validators\strings\UrlValidator; |
30
|
|
|
use Ubiquity\log\Logger; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Validators manager |
34
|
|
|
* |
35
|
|
|
* Ubiquity\contents\validation$ValidatorsManager |
36
|
|
|
* This class is part of Ubiquity |
37
|
|
|
* |
38
|
|
|
* @author jcheron <[email protected]> |
39
|
|
|
* @version 1.0.1 |
40
|
|
|
* |
41
|
|
|
*/ |
42
|
|
|
class ValidatorsManager { |
43
|
|
|
use ValidatorsManagerInitTrait; |
44
|
|
|
protected static $instanceValidators = [ ]; |
45
|
|
|
protected static $cache; |
46
|
|
|
|
47
|
3 |
|
public static function start() { |
48
|
3 |
|
self::$cache = new SessionCache (); |
49
|
3 |
|
} |
50
|
|
|
public static $validatorTypes = [ "notNull" => NotNullValidator::class,"isNull" => IsNullValidator::class,"notEmpty" => NotEmptyValidator::class,"isEmpty" => IsEmptyValidator::class,"isTrue" => IsTrueValidator::class,"isFalse" => IsFalseValidator::class,"isBool" => IsBooleanValidator::class,"equals" => EqualsValidator::class,"type" => TypeValidator::class, |
51
|
|
|
"greaterThan" => GreaterThanValidator::class,"greaterThanOrEqual" => GreaterThanOrEqualValidator::class,"lessThan" => LessThanValidator::class,"lessThanOrEqual" => LessThanOrEqualValidator::class,"length" => LengthValidator::class,"id" => IdValidator::class,"regex" => RegexValidator::class,"email" => EmailValidator::class,"url" => UrlValidator::class,"ip" => IpValidator::class, |
52
|
|
|
"range" => RangeValidator::class,"date" => DateValidator::class,"dateTime" => DateTimeValidator::class,"time" => TimeValidator::class ]; |
53
|
|
|
protected static $key = "contents/validators/"; |
54
|
|
|
|
55
|
1 |
|
protected static function store($model, $validators) { |
56
|
1 |
|
CacheManager::$cache->store ( self::getModelCacheKey ( $model ), $validators ); |
57
|
1 |
|
} |
58
|
|
|
|
59
|
3 |
|
protected static function fetch($model) { |
60
|
3 |
|
$key = self::getModelCacheKey ( $model ); |
61
|
3 |
|
if (CacheManager::$cache->exists ( $key )) { |
62
|
3 |
|
return CacheManager::$cache->fetch ( $key ); |
63
|
|
|
} |
64
|
|
|
return [ ]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected static function getGroupArrayValidators(array $validators, $group) { |
68
|
|
|
$result = [ ]; |
69
|
|
|
foreach ( $validators as $member => $validators ) { |
70
|
|
|
$filteredValidators = self::getGroupMemberValidators ( $validators, $group ); |
71
|
|
|
if (sizeof ( $filteredValidators )) { |
72
|
|
|
$result [$member] = $filteredValidators; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
return $result; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected static function getGroupMemberValidators(array $validators, $group) { |
79
|
|
|
$result = [ ]; |
80
|
|
|
foreach ( $validators as $validator ) { |
81
|
|
|
if (isset ( $validator ["group"] ) && $validator ["group"] === $group) { |
82
|
|
|
$result [] = $validator; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
3 |
|
private static function getCacheValidators($instance, $group = "") { |
89
|
3 |
|
if (isset ( self::$cache )) { |
90
|
3 |
|
$key = self::getHash ( get_class ( $instance ) . $group ); |
91
|
3 |
|
if (self::$cache->exists ( $key )) { |
92
|
|
|
return self::$cache->fetch ( $key ); |
93
|
|
|
} |
94
|
|
|
} |
95
|
3 |
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Validates an instance |
100
|
|
|
* |
101
|
|
|
* @param object $instance |
102
|
|
|
* @param string $group |
103
|
|
|
* @return \Ubiquity\contents\validation\validators\ConstraintViolation[] |
104
|
|
|
*/ |
105
|
1 |
|
public static function validate($instance, $group = "") { |
106
|
1 |
|
$cache = self::getCacheValidators ( $instance, $group ); |
107
|
1 |
|
if ($cache !== false) { |
108
|
|
|
return self::validateFromCache_ ( $instance, $cache ); |
109
|
|
|
} |
110
|
1 |
|
$members = self::fetch ( get_class ( $instance ) ); |
111
|
1 |
|
if ($group !== "") { |
112
|
|
|
$members = self::getGroupArrayValidators ( $members, $group ); |
113
|
|
|
} |
114
|
1 |
|
return self::validate_ ( $instance, $members ); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Validates an array of objects |
119
|
|
|
* |
120
|
|
|
* @param array $instances |
121
|
|
|
* @param string $group |
122
|
|
|
* @return \Ubiquity\contents\validation\validators\ConstraintViolation[] |
123
|
|
|
*/ |
124
|
2 |
|
public static function validateInstances($instances, $group = "") { |
125
|
2 |
|
if (sizeof ( $instances ) > 0) { |
126
|
2 |
|
$instance = current ( $instances ); |
127
|
2 |
|
$cache = self::getCacheValidators ( $instance, $group ); |
128
|
2 |
|
if ($cache === false) { |
129
|
2 |
|
$class = get_class ( $instance ); |
130
|
2 |
|
$members = self::fetch ( $class ); |
131
|
2 |
|
self::initInstancesValidators ( $instance, $members, $group ); |
132
|
2 |
|
$cache = self::$instanceValidators [$class]; |
133
|
|
|
} |
134
|
2 |
|
return self::validateInstances_ ( $instances, $cache ); |
135
|
|
|
} |
136
|
|
|
return [ ]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public static function clearCache($model = null, $group = "") { |
140
|
|
|
if (isset ( self::$cache )) { |
141
|
|
|
if (isset ( $model )) { |
142
|
|
|
$key = self::getHash ( $model . $group ); |
143
|
|
|
self::$cache->remove ( $key ); |
144
|
|
|
} else { |
145
|
|
|
self::$cache->clear (); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
2 |
|
protected static function validateInstances_($instances, $members) { |
151
|
2 |
|
$result = [ ]; |
152
|
2 |
|
foreach ( $instances as $instance ) { |
153
|
2 |
|
foreach ( $members as $accessor => $validators ) { |
154
|
2 |
|
foreach ( $validators as $validator ) { |
155
|
2 |
|
$valid = $validator->validate_ ( $instance->$accessor () ); |
156
|
2 |
|
if ($valid !== true) { |
157
|
2 |
|
$result [] = $valid; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
2 |
|
return $result; |
163
|
|
|
} |
164
|
|
|
|
165
|
1 |
|
protected static function validate_($instance, $members) { |
166
|
1 |
|
$result = [ ]; |
167
|
1 |
|
foreach ( $members as $member => $validators ) { |
168
|
1 |
|
$accessor = "get" . ucfirst ( $member ); |
169
|
1 |
|
if (method_exists ( $instance, $accessor )) { |
170
|
1 |
|
foreach ( $validators as $validator ) { |
171
|
1 |
|
$validatorInstance = self::getValidatorInstance ( $validator ["type"] ); |
172
|
1 |
|
if ($validatorInstance !== false) { |
173
|
1 |
|
$validatorInstance->setValidationParameters ( $member, $validator ["constraints"], @$validator ["severity"], @$validator ["message"] ); |
174
|
1 |
|
$valid = $validatorInstance->validate_ ( $instance->$accessor () ); |
175
|
1 |
|
if ($valid !== true) { |
176
|
1 |
|
$result [] = $valid; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |
182
|
1 |
|
return $result; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
protected static function validateFromCache_($instance, $members) { |
186
|
|
|
$result = [ ]; |
187
|
|
|
foreach ( $members as $accessor => $validators ) { |
188
|
|
|
foreach ( $validators as $validatorInstance ) { |
189
|
|
|
$valid = $validatorInstance->validate_ ( $instance->$accessor () ); |
190
|
|
|
if ($valid !== true) { |
191
|
|
|
$result [] = $valid; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
return $result; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Initializes the cache (SessionCache) for the class of instance |
200
|
|
|
* |
201
|
|
|
* @param object $instance |
202
|
|
|
* @param string $group |
203
|
|
|
*/ |
204
|
|
|
public static function initCacheInstanceValidators($instance, $group = "") { |
205
|
|
|
$class = get_class ( $instance ); |
206
|
|
|
$members = self::fetch ( $class ); |
207
|
|
|
self::initInstancesValidators ( $instance, $members, $group ); |
208
|
|
|
} |
209
|
|
|
|
210
|
2 |
|
protected static function initInstancesValidators($instance, $members, $group = "") { |
211
|
2 |
|
$class = get_class ( $instance ); |
212
|
2 |
|
$result = [ ]; |
213
|
2 |
|
foreach ( $members as $member => $validators ) { |
214
|
2 |
|
$accessor = "get" . ucfirst ( $member ); |
215
|
2 |
|
if (method_exists ( $instance, $accessor )) { |
216
|
2 |
|
foreach ( $validators as $validator ) { |
217
|
2 |
|
$validatorInstance = self::getValidatorInstance ( $validator ["type"] ); |
218
|
2 |
|
if ($validatorInstance !== false) { |
219
|
2 |
|
$validatorInstance->setValidationParameters ( $member, $validator ["constraints"], @$validator ["severity"], @$validator ["message"] ); |
220
|
2 |
|
if ($group === "" || (isset ( $validator ["group"] ) && $validator ["group"] === $group)) { |
221
|
2 |
|
self::$instanceValidators [$class] [$accessor] [] = $validatorInstance; |
222
|
2 |
|
$result [$accessor] [] = $validatorInstance; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} |
228
|
2 |
|
self::$cache->store ( self::getHash ( $class . $group ), $result ); |
229
|
2 |
|
} |
230
|
|
|
|
231
|
3 |
|
protected static function getHash($class) { |
232
|
3 |
|
return hash ( "sha1", $class ); |
233
|
|
|
} |
234
|
|
|
|
235
|
3 |
|
protected static function getModelCacheKey($classname) { |
236
|
3 |
|
return self::$key . \str_replace ( "\\", \DS, $classname ); |
237
|
|
|
} |
238
|
|
|
|
239
|
3 |
|
protected static function getValidatorInstance($type) { |
240
|
3 |
|
if (isset ( self::$validatorTypes [$type] )) { |
241
|
3 |
|
$class = self::$validatorTypes [$type]; |
242
|
3 |
|
return new $class (); |
243
|
|
|
} else { |
244
|
|
|
Logger::warn ( "validation", "Validator " . $type . " does not exists!" ); |
245
|
|
|
return false; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
|