Question.php 6.8 KB

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