Question.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 $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 function () use ($values) {
  129. return $values;
  130. };
  131. } elseif ($values instanceof \Traversable) {
  132. $valueCache = null;
  133. $callback = static function () use ($values, &$valueCache) {
  134. return $valueCache ?? $valueCache = iterator_to_array($values, false);
  135. };
  136. } else {
  137. $callback = null;
  138. }
  139. return $this->setAutocompleterCallback($callback);
  140. }
  141. /**
  142. * Gets the callback function used for the autocompleter.
  143. */
  144. public function getAutocompleterCallback(): ?callable
  145. {
  146. return $this->autocompleterCallback;
  147. }
  148. /**
  149. * Sets the callback function used for the autocompleter.
  150. *
  151. * The callback is passed the user input as argument and should return an iterable of corresponding suggestions.
  152. *
  153. * @return $this
  154. */
  155. public function setAutocompleterCallback(callable $callback = null): static
  156. {
  157. if ($this->hidden && null !== $callback) {
  158. throw new LogicException('A hidden question cannot use the autocompleter.');
  159. }
  160. $this->autocompleterCallback = null === $callback || $callback instanceof \Closure ? $callback : \Closure::fromCallable($callback);
  161. return $this;
  162. }
  163. /**
  164. * Sets a validator for the question.
  165. *
  166. * @return $this
  167. */
  168. public function setValidator(callable $validator = null): static
  169. {
  170. $this->validator = null === $validator || $validator instanceof \Closure ? $validator : \Closure::fromCallable($validator);
  171. return $this;
  172. }
  173. /**
  174. * Gets the validator for the question.
  175. */
  176. public function getValidator(): ?callable
  177. {
  178. return $this->validator;
  179. }
  180. /**
  181. * Sets the maximum number of attempts.
  182. *
  183. * Null means an unlimited number of attempts.
  184. *
  185. * @return $this
  186. *
  187. * @throws InvalidArgumentException in case the number of attempts is invalid
  188. */
  189. public function setMaxAttempts(?int $attempts): static
  190. {
  191. if (null !== $attempts && $attempts < 1) {
  192. throw new InvalidArgumentException('Maximum number of attempts must be a positive value.');
  193. }
  194. $this->attempts = $attempts;
  195. return $this;
  196. }
  197. /**
  198. * Gets the maximum number of attempts.
  199. *
  200. * Null means an unlimited number of attempts.
  201. */
  202. public function getMaxAttempts(): ?int
  203. {
  204. return $this->attempts;
  205. }
  206. /**
  207. * Sets a normalizer for the response.
  208. *
  209. * The normalizer can be a callable (a string), a closure or a class implementing __invoke.
  210. *
  211. * @return $this
  212. */
  213. public function setNormalizer(callable $normalizer): static
  214. {
  215. $this->normalizer = $normalizer instanceof \Closure ? $normalizer : \Closure::fromCallable($normalizer);
  216. return $this;
  217. }
  218. /**
  219. * Gets the normalizer for the response.
  220. *
  221. * The normalizer can ba a callable (a string), a closure or a class implementing __invoke.
  222. */
  223. public function getNormalizer(): ?callable
  224. {
  225. return $this->normalizer;
  226. }
  227. protected function isAssoc(array $array)
  228. {
  229. return (bool) \count(array_filter(array_keys($array), 'is_string'));
  230. }
  231. public function isTrimmable(): bool
  232. {
  233. return $this->trimmable;
  234. }
  235. /**
  236. * @return $this
  237. */
  238. public function setTrimmable(bool $trimmable): static
  239. {
  240. $this->trimmable = $trimmable;
  241. return $this;
  242. }
  243. }