1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Linio\Component\Input\Constraint; |
6
|
|
|
|
7
|
|
|
class Password extends Constraint |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var int |
11
|
|
|
*/ |
12
|
|
|
protected $minLength; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var int |
16
|
|
|
*/ |
17
|
|
|
protected $maxLength; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
protected $mustHaveUpperCase; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
protected $mustHaveLowerCase; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
protected $mustHaveDigits; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
protected $mustHaveSymbols; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
protected $mustHaveDigitsOrSymbols; |
43
|
|
|
|
44
|
|
|
public function __construct( |
45
|
|
|
int $minLength, |
46
|
|
|
int $maxLength, |
47
|
|
|
bool $mustHaveUpperCase, |
48
|
|
|
bool $mustHaveLowerCase, |
49
|
|
|
bool $mustHaveDigits, |
50
|
|
|
bool $mustHaveSymbols, |
51
|
|
|
bool $mustHaveDigitsOrSymbols, |
52
|
|
|
string $errorMessage = null) |
53
|
|
|
{ |
54
|
|
|
$this->setErrorMessage($errorMessage ?? 'Invalid password format'); |
55
|
|
|
$this->minLength = $minLength; |
56
|
|
|
$this->maxLength = $maxLength; |
57
|
|
|
$this->mustHaveUpperCase = $mustHaveUpperCase; |
58
|
|
|
$this->mustHaveLowerCase = $mustHaveLowerCase; |
59
|
|
|
$this->mustHaveDigits = $mustHaveDigits; |
60
|
|
|
$this->mustHaveSymbols = $mustHaveSymbols; |
61
|
|
|
$this->mustHaveDigitsOrSymbols = $mustHaveDigitsOrSymbols; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function validate($content): bool |
65
|
|
|
{ |
66
|
|
|
if (strlen($content) < $this->minLength || strlen($content) > $this->maxLength) { |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$hasUpperCase = (bool) preg_match('/[A-Z]/', $content); |
71
|
|
|
$hasLowerCase = (bool) preg_match('/[a-z]/', $content); |
72
|
|
|
$hasDigits = (bool) preg_match('/[0-9]/', $content); |
73
|
|
|
$hasSymbols = (bool) preg_match('/[^A-Za-z0-9]/', $content); |
74
|
|
|
|
75
|
|
|
if ($this->mustHaveUpperCase && !$hasUpperCase) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($this->mustHaveLowerCase && !$hasLowerCase) { |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($this->mustHaveDigits && !$hasDigits) { |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ($this->mustHaveSymbols && !$hasSymbols) { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($this->mustHaveDigitsOrSymbols && (!$hasDigits && !$hasSymbols)) { |
|
|
|
|
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|