Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
LengthValidator.php
Go to the documentation of this file.
1<?php
2
4
7
20 protected $min;
21 protected $max;
22 protected $charset = 'UTF-8';
23
24 public function __construct() {
25 parent::__construct ();
26 $this->message = \array_merge ( $this->message, [ 'max' => 'This value must be at least {min} characters long','min' => 'This value cannot be longer than {max} characters','exact' => 'This value should have exactly {min} characters.',"charset" => "This value is not in {charset} charset" ] );
27 }
28
29 public function validate($value) {
30 if (parent::validate ( $value ) === false) {
31 return false;
32 }
33 if (! is_scalar ( $value ) && ! (\is_object ( $value ) && method_exists ( $value, '__toString' ))) {
34 Logger::warn ( 'Validation', 'This value is not valid string for the charset ' . $this->charset );
35 return true;
36 }
37 return $this->validateValue ( ( string ) $value );
38 }
39
40 protected function validateValue($stringValue) {
41 if (@mb_check_encoding ( $stringValue, $this->charset )) {
42 $length = mb_strlen ( $stringValue, $this->charset );
43 if ($this->min === $this->max && $this->min !== null && $length !== $this->max) {
44 $this->violation = 'exact';
45 return false;
46 } elseif ($this->max !== null && $length > $this->max) {
47 $this->violation = 'max';
48 return false;
49 } elseif ($this->min !== null && $length < $this->min) {
50 $this->violation = 'min';
51 return false;
52 }
53 return true;
54 } else {
55 $this->violation = 'charset';
56 return false;
57 }
58 }
59
65 public function getParameters(): array {
66 return [ 'min','max','charset','value' ];
67 }
68
74 public function asUI(): array {
75 $rules=[];
76 if ($this->min === $this->max && $this->min !== null) {
77 $rules[] = ['type' => 'exactLength', 'prompt' => $this->_getMessage()['exact'], 'value' => $this->min];
78 }else {
79 if (isset($this->min)) {
80 $rules[] = ['type' => 'minLength', 'prompt' => $this->_getMessage()['min'], 'value' => $this->min];
81 }
82 if (isset($this->max)) {
83 $rules[] = ['type' => 'maxLength', 'prompt' => $this->_getMessage()['max'], 'value' => $this->max];
84 }
85 }
86 return \array_merge_recursive(parent::asUI () , ['rules' => $rules]);
87 }
88}
89
Validate Strings length using min, max, charset,notNull parameters.
getParameters()
{{}\Ubiquity\contents\validation\validators\ValidatorInterfacegetParameters()}
Abstract class for logging Ubiquity\log$Logger This class is part of Ubiquity.
Definition Logger.php:14