1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stadly\PasswordPolice\CodeMap; |
6
|
|
|
|
7
|
|
|
use Stadly\PasswordPolice\CharTree; |
8
|
|
|
use Stadly\PasswordPolice\CodeMap; |
9
|
|
|
use Stadly\PasswordPolice\Formatter\LengthFilter; |
10
|
|
|
use Stadly\PasswordPolice\Formatter\Truncator; |
11
|
|
|
|
12
|
|
|
final class LeetspeakMap implements CodeMap |
13
|
|
|
{ |
14
|
|
|
private const ENCODE_MAP = [ |
15
|
|
|
'A' => ['4', '@', '∂'], |
16
|
|
|
'B' => ['8', 'ß'], |
17
|
|
|
'C' => ['(', '¢', '<', '[', '©'], |
18
|
|
|
'D' => ['∂'], |
19
|
|
|
'E' => ['3', '€', 'є'], |
20
|
|
|
'F' => ['ƒ'], |
21
|
|
|
'G' => ['6', '9'], |
22
|
|
|
'H' => ['#'], |
23
|
|
|
'I' => ['1', '!', '|', ':'], |
24
|
|
|
'J' => ['¿'], |
25
|
|
|
'K' => ['X'], |
26
|
|
|
'L' => ['1', '£', 'ℓ'], |
27
|
|
|
'O' => ['0', '°'], |
28
|
|
|
'R' => ['2', '®', 'Я'], |
29
|
|
|
'S' => ['5', '$', '§'], |
30
|
|
|
'T' => ['7', '†'], |
31
|
|
|
'U' => ['µ'], |
32
|
|
|
'W' => ['vv'], |
33
|
|
|
'X' => ['×'], |
34
|
|
|
'Y' => ['φ', '¥'], |
35
|
|
|
'Z' => ['2', '≥'], |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array<int, array<string|int, string[]>> Leetspeak code map. |
40
|
|
|
*/ |
41
|
|
|
private $codeMap = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Truncator[] Formatter for extracting the first characters. |
45
|
|
|
*/ |
46
|
|
|
private $charExtractors; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param bool $encode Whether the map should encode or decode Leetspeak. |
50
|
|
|
*/ |
51
|
8 |
|
public function __construct($encode = false) |
52
|
|
|
{ |
53
|
8 |
|
if ($encode) { |
54
|
4 |
|
foreach (self::ENCODE_MAP as $char => $codedChars) { |
55
|
4 |
|
$this->codeMap[mb_strlen($char)][mb_strtoupper($char)] = $codedChars; |
56
|
|
|
} |
57
|
|
|
} else { |
58
|
4 |
|
foreach (self::ENCODE_MAP as $char => $codedChars) { |
59
|
4 |
|
foreach ($codedChars as $codedChar) { |
60
|
4 |
|
$this->codeMap[mb_strlen($codedChar)][mb_strtoupper($codedChar)][] = $char; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
8 |
|
foreach (array_keys($this->codeMap + [1 => true]) as $length) { |
65
|
8 |
|
$truncator = new Truncator($length); |
66
|
8 |
|
$truncator->setNext(new LengthFilter($length, $length)); |
67
|
8 |
|
$this->charExtractors[$length] = $truncator; |
68
|
|
|
} |
69
|
8 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritDoc} |
73
|
|
|
*/ |
74
|
6 |
|
public function getMap(CharTree $charTree): array |
75
|
|
|
{ |
76
|
6 |
|
$codeMap = []; |
77
|
|
|
|
78
|
|
|
// Add coded characters. |
79
|
6 |
|
foreach ($this->codeMap as $length => $chars) { |
80
|
6 |
|
foreach ($this->charExtractors[$length]->apply($charTree) as $char) { |
81
|
2 |
|
if (isset($chars[mb_strtoupper($char)])) { |
82
|
6 |
|
$codeMap[$char] = $chars[mb_strtoupper($char)]; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// Add uncoded characters. |
88
|
6 |
|
foreach ($this->charExtractors[1]->apply($charTree) as $char) { |
89
|
2 |
|
$codeMap[$char][] = $char; |
90
|
|
|
} |
91
|
|
|
|
92
|
6 |
|
return $codeMap; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritDoc} |
97
|
|
|
*/ |
98
|
2 |
|
public function getLengths(): array |
99
|
|
|
{ |
100
|
2 |
|
return array_keys($this->codeMap); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|