ChoiceQuestion.php 4.8 KB

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