Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ValidatorsManager.php
Go to the documentation of this file.
1<?php
2
4
34
46 protected static $instanceValidators = [ ];
47
48 public static function start() {
49 self::$cache = new SessionCache();
50 }
51 public static $validatorTypes = [
52 'notNull' => NotNullValidator::class,
53 'isNull' => IsNullValidator::class,
54 'notEmpty' => NotEmptyValidator::class,
55 'isEmpty' => IsEmptyValidator::class,
56 'isTrue' => IsTrueValidator::class,
57 'isFalse' => IsFalseValidator::class,
58 'isBool' => IsBooleanValidator::class,
59 'equals' => EqualsValidator::class,
60 'type' => TypeValidator::class,
61 'greaterThan' => GreaterThanValidator::class,
62 'greaterThanOrEqual' => GreaterThanOrEqualValidator::class,
63 'lessThan' => LessThanValidator::class,
64 'lessThanOrEqual' => LessThanOrEqualValidator::class,
65 'length' => LengthValidator::class,
66 'id' => IdValidator::class,
67 'regex' => RegexValidator::class,
68 'email' => EmailValidator::class,
69 'url' => UrlValidator::class,
70 'ip' => IpValidator::class,
71 'range' => RangeValidator::class,
72 'date' => DateValidator::class,
73 'dateTime' => DateTimeValidator::class,
74 'time' => TimeValidator::class,
75 'match'=>MatchWithValidator::class
76 ];
77
78
79
80 public static function getCacheInfo($model) {
81 return self::fetch( $model );
82 }
83
84 protected static function getGroupArrayValidators(array $validators, $group) {
85 $result = [ ];
86 foreach ( $validators as $member => $validators ) {
87 $filteredValidators = self::getGroupMemberValidators ( $validators, $group );
88 if (\count ( $filteredValidators ) > 0) {
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
114 public static function validate($instance, $group = '', $excludedValidators = [ ]) {
115 $class = \get_class ( $instance );
116 $cache = self::getClassCacheValidators ( $class, $group );
117 if ($cache !== false) {
118 return self::validateFromCache_ ( $instance, $cache, $excludedValidators );
119 }
120 $members = self::fetch ( $class );
121 if ($group !== '') {
122 $members = self::getGroupArrayValidators ( $members, $group );
123 }
124 return self::validate_ ( $instance, $members, $excludedValidators );
125 }
126
134 public static function getUIConstraints($instance, $group = '', $excludedValidators = [ ]) {
135 $class = \get_class ( $instance );
136 $cache = self::getClassCacheValidators ( $class, $group );
137 if ($cache !== false) {
138 return self::getUIConstraintsFromCache_ ( $instance, $cache, $excludedValidators );
139 }
140 $members = self::fetch ( $class );
141 if ($group !== '') {
142 $members = self::getGroupArrayValidators ( $members, $group );
143 }
144 return self::getUIConstraints_ ( $instance, $members, $excludedValidators );
145 }
146
154 public static function validateInstances($instances, $group = '') {
155 if (\count ( $instances ) > 0) {
156 $instance = \current ( $instances );
157 $class = \get_class ( $instance );
158 $cache = self::getClassCacheValidators ( $class, $group );
159 if ($cache === false) {
160 $members = self::fetch ( $class );
161 self::initInstancesValidators ( $instance, $members, $group );
162 $cache = self::$instanceValidators [$class];
163 }
164 return self::validateInstances_ ( $instances, $cache );
165 }
166 return [ ];
167 }
168
169 protected static function validateInstances_($instances, $members) {
170 $result = [ ];
171 foreach ( $instances as $instance ) {
172 foreach ( $members as $accessor => $validators ) {
173 foreach ( $validators as $validator ) {
174 $valid = $validator->validate_ ( $instance->$accessor () );
175 if ($valid !== true) {
176 $result [] = $valid;
177 }
178 }
179 }
180 }
181 return $result;
182 }
183
184 protected static function validate_($instance, $members, $excludedValidators = [ ]) {
185 $result = [ ];
186 foreach ( $members as $member => $validators ) {
187 $accessor = 'get' . \ucfirst ( $member );
188 if (\method_exists ( $instance, $accessor )) {
189 foreach ( $validators as $validator ) {
190 $typeV = $validator ['type'];
191 if (! isset ( $excludedValidators [$typeV] )) {
192 $validatorInstance = self::getValidatorInstance ( $typeV );
193 if ($validatorInstance !== false) {
194 $validatorInstance->setValidationParameters ( $member, $validator ['constraints'] ?? [ ], $validator ['severity'] ?? null, $validator ['message'] ?? null);
195 $valid = $validatorInstance->validate_ ( $instance->$accessor () );
196 if ($valid !== true) {
197 $result [] = $valid;
198 }
199 }
200 }
201 }
202 }
203 }
204 return $result;
205 }
206
207 protected static function getUIConstraints_($instance, $members, $excludedValidators = [ ]) {
208 $result = [ ];
209 foreach ( $members as $member => $validators ) {
210 $result [$member]=[];
211 $accessor = 'get' . \ucfirst ( $member );
212 if (\method_exists ( $instance, $accessor )) {
213 foreach ( $validators as $validator ) {
214 $typeV = $validator ['type'];
215 if (! isset ( $excludedValidators [$typeV] )) {
216 $validatorInstance = self::getValidatorInstance ( $typeV );
217 if ($validatorInstance !== false) {
218 $validatorInstance->setValidationParameters ( $member, $validator ['constraints'] ?? [ ], $validator ['severity'] ?? null, $validator ['message'] ?? null);
219 $result [$member] = \array_merge_recursive($result[$member],$validatorInstance->asUI ());
220 }
221 }
222 }
223 }
224 }
225 return $result;
226 }
227}
228
Manager for caches (Router, Rest, models).
static getGroupMemberValidators(array $validators, $group)
static validate($instance, $group='', $excludedValidators=[])
Validates an instance.
static validateInstances($instances, $group='')
Validates an array of objects.
static validate_($instance, $members, $excludedValidators=[])
static getUIConstraints_($instance, $members, $excludedValidators=[])
static getUIConstraints($instance, $group='', $excludedValidators=[])
Returns an array of UI rules for Javascript validation.
static getGroupArrayValidators(array $validators, $group)
static getUIConstraintsFromCache_($instance, $members, $excludedValidators=[])
static validateFromCache_($instance, $members, $excludedValidators=[])
Ubiquity\contents\validation\validators\comparison$EqualsValidator This class is part of Ubiquity.
Ubiquity\contents\validation\validators\comparison$GreaterThanValidator This class is part of Ubiquit...
Ubiquity\contents\validation\validators\comparison$LessThanOrEqualValidator This class is part of Ubi...
Ubiquity\contents\validation\validators\comparison$MatchWithValidator This class is part of Ubiquity.
Validate int identifiers (notNull positive integer).
Validate Strings length using min, max, charset,notNull parameters.
Validates an email address Usage @validator("email")
Validates an address ip Usage @validator("ip","4") Inspired from Bernhard Schussek Symfony IpValidato...
Validates a string with a regex Usage @validator("regex",pattern)
Validates an URL Usage @validator("url","constraints"=>["relative"=>false,"protocols"=>["http",...
Abstract class for logging Ubiquity\log$Logger This class is part of Ubiquity.
Definition Logger.php:14
Validation managment.