Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ValidatorsManagerCacheTrait.php
Go to the documentation of this file.
1<?php
2
4
7
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 protected static function getClassCacheValidators($class, $group = '') {
29 if (isset ( self::$cache )) {
30 $key = self::getHash ( $class . $group );
31 if (self::$cache->exists ( $key )) {
32 return self::$cache->fetch ( $key );
33 }
34 }
35 return false;
36 }
37
38 protected static function getHash($class) {
39 return \hash ( 'sha1', $class );
40 }
41
42 protected static function getModelCacheKey($classname) {
43 return self::$key . \str_replace ( '\\', \DS, $classname );
44 }
45
46 protected static function store($model, $validators) {
47 CacheManager::$cache->store ( self::getModelCacheKey ( $model ), $validators, 'validators' );
48 }
49
50 protected static function fetch($model) {
51 $key = self::getModelCacheKey ( $model );
52 if (CacheManager::$cache->exists ( $key )) {
53 return CacheManager::$cache->fetch ( $key );
54 }
55 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
107 public static function initCacheInstanceValidators($instance, $group = '') {
108 $class = \get_class ( $instance );
109 $members = self::fetch ( $class );
110 self::initInstancesValidators ( $instance, $members, $group );
111 }
112
113 protected static function initInstancesValidators($instance, $members, $group = '') {
114 $class = \get_class ( $instance );
115 $result = [ ];
116 foreach ( $members as $member => $validators ) {
117 $accessor = 'get' . \ucfirst ( $member );
118 if (\method_exists ( $instance, $accessor )) {
119 foreach ( $validators as $validator ) {
120 $validatorInstance = self::getValidatorInstance ( $validator ['type'] );
121 if ($validatorInstance !== false) {
122 $validatorInstance->setValidationParameters ( $member, $validator ['constraints'] ?? [ ], $validator ['severity'] ?? null, $validator ['message'] ?? null);
123 if ($group === '' || (isset ( $validator ['group'] ) && $validator ['group'] === $group)) {
124 self::$instanceValidators [$class] [$accessor] [] = $validatorInstance;
125 $result [$accessor] [] = $validatorInstance;
126 }
127 }
128 }
129 }
130 }
131 self::$cache->store ( self::getHash ( $class . $group ), $result );
132 }
133
134 protected static function getValidatorInstance($type) {
135 if (isset ( self::$validatorTypes [$type] )) {
136 $class = self::$validatorTypes [$type];
137 return new $class ();
138 } else {
139 Logger::warn ( 'validation', "Validator $type does not exists!" );
140 return false;
141 }
142 }
143}
144
Manager for caches (Router, Rest, models).
static getUIConstraintsFromCache_($instance, $members, $excludedValidators=[])
static initCacheInstanceValidators($instance, $group='')
Initializes the cache (SessionCache) for the class of instance.
static validateFromCache_($instance, $members, $excludedValidators=[])
Abstract class for logging Ubiquity\log$Logger This class is part of Ubiquity.
Definition Logger.php:14