Question.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. /**
  29. * @param string $question The question to ask to the user
  30. * @param mixed $default The default answer to return if the user enters nothing
  31. */
  32. public function __construct(string $question, $default = null)
  33. {
  34. $this->question = $question;
  35. $this->default = $default;
  36. }
  37. /**
  38. * Returns the question.
  39. *
  40. * @return string
  41. */
  42. public function getQuestion()
  43. {
  44. return $this->question;
  45. }
  46. /**
  47. * Returns the default answer.
  48. *
  49. * @return mixed
  50. */
  51. public function getDefault()
  52. {
  53. return $this->default;
  54. }
  55. /**
  56. * Returns whether the user response must be hidden.
  57. *
  58. * @return bool
  59. */
  60. public function isHidden()
  61. {
  62. return $this->hidden;
  63. }
  64. /**
  65. * Sets whether the user response must be hidden or not.
  66. *
  67. * @param bool $hidden
  68. *
  69. * @return $this
  70. *
  71. * @throws LogicException In case the autocompleter is also used
  72. */
  73. public function setHidden($hidden)
  74. {
  75. if ($this->autocompleterCallback) {
  76. throw new LogicException('A hidden question cannot use the autocompleter.');
  77. }
  78. $this->hidden = (bool) $hidden;
  79. return $this;
  80. }
  81. /**
  82. * In case the response can not be hidden, whether to fallback on non-hidden question or not.
  83. *
  84. * @return bool
  85. */
  86. public function isHiddenFallback()
  87. {
  88. return $this->hiddenFallback;
  89. }
  90. /**
  91. * Sets whether to fallback on non-hidden question if the response can not be hidden.
  92. *
  93. * @param bool $fallback
  94. *
  95. * @return $this
  96. */
  97. public function setHiddenFallback($fallback)
  98. {
  99. $this->hiddenFallback = (bool) $fallback;
  100. return $this;
  101. }
  102. /**
  103. * Gets values for the autocompleter.
  104. *
  105. * @return iterable|null
  106. */
  107. public function getAutocompleterValues()
  108. {
  109. $callback = $this->getAutocompleterCallback();
  110. return $callback ? $callback('') : null;
  111. }
  112. /**
  113. * Sets values for the autocompleter.
  114. *
  115. * @param iterable|null $values
  116. *
  117. * @return $this
  118. *
  119. * @throws InvalidArgumentException
  120. * @throws LogicException
  121. */
  122. public function setAutocompleterValues($values)
  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 function () use ($values) {
  127. return $values;
  128. };
  129. } elseif ($values instanceof \Traversable) {
  130. $valueCache = null;
  131. $callback = static function () use ($values, &$valueCache) {
  132. return $valueCache ?? $valueCache = iterator_to_array($values, false);
  133. };
  134. } elseif (null === $values) {
  135. $callback = null;
  136. } else {
  137. throw new InvalidArgumentException('Autocompleter values can be either an array, "null" or a "Traversable" object.');
  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): self
  156. {
  157. if ($this->hidden && null !== $callback) {
  158. throw new LogicException('A hidden question cannot use the autocompleter.');
  159. }
  160. $this->autocompleterCallback = $callback;
  161. return $this;
  162. }
  163. /**
  164. * Sets a validator for the question.
  165. *
  166. * @param callable|null $validator
  167. *
  168. * @return $this
  169. */
  170. public function setValidator(callable $validator = null)
  171. {
  172. $this->validator = $validator;
  173. return $this;
  174. }
  175. /**
  176. * Gets the validator for the question.
  177. *
  178. * @return callable|null
  179. */
  180. public function getValidator()
  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. * @param int|null $attempts
  190. *
  191. * @return $this
  192. *
  193. * @throws InvalidArgumentException in case the number of attempts is invalid
  194. */
  195. public function setMaxAttempts($attempts)
  196. {
  197. if (null !== $attempts && $attempts < 1) {
  198. throw new InvalidArgumentException('Maximum number of attempts must be a positive value.');
  199. }
  200. $this->attempts = $attempts;
  201. return $this;
  202. }
  203. /**
  204. * Gets the maximum number of attempts.
  205. *
  206. * Null means an unlimited number of attempts.
  207. *
  208. * @return int|null
  209. */
  210. public function getMaxAttempts()
  211. {
  212. return $this->attempts;
  213. }
  214. /**
  215. * Sets a normalizer for the response.
  216. *
  217. * The normalizer can be a callable (a string), a closure or a class implementing __invoke.
  218. *
  219. * @param callable $normalizer
  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
  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. }