暫無描述

ChoiceQuestion.php 4.9KB

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