Question.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. * Constructor.
  30. *
  31. * @param string $question The question to ask to the user
  32. * @param mixed $default The default answer to return if the user enters nothing
  33. */
  34. public function __construct($question, $default = null)
  35. {
  36. $this->question = $question;
  37. $this->default = $default;
  38. }
  39. /**
  40. * Returns the question.
  41. *
  42. * @return string
  43. */
  44. public function getQuestion()
  45. {
  46. return $this->question;
  47. }
  48. /**
  49. * Returns the default answer.
  50. *
  51. * @return mixed
  52. */
  53. public function getDefault()
  54. {
  55. return $this->default;
  56. }
  57. /**
  58. * Returns whether the user response must be hidden.
  59. *
  60. * @return bool
  61. */
  62. public function isHidden()
  63. {
  64. return $this->hidden;
  65. }
  66. /**
  67. * Sets whether the user response must be hidden or not.
  68. *
  69. * @param bool $hidden
  70. *
  71. * @return $this
  72. *
  73. * @throws LogicException In case the autocompleter is also used
  74. */
  75. public function setHidden($hidden)
  76. {
  77. if ($this->autocompleterValues) {
  78. throw new LogicException('A hidden question cannot use the autocompleter.');
  79. }
  80. $this->hidden = (bool) $hidden;
  81. return $this;
  82. }
  83. /**
  84. * In case the response can not be hidden, whether to fallback on non-hidden question or not.
  85. *
  86. * @return bool
  87. */
  88. public function isHiddenFallback()
  89. {
  90. return $this->hiddenFallback;
  91. }
  92. /**
  93. * Sets whether to fallback on non-hidden question if the response can not be hidden.
  94. *
  95. * @param bool $fallback
  96. *
  97. * @return $this
  98. */
  99. public function setHiddenFallback($fallback)
  100. {
  101. $this->hiddenFallback = (bool) $fallback;
  102. return $this;
  103. }
  104. /**
  105. * Gets values for the autocompleter.
  106. *
  107. * @return null|array|\Traversable
  108. */
  109. public function getAutocompleterValues()
  110. {
  111. return $this->autocompleterValues;
  112. }
  113. /**
  114. * Sets values for the autocompleter.
  115. *
  116. * @param null|array|\Traversable $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. }
  128. if (null !== $values && !is_array($values)) {
  129. if (!$values instanceof \Traversable || !$values instanceof \Countable) {
  130. throw new InvalidArgumentException('Autocompleter values can be either an array, `null` or an object implementing both `Countable` and `Traversable` interfaces.');
  131. }
  132. }
  133. if ($this->hidden) {
  134. throw new LogicException('A hidden question cannot use the autocompleter.');
  135. }
  136. $this->autocompleterValues = $values;
  137. return $this;
  138. }
  139. /**
  140. * Sets a validator for the question.
  141. *
  142. * @param null|callable $validator
  143. *
  144. * @return $this
  145. */
  146. public function setValidator(callable $validator = null)
  147. {
  148. $this->validator = $validator;
  149. return $this;
  150. }
  151. /**
  152. * Gets the validator for the question.
  153. *
  154. * @return null|callable
  155. */
  156. public function getValidator()
  157. {
  158. return $this->validator;
  159. }
  160. /**
  161. * Sets the maximum number of attempts.
  162. *
  163. * Null means an unlimited number of attempts.
  164. *
  165. * @param null|int $attempts
  166. *
  167. * @return $this
  168. *
  169. * @throws InvalidArgumentException In case the number of attempts is invalid.
  170. */
  171. public function setMaxAttempts($attempts)
  172. {
  173. if (null !== $attempts && $attempts < 1) {
  174. throw new InvalidArgumentException('Maximum number of attempts must be a positive value.');
  175. }
  176. $this->attempts = $attempts;
  177. return $this;
  178. }
  179. /**
  180. * Gets the maximum number of attempts.
  181. *
  182. * Null means an unlimited number of attempts.
  183. *
  184. * @return null|int
  185. */
  186. public function getMaxAttempts()
  187. {
  188. return $this->attempts;
  189. }
  190. /**
  191. * Sets a normalizer for the response.
  192. *
  193. * The normalizer can be a callable (a string), a closure or a class implementing __invoke.
  194. *
  195. * @param callable $normalizer
  196. *
  197. * @return $this
  198. */
  199. public function setNormalizer(callable $normalizer)
  200. {
  201. $this->normalizer = $normalizer;
  202. return $this;
  203. }
  204. /**
  205. * Gets the normalizer for the response.
  206. *
  207. * The normalizer can ba a callable (a string), a closure or a class implementing __invoke.
  208. *
  209. * @return callable
  210. */
  211. public function getNormalizer()
  212. {
  213. return $this->normalizer;
  214. }
  215. protected function isAssoc($array)
  216. {
  217. return (bool) count(array_filter(array_keys($array), 'is_string'));
  218. }
  219. }