Passed
Push — master ( c32a75...667bcf )
by Johnny
01:55
created

Morsecode::decode()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 7
cts 8
cp 0.875
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 2
crap 4.0312
1
<?php
2
namespace JM\Morsecode;
3
4
class Morsecode
5
{
6
    /**
7
     * @var array
8
     */
9
    private $translation = [
10
        '0' => '-----',
11
        '1' => '.----',
12
        '2' => '..---',
13
        '3' => '...--',
14
        '4' => '....-',
15
        '5' => '.....',
16
        '6' => '-....',
17
        '7' => '--...',
18
        '8' => '---..',
19
        '9' => '----.',
20
        'a' => '.-',
21
        'b' => '-...',
22
        'c' => '-.-.',
23
        'd' => '-..',
24
        'e' => '.',
25
        'f' => '..-.',
26
        'g' => '--.',
27
        'h' => '....',
28
        'i' => '..',
29
        'j' => '.---',
30
        'k' => '-.-',
31
        'l' => '.-..',
32
        'm' => '--',
33
        'n' => '-.',
34
        'o' => '---',
35
        'p' => '.--.',
36
        'q' => '--.-',
37
        'r' => '.-.',
38
        's' => '...',
39
        't' => '-',
40
        'u' => '..-',
41
        'v' => '...-',
42
        'w' => '.--',
43
        'x' => '-..-',
44
        'y' => '-.--',
45
        'z' => '--..',
46
        '.' => '.-.-.-',
47
        ',' => '--..--',
48
        '?' => '..--..',
49
        '!' => '-.-.--',
50
        '-' => '-....-',
51
        '/' => '-..-.',
52
        '@' => '.--.-.',
53
        '(' => '-.--.',
54
        ')' => '-.--.-',
55
        ' ' => '/',
56
    ];
57
58
    /**
59
     * The value the class constructed with.
60
     *
61
     * @var string
62
     */
63
    private $value = '';
64
65
    /**
66
     * Morsecode constructor.
67
     *
68
     * @param string $value
69
     */
70 2
    public function __construct(string $value = '')
71
    {
72 2
        $this->value = $value;
73 2
    }
74
75
    /**
76
     * @param string $str
77
     * @return string
78
     */
79 1
    public function encode(string $str = '', string $result = ''): string
80
    {
81 1
        if (empty($str) == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
82
            return $this->encode($this->value);
83
        }
84
85 1
        foreach (str_split($str) as $char) {
86 1
            if (isset($this->translation[$char])) {
87 1
                $result .= " ".$this->translation[$char];
88
            }
89
        }
90
91 1
        return trim($result);
92
    }
93
94
    /**
95
     * @param string $str
96
     * @return string
97
     */
98 1
    public function decode(string $str = '', string $result = ''): string
99
    {
100 1
        if (empty($str) == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
101
            return $this->decode($this->value);
102
        }
103
104 1
        $characters = array_flip($this->translation);
105 1
        foreach (explode(' ', $str) as $character) {
106 1
            if (isset($characters[$character])) {
107 1
                $result .= $characters[$character];
108
            }
109
        }
110
111 1
        return $result;
112
    }
113
}
114