1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\Input; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @author Aydin Hassan <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
class Number implements Input |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var InputIO |
12
|
|
|
*/ |
13
|
|
|
private $inputIO; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $promptText = 'Enter a number:'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $validationFailedText = 'Not a valid number, try again'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $placeholderText = ''; |
29
|
|
|
|
30
|
|
|
public function __construct(InputIO $inputIO) |
31
|
|
|
{ |
32
|
|
|
$this->inputIO = $inputIO; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function setPromptText(string $promptText) : Input |
36
|
|
|
{ |
37
|
|
|
$this->promptText = $promptText; |
38
|
|
|
|
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getPromptText() : string |
43
|
|
|
{ |
44
|
|
|
return $this->promptText; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function setValidationFailedText(string $validationFailedText) : Input |
48
|
|
|
{ |
49
|
|
|
$this->validationFailedText = $validationFailedText; |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getValidationFailedText() : string |
55
|
|
|
{ |
56
|
|
|
return $this->validationFailedText; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setPlaceholderText(string $placeholderText) : Input |
60
|
|
|
{ |
61
|
|
|
$this->placeholderText = $placeholderText; |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getPlaceholderText() : string |
67
|
|
|
{ |
68
|
|
|
return $this->placeholderText; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function ask() : InputResult |
72
|
|
|
{ |
73
|
|
|
$this->inputIO->registerInputMap("\033[A", 'up'); |
74
|
|
|
$this->inputIO->registerInputMap("\033[B", 'down'); |
75
|
|
|
|
76
|
|
|
$this->inputIO->registerControlCallback('up', function (InputIO $inputIO, string $input) { |
77
|
|
|
return $this->validate($input) ? $input + 1 : $input; |
78
|
|
|
}); |
79
|
|
|
|
80
|
|
|
$this->inputIO->registerControlCallback('down', function (InputIO $inputIO, string $input) { |
81
|
|
|
return $this->validate($input) ? $input - 1 : $input; |
82
|
|
|
}); |
83
|
|
|
|
84
|
|
|
return $this->inputIO->collect($this); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function validate(string $input) : bool |
88
|
|
|
{ |
89
|
|
|
return (bool) preg_match('/^\d+$/', $input); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function format(string $value) : string |
93
|
|
|
{ |
94
|
|
|
return $value; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|