1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Console\Prompt;
use Zend\Console\Exception;
use Zend\Stdlib\ArrayUtils;
final class Checkbox extends AbstractPrompt
{
/**
* @var string
*/
private $promptText;
/**
* @var bool
*/
private $ignoreCase;
/**
* @var array|Transversable
*/
private $options;
/**
* Checked options
* @var array
*/
private $checkedOptions = [];
/**
* If the response should be echoed to the console or not
* @var bool
*/
private $echo;
/**
* Ask the user to select any number of pre-defined options
*
* @param string $promptText The prompt text to display in console
* @param array|Transversable $options Allowed options
* @param bool $echo True to display selected option?
*/
public function __construct(
$promptText = 'Please select one option (Enter to finish) ',
$options = [],
$ignoreCase = true,
$echo = false
) {
$this->promptText = (string) $promptText;
$this->setOptions($options);
$this->echo = (bool) $echo;
$this->ignoreCase = (bool) $ignoreCase;
}
/**
* Show a list of options and prompt the user to select any number of them.
*
* @return array Checked options
*/
public function show()
{
$this->checkedOptions = [];
$mask = $this->prepareMask();
do {
$this->showAvailableOptions();
$response = $this->readOption($mask);
if ($this->echo) {
$this->showResponse($response);
}
$this->checkOrUncheckOption($response);
} while ($response != "\r" && $response != "\n");
$this->lastResponse = $this->checkedOptions;
return $this->checkedOptions;
}
/**
* Shows the selected option to the screen
* @param string $response
*/
private function showResponse($response)
{
$console = $this->getConsole();
if (isset($this->options[$response])) {
$console->writeLine($this->options[$response]);
} else {
$console->writeLine();
}
}
/**
* Check or uncheck an option
*
* @param string $response
*/
private function checkOrUncheckOption($response)
{
if ($response != "\r" && $response != "\n" && isset($this->options[$response])) {
$pos = array_search($this->options[$response], $this->checkedOptions);
if ($pos === false) {
$this->checkedOptions[] = $this->options[$response];
} else {
array_splice($this->checkedOptions, $pos, 1);
}
}
}
/**
* Generates a mask to to be used by the readChar method.
*
* @return string
*/
private function prepareMask()
{
$mask = implode("", array_keys($this->options)) . "\r\n";
/**
* Normalize the mask if case is irrelevant
*/
if (! $this->ignoreCase) {
return $mask;
}
$mask = implode("", array_unique(str_split(strtolower($mask) . strtoupper($mask))));
return $mask;
}
/**
* Reads a char from console.
*
* @param string $mask
* @return string
*/
private function readOption($mask)
{
/**
* Read char from console
*/
return $this->getConsole()->readChar($mask);
}
/**
* Shows the available options with checked and unchecked states
*/
private function showAvailableOptions()
{
$console = $this->getConsole();
$console->writeLine($this->promptText);
foreach ($this->options as $k => $v) {
$console->writeLine(' ' . $k . ') ' . (in_array($v, $this->checkedOptions) ? '[X] ' : '[ ] ') . $v);
}
}
/**
* Set allowed options
*
* @param array|\Traversable $options
* @throws Exception\InvalidArgumentException
*/
private function setOptions($options)
{
$options = ArrayUtils::iteratorToArray($options);
if (empty($options)) {
throw new Exception\InvalidArgumentException('Please, specify at least one option');
}
$this->options = $options;
}
}