1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\contents\validation\traits; |
4
|
|
|
|
5
|
|
|
use Ubiquity\cache\CacheManager; |
6
|
|
|
use Ubiquity\log\Logger; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Ubiquity\contents\validation\traits$ValidatorsManagerCacheTrait |
10
|
|
|
* This class is part of Ubiquity |
11
|
|
|
* @author jc |
12
|
|
|
* @version 1.0.0 |
13
|
|
|
* |
14
|
|
|
* @property array $validatorTypes |
15
|
|
|
* @property array $instanceValidators |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
trait ValidatorsManagerCacheTrait { |
19
|
|
|
|
20
|
|
|
protected static $cache; |
21
|
|
|
|
22
|
|
|
protected static $key = 'contents/validators/'; |
23
|
|
|
|
24
|
|
|
private static function getCacheValidators($instance, $group = '') { |
25
|
|
|
return self::getClassCacheValidators ( \get_class ( $instance ), $group ); |
26
|
|
|
} |
27
|
|
|
|
28
|
9 |
|
protected static function getClassCacheValidators($class, $group = '') { |
29
|
9 |
|
if (isset ( self::$cache )) { |
30
|
4 |
|
$key = self::getHash ( $class . $group ); |
31
|
4 |
|
if (self::$cache->exists ( $key )) { |
32
|
|
|
return self::$cache->fetch ( $key ); |
33
|
|
|
} |
34
|
|
|
} |
35
|
9 |
|
return false; |
36
|
|
|
} |
37
|
|
|
|
38
|
5 |
|
protected static function getHash($class) { |
39
|
5 |
|
return \hash ( 'sha1', $class ); |
40
|
|
|
} |
41
|
|
|
|
42
|
14 |
|
protected static function getModelCacheKey($classname) { |
43
|
14 |
|
return self::$key . \str_replace ( '\\', \DS, $classname ); |
44
|
|
|
} |
45
|
|
|
|
46
|
4 |
|
protected static function store($model, $validators) { |
47
|
4 |
|
CacheManager::$cache->store ( self::getModelCacheKey ( $model ), $validators, 'validators' ); |
48
|
|
|
} |
49
|
|
|
|
50
|
10 |
|
protected static function fetch($model) { |
51
|
10 |
|
$key = self::getModelCacheKey ( $model ); |
52
|
10 |
|
if (CacheManager::$cache->exists ( $key )) { |
53
|
9 |
|
return CacheManager::$cache->fetch ( $key ); |
54
|
|
|
} |
55
|
1 |
|
return [ ]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function clearCache($model = null, $group = '') { |
59
|
|
|
if (isset ( self::$cache )) { |
60
|
|
|
if (isset ( $model )) { |
61
|
|
|
$key = self::getHash ( $model . $group ); |
62
|
|
|
self::$cache->remove ( $key ); |
63
|
|
|
} else { |
64
|
|
|
self::$cache->clear (); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected static function validateFromCache_($instance, $members, $excludedValidators = [ ]) { |
70
|
|
|
$result = [ ]; |
71
|
|
|
$types = \array_flip ( self::$validatorTypes ); |
72
|
|
|
foreach ( $members as $accessor => $validators ) { |
73
|
|
|
foreach ( $validators as $validatorInstance ) { |
74
|
|
|
$typeV = $types [get_class ( $validatorInstance )]; |
75
|
|
|
if (! isset ( $excludedValidators [$typeV] )) { |
76
|
|
|
$valid = $validatorInstance->validate_ ( $instance->$accessor () ); |
77
|
|
|
if ($valid !== true) { |
78
|
|
|
$result [] = $valid; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
return $result; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected static function getUIConstraintsFromCache_($instance, $members, $excludedValidators = [ ]) { |
87
|
|
|
$result = [ ]; |
88
|
|
|
$types = \array_flip ( self::$validatorTypes ); |
89
|
|
|
foreach ( $members as $accessor => $validators ) { |
90
|
|
|
$member = \lcfirst ( \ltrim ( 'get', $accessor ) ); |
91
|
|
|
foreach ( $validators as $validatorInstance ) { |
92
|
|
|
$typeV = $types [get_class ( $validatorInstance )]; |
93
|
|
|
if (! isset ( $excludedValidators [$typeV] )) { |
94
|
|
|
$result [$member] += $validatorInstance->asUI (); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
return $result; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Initializes the cache (SessionCache) for the class of instance |
103
|
|
|
* |
104
|
|
|
* @param object $instance |
105
|
|
|
* @param string $group |
106
|
|
|
*/ |
107
|
1 |
|
public static function initCacheInstanceValidators($instance, $group = '') { |
108
|
1 |
|
$class = \get_class ( $instance ); |
109
|
1 |
|
$members = self::fetch ( $class ); |
110
|
1 |
|
self::initInstancesValidators ( $instance, $members, $group ); |
111
|
|
|
} |
112
|
|
|
|
113
|
3 |
|
protected static function initInstancesValidators($instance, $members, $group = '') { |
114
|
3 |
|
$class = \get_class ( $instance ); |
115
|
3 |
|
$result = [ ]; |
116
|
3 |
|
foreach ( $members as $member => $validators ) { |
117
|
3 |
|
$accessor = 'get' . \ucfirst ( $member ); |
118
|
3 |
|
if (\method_exists ( $instance, $accessor )) { |
119
|
3 |
|
foreach ( $validators as $validator ) { |
120
|
3 |
|
$validatorInstance = self::getValidatorInstance ( $validator ['type'] ); |
121
|
3 |
|
if ($validatorInstance !== false) { |
122
|
3 |
|
$validatorInstance->setValidationParameters ( $member, $validator ['constraints'] ?? [ ], $validator ['severity'] ?? null, $validator ['message'] ?? null); |
123
|
3 |
|
if ($group === '' || (isset ( $validator ['group'] ) && $validator ['group'] === $group)) { |
124
|
3 |
|
self::$instanceValidators [$class] [$accessor] [] = $validatorInstance; |
125
|
3 |
|
$result [$accessor] [] = $validatorInstance; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
3 |
|
self::$cache->store ( self::getHash ( $class . $group ), $result ); |
132
|
|
|
} |
133
|
|
|
|
134
|
9 |
|
protected static function getValidatorInstance($type) { |
135
|
9 |
|
if (isset ( self::$validatorTypes [$type] )) { |
136
|
9 |
|
$class = self::$validatorTypes [$type]; |
137
|
9 |
|
return new $class (); |
138
|
|
|
} else { |
139
|
|
|
Logger::warn ( 'validation', "Validator $type does not exists!" ); |
140
|
|
|
return false; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
|