-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLetterTransform.php
193 lines (174 loc) · 4.87 KB
/
LetterTransform.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
class Text_LetterTransform {
private static $NATO = array(
"A" => "Alpha",
"B" => "Bravo",
"C" => "Charlie",
"D" => "Delta",
"E" => "Echo",
"F" => "Foxtrot",
"G" => "Golf",
"H" => "Hotel",
"I" => "India",
"J" => "Juliett",
"K" => "Kilo",
"L" => "Lima",
"M" => "Mike",
"N" => "November",
"O" => "Oscar",
"P" => "Papa",
"Q" => "Quebec",
"R" => "Romeo",
"S" => "Sierra",
"T" => "Tango",
"U" => "Uniform",
"V" => "Victor",
"W" => "Whiskey",
"X" => "X-ray",
"Y" => "Yankee",
"Z" => "Zulu",
0 => "Zero",
1 => "One",
2 => "Two",
3 => "Three",
4 => "Four",
5 => "Five",
6 => "Six",
7 => "Seven",
8 => "Eight",
9 => "Nine");
const MORSE_SHORT_GAP = " ";
const MORSE_MEDIUM_GAP = " ";
private static $MORSE = array(
"A" => ".-",
"B" => "-...",
"C" => "-.-.",
"D" => "-..",
"E" => ".",
"F" => "..-.",
"G" => "--.",
"H" => "....",
"I" => "..",
"J" => ".---",
"K" => "-.-",
"L" => ".-..",
"M" => "--",
"N" => "-.",
"O" => "---",
"P" => ".--.",
"Q" => "--.-",
"R" => ".-.",
"S" => "...",
"T" => "-",
"U" => "..-",
"V" => "...-",
"W" => ".--",
"X" => "-..-",
"Y" => "-.--",
"Z" => "--..",
/* Non-english extensions */
"ä" => ".-.-",
"æ" => ".-.-",
"à" => ".--.-",
"å" => ".--.-",
"ç" => "-.-..",
"ĉ" => "-.-..",
"š" => "----",
"ð" => "..--.",
"è" => ".-..-",
"é" => "..-..",
"đ" => "..-..",
"ĝ" => "--.-.",
"ĥ" => "----",
"ĵ" => ".---.",
"ñ" => "--.--",
"ö" => "---.",
"ø" => "---.",
"ŝ" => "...-.",
"þ" => ".--..",
"ü" => "..--",
"ŭ" => "..--",
"?" => "..--..",
"=" => "-...-",
"," => "--..--",
";" => "-.-.-.",
"-" => "-....-",
"'" => ".----.",
"+" => ".-.-.",
"/" => "-..-.",
"_" => "..--.-",
"!" => "-.-.--",
"\$" => "...-..-",
"." => ".-.-.-",
":" => "---...",
"(" => "-.--.",
")" => "-.--.-",
"\"" => ".-..-.",
"0" => "-----",
"1" => ".----",
"2" => "..---",
"3" => "...--",
"4" => "....-",
"5" => ".....",
"6" => "-....",
"7" => "--...",
"8" => "---..",
"9" => "----.",
"&" => ".-...",
"@" => ".--.-."
);
/**
* Returns the representation of the letter in the NATO phonetic alphabet
*
* If the letter is not supported by the NATO phonetic alphabet, the letter
* itself is returned. For convenience it is also possible to pass whole
* words to this method. In this case, every single is transformed to its
* representation in the NATO phonetic alphabet.
*
* @param string Letter or whole word/sentence
* @return string Representation of the letter in the NATO phonetic alphabet
*/
public static function toNATO($letter) {
if (strlen($letter) > 1) {
$string = new StringIterator($letter);
$result = array();
foreach ($string as $letter) {
$result[] = self::toNATO($letter);
}
return join(" ", $result);
}
$letter = strtoupper($letter);
if (!isset(self::$NATO[$letter])) {
return $letter;
}
return self::$NATO[$letter];
}
/**
* Returns the representation of the letter in the Morse code alphabet
*
* @param string Letter or whole word/sentence
* @return string Representation of the input in the Morse code alphabet
*/
public static function toMorse($letter, &$warnings = array()) {
if (strlen($letter) > 1) {
$string = $letter;
$result = array();
for ($i = 0; $i < strlen($string); $i++) {
$letter = $string{$i};
if ($letter == " ") {
$result[] = self::MORSE_MEDIUM_GAP;
} else {
$result[] = self::toMorse($letter, $warnings);
}
}
return join(self::MORSE_SHORT_GAP, $result);
}
if (isset(self::$MORSE[$letter])) {
return self::$MORSE[$letter];
}
if (isset(self::$MORSE[strtoupper($letter)])) {
return self::$MORSE[strtoupper($letter)];
}
$warnings[] = "No morse representation found for " . $letter;
}
}