1 | <?php |
||
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 = '') |
||
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 |
||
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 |
||
122 | } |
||
123 |