Question.php 7.7 KB

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