Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
PasswordValidator.php
Go to the documentation of this file.
1<?php
3
5
17
18 protected $min;
19
20 protected $max;
21
22 protected $upperCase;
23
24 protected $numeric;
25
26 protected $specialChar;
27
28 protected $charset = 'UTF-8';
29
30 protected const PASSWORD_CONSTRAINTS = [
31 'upperCase',
32 'numeric',
33 'specialChar'
34 ];
35
36 public function __construct() {
37 parent::__construct();
38 $this->message = array_merge($this->message, [
39 'max' => 'This value cannot be longer than {max} characters.',
40 'min' => 'This value should have at least {min} characters.',
41 'charset' => 'This value is not in {charset} charset.',
42 'upperCase' => 'This value must contain at least {upperCase} uppercase characters.',
43 'numeric' => 'This value must contain at least {numeric} numeric characters.',
44 'specialChar' => 'This value must contain at least {specialChar} special characters.'
45 ]);
46 }
47
48 protected function getCallbacks() {
49 return [
50 'upperCase' => function ($c) {
51 return \ctype_alpha($c) && \strtoupper($c) === $c;
52 },
53 'numeric' => function ($c) {
54 return \is_numeric($c);
55 },
56 'specialChar' => function ($c) {
57 return \strpos("!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~", $c) !== false;
58 }
59 ];
60 }
61
62 protected function getLengths($string) {
63 $result = \array_combine(self::PASSWORD_CONSTRAINTS, \array_fill(0, \count(self::PASSWORD_CONSTRAINTS), 0));
64 $size = \strlen($string);
65 $callbacks = $this->getCallbacks();
66 for ($i = 0; $i < $size; $i ++) {
67 $c = $string[$i];
68 foreach ($callbacks as $key => $callback) {
69 if ($callback($c)) {
70 $result[$key] ++;
71 }
72 }
73 }
74 return $result;
75 }
76
77 protected function validateValue($stringValue) {
78 $result = parent::validateValue($stringValue);
79 if (! $result) {
80 return $result;
81 }
82 $lengths = $this->getLengths($stringValue);
83 foreach (self::PASSWORD_CONSTRAINTS as $constraint) {
84 if ($this->$constraint && $lengths[$constraint] < $this->$constraint) {
85 $this->violation = $constraint;
86 return false;
87 }
88 }
89 return true;
90 }
91
97 public function getParameters(): array {
98 return \array_merge(parent::getParameters(), self::PASSWORD_CONSTRAINTS);
99 }
100}
101
Validate Strings length using min, max, charset,notNull parameters.
Validates a password Usage @validator("password","constraints"=>["min"=>v,"max"=>v,...
getParameters()
{{{}\Ubiquity\contents\validation\validators\ValidatorInterfacegetParameters()}\Ubiquity\contents\val...