ChoiceQuestion.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. // Collapse all spaces.
  115. $selectedChoices = str_replace(' ', '', $selected);
  116. if ($multiselect) {
  117. // Check for a separated comma values
  118. if (!preg_match('/^[^,]+(?:,[^,]+)*$/', $selectedChoices, $matches)) {
  119. throw new InvalidArgumentException(sprintf($errorMessage, $selected));
  120. }
  121. $selectedChoices = explode(',', $selectedChoices);
  122. } else {
  123. $selectedChoices = array($selected);
  124. }
  125. $multiselectChoices = array();
  126. foreach ($selectedChoices as $value) {
  127. $results = array();
  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. $multiselectChoices[] = (string) $result;
  150. }
  151. if ($multiselect) {
  152. return $multiselectChoices;
  153. }
  154. return current($multiselectChoices);
  155. };
  156. }
  157. }