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