|
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
|
24 |
|
public function __construct($encode = false) |
|
52
|
|
|
{ |
|
53
|
24 |
|
if ($encode) { |
|
54
|
12 |
|
foreach (self::ENCODE_MAP as $char => $codedChars) { |
|
55
|
12 |
|
$this->codeMap[mb_strlen($char)][mb_strtoupper($char)] = $codedChars; |
|
56
|
|
|
} |
|
57
|
|
|
} else { |
|
58
|
12 |
|
foreach (self::ENCODE_MAP as $char => $codedChars) { |
|
59
|
12 |
|
foreach ($codedChars as $codedChar) { |
|
60
|
12 |
|
$this->codeMap[mb_strlen($codedChar)][mb_strtoupper($codedChar)][] = $char; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
24 |
|
foreach (array_keys($this->codeMap + [1 => true]) as $length) { |
|
65
|
24 |
|
$truncator = new Truncator($length); |
|
66
|
24 |
|
$truncator->setNext(new LengthFilter($length, $length)); |
|
67
|
24 |
|
$this->charExtractors[$length] = $truncator; |
|
68
|
|
|
} |
|
69
|
24 |
|
} |
|
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
|
|
|
/** |
|
104
|
|
|
* {@inheritDoc} |
|
105
|
|
|
*/ |
|
106
|
16 |
|
public function code(string $string): array |
|
107
|
|
|
{ |
|
108
|
|
|
// Add coded characters. |
|
109
|
16 |
|
$codeMap = $this->codeMap[mb_strlen($string)][mb_strtoupper($string)] ?? []; |
|
110
|
|
|
|
|
111
|
|
|
// Add uncoded character. |
|
112
|
16 |
|
$codeMap[] = $string; |
|
113
|
|
|
|
|
114
|
16 |
|
return $codeMap; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|