|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\contents\validation; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\cache\CacheManager; |
|
6
|
|
|
use Ubiquity\log\Logger; |
|
7
|
|
|
use Ubiquity\contents\validation\validators\multiples\LengthValidator; |
|
8
|
|
|
use Ubiquity\contents\validation\validators\multiples\IdValidator; |
|
9
|
|
|
use Ubiquity\contents\validation\validators\basic\NotNullValidator; |
|
10
|
|
|
use Ubiquity\contents\validation\validators\basic\NotEmptyValidator; |
|
11
|
|
|
use Ubiquity\contents\validation\validators\comparison\EqualsValidator; |
|
12
|
|
|
use Ubiquity\contents\validation\validators\basic\TypeValidator; |
|
13
|
|
|
use Ubiquity\contents\validation\validators\comparison\GreaterThanValidator; |
|
14
|
|
|
use Ubiquity\contents\validation\validators\comparison\LessThanValidator; |
|
15
|
|
|
use Ubiquity\contents\validation\validators\basic\IsNullValidator; |
|
16
|
|
|
use Ubiquity\contents\validation\validators\basic\IsEmptyValidator; |
|
17
|
|
|
use Ubiquity\contents\validation\validators\basic\IsTrueValidator; |
|
18
|
|
|
use Ubiquity\contents\validation\validators\basic\IsFalseValidator; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author jc |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
class ValidatorsManager { |
|
25
|
|
|
|
|
26
|
|
|
protected static $validatorInstances=[]; |
|
27
|
|
|
|
|
28
|
|
|
public static $validatorTypes=[ |
|
29
|
|
|
"notNull"=>NotNullValidator::class, |
|
30
|
|
|
"isNull"=>IsNullValidator::class, |
|
31
|
|
|
"notEmpty"=>NotEmptyValidator::class, |
|
32
|
|
|
"isEmpty"=>IsEmptyValidator::class, |
|
33
|
|
|
"isTrue"=>IsTrueValidator::class, |
|
34
|
|
|
"isFalse"=>IsFalseValidator::class, |
|
35
|
|
|
"equals"=>EqualsValidator::class, |
|
36
|
|
|
"type"=>TypeValidator::class, |
|
37
|
|
|
"greaterThan"=>GreaterThanValidator::class, |
|
38
|
|
|
"lessThan"=>LessThanValidator::class, |
|
39
|
|
|
"length"=>LengthValidator::class, |
|
40
|
|
|
"id"=>IdValidator::class |
|
41
|
|
|
|
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
|
|
protected static $key="contents/validators/"; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Registers a validator type for using with @validator annotation |
|
48
|
|
|
* @param string $type |
|
49
|
|
|
* @param string $validatorClass |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function registerType($type,$validatorClass){ |
|
52
|
|
|
self::$validatorTypes[$type]=$validatorClass; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Parses models and save validators in cache |
|
57
|
|
|
* to use in dev only |
|
58
|
|
|
* @param array $config |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function initModelsValidators(&$config){ |
|
61
|
|
|
$models=CacheManager::getModels($config,true); |
|
62
|
|
|
foreach ($models as $model){ |
|
63
|
|
|
$parser=new ValidationModelParser(); |
|
64
|
|
|
$parser->parse($model); |
|
65
|
|
|
$validators=$parser->getValidators(); |
|
66
|
|
|
if(sizeof($validators)>0){ |
|
67
|
|
|
self::store($model, $parser->__toString()); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected static function store($model,$validators){ |
|
73
|
|
|
CacheManager::$cache->store(self::getModelCacheKey($model), $validators); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
protected static function fetch($model){ |
|
77
|
|
|
$key=self::getModelCacheKey($model); |
|
78
|
|
|
if(CacheManager::$cache->exists($key)){ |
|
79
|
|
|
return CacheManager::$cache->fetch($key); |
|
80
|
|
|
} |
|
81
|
|
|
return []; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected static function getGroupValidators(array $validators,$group){ |
|
85
|
|
|
$result=[]; |
|
86
|
|
|
foreach ($validators as $member=>$validators){ |
|
87
|
|
|
$filteredValidators=self::getGroupMemberValidators($validators, $group); |
|
88
|
|
|
if(sizeof($filteredValidators)){ |
|
89
|
|
|
$result[$member]=$filteredValidators; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
return $result; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
protected static function getGroupMemberValidators(array $validators,$group){ |
|
96
|
|
|
$result=[]; |
|
97
|
|
|
foreach ($validators as $validator){ |
|
98
|
|
|
if(isset($validator["group"]) && $validator["group"]===$group){ |
|
99
|
|
|
$result[]=$validator; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
return $result; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Validates an instance |
|
107
|
|
|
* @param object $instance |
|
108
|
|
|
* @return \Ubiquity\contents\validation\validators\ConstraintViolation[] |
|
109
|
|
|
*/ |
|
110
|
|
|
public static function validate($instance){ |
|
111
|
|
|
return self::validate_($instance,self::fetch(get_class($instance))); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Validates an instance using a group of validators |
|
116
|
|
|
* @param object $instance |
|
117
|
|
|
* @param string $group |
|
118
|
|
|
* @return \Ubiquity\contents\validation\validators\ConstraintViolation[] |
|
119
|
|
|
*/ |
|
120
|
|
|
public static function validateGroup($instance,$group){ |
|
121
|
|
|
$members=self::getGroupValidators(self::fetch(get_class($instance)), $group); |
|
122
|
|
|
return self::validate_($instance,$members); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
protected static function validate_($instance,$members){ |
|
126
|
|
|
$result=[]; |
|
127
|
|
|
foreach ($members as $member=>$validators){ |
|
128
|
|
|
$accessor="get".ucfirst($member); |
|
129
|
|
|
if(method_exists($instance, $accessor)){ |
|
130
|
|
|
foreach ($validators as $validator){ |
|
131
|
|
|
$validatorInstance=self::getValidatorInstance($validator["type"]); |
|
132
|
|
|
if($validatorInstance!==false){ |
|
133
|
|
|
$valid=$validatorInstance->validate_($instance->$accessor(),$member,$validator["constraints"],@$validator["severity"],@$validator["message"]); |
|
134
|
|
|
if($valid!==true){ |
|
135
|
|
|
$result[]=$valid; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
return $result; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
protected static function getModelCacheKey($classname){ |
|
145
|
|
|
return self::$key.\str_replace("\\", DIRECTORY_SEPARATOR, $classname); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
protected static function getValidatorInstance($type){ |
|
149
|
|
|
if(!isset(self::$validatorInstances[$type])){ |
|
150
|
|
|
if(isset(self::$validatorTypes[$type])){ |
|
151
|
|
|
$class=self::$validatorTypes[$type]; |
|
152
|
|
|
self::$validatorInstances[$type]=new $class(); |
|
153
|
|
|
}else{ |
|
154
|
|
|
Logger::warn("validation", "Validator ".$type." does not exists!"); |
|
155
|
|
|
return false; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
return self::$validatorInstances[$type]; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
|