Ubiquity
2.5.2
php rapid development framework
Loading...
Searching...
No Matches
PasswordValidator.php
Go to the documentation of this file.
1
<?php
2
namespace
Ubiquity\security\auth\user
;
3
4
use
Ubiquity\contents\validation\validators\multiples\LengthValidator
;
5
16
class
PasswordValidator
extends
LengthValidator
{
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
Ubiquity\contents\validation\validators\multiples\LengthValidator
Validate Strings length using min, max, charset,notNull parameters.
Definition
LengthValidator.php:19
Ubiquity\security\auth\user\PasswordValidator
Validates a password Usage @validator("password","constraints"=>["min"=>v,"max"=>v,...
Definition
PasswordValidator.php:16
Ubiquity\security\auth\user\PasswordValidator\getParameters
getParameters()
{{{}\Ubiquity\contents\validation\validators\ValidatorInterfacegetParameters()}\Ubiquity\contents\val...
Definition
PasswordValidator.php:97
Ubiquity\security\auth\user\PasswordValidator\__construct
__construct()
Definition
PasswordValidator.php:36
Ubiquity\security\auth\user\PasswordValidator\getLengths
getLengths($string)
Definition
PasswordValidator.php:62
Ubiquity\security\auth\user\PasswordValidator\$min
$min
Definition
PasswordValidator.php:18
Ubiquity\security\auth\user\PasswordValidator\$upperCase
$upperCase
Definition
PasswordValidator.php:22
Ubiquity\security\auth\user\PasswordValidator\$specialChar
$specialChar
Definition
PasswordValidator.php:26
Ubiquity\security\auth\user\PasswordValidator\getCallbacks
getCallbacks()
Definition
PasswordValidator.php:48
Ubiquity\security\auth\user\PasswordValidator\$max
$max
Definition
PasswordValidator.php:20
Ubiquity\security\auth\user\PasswordValidator\validateValue
validateValue($stringValue)
Definition
PasswordValidator.php:77
Ubiquity\security\auth\user\PasswordValidator\PASSWORD_CONSTRAINTS
const PASSWORD_CONSTRAINTS
Definition
PasswordValidator.php:30
Ubiquity\security\auth\user\PasswordValidator\$charset
$charset
Definition
PasswordValidator.php:28
Ubiquity\security\auth\user\PasswordValidator\$numeric
$numeric
Definition
PasswordValidator.php:24
Ubiquity\security\auth\user
Definition
PasswordValidator.php:2
C:
Users
myadd
Documents
GitHub
ubiquity-doc-api
vendor
phpmv
ubiquity-security
src
Ubiquity
security
auth
user
PasswordValidator.php
Generated on Sun Dec 10 2023 15:04:09 for
Ubiquity
by
doxygen
1.9.8