1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PasswordHelper; |
6
|
|
|
|
7
|
|
|
class Validator |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var Policy Contains password policy logic |
11
|
|
|
*/ |
12
|
|
|
protected $policy; |
13
|
|
|
|
14
|
|
|
public function __construct(Policy $policy) |
15
|
|
|
{ |
16
|
|
|
$this->policy = $policy; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Determines if a password satisfies the password policy |
21
|
|
|
* |
22
|
|
|
* @param string $password |
23
|
|
|
* |
24
|
|
|
* @return bool |
25
|
|
|
*/ |
26
|
|
|
public function isValidPassword(string $password): bool |
27
|
|
|
{ |
28
|
|
|
$password = trim($password); |
29
|
|
|
return $this->meetsMinimumDigits($password) && |
30
|
|
|
$this->meetsMinimumLength($password) && |
31
|
|
|
$this->meetsMinimumLetters($password) && |
32
|
|
|
$this->meetsMinimumUppercase($password) && |
33
|
|
|
$this->meetsMinimumLowercase($password) && |
34
|
|
|
$this->meetsMinimumSpecialChars($password); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Determines if a password meets the minimum password length |
39
|
|
|
* |
40
|
|
|
* @param string $password |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
|
|
protected function meetsMinimumLength(string $password): bool |
45
|
|
|
{ |
46
|
|
|
return strlen($password) >= $this->policy->getMinimumLength(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Determines if a password contains the minimum number of digits |
51
|
|
|
* |
52
|
|
|
* @param string $password |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
protected function meetsMinimumDigits(string $password): bool |
57
|
|
|
{ |
58
|
|
|
if ($this->policy->getMinimumDigits() === 0) { |
59
|
|
|
return true; |
60
|
|
|
} |
61
|
|
|
return preg_match_all('/\d/', $password, $matches) >= $this->policy->getMinimumDigits(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Determines if a password contains the minimum number of letters (any case) |
66
|
|
|
* |
67
|
|
|
* @param string $password |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
protected function meetsMinimumLetters(string $password): bool |
72
|
|
|
{ |
73
|
|
|
if ($this->policy->getMinimumLetters() === 0) { |
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
return preg_match_all('/[a-z]/i', $password, $matches) >= $this->policy->getMinimumDigits(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Determines if a password contains the minimum number of uppercase letters |
81
|
|
|
* |
82
|
|
|
* @param string $password |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
protected function meetsMinimumUppercase(string $password): bool |
87
|
|
|
{ |
88
|
|
|
if ($this->policy->getMinimumUppercase() === 0) { |
89
|
|
|
return true; |
90
|
|
|
} |
91
|
|
|
return preg_match_all('/[A-Z]/', $password, $matches) >= $this->policy->getMinimumDigits(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Determines if a password contains the minimum number of lowercase letters |
96
|
|
|
* |
97
|
|
|
* @param string $password |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
protected function meetsMinimumLowercase(string $password): bool |
102
|
|
|
{ |
103
|
|
|
if ($this->policy->getMinimumLowercase() === 0) { |
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
return preg_match_all('/[a-z]/', $password, $matches) >= $this->policy->getMinimumDigits(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Determines if a password contains the minimum number of special characters |
111
|
|
|
* |
112
|
|
|
* @param string $password |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
protected function meetsMinimumSpecialChars(string $password): bool |
117
|
|
|
{ |
118
|
|
|
if ($this->policy->getMinimumSpecialChars() === 0) { |
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
return preg_match_all('/[^a-z\d ]/i', $password, $matches) >= $this->policy->getMinimumDigits(); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|