diff --git a/DIRECTORY.md b/DIRECTORY.md index 86a0e9c..935cce5 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -3,6 +3,7 @@ ## Ciphers * [Caesarcipher](./ciphers/caesarCipher.php) * [Xorcipher](./ciphers/XORCipher.php) + * [Morse Code](./ciphers/morseCode.php) ## Conversions * [Binarytodecimal](./Conversions/BinaryToDecimal.php) diff --git a/ciphers/morseCode.php b/ciphers/morseCode.php new file mode 100644 index 0000000..aa2673a --- /dev/null +++ b/ciphers/morseCode.php @@ -0,0 +1,119 @@ + ".-", + "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" => "--..", + "1" => ".----", + "2" => "..---", + "3" => "...--", + "4" => "....-", + "5" => ".....", + "6" => "-....", + "7" => "--...", + "8" => "---..", + "9" => "----.", + "0" => "-----", + " " => "/" + ); + + $encodedText = ""; // Stores the encoded text + foreach (str_split($text) as $c) { // Going through each character + if (array_key_exists($c, $MORSE_CODE)) { // Checks if it is a valid character + $encodedText .= $MORSE_CODE[$c] . " "; // Appends the correct character + } else { + throw new \Exception("Invalid character: $c"); + } + } + substr_replace($encodedText ,"", -1); // Removes trailing space + return $encodedText; +} + +/** + * Decode Morse Code to text. + * @param string text to decode + */ +function decode(string $text): string +{ + $MORSE_CODE = array( // An array containing morse code to text translations + ".-" => "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", + ".----" => "1", + "..---" => "2", + "...--" => "3", + "....-" => "4", + "....." => "5", + "-...." => "6", + "--..." => "7", + "---.." => "8", + "----." => "9", + "-----" => "0", + "/" => " " + ); + + $decodedText = ""; // Stores the decoded text + foreach (explode(" ", $text) as $c) { // Going through each group + if (array_key_exists($c, $MORSE_CODE)) { // Checks if it is a valid character + $decodedText .= $MORSE_CODE[$c]; // Appends the correct character + } else { + if ($c) { // Makes sure that the string is not empty to prevent trailing spaces or extra spaces from breaking this + throw new \Exception("Invalid character: $c"); + } + } + } + return $decodedText; +} diff --git a/tests/MorseCodeTest.php b/tests/MorseCodeTest.php new file mode 100644 index 0000000..51dd5ac --- /dev/null +++ b/tests/MorseCodeTest.php @@ -0,0 +1,15 @@ +