Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Keyboard
- {
- const KEYBOARD = "1234567890qwertyuiopasdfghjkl;zxcvbnm,./";
- private function horizontalTransform($index)
- {
- if ($index < 10) {
- return 9 - $index;
- }
- if ($index < 20) {
- return 29 - $index;
- }
- if ($index < 30) {
- return 49 - $index;
- }
- if ($index < 40) {
- return 69 - $index;
- }
- }
- private function verticalTransform($index)
- {
- if ($index < 10) {
- return 30 + $index;
- }
- if ($index < 20) {
- return 10 + $index;
- }
- if ($index < 30) {
- return $index - 10;
- }
- if ($index < 40) {
- return $index - 30;
- }
- }
- private function shift($index, $shiftBy)
- {
- $newIndex = $index - $shiftBy;
- if ($newIndex >= 40) {
- return $newIndex % 40;
- }
- if ($newIndex < 0) {
- return $newIndex + 40;
- }
- return $newIndex;
- }
- private function transformCharacter($sequence, $index)
- {
- foreach (explode(',', $sequence) as $transformation) {
- if (is_numeric($transformation)) {
- $index = $this->shift($index, (int)$transformation);
- }
- if (strtoupper($transformation) == 'H') {
- $index = $this->horizontalTransform($index);
- }
- if (strtoupper($transformation) == 'V') {
- $index = $this->verticalTransform($index);
- }
- }
- return self::KEYBOARD[$index];
- }
- public function transformString($transformationSequence, $string) {
- $transformedCharacters = [];
- $transformedString = '';
- foreach (str_split(strtolower($string)) as $character) {
- $position = strpos(self::KEYBOARD, $character);
- if ($position !== false) {
- if (empty($transformedCharacters[$character])) {
- $transformedCharacters[$character] = $this->transformCharacter($transformationSequence, $position);
- }
- $transformedString .= $transformedCharacters[$character];
- } else {
- $transformedString .= $character;
- }
- }
- return $transformedString;
- }
- }
- echo (new Keyboard)->transformString("H,V", "qqww");
Add Comment
Please, Sign In to add comment