TYPO3  7.6
ChoiceQuestion.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Symfony\Component\Console\Question;
13 
19 class ChoiceQuestion extends Question
20 {
21  private $choices;
22  private $multiselect = false;
23  private $prompt = ' > ';
24  private $errorMessage = 'Value "%s" is invalid';
25 
33  public function __construct($question, array $choices, $default = null)
34  {
35  parent::__construct($question, $default);
36 
37  $this->choices = $choices;
38  $this->setValidator($this->getDefaultValidator());
39  $this->setAutocompleterValues($choices);
40  }
41 
47  public function getChoices()
48  {
49  return $this->choices;
50  }
51 
61  public function setMultiselect($multiselect)
62  {
63  $this->multiselect = $multiselect;
64  $this->setValidator($this->getDefaultValidator());
65 
66  return $this;
67  }
68 
74  public function getPrompt()
75  {
76  return $this->prompt;
77  }
78 
86  public function setPrompt($prompt)
87  {
88  $this->prompt = $prompt;
89 
90  return $this;
91  }
92 
103  {
104  $this->errorMessage = $errorMessage;
105  $this->setValidator($this->getDefaultValidator());
106 
107  return $this;
108  }
109 
115  private function getDefaultValidator()
116  {
120  $isAssoc = $this->isAssoc($choices);
121 
122  return function ($selected) use ($choices, $errorMessage, $multiselect, $isAssoc) {
123  // Collapse all spaces.
124  $selectedChoices = str_replace(' ', '', $selected);
125 
126  if ($multiselect) {
127  // Check for a separated comma values
128  if (!preg_match('/^[a-zA-Z0-9_-]+(?:,[a-zA-Z0-9_-]+)*$/', $selectedChoices, $matches)) {
129  throw new \InvalidArgumentException(sprintf($errorMessage, $selected));
130  }
131  $selectedChoices = explode(',', $selectedChoices);
132  } else {
133  $selectedChoices = array($selected);
134  }
135 
136  $multiselectChoices = array();
137  foreach ($selectedChoices as $value) {
138  $results = array();
139  foreach ($choices as $key => $choice) {
140  if ($choice === $value) {
141  $results[] = $key;
142  }
143  }
144 
145  if (count($results) > 1) {
146  throw new \InvalidArgumentException(sprintf('The provided answer is ambiguous. Value should be one of %s.', implode(' or ', $results)));
147  }
148 
149  $result = array_search($value, $choices);
150 
151  if (!$isAssoc) {
152  if (false !== $result) {
153  $result = $choices[$result];
154  } elseif (isset($choices[$value])) {
155  $result = $choices[$value];
156  }
157  } elseif (false === $result && isset($choices[$value])) {
158  $result = $value;
159  }
160 
161  if (false === $result) {
162  throw new \InvalidArgumentException(sprintf($errorMessage, $value));
163  }
164 
165  $multiselectChoices[] = (string) $result;
166  }
167 
168  if ($multiselect) {
169  return $multiselectChoices;
170  }
171 
172  return current($multiselectChoices);
173  };
174  }
175 }