1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PasswordHelper; |
6
|
|
|
|
7
|
|
|
class Policy |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var int default minimum number of numbers |
11
|
|
|
*/ |
12
|
|
|
public const MINIMUM_DIGITS = 1; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var int default minimum password length |
16
|
|
|
*/ |
17
|
|
|
public const MINIMUM_LENGTH = 10; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var int default minimum number of letters |
21
|
|
|
*/ |
22
|
|
|
public const MINIMUM_LETTERS = 1; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var int default minimum number of lowercase letters |
26
|
|
|
*/ |
27
|
|
|
public const MINIMUM_LOWERCASE = 1; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int default minimum number of special characters |
31
|
|
|
*/ |
32
|
|
|
public const MINIMUM_SPECIAL_CHARS = 1; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var int default minimum number of uppercase letters |
36
|
|
|
*/ |
37
|
|
|
public const MINIMUM_UPPERCASE = 1; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var int default minimum number of numbers |
41
|
|
|
*/ |
42
|
|
|
protected $minimumDigits; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var int default minimum password length |
46
|
|
|
*/ |
47
|
|
|
protected $minimumLength; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var int default minimum number of letters |
51
|
|
|
*/ |
52
|
|
|
protected $minimumLetters; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var int minimum number of lowercase letters |
56
|
|
|
*/ |
57
|
|
|
protected $minimumLowercase; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var int minimum number of special characters |
61
|
|
|
*/ |
62
|
|
|
protected $minimumSpecialChars; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var int minimum number of uppercase letters |
66
|
|
|
*/ |
67
|
|
|
protected $minimumUppercase; |
68
|
|
|
|
69
|
|
|
public function __construct(array $config = []) |
70
|
|
|
{ |
71
|
|
|
$this->minimumDigits = abs((int) ($config['minimumDigits'] ?? self::MINIMUM_DIGITS )); |
72
|
|
|
$this->minimumLowercase = abs((int) ($config['minimumLowercase'] ?? self::MINIMUM_LOWERCASE )); |
73
|
|
|
$this->minimumSpecialChars = abs((int) ($config['minimumSpecialChars'] ?? self::MINIMUM_SPECIAL_CHARS)); |
74
|
|
|
$this->minimumUppercase = abs((int) ($config['minimumUppercase'] ?? self::MINIMUM_UPPERCASE )); |
75
|
|
|
|
76
|
|
|
$minimumLetters = abs((int) ($config['minimumLetters'] ?? self::MINIMUM_LETTERS )); |
77
|
|
|
$this->minimumLetters = max([$minimumLetters, $this->minimumLowercase + $this->minimumUppercase]); |
78
|
|
|
|
79
|
|
|
$minimumLength = abs((int) ($config['minimumLength'] ?? self::MINIMUM_LENGTH )); |
80
|
|
|
$this->minimumLength = max([ |
81
|
|
|
$minimumLength, |
82
|
|
|
$this->minimumLetters + $this->minimumDigits + $this->minimumSpecialChars |
83
|
|
|
]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns the minimum number of numbers required in a password |
88
|
|
|
* |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
|
|
public function getMinimumDigits(): int |
92
|
|
|
{ |
93
|
|
|
return $this->minimumDigits; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns the minimum number of characters required in a password |
98
|
|
|
* |
99
|
|
|
* @return int |
100
|
|
|
*/ |
101
|
|
|
public function getMinimumLength(): int |
102
|
|
|
{ |
103
|
|
|
return $this->minimumLength; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns the minimum number of letters required in a password |
108
|
|
|
* |
109
|
|
|
* @return int |
110
|
|
|
*/ |
111
|
|
|
public function getMinimumLetters(): int |
112
|
|
|
{ |
113
|
|
|
return $this->minimumLetters; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns the minimum number of lowercase letters required in a password |
118
|
|
|
* |
119
|
|
|
* @return int |
120
|
|
|
*/ |
121
|
|
|
public function getMinimumLowercase(): int |
122
|
|
|
{ |
123
|
|
|
return $this->minimumLowercase; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns the minimum number of special characters required in a password |
128
|
|
|
* |
129
|
|
|
* @return int |
130
|
|
|
*/ |
131
|
|
|
public function getMinimumSpecialChars(): int |
132
|
|
|
{ |
133
|
|
|
return $this->minimumSpecialChars; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Returns the minimum number of uppercase letters required in a password |
138
|
|
|
* |
139
|
|
|
* @return int |
140
|
|
|
*/ |
141
|
|
|
public function getMinimumUppercase(): int |
142
|
|
|
{ |
143
|
|
|
return $this->minimumUppercase; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|