ChoiceQuestion.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 $choices;
  20. private $multiselect = false;
  21. private $prompt = ' > ';
  22. private $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, $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. * @return array
  42. */
  43. public function getChoices()
  44. {
  45. return $this->choices;
  46. }
  47. /**
  48. * Sets multiselect option.
  49. *
  50. * When multiselect is set to true, multiple choices can be answered.
  51. *
  52. * @param bool $multiselect
  53. *
  54. * @return $this
  55. */
  56. public function setMultiselect($multiselect)
  57. {
  58. $this->multiselect = $multiselect;
  59. $this->setValidator($this->getDefaultValidator());
  60. return $this;
  61. }
  62. /**
  63. * Returns whether the choices are multiselect.
  64. *
  65. * @return bool
  66. */
  67. public function isMultiselect()
  68. {
  69. return $this->multiselect;
  70. }
  71. /**
  72. * Gets the prompt for choices.
  73. *
  74. * @return string
  75. */
  76. public function getPrompt()
  77. {
  78. return $this->prompt;
  79. }
  80. /**
  81. * Sets the prompt for choices.
  82. *
  83. * @param string $prompt
  84. *
  85. * @return $this
  86. */
  87. public function setPrompt($prompt)
  88. {
  89. $this->prompt = $prompt;
  90. return $this;
  91. }
  92. /**
  93. * Sets the error message for invalid values.
  94. *
  95. * The error message has a string placeholder (%s) for the invalid value.
  96. *
  97. * @param string $errorMessage
  98. *
  99. * @return $this
  100. */
  101. public function setErrorMessage($errorMessage)
  102. {
  103. $this->errorMessage = $errorMessage;
  104. $this->setValidator($this->getDefaultValidator());
  105. return $this;
  106. }
  107. private function getDefaultValidator(): callable
  108. {
  109. $choices = $this->choices;
  110. $errorMessage = $this->errorMessage;
  111. $multiselect = $this->multiselect;
  112. $isAssoc = $this->isAssoc($choices);
  113. return function ($selected) use ($choices, $errorMessage, $multiselect, $isAssoc) {
  114. if ($multiselect) {
  115. // Check for a separated comma values
  116. if (!preg_match('/^[^,]+(?:,[^,]+)*$/', $selected, $matches)) {
  117. throw new InvalidArgumentException(sprintf($errorMessage, $selected));
  118. }
  119. $selectedChoices = array_map('trim', explode(',', $selected));
  120. } else {
  121. $selectedChoices = [trim($selected)];
  122. }
  123. $multiselectChoices = [];
  124. foreach ($selectedChoices as $value) {
  125. $results = [];
  126. foreach ($choices as $key => $choice) {
  127. if ($choice === $value) {
  128. $results[] = $key;
  129. }
  130. }
  131. if (\count($results) > 1) {
  132. throw new InvalidArgumentException(sprintf('The provided answer is ambiguous. Value should be one of %s.', implode(' or ', $results)));
  133. }
  134. $result = array_search($value, $choices);
  135. if (!$isAssoc) {
  136. if (false !== $result) {
  137. $result = $choices[$result];
  138. } elseif (isset($choices[$value])) {
  139. $result = $choices[$value];
  140. }
  141. } elseif (false === $result && isset($choices[$value])) {
  142. $result = $value;
  143. }
  144. if (false === $result) {
  145. throw new InvalidArgumentException(sprintf($errorMessage, $value));
  146. }
  147. $multiselectChoices[] = (string) $result;
  148. }
  149. if ($multiselect) {
  150. return $multiselectChoices;
  151. }
  152. return current($multiselectChoices);
  153. };
  154. }
  155. }