ChoiceQuestion.php 5.1 KB

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