Total Complexity | 7 |
Total Lines | 69 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
9 | final class LeetspeakMap implements CodeMap |
||
10 | { |
||
11 | private const ENCODE_MAP = [ |
||
12 | 'A' => ['4', '@', '∂'], |
||
13 | 'B' => ['8', 'ß'], |
||
14 | 'C' => ['(', '¢', '<', '[', '©'], |
||
15 | 'D' => ['∂'], |
||
16 | 'E' => ['3', '€', 'є'], |
||
17 | 'F' => ['ƒ'], |
||
18 | 'G' => ['6', '9'], |
||
19 | 'H' => ['#'], |
||
20 | 'I' => ['1', '!', '|', ':'], |
||
21 | 'J' => ['¿'], |
||
22 | 'K' => ['X'], |
||
23 | 'L' => ['1', '£', 'ℓ'], |
||
24 | 'O' => ['0', '°'], |
||
25 | 'R' => ['2', '®', 'Я'], |
||
26 | 'S' => ['5', '$', '§'], |
||
27 | 'T' => ['7', '†'], |
||
28 | 'U' => ['µ'], |
||
29 | 'W' => ['vv'], |
||
30 | 'X' => ['×'], |
||
31 | 'Y' => ['φ', '¥'], |
||
32 | 'Z' => ['2', '≥'], |
||
33 | ]; |
||
34 | |||
35 | /** |
||
36 | * @var array<int, array<string|int, array<string>>> Leetspeak code map. |
||
37 | */ |
||
38 | private $codeMap = []; |
||
39 | |||
40 | /** |
||
41 | * @param bool $encode Whether the map should encode or decode Leetspeak. |
||
42 | */ |
||
43 | 18 | public function __construct(bool $encode = false) |
|
44 | { |
||
45 | 18 | if ($encode) { |
|
46 | 9 | foreach (self::ENCODE_MAP as $char => $codedChars) { |
|
47 | 9 | $this->codeMap[mb_strlen($char)][mb_strtoupper($char)] = $codedChars; |
|
48 | } |
||
49 | } else { |
||
50 | 9 | foreach (self::ENCODE_MAP as $char => $codedChars) { |
|
51 | 9 | foreach ($codedChars as $codedChar) { |
|
52 | 9 | $this->codeMap[mb_strlen($codedChar)][mb_strtoupper($codedChar)][] = $char; |
|
53 | } |
||
54 | } |
||
55 | } |
||
56 | 18 | } |
|
57 | |||
58 | /** |
||
59 | * @inheritDoc |
||
60 | */ |
||
61 | 2 | public function getLengths(): array |
|
62 | { |
||
63 | 2 | return array_keys($this->codeMap); |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * @inheritDoc |
||
68 | */ |
||
69 | 16 | public function code(string $string): array |
|
78 | } |
||
79 | } |
||
80 |