Question.php 6.7 KB

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