Question.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 $autocompleterValues;
  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->autocompleterValues) {
  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. return $this->autocompleterValues;
  110. }
  111. /**
  112. * Sets values for the autocompleter.
  113. *
  114. * @param iterable|null $values
  115. *
  116. * @return $this
  117. *
  118. * @throws InvalidArgumentException
  119. * @throws LogicException
  120. */
  121. public function setAutocompleterValues($values)
  122. {
  123. if (\is_array($values)) {
  124. $values = $this->isAssoc($values) ? array_merge(array_keys($values), array_values($values)) : array_values($values);
  125. }
  126. if (null !== $values && !\is_array($values) && !$values instanceof \Traversable) {
  127. throw new InvalidArgumentException('Autocompleter values can be either an array, "null" or a "Traversable" object.');
  128. }
  129. if ($this->hidden) {
  130. throw new LogicException('A hidden question cannot use the autocompleter.');
  131. }
  132. $this->autocompleterValues = $values;
  133. return $this;
  134. }
  135. /**
  136. * Sets a validator for the question.
  137. *
  138. * @param callable|null $validator
  139. *
  140. * @return $this
  141. */
  142. public function setValidator(callable $validator = null)
  143. {
  144. $this->validator = $validator;
  145. return $this;
  146. }
  147. /**
  148. * Gets the validator for the question.
  149. *
  150. * @return callable|null
  151. */
  152. public function getValidator()
  153. {
  154. return $this->validator;
  155. }
  156. /**
  157. * Sets the maximum number of attempts.
  158. *
  159. * Null means an unlimited number of attempts.
  160. *
  161. * @param int|null $attempts
  162. *
  163. * @return $this
  164. *
  165. * @throws InvalidArgumentException in case the number of attempts is invalid
  166. */
  167. public function setMaxAttempts($attempts)
  168. {
  169. if (null !== $attempts && $attempts < 1) {
  170. throw new InvalidArgumentException('Maximum number of attempts must be a positive value.');
  171. }
  172. $this->attempts = $attempts;
  173. return $this;
  174. }
  175. /**
  176. * Gets the maximum number of attempts.
  177. *
  178. * Null means an unlimited number of attempts.
  179. *
  180. * @return int|null
  181. */
  182. public function getMaxAttempts()
  183. {
  184. return $this->attempts;
  185. }
  186. /**
  187. * Sets a normalizer for the response.
  188. *
  189. * The normalizer can be a callable (a string), a closure or a class implementing __invoke.
  190. *
  191. * @param callable $normalizer
  192. *
  193. * @return $this
  194. */
  195. public function setNormalizer(callable $normalizer)
  196. {
  197. $this->normalizer = $normalizer;
  198. return $this;
  199. }
  200. /**
  201. * Gets the normalizer for the response.
  202. *
  203. * The normalizer can ba a callable (a string), a closure or a class implementing __invoke.
  204. *
  205. * @return callable
  206. */
  207. public function getNormalizer()
  208. {
  209. return $this->normalizer;
  210. }
  211. protected function isAssoc($array)
  212. {
  213. return (bool) \count(array_filter(array_keys($array), 'is_string'));
  214. }
  215. }