No Description

ChoiceQuestion.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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(array_keys($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. return function ($selected) use ($choices, $errorMessage, $multiselect) {
  107. // Collapse all spaces.
  108. $selectedChoices = str_replace(' ', '', $selected);
  109. if ($multiselect) {
  110. // Check for a separated comma values
  111. if (!preg_match('/^[a-zA-Z0-9_-]+(?:,[a-zA-Z0-9_-]+)*$/', $selectedChoices, $matches)) {
  112. throw new \InvalidArgumentException(sprintf($errorMessage, $selected));
  113. }
  114. $selectedChoices = explode(',', $selectedChoices);
  115. } else {
  116. $selectedChoices = array($selected);
  117. }
  118. $multiselectChoices = array();
  119. foreach ($selectedChoices as $value) {
  120. if (empty($choices[$value])) {
  121. throw new \InvalidArgumentException(sprintf($errorMessage, $value));
  122. }
  123. array_push($multiselectChoices, $choices[$value]);
  124. }
  125. if ($multiselect) {
  126. return $multiselectChoices;
  127. }
  128. return $choices[$selected];
  129. };
  130. }
  131. }