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