ChoiceQuestion.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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($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. /**
  108. * Returns the default answer validator.
  109. *
  110. * @return callable
  111. */
  112. private function getDefaultValidator()
  113. {
  114. $choices = $this->choices;
  115. $errorMessage = $this->errorMessage;
  116. $multiselect = $this->multiselect;
  117. $isAssoc = $this->isAssoc($choices);
  118. return function ($selected) use ($choices, $errorMessage, $multiselect, $isAssoc) {
  119. // Collapse all spaces.
  120. $selectedChoices = str_replace(' ', '', $selected);
  121. if ($multiselect) {
  122. // Check for a separated comma values
  123. if (!preg_match('/^[^,]+(?:,[^,]+)*$/', $selectedChoices, $matches)) {
  124. throw new InvalidArgumentException(sprintf($errorMessage, $selected));
  125. }
  126. $selectedChoices = explode(',', $selectedChoices);
  127. } else {
  128. $selectedChoices = array($selected);
  129. }
  130. $multiselectChoices = array();
  131. foreach ($selectedChoices as $value) {
  132. $results = array();
  133. foreach ($choices as $key => $choice) {
  134. if ($choice === $value) {
  135. $results[] = $key;
  136. }
  137. }
  138. if (\count($results) > 1) {
  139. throw new InvalidArgumentException(sprintf('The provided answer is ambiguous. Value should be one of %s.', implode(' or ', $results)));
  140. }
  141. $result = array_search($value, $choices);
  142. if (!$isAssoc) {
  143. if (false !== $result) {
  144. $result = $choices[$result];
  145. } elseif (isset($choices[$value])) {
  146. $result = $choices[$value];
  147. }
  148. } elseif (false === $result && isset($choices[$value])) {
  149. $result = $value;
  150. }
  151. if (false === $result) {
  152. throw new InvalidArgumentException(sprintf($errorMessage, $value));
  153. }
  154. $multiselectChoices[] = (string) $result;
  155. }
  156. if ($multiselect) {
  157. return $multiselectChoices;
  158. }
  159. return current($multiselectChoices);
  160. };
  161. }
  162. }