1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PasswordHelper; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Defines password policy requirements and constraints. |
9
|
|
|
* |
10
|
|
|
* This class encapsulates all the rules and requirements that passwords |
11
|
|
|
* must meet to be considered valid according to the policy. |
12
|
|
|
* |
13
|
|
|
* @package PasswordHelper |
14
|
|
|
*/ |
15
|
|
|
class Policy |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Creates a new password policy with the specified requirements. |
19
|
|
|
*/ |
20
|
|
|
public function __construct( |
21
|
|
|
private int $minimumLength = 8, |
22
|
|
|
private int $maximumLength = 20, |
23
|
|
|
private int $minimumCharacterTypes = 3, |
24
|
|
|
private bool $allowRepeatedCharacters = false, |
25
|
|
|
private bool $allowSequentialCharacters = false, |
26
|
|
|
private bool $allowCommonPatterns = false |
27
|
|
|
) { |
28
|
|
|
if ($this->minimumLength < 8) { |
29
|
|
|
throw new \InvalidArgumentException('Minimum length must be at least 8 characters'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if ($this->maximumLength < $this->minimumLength) { |
33
|
|
|
throw new \InvalidArgumentException('Maximum length must be greater than minimum length'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($this->minimumCharacterTypes < 1 || $this->minimumCharacterTypes > 4) { |
37
|
|
|
throw new \InvalidArgumentException('Minimum character types must be between 1 and 4'); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Gets the minimum required password length. |
43
|
|
|
* |
44
|
|
|
* @return int The minimum length |
45
|
|
|
*/ |
46
|
|
|
public function getMinimumLength(): int |
47
|
|
|
{ |
48
|
|
|
return $this->minimumLength; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Gets the maximum allowed password length. |
53
|
|
|
* |
54
|
|
|
* @return int The maximum length |
55
|
|
|
*/ |
56
|
|
|
public function getMaximumLength(): int |
57
|
|
|
{ |
58
|
|
|
return $this->maximumLength; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Gets the minimum required number of character types. |
63
|
|
|
* |
64
|
|
|
* @return int The minimum number of character types |
65
|
|
|
*/ |
66
|
|
|
public function getMinimumCharacterTypes(): int |
67
|
|
|
{ |
68
|
|
|
return $this->minimumCharacterTypes; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Checks if repeated characters are allowed. |
73
|
|
|
* |
74
|
|
|
* @return bool True if repeated characters are allowed |
75
|
|
|
*/ |
76
|
|
|
public function areRepeatedCharactersAllowed(): bool |
77
|
|
|
{ |
78
|
|
|
return $this->allowRepeatedCharacters; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Checks if sequential characters are allowed. |
83
|
|
|
* |
84
|
|
|
* @return bool True if sequential characters are allowed |
85
|
|
|
*/ |
86
|
|
|
public function areSequentialCharactersAllowed(): bool |
87
|
|
|
{ |
88
|
|
|
return $this->allowSequentialCharacters; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Checks if common patterns are allowed. |
93
|
|
|
* |
94
|
|
|
* @return bool True if common patterns are allowed |
95
|
|
|
*/ |
96
|
|
|
public function areCommonPatternsAllowed(): bool |
97
|
|
|
{ |
98
|
|
|
return $this->allowCommonPatterns; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|