Guest User

Untitled

a guest
Mar 8th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. <?php
  2.  
  3. class Keyboard
  4. {
  5. const KEYBOARD = "1234567890qwertyuiopasdfghjkl;zxcvbnm,./";
  6.  
  7. private function horizontalTransform($index)
  8. {
  9. if ($index < 10) {
  10. return 9 - $index;
  11. }
  12.  
  13. if ($index < 20) {
  14. return 29 - $index;
  15. }
  16.  
  17. if ($index < 30) {
  18. return 49 - $index;
  19. }
  20.  
  21. if ($index < 40) {
  22. return 69 - $index;
  23. }
  24. }
  25.  
  26. private function verticalTransform($index)
  27. {
  28. if ($index < 10) {
  29. return 30 + $index;
  30. }
  31.  
  32. if ($index < 20) {
  33. return 10 + $index;
  34. }
  35.  
  36. if ($index < 30) {
  37. return $index - 10;
  38. }
  39.  
  40. if ($index < 40) {
  41. return $index - 30;
  42. }
  43. }
  44.  
  45. private function shift($index, $shiftBy)
  46. {
  47. $newIndex = $index - $shiftBy;
  48.  
  49. if ($newIndex >= 40) {
  50. return $newIndex % 40;
  51. }
  52.  
  53. if ($newIndex < 0) {
  54. return $newIndex + 40;
  55. }
  56.  
  57. return $newIndex;
  58. }
  59. private function transformCharacter($sequence, $index)
  60. {
  61. foreach (explode(',', $sequence) as $transformation) {
  62. if (is_numeric($transformation)) {
  63. $index = $this->shift($index, (int)$transformation);
  64. }
  65.  
  66. if (strtoupper($transformation) == 'H') {
  67. $index = $this->horizontalTransform($index);
  68. }
  69.  
  70. if (strtoupper($transformation) == 'V') {
  71. $index = $this->verticalTransform($index);
  72. }
  73. }
  74. return self::KEYBOARD[$index];
  75. }
  76.  
  77. public function transformString($transformationSequence, $string) {
  78. $transformedCharacters = [];
  79. $transformedString = '';
  80. foreach (str_split(strtolower($string)) as $character) {
  81. $position = strpos(self::KEYBOARD, $character);
  82. if ($position !== false) {
  83. if (empty($transformedCharacters[$character])) {
  84. $transformedCharacters[$character] = $this->transformCharacter($transformationSequence, $position);
  85. }
  86. $transformedString .= $transformedCharacters[$character];
  87. } else {
  88. $transformedString .= $character;
  89. }
  90. }
  91. return $transformedString;
  92. }
  93. }
  94.  
  95. echo (new Keyboard)->transformString("H,V", "qqww");
Add Comment
Please, Sign In to add comment