Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
85.71% |
6 / 7 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
EncryptedPassword | |
85.71% |
6 / 7 |
|
80.00% |
4 / 5 |
5.07 | |
0.00% |
0 / 1 |
fromUnencryptedPassword | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getRegularExpression | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
verifyUnencryptedPassword | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getIndexes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
createRandom | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | namespace Apie\TextValueObjects; |
3 | |
4 | use Apie\Core\Attributes\Description; |
5 | use Apie\Core\Attributes\FakeMethod; |
6 | use Apie\Core\Attributes\ProvideIndex; |
7 | use Apie\Core\ValueObjects\Interfaces\StringValueObjectInterface; |
8 | use Apie\Core\ValueObjects\IsStringWithRegexValueObject; |
9 | use Faker\Generator; |
10 | use Stringable; |
11 | |
12 | #[FakeMethod('createRandom')] |
13 | #[ProvideIndex('getIndexes')] |
14 | #[Description('An encrypted password is a password in such a way that you can only verify passwords and not read the password')] |
15 | final class EncryptedPassword implements StringValueObjectInterface |
16 | { |
17 | use IsStringWithRegexValueObject; |
18 | |
19 | public static function fromUnencryptedPassword(Stringable|string $password): self |
20 | { |
21 | $password = (string) $password; |
22 | return new self(password_hash($password, null)); |
23 | } |
24 | |
25 | public static function getRegularExpression(): string |
26 | { |
27 | return '/^[$]2[abxy]?[$](?:0[4-9]|[12][0-9]|3[01])[$][.\/0-9a-zA-Z]{53,60}$/'; |
28 | } |
29 | |
30 | public function verifyUnencryptedPassword(Stringable|string $password): bool |
31 | { |
32 | $password = (string) $password; |
33 | return password_verify($password, $this->internal); |
34 | } |
35 | |
36 | /** |
37 | * @return array<string, int> |
38 | */ |
39 | public static function getIndexes(): array |
40 | { |
41 | return []; |
42 | } |
43 | |
44 | public static function createRandom(Generator $generator): self |
45 | { |
46 | return self::fromUnencryptedPassword($generator->password(6, 42)); |
47 | } |
48 | } |