No Description

Question.php 6.8KB

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