Completed
Push — master ( d492a5...c08f37 )
by Johnny
02:30
created

Morsecode::decode()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
ccs 0
cts 13
cp 0
cc 4
eloc 8
nc 4
nop 2
crap 20
1
<?php
2
3
namespace johnnymast\Morsecode;
4
5
class Morsecode
6
{
7
    /**
8
     * The translation table we use to encode or
9
     * decode messages.
10
     *
11
     * @var array
12
     */
13
    private $translation = [
14
        '0' => '-----',
15
        '1' => '.----',
16
        '2' => '..---',
17
        '3' => '...--',
18
        '4' => '....-',
19
        '5' => '.....',
20
        '6' => '-....',
21
        '7' => '--...',
22
        '8' => '---..',
23
        '9' => '----.',
24
        'a' => '.-',
25
        'b' => '-...',
26
        'c' => '-.-.',
27
        'd' => '-..',
28
        'e' => '.',
29
        'f' => '..-.',
30
        'g' => '--.',
31
        'h' => '....',
32
        'i' => '..',
33
        'j' => '.---',
34
        'k' => '-.-',
35
        'l' => '.-..',
36
        'm' => '--',
37
        'n' => '-.',
38
        'o' => '---',
39
        'p' => '.--.',
40
        'q' => '--.-',
41
        'r' => '.-.',
42
        's' => '...',
43
        't' => '-',
44
        'u' => '..-',
45
        'v' => '...-',
46
        'w' => '.--',
47
        'x' => '-..-',
48
        'y' => '-.--',
49
        'z' => '--..',
50
        '.' => '.-.-.-',
51
        ',' => '--..--',
52
        '?' => '..--..',
53
        '!' => '-.-.--',
54
        '-' => '-....-',
55
        '/' => '-..-.',
56
        '@' => '.--.-.',
57
        '(' => '-.--.',
58
        ')' => '-.--.-',
59
        ' ' => '/',
60
    ];
61
62
    /**
63
     * The value the class constructed with.
64
     *
65
     * @var string
66
     */
67
    private $value = '';
68
69
    /**
70
     * Morsecode constructor.
71
     *
72
     * @param string $value
73
     */
74
    public function __construct(string $value = '')
75
    {
76
        $this->value = $value;
77
    }
78
79
    /**
80
     * Encode a given string into morsecode.
81
     *
82
     * @param string $str
83
     * @return string
84
     */
85
    public function encode(string $str = '', string $result = ''): string
86
    {
87
        if (empty($str) === true) {
88
            return $this->encode($this->value);
89
        }
90
91
        foreach (str_split($str) as $char) {
92
            if (isset($this->translation[$char])) {
93
                $result .= " ".$this->translation[$char];
94
            }
95
        }
96
97
        return trim($result);
98
    }
99
100
    /**
101
     * Decode a given morsecode string into something
102
     * readable for humans.
103
     *
104
     * @param string $str
105
     * @return string
106
     */
107
    public function decode(string $str = '', string $result = ''): string
108
    {
109
        if (empty($str) === true) {
110
            return $this->decode($this->value);
111
        }
112
113
        $characters = array_flip($this->translation);
114
        foreach (explode(' ', $str) as $character) {
115
            if (isset($characters[$character])) {
116
                $result .= $characters[$character];
117
            }
118
        }
119
120
        return $result;
121
    }
122
}
123