1 | <?php |
||
8 | class Input extends InputAbstract |
||
9 | { |
||
10 | /** |
||
11 | * An array of acceptable responses |
||
12 | * |
||
13 | * @var array|object $acceptable |
||
14 | */ |
||
15 | protected $acceptable; |
||
16 | |||
17 | /** |
||
18 | * Whether we should be strict about the response given |
||
19 | * |
||
20 | * @var boolean $strict |
||
21 | */ |
||
22 | protected $strict = false; |
||
23 | |||
24 | /** |
||
25 | * Whether to accept multiple lines of input |
||
26 | * |
||
27 | * Terminated by ^D |
||
28 | * |
||
29 | * @var boolean $multiLine |
||
30 | */ |
||
31 | protected $multiLine = false; |
||
32 | |||
33 | /** |
||
34 | * Whether we should display the acceptable responses to the user |
||
35 | * |
||
36 | * @var boolean $show_acceptable |
||
37 | */ |
||
38 | protected $show_acceptable = false; |
||
39 | |||
40 | /** |
||
41 | * A default answer in the case of no user response, |
||
42 | * prevents re-prompting |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $default = ''; |
||
47 | |||
48 | 68 | public function __construct($prompt, ReaderInterface $reader = null) |
|
53 | |||
54 | /** |
||
55 | * Do it! Prompt the user for information! |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | 64 | public function prompt() |
|
71 | |||
72 | /** |
||
73 | * Define the acceptable responses and whether or not to |
||
74 | * display them to the user |
||
75 | * |
||
76 | * @param array|object $acceptable |
||
77 | * @param boolean $show |
||
78 | * |
||
79 | * @return \League\CLImate\TerminalObject\Dynamic\Input |
||
80 | */ |
||
81 | 48 | public function accept($acceptable, $show = false) |
|
88 | |||
89 | /** |
||
90 | * Define whether we should be strict about exact responses |
||
91 | * |
||
92 | * @return \League\CLImate\TerminalObject\Dynamic\Input |
||
93 | */ |
||
94 | 24 | public function strict() |
|
100 | |||
101 | /** |
||
102 | * Set a default response |
||
103 | * |
||
104 | * @param string $default |
||
105 | * |
||
106 | * @return \League\CLImate\TerminalObject\Dynamic\Input |
||
107 | */ |
||
108 | 12 | public function defaultTo($default) |
|
114 | |||
115 | /** |
||
116 | * Set multiline input to true |
||
117 | * |
||
118 | * @return \League\CLImate\TerminalObject\Dynamic\Input |
||
119 | */ |
||
120 | 8 | public function multiLine() |
|
126 | |||
127 | /** |
||
128 | * @return string |
||
129 | */ |
||
130 | 64 | protected function getUserInput() |
|
138 | |||
139 | /** |
||
140 | * Write out the formatted prompt |
||
141 | */ |
||
142 | 68 | protected function writePrompt() |
|
148 | |||
149 | /** |
||
150 | * If no response was given and there is a default, return it, |
||
151 | * otherwise return response |
||
152 | * |
||
153 | * @param string $response |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | 64 | protected function valueOrDefault($response) |
|
165 | |||
166 | /** |
||
167 | * Format the acceptable responses as options |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | 28 | protected function acceptableFormatted() |
|
181 | |||
182 | /** |
||
183 | * Format the acceptable item depending on whether it is the default or not |
||
184 | * |
||
185 | * @param string $item |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | 28 | protected function acceptableItemFormatted($item) |
|
197 | |||
198 | /** |
||
199 | * Format the prompt incorporating spacing and any acceptable options |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | 68 | protected function promptFormatted() |
|
213 | |||
214 | /** |
||
215 | * Apply some string manipulation functions for normalization |
||
216 | * |
||
217 | * @param string|array $var |
||
218 | * @return array |
||
219 | */ |
||
220 | 20 | protected function levelPlayingField($var) |
|
234 | |||
235 | /** |
||
236 | * Determine whether or not the acceptable property is of type closure |
||
237 | * |
||
238 | * @return boolean |
||
239 | */ |
||
240 | 48 | protected function acceptableIsClosure() |
|
244 | |||
245 | /** |
||
246 | * Determine if the user's response is in the acceptable responses array |
||
247 | * |
||
248 | * @param string $response |
||
249 | * |
||
250 | * @return boolean $response |
||
251 | */ |
||
252 | 40 | protected function isAcceptableResponse($response) |
|
263 | |||
264 | /** |
||
265 | * Determine if the user's response is valid based on the current settings |
||
266 | * |
||
267 | * @param string $response |
||
268 | * |
||
269 | * @return boolean $response |
||
270 | */ |
||
271 | 64 | protected function isValidResponse($response) |
|
283 | } |
||
284 |