ChoiceQuestion.php 4.9 KB

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