| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 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 |
|
| 14 |
use Symfony\Component\Console\Exception\InvalidArgumentException; |
| 15 |
|
| 16 |
/** |
| 17 |
* Represents a choice question. |
| 18 |
* |
| 19 |
* @author Fabien Potencier <[email protected]> |
| 20 |
*/ |
| 21 |
class ChoiceQuestion extends Question |
| 22 |
{ |
| 23 |
private bool $multiselect = false; |
| 24 |
private string $prompt = ' > '; |
| 25 |
private string $errorMessage = 'Value "%s" is invalid'; |
| 26 |
|
| 27 |
/** |
| 28 |
* @param string $question The question to ask to the user |
| 29 |
* @param array<string|bool|int|float|\Stringable> $choices The list of available choices |
| 30 |
* @param string|bool|int|float|null $default The default answer to return |
| 31 |
*/ |
| 32 |
public function __construct( |
| 33 |
string $question, |
| 34 |
private array $choices, |
| 35 |
string|bool|int|float|null $default = null, |
| 36 |
) { |
| 37 |
if (!$choices) { |
| 38 |
throw new \LogicException('Choice question must have at least 1 choice available.'); |
| 39 |
} |
| 40 |
|
| 41 |
parent::__construct($question, $default); |
| 42 |
|
| 43 |
$this->setValidator($this->getDefaultValidator()); |
| 44 |
$this->setAutocompleterValues($choices); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @return array<string|bool|int|float|\Stringable> |
| 49 |
*/ |
| 50 |
public function getChoices(): array |
| 51 |
{ |
| 52 |
return $this->choices; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Sets multiselect option. |
| 57 |
* |
| 58 |
* When multiselect is set to true, multiple choices can be answered. |
| 59 |
* |
| 60 |
* @return $this |
| 61 |
*/ |
| 62 |
public function setMultiselect(bool $multiselect): static |
| 63 |
{ |
| 64 |
$this->multiselect = $multiselect; |
| 65 |
$this->setValidator($this->getDefaultValidator()); |
| 66 |
|
| 67 |
return $this; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Returns whether the choices are multiselect. |
| 72 |
*/ |
| 73 |
public function isMultiselect(): bool |
| 74 |
{ |
| 75 |
return $this->multiselect; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Gets the prompt for choices. |
| 80 |
*/ |
| 81 |
public function getPrompt(): string |
| 82 |
{ |
| 83 |
return $this->prompt; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Sets the prompt for choices. |
| 88 |
* |
| 89 |
* @return $this |
| 90 |
*/ |
| 91 |
public function setPrompt(string $prompt): static |
| 92 |
{ |
| 93 |
$this->prompt = $prompt; |
| 94 |
|
| 95 |
return $this; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Sets the error message for invalid values. |
| 100 |
* |
| 101 |
* The error message has a string placeholder (%s) for the invalid value. |
| 102 |
* |
| 103 |
* @return $this |
| 104 |
*/ |
| 105 |
public function setErrorMessage(string $errorMessage): static |
| 106 |
{ |
| 107 |
$this->errorMessage = $errorMessage; |
| 108 |
$this->setValidator($this->getDefaultValidator()); |
| 109 |
|
| 110 |
return $this; |
| 111 |
} |
| 112 |
|
| 113 |
private function getDefaultValidator(): callable |
| 114 |
{ |
| 115 |
$choices = $this->choices; |
| 116 |
$errorMessage = $this->errorMessage; |
| 117 |
$multiselect = $this->multiselect; |
| 118 |
$isAssoc = $this->isAssoc($choices); |
| 119 |
|
| 120 |
return function ($selected) use ($choices, $errorMessage, $multiselect, $isAssoc) { |
| 121 |
if ($multiselect) { |
| 122 |
// Check for a separated comma values |
| 123 |
if (!preg_match('/^[^,]+(?:,[^,]+)*$/', (string) $selected, $matches)) { |
| 124 |
throw new InvalidArgumentException(\sprintf($errorMessage, $selected)); |
| 125 |
} |
| 126 |
|
| 127 |
$selectedChoices = explode(',', (string) $selected); |
| 128 |
} else { |
| 129 |
$selectedChoices = [$selected]; |
| 130 |
} |
| 131 |
|
| 132 |
if ($this->isTrimmable()) { |
| 133 |
foreach ($selectedChoices as $k => $v) { |
| 134 |
$selectedChoices[$k] = trim((string) $v); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
$multiselectChoices = []; |
| 139 |
foreach ($selectedChoices as $value) { |
| 140 |
$results = []; |
| 141 |
foreach ($choices as $key => $choice) { |
| 142 |
if ($choice === $value) { |
| 143 |
$results[] = $key; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
if (\count($results) > 1) { |
| 148 |
throw new InvalidArgumentException(\sprintf('The provided answer is ambiguous. Value should be one of "%s".', implode('" or "', $results))); |
| 149 |
} |
| 150 |
|
| 151 |
$result = array_search($value, $choices); |
| 152 |
|
| 153 |
if (!$isAssoc) { |
| 154 |
if (false !== $result) { |
| 155 |
$result = $choices[$result]; |
| 156 |
} elseif (isset($choices[$value])) { |
| 157 |
$result = $choices[$value]; |
| 158 |
} |
| 159 |
} elseif (false === $result && isset($choices[$value])) { |
| 160 |
$result = $value; |
| 161 |
} |
| 162 |
|
| 163 |
if (false === $result) { |
| 164 |
throw new InvalidArgumentException(\sprintf($errorMessage, $value)); |
| 165 |
} |
| 166 |
|
| 167 |
// For associative choices, consistently return the key as string: |
| 168 |
$multiselectChoices[] = $isAssoc ? (string) $result : $result; |
| 169 |
} |
| 170 |
|
| 171 |
if ($multiselect) { |
| 172 |
return $multiselectChoices; |
| 173 |
} |
| 174 |
|
| 175 |
return current($multiselectChoices); |
| 176 |
}; |
| 177 |
} |
| 178 |
} |