|
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
|
|
|
public static $validatorTypes=[ |
|
28
|
|
|
"notNull"=>NotNullValidator::class, |
|
29
|
|
|
"isNull"=>IsNullValidator::class, |
|
30
|
|
|
"notEmpty"=>NotEmptyValidator::class, |
|
31
|
|
|
"isEmpty"=>IsEmptyValidator::class, |
|
32
|
|
|
"isTrue"=>IsTrueValidator::class, |
|
33
|
|
|
"isFalse"=>IsFalseValidator::class, |
|
34
|
|
|
"equals"=>EqualsValidator::class, |
|
35
|
|
|
"type"=>TypeValidator::class, |
|
36
|
|
|
"greaterThan"=>GreaterThanValidator::class, |
|
37
|
|
|
"lessThan"=>LessThanValidator::class, |
|
38
|
|
|
"length"=>LengthValidator::class, |
|
39
|
|
|
"id"=>IdValidator::class |
|
40
|
|
|
|
|
41
|
|
|
]; |
|
42
|
|
|
protected static $key="contents/validators/"; |
|
43
|
|
|
|
|
44
|
|
|
public static function registerType($type,$validatorClass){ |
|
45
|
|
|
self::$validatorTypes[$type]=$validatorClass; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public static function initModelsValidators(&$config){ |
|
49
|
|
|
$models=CacheManager::getModels($config,true); |
|
50
|
|
|
foreach ($models as $model){ |
|
51
|
|
|
$parser=new ValidationModelParser(); |
|
52
|
|
|
$parser->parse($model); |
|
53
|
|
|
$validators=$parser->getValidators(); |
|
54
|
|
|
if(sizeof($validators)>0){ |
|
55
|
|
|
self::store($model, $parser->__toString()); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected static function store($model,$validators){ |
|
61
|
|
|
CacheManager::$cache->store(self::getModelCacheKey($model), $validators); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected static function fetch($model){ |
|
65
|
|
|
$key=self::getModelCacheKey($model); |
|
66
|
|
|
if(CacheManager::$cache->exists($key)){ |
|
67
|
|
|
return CacheManager::$cache->fetch($key); |
|
68
|
|
|
} |
|
69
|
|
|
return []; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public static function validate($instance){ |
|
73
|
|
|
$result=[]; |
|
74
|
|
|
$members=self::fetch(get_class($instance)); |
|
75
|
|
|
foreach ($members as $member=>$validators){ |
|
76
|
|
|
$accessor="get".ucfirst($member); |
|
77
|
|
|
if(method_exists($instance, $accessor)){ |
|
78
|
|
|
foreach ($validators as $validator){ |
|
79
|
|
|
$validatorInstance=self::getValidatorInstance($validator["type"]); |
|
80
|
|
|
if($validatorInstance!==false){ |
|
81
|
|
|
$valid=$validatorInstance->validate_($instance->$accessor(),$member,$instance,$validator["constraints"],@$validator["severity"],@$validator["message"]); |
|
82
|
|
|
if($valid!==true){ |
|
83
|
|
|
$result[]=$valid; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
return $result; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected static function getModelCacheKey($classname){ |
|
93
|
|
|
return self::$key.\str_replace("\\", DIRECTORY_SEPARATOR, $classname); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
protected static function getValidatorInstance($type){ |
|
97
|
|
|
if(!isset(self::$validatorInstances[$type])){ |
|
98
|
|
|
if(isset(self::$validatorTypes[$type])){ |
|
99
|
|
|
$class=self::$validatorTypes[$type]; |
|
100
|
|
|
self::$validatorInstances[$type]=new $class(); |
|
101
|
|
|
}else{ |
|
102
|
|
|
Logger::warn("validation", "Validator ".$type." does not exists!"); |
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
return self::$validatorInstances[$type]; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
|