|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpSchool\Terminal; |
| 4 | + |
| 5 | +use function in_array; |
| 6 | + |
| 7 | +/** |
| 8 | + * @author Aydin Hassan <[email protected]> |
| 9 | + */ |
| 10 | +class InputCharacter |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @var string |
| 14 | + */ |
| 15 | + private $data; |
| 16 | + |
| 17 | + public const UP = 'UP'; |
| 18 | + public const DOWN = 'DOWN'; |
| 19 | + public const RIGHT = 'RIGHT'; |
| 20 | + public const LEFT = 'LEFT'; |
| 21 | + public const CTRLA = 'CTRLA'; |
| 22 | + public const CTRLB = 'CTRLB'; |
| 23 | + public const CTRLE = 'CTRLE'; |
| 24 | + public const CTRLF = 'CTRLF'; |
| 25 | + public const BACKSPACE = 'BACKSPACE'; |
| 26 | + public const CTRLW = 'CTRLW'; |
| 27 | + public const ENTER = 'ENTER'; |
| 28 | + public const TAB = 'TAB'; |
| 29 | + |
| 30 | + private static $controls = [ |
| 31 | + "\033[A" => self::UP, |
| 32 | + "\033[B" => self::DOWN, |
| 33 | + "\033[C" => self::RIGHT, |
| 34 | + "\033[D" => self::LEFT, |
| 35 | + "\001" => self::CTRLA, |
| 36 | + "\002" => self::CTRLB, |
| 37 | + "\005" => self::CTRLE, |
| 38 | + "\006" => self::CTRLF, |
| 39 | + "\010" => self::BACKSPACE, |
| 40 | + "\177" => self::BACKSPACE, |
| 41 | + "\027" => self::CTRLW, |
| 42 | + "\n" => self::ENTER, |
| 43 | + "\t" => self::TAB, |
| 44 | + ]; |
| 45 | + |
| 46 | + public function __construct(string $data) |
| 47 | + { |
| 48 | + $this->data = $data; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Is this character a control sequence? |
| 53 | + */ |
| 54 | + public function isControl() : bool |
| 55 | + { |
| 56 | + return isset(static::$controls[$this->data]); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Is this character a normal character? |
| 61 | + */ |
| 62 | + public function isNotControl() : bool |
| 63 | + { |
| 64 | + return ! $this->isControl(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Get the raw character or control sequence |
| 69 | + */ |
| 70 | + public function get() : string |
| 71 | + { |
| 72 | + return $this->data; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Get the actual control name that this sequence represents. |
| 77 | + * One of the class constants. Eg. self::UP. |
| 78 | + * |
| 79 | + * Throws an exception if the character is not actually a control sequence |
| 80 | + */ |
| 81 | + public function getControl() : string |
| 82 | + { |
| 83 | + if (!isset(static::$controls[$this->data])) { |
| 84 | + throw new \RuntimeException(sprintf('Character "%s" is not a control', $this->data)); |
| 85 | + } |
| 86 | + |
| 87 | + return static::$controls[$this->data]; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Get the raw character or control sequence |
| 92 | + */ |
| 93 | + public function __toString() : string |
| 94 | + { |
| 95 | + return $this->get(); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Does the given control name exist? eg self::UP. |
| 100 | + */ |
| 101 | + public static function controlExists(string $controlName) : bool |
| 102 | + { |
| 103 | + return in_array($controlName, static::$controls, true); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Get all of the available control names |
| 108 | + */ |
| 109 | + public static function getControls() : array |
| 110 | + { |
| 111 | + return array_values(array_unique(static::$controls)); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Create a instance from a given control name. Throws an exception if the |
| 116 | + * control name does not exist. |
| 117 | + */ |
| 118 | + public static function fromControlName(string $controlName) : self |
| 119 | + { |
| 120 | + if (!static::controlExists($controlName)) { |
| 121 | + throw new \InvalidArgumentException(sprintf('Control "%s" does not exist', $controlName)); |
| 122 | + } |
| 123 | + |
| 124 | + return new static(array_search($controlName, static::$controls, true)); |
| 125 | + } |
| 126 | +} |
0 commit comments