Passed
Branch master (f29434)
by Johnny
01:57
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 JM\Morsecode;
4
5
class Morsecode
6
{
7
    /**
8
     * @var array
9
     */
10
    private $translation = [
11
        '0' => '-----',
12
        '1' => '.----',
13
        '2' => '..---',
14
        '3' => '...--',
15
        '4' => '....-',
16
        '5' => '.....',
17
        '6' => '-....',
18
        '7' => '--...',
19
        '8' => '---..',
20
        '9' => '----.',
21
        'a' => '.-',
22
        'b' => '-...',
23
        'c' => '-.-.',
24
        'd' => '-..',
25
        'e' => '.',
26
        'f' => '..-.',
27
        'g' => '--.',
28
        'h' => '....',
29
        'i' => '..',
30
        'j' => '.---',
31
        'k' => '-.-',
32
        'l' => '.-..',
33
        'm' => '--',
34
        'n' => '-.',
35
        'o' => '---',
36
        'p' => '.--.',
37
        'q' => '--.-',
38
        'r' => '.-.',
39
        's' => '...',
40
        't' => '-',
41
        'u' => '..-',
42
        'v' => '...-',
43
        'w' => '.--',
44
        'x' => '-..-',
45
        'y' => '-.--',
46
        'z' => '--..',
47
        '.' => '.-.-.-',
48
        ',' => '--..--',
49
        '?' => '..--..',
50
        '!' => '-.-.--',
51
        '-' => '-....-',
52
        '/' => '-..-.',
53
        '@' => '.--.-.',
54
        '(' => '-.--.',
55
        ')' => '-.--.-',
56
        ' ' => '/',
57
    ];
58
59
    /**
60
     * The value the class constructed with.
61
     *
62
     * @var string
63
     */
64
    private $value = '';
65
66
    /**
67
     * Morsecode constructor.
68
     *
69
     * @param string $value
70
     */
71
    public function __construct(string $value = '')
72
    {
73
        $this->value = $value;
74
    }
75
76
    /**
77
     * @param string $str
78
     * @return string
79
     */
80
    public function encode(string $str = '', string $result = ''): string
81
    {
82
        if (empty($str) === true) {
83
            return $this->encode($this->value);
84
        }
85
86
        foreach (str_split($str) as $char) {
87
            if (isset($this->translation[$char])) {
88
                $result .= " ".$this->translation[$char];
89
            }
90
        }
91
92
        return trim($result);
93
    }
94
95
    /**
96
     * @param string $str
97
     * @return string
98
     */
99
    public function decode(string $str = '', string $result = ''): string
100
    {
101
        if (empty($str) === true) {
102
            return $this->decode($this->value);
103
        }
104
105
        $characters = array_flip($this->translation);
106
        foreach (explode(' ', $str) as $character) {
107
            if (isset($characters[$character])) {
108
                $result .= $characters[$character];
109
            }
110
        }
111
112
        return $result;
113
    }
114
}
115