Question.php 7.3 KB

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