ChoiceQuestion.php 4.7 KB

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