/
AlwaysReturnKeyOfChoiceQuestion.php
175 lines (144 loc) · 4.67 KB
/
AlwaysReturnKeyOfChoiceQuestion.php
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
<?php
namespace Matthias\SymfonyConsoleForm\Console\Helper\Question;
use InvalidArgumentException;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
final class AlwaysReturnKeyOfChoiceQuestion extends ChoiceQuestion
{
/**
* @var ChoiceView[]
*/
private $choiceViews;
/**
* @var bool
*/
private $_multiselect = false;
/**
* @var string
*/
private $_errorMessage = 'Value "%s" is invalid';
/**
* @param string $question The question to ask to the user
* @param array $choiceViews The list of available choices
* @param mixed $default The default answer to return
*/
public function __construct($question, array $choiceViews, $default = null)
{
$this->assertFlatChoiceViewsArray($choiceViews);
$this->choiceViews = $choiceViews;
parent::__construct($question, $this->prepareChoices(), $default);
$this->setAutocompleterValues($this->prepareAutocompleteValues());
}
/**
* @param bool $multiselect
*/
public function setMultiselect(bool $multiselect): static
{
$this->_multiselect = $multiselect;
return parent::setMultiselect($multiselect);
}
/**
* @param string $errorMessage
*/
public function setErrorMessage(string $errorMessage): static
{
$this->_errorMessage = $errorMessage;
return parent::setErrorMessage($errorMessage);
}
/**
* @return callable|null
*/
public function getValidator(): ?callable
{
return function ($selected) {
// Collapse all spaces.
$selectedChoices = str_replace(' ', '', $selected);
if ($this->_multiselect) {
// Check for a separated comma values
if (!preg_match('/^[a-zA-Z0-9_-]+(?:,[a-zA-Z0-9_-]+)*$/', $selectedChoices)) {
throw new InvalidArgumentException(sprintf($this->_errorMessage, $selected));
}
$selectedChoices = explode(',', $selectedChoices);
} else {
$selectedChoices = [$selected];
}
$selectedKeys = [];
foreach ($selectedChoices as $selectedValue) {
$selectedKeys[] = $this->resolveChoiceViewValue($selectedValue);
}
if ($this->_multiselect) {
return $selectedKeys;
}
return current($selectedKeys);
};
}
/**
* @param string $selectedValue The selected value
*
* @throws InvalidArgumentException
*
* @return string The corresponding value of the ChoiceView
*/
private function resolveChoiceViewValue($selectedValue)
{
foreach ($this->choiceViews as $choiceView) {
if (in_array($selectedValue, [$choiceView->data, $choiceView->value, $choiceView->label])) {
return $choiceView->value;
}
}
throw new InvalidArgumentException(sprintf($this->_errorMessage, $selectedValue));
}
/**
* @return array
*/
private function prepareChoices()
{
$choices = [];
foreach ($this->choiceViews as $choiceView) {
$label = $choiceView->label;
$data = $choiceView->data;
if ($data != $choiceView->value && $this->canBeConvertedToString($data)) {
$label .= ' (<comment>' . $data . '</comment>)';
}
$choices[$choiceView->value] = $label;
}
return $choices;
}
/**
* @return array
*/
private function prepareAutocompleteValues()
{
$autocompleteValues = [];
foreach ($this->choiceViews as $choiceView) {
$autocompleteValues[] = $choiceView->value;
$data = $choiceView->data;
if ($this->canBeConvertedToString($data)) {
$autocompleteValues[] = (string)$data;
}
$autocompleteValues[] = $choiceView->label;
}
return $autocompleteValues;
}
/**
* @param array $choiceViews
*
* @throws InvalidArgumentException
*/
private function assertFlatChoiceViewsArray(array $choiceViews)
{
foreach ($choiceViews as $choiceView) {
if (!$choiceView instanceof ChoiceView) {
throw new InvalidArgumentException('Only a flat choice hierarchy is supported');
}
}
}
/**
* @param mixed $value
* @return bool
*/
private function canBeConvertedToString($value)
{
return $value === null || is_scalar($value) || (\is_object($value) && method_exists($value, '__toString'));
}
}