Question.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. use Symfony\Component\Console\Exception\LogicException;
  13. /**
  14. * Represents a Question.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class Question
  19. {
  20. private $question;
  21. private $attempts;
  22. private $hidden = false;
  23. private $hiddenFallback = true;
  24. private $autocompleterValues;
  25. private $validator;
  26. private $default;
  27. private $normalizer;
  28. /**
  29. * @param string $question The question to ask to the user
  30. * @param mixed $default The default answer to return if the user enters nothing
  31. */
  32. public function __construct($question, $default = null)
  33. {
  34. $this->question = $question;
  35. $this->default = $default;
  36. }
  37. /**
  38. * Returns the question.
  39. *
  40. * @return string
  41. */
  42. public function getQuestion()
  43. {
  44. return $this->question;
  45. }
  46. /**
  47. * Returns the default answer.
  48. *
  49. * @return mixed
  50. */
  51. public function getDefault()
  52. {
  53. return $this->default;
  54. }
  55. /**
  56. * Returns whether the user response must be hidden.
  57. *
  58. * @return bool
  59. */
  60. public function isHidden()
  61. {
  62. return $this->hidden;
  63. }
  64. /**
  65. * Sets whether the user response must be hidden or not.
  66. *
  67. * @param bool $hidden
  68. *
  69. * @return $this
  70. *
  71. * @throws LogicException In case the autocompleter is also used
  72. */
  73. public function setHidden($hidden)
  74. {
  75. if ($this->autocompleterValues) {
  76. throw new LogicException('A hidden question cannot use the autocompleter.');
  77. }
  78. $this->hidden = (bool) $hidden;
  79. return $this;
  80. }
  81. /**
  82. * In case the response can not be hidden, whether to fallback on non-hidden question or not.
  83. *
  84. * @return bool
  85. */
  86. public function isHiddenFallback()
  87. {
  88. return $this->hiddenFallback;
  89. }
  90. /**
  91. * Sets whether to fallback on non-hidden question if the response can not be hidden.
  92. *
  93. * @param bool $fallback
  94. *
  95. * @return $this
  96. */
  97. public function setHiddenFallback($fallback)
  98. {
  99. $this->hiddenFallback = (bool) $fallback;
  100. return $this;
  101. }
  102. /**
  103. * Gets values for the autocompleter.
  104. *
  105. * @return iterable|null
  106. */
  107. public function getAutocompleterValues()
  108. {
  109. return $this->autocompleterValues;
  110. }
  111. /**
  112. * Sets values for the autocompleter.
  113. *
  114. * @param iterable|null $values
  115. *
  116. * @return $this
  117. *
  118. * @throws InvalidArgumentException
  119. * @throws LogicException
  120. */
  121. public function setAutocompleterValues($values)
  122. {
  123. if (\is_array($values)) {
  124. $values = $this->isAssoc($values) ? array_merge(array_keys($values), array_values($values)) : array_values($values);
  125. }
  126. if (null !== $values && !\is_array($values) && !$values instanceof \Traversable) {
  127. throw new InvalidArgumentException('Autocompleter values can be either an array, `null` or a `Traversable` object.');
  128. }
  129. if ($this->hidden) {
  130. throw new LogicException('A hidden question cannot use the autocompleter.');
  131. }
  132. $this->autocompleterValues = $values;
  133. return $this;
  134. }
  135. /**
  136. * Sets a validator for the question.
  137. *
  138. * @return $this
  139. */
  140. public function setValidator(callable $validator = null)
  141. {
  142. $this->validator = $validator;
  143. return $this;
  144. }
  145. /**
  146. * Gets the validator for the question.
  147. *
  148. * @return callable|null
  149. */
  150. public function getValidator()
  151. {
  152. return $this->validator;
  153. }
  154. /**
  155. * Sets the maximum number of attempts.
  156. *
  157. * Null means an unlimited number of attempts.
  158. *
  159. * @param int|null $attempts
  160. *
  161. * @return $this
  162. *
  163. * @throws InvalidArgumentException in case the number of attempts is invalid
  164. */
  165. public function setMaxAttempts($attempts)
  166. {
  167. if (null !== $attempts && $attempts < 1) {
  168. throw new InvalidArgumentException('Maximum number of attempts must be a positive value.');
  169. }
  170. $this->attempts = $attempts;
  171. return $this;
  172. }
  173. /**
  174. * Gets the maximum number of attempts.
  175. *
  176. * Null means an unlimited number of attempts.
  177. *
  178. * @return int|null
  179. */
  180. public function getMaxAttempts()
  181. {
  182. return $this->attempts;
  183. }
  184. /**
  185. * Sets a normalizer for the response.
  186. *
  187. * The normalizer can be a callable (a string), a closure or a class implementing __invoke.
  188. *
  189. * @return $this
  190. */
  191. public function setNormalizer(callable $normalizer)
  192. {
  193. $this->normalizer = $normalizer;
  194. return $this;
  195. }
  196. /**
  197. * Gets the normalizer for the response.
  198. *
  199. * The normalizer can ba a callable (a string), a closure or a class implementing __invoke.
  200. *
  201. * @return callable|null
  202. */
  203. public function getNormalizer()
  204. {
  205. return $this->normalizer;
  206. }
  207. protected function isAssoc($array)
  208. {
  209. return (bool) \count(array_filter(array_keys($array), 'is_string'));
  210. }
  211. }