Passed
Push — master ( c8578e...f5141b )
by Sam
01:10 queued 12s
created

Maori::mapChars()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CustomD\WordFinder\CharacterMaps;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Collection;
7
use CustomD\WordFinder\CharacterMap;
8
9
class Maori implements CharacterMap
10
{
11
12
    protected Collection $chars;
13
14
    public array $mappedChars = [
15
        'ā' => '1',
16
        'ē' => '2',
17
        'ī' => '3',
18
        'ō' => '4',
19
        'ū' => '5',
20
    ];
21
22
    public function __construct()
23
    {
24
        $this->chars = collect([
25
            'a',
26
            '1',
27
            'e',
28
            '2',
29
            'g',
30
            'h',
31
            'i',
32
            '3',
33
            'k',
34
            'm',
35
            'n',
36
            'o',
37
            '4',
38
            'p',
39
            'r',
40
            't',
41
            'u',
42
            '5',
43
            'w'
44
        ]);
45
    }
46
47
48
    public function getRandomChar()
49
    {
50
51
        return Str::upper($this->chars->random());
52
    }
53
54
55
    public function mapChars($word)
56
    {
57
        return Str::replace(array_keys($this->mappedChars), $this->mappedChars, Str::lower($word));
58
    }
59
60
    public function unmapChars($word)
61
    {
62
        $keys = collect($this->mappedChars)->mapWithKeys(function ($val, $key) {
63
            return [Str::upper($key) => Str::upper($val)];
64
        })->toArray();
65
66
        return Str::replace($keys, array_keys($keys), Str::upper($word));
67
    }
68
}
69