Question.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 $autocompleterCallback;
  25. private $validator;
  26. private $default;
  27. private $normalizer;
  28. private $trimmable = true;
  29. /**
  30. * @param string $question The question to ask to the user
  31. * @param mixed $default The default answer to return if the user enters nothing
  32. */
  33. public function __construct(string $question, $default = null)
  34. {
  35. $this->question = $question;
  36. $this->default = $default;
  37. }
  38. /**
  39. * Returns the question.
  40. *
  41. * @return string
  42. */
  43. public function getQuestion()
  44. {
  45. return $this->question;
  46. }
  47. /**
  48. * Returns the default answer.
  49. *
  50. * @return mixed
  51. */
  52. public function getDefault()
  53. {
  54. return $this->default;
  55. }
  56. /**
  57. * Returns whether the user response must be hidden.
  58. *
  59. * @return bool
  60. */
  61. public function isHidden()
  62. {
  63. return $this->hidden;
  64. }
  65. /**
  66. * Sets whether the user response must be hidden or not.
  67. *
  68. * @param bool $hidden
  69. *
  70. * @return $this
  71. *
  72. * @throws LogicException In case the autocompleter is also used
  73. */
  74. public function setHidden($hidden)
  75. {
  76. if ($this->autocompleterCallback) {
  77. throw new LogicException('A hidden question cannot use the autocompleter.');
  78. }
  79. $this->hidden = (bool) $hidden;
  80. return $this;
  81. }
  82. /**
  83. * In case the response can not be hidden, whether to fallback on non-hidden question or not.
  84. *
  85. * @return bool
  86. */
  87. public function isHiddenFallback()
  88. {
  89. return $this->hiddenFallback;
  90. }
  91. /**
  92. * Sets whether to fallback on non-hidden question if the response can not be hidden.
  93. *
  94. * @param bool $fallback
  95. *
  96. * @return $this
  97. */
  98. public function setHiddenFallback($fallback)
  99. {
  100. $this->hiddenFallback = (bool) $fallback;
  101. return $this;
  102. }
  103. /**
  104. * Gets values for the autocompleter.
  105. *
  106. * @return iterable|null
  107. */
  108. public function getAutocompleterValues()
  109. {
  110. $callback = $this->getAutocompleterCallback();
  111. return $callback ? $callback('') : null;
  112. }
  113. /**
  114. * Sets values for the autocompleter.
  115. *
  116. * @return $this
  117. *
  118. * @throws LogicException
  119. */
  120. public function setAutocompleterValues(?iterable $values)
  121. {
  122. if (\is_array($values)) {
  123. $values = $this->isAssoc($values) ? array_merge(array_keys($values), array_values($values)) : array_values($values);
  124. $callback = static function () use ($values) {
  125. return $values;
  126. };
  127. } elseif ($values instanceof \Traversable) {
  128. $valueCache = null;
  129. $callback = static function () use ($values, &$valueCache) {
  130. return $valueCache ?? $valueCache = iterator_to_array($values, false);
  131. };
  132. } else {
  133. $callback = null;
  134. }
  135. return $this->setAutocompleterCallback($callback);
  136. }
  137. /**
  138. * Gets the callback function used for the autocompleter.
  139. */
  140. public function getAutocompleterCallback(): ?callable
  141. {
  142. return $this->autocompleterCallback;
  143. }
  144. /**
  145. * Sets the callback function used for the autocompleter.
  146. *
  147. * The callback is passed the user input as argument and should return an iterable of corresponding suggestions.
  148. *
  149. * @return $this
  150. */
  151. public function setAutocompleterCallback(callable $callback = null): self
  152. {
  153. if ($this->hidden && null !== $callback) {
  154. throw new LogicException('A hidden question cannot use the autocompleter.');
  155. }
  156. $this->autocompleterCallback = $callback;
  157. return $this;
  158. }
  159. /**
  160. * Sets a validator for the question.
  161. *
  162. * @return $this
  163. */
  164. public function setValidator(callable $validator = null)
  165. {
  166. $this->validator = $validator;
  167. return $this;
  168. }
  169. /**
  170. * Gets the validator for the question.
  171. *
  172. * @return callable|null
  173. */
  174. public function getValidator()
  175. {
  176. return $this->validator;
  177. }
  178. /**
  179. * Sets the maximum number of attempts.
  180. *
  181. * Null means an unlimited number of attempts.
  182. *
  183. * @return $this
  184. *
  185. * @throws InvalidArgumentException in case the number of attempts is invalid
  186. */
  187. public function setMaxAttempts(?int $attempts)
  188. {
  189. if (null !== $attempts) {
  190. $attempts = (int) $attempts;
  191. if ($attempts < 1) {
  192. throw new InvalidArgumentException('Maximum number of attempts must be a positive value.');
  193. }
  194. }
  195. $this->attempts = $attempts;
  196. return $this;
  197. }
  198. /**
  199. * Gets the maximum number of attempts.
  200. *
  201. * Null means an unlimited number of attempts.
  202. *
  203. * @return int|null
  204. */
  205. public function getMaxAttempts()
  206. {
  207. return $this->attempts;
  208. }
  209. /**
  210. * Sets a normalizer for the response.
  211. *
  212. * The normalizer can be a callable (a string), a closure or a class implementing __invoke.
  213. *
  214. * @return $this
  215. */
  216. public function setNormalizer(callable $normalizer)
  217. {
  218. $this->normalizer = $normalizer;
  219. return $this;
  220. }
  221. /**
  222. * Gets the normalizer for the response.
  223. *
  224. * The normalizer can ba a callable (a string), a closure or a class implementing __invoke.
  225. *
  226. * @return callable|null
  227. */
  228. public function getNormalizer()
  229. {
  230. return $this->normalizer;
  231. }
  232. protected function isAssoc(array $array)
  233. {
  234. return (bool) \count(array_filter(array_keys($array), 'is_string'));
  235. }
  236. public function isTrimmable(): bool
  237. {
  238. return $this->trimmable;
  239. }
  240. /**
  241. * @return $this
  242. */
  243. public function setTrimmable(bool $trimmable): self
  244. {
  245. $this->trimmable = $trimmable;
  246. return $this;
  247. }
  248. }