1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DBUnt1tled\VO\VObjects\Person; |
6
|
|
|
|
7
|
|
|
use DBUnt1tled\VO\Exception\InvalidVOArgumentException; |
8
|
|
|
use DBUnt1tled\VO\VObjects\Scalar\Strings; |
9
|
|
|
use DBUnt1tled\VO\VObjects\ValueObjectComplex; |
10
|
|
|
use DBUnt1tled\VO\VObjects\ValueObjectInterface; |
11
|
|
|
|
12
|
|
|
class Password extends ValueObjectComplex |
13
|
|
|
{ |
14
|
|
|
public const PASSWORD_MIN_LENGTH = 8; |
15
|
|
|
public const PASSWORD_MAX_LENGTH = 20; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param mixed $value |
19
|
|
|
* @param mixed ...$other |
20
|
|
|
* @throws \ReflectionException |
21
|
|
|
*/ |
22
|
2 |
|
public function guard($value, ...$other): void |
23
|
|
|
{ |
24
|
2 |
|
parent::guard($value); |
25
|
|
|
/** @var ValueObjectInterface $value*/ |
26
|
2 |
|
$pwd = $value->getValue(); |
27
|
2 |
|
$error = ''; |
28
|
2 |
|
if (mb_strlen($pwd) < 8) { |
29
|
1 |
|
$error .= 'Password too short!'; |
30
|
|
|
} |
31
|
|
|
|
32
|
2 |
|
if (mb_strlen($pwd) > 20) { |
33
|
1 |
|
$error .= "Password too long!\n"; |
34
|
|
|
} |
35
|
|
|
|
36
|
2 |
|
if (!preg_match('#[\d]+#', $pwd)) { |
37
|
1 |
|
$error .= "Password must include at least one number!\n"; |
38
|
|
|
} |
39
|
|
|
|
40
|
2 |
|
if (!preg_match('#[a-z]+#', $pwd)) { |
41
|
1 |
|
$error .= "Password must include at least one letter!\n"; |
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
if (!preg_match('#[A-Z]+#', $pwd)) { |
45
|
1 |
|
$error .= "Password must include at least one CAPS!\n"; |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
if (!preg_match('#\W+#', $pwd)) { |
49
|
1 |
|
$error .= "Password must include at least one symbol!\n"; |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
if ($error !== '') { |
53
|
1 |
|
throw new InvalidVOArgumentException($error, $value); |
54
|
|
|
} |
55
|
1 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $value |
59
|
|
|
* @return Password |
60
|
|
|
* @throws \ReflectionException |
61
|
|
|
*/ |
62
|
2 |
|
public static function createFromString(string $value): self |
63
|
|
|
{ |
64
|
2 |
|
return new static(new Strings($value)); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|