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