Question.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. /**
  12. * Represents a Question.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Question
  17. {
  18. private $question;
  19. private $attempts;
  20. private $hidden = false;
  21. private $hiddenFallback = true;
  22. private $autocompleterValues;
  23. private $validator;
  24. private $default;
  25. private $normalizer;
  26. /**
  27. * Constructor.
  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($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 Question The current instance
  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 Question The current instance
  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 null|array|\Traversable
  106. */
  107. public function getAutocompleterValues()
  108. {
  109. return $this->autocompleterValues;
  110. }
  111. /**
  112. * Sets values for the autocompleter.
  113. *
  114. * @param null|array|\Traversable $values
  115. *
  116. * @return Question The current instance
  117. *
  118. * @throws \InvalidArgumentException
  119. * @throws \LogicException
  120. */
  121. public function setAutocompleterValues($values)
  122. {
  123. if (is_array($values) && $this->isAssoc($values)) {
  124. $values = array_merge(array_keys($values), array_values($values));
  125. }
  126. if (null !== $values && !is_array($values)) {
  127. if (!$values instanceof \Traversable || $values instanceof \Countable) {
  128. throw new \InvalidArgumentException('Autocompleter values can be either an array, `null` or an object implementing both `Countable` and `Traversable` interfaces.');
  129. }
  130. }
  131. if ($this->hidden) {
  132. throw new \LogicException('A hidden question cannot use the autocompleter.');
  133. }
  134. $this->autocompleterValues = $values;
  135. return $this;
  136. }
  137. /**
  138. * Sets a validator for the question.
  139. *
  140. * @param null|callable $validator
  141. *
  142. * @return Question The current instance
  143. */
  144. public function setValidator($validator)
  145. {
  146. $this->validator = $validator;
  147. return $this;
  148. }
  149. /**
  150. * Gets the validator for the question.
  151. *
  152. * @return null|callable
  153. */
  154. public function getValidator()
  155. {
  156. return $this->validator;
  157. }
  158. /**
  159. * Sets the maximum number of attempts.
  160. *
  161. * Null means an unlimited number of attempts.
  162. *
  163. * @param null|int $attempts
  164. *
  165. * @return Question The current instance
  166. *
  167. * @throws \InvalidArgumentException In case the number of attempts is invalid.
  168. */
  169. public function setMaxAttempts($attempts)
  170. {
  171. if (null !== $attempts && $attempts < 1) {
  172. throw new \InvalidArgumentException('Maximum number of attempts must be a positive value.');
  173. }
  174. $this->attempts = $attempts;
  175. return $this;
  176. }
  177. /**
  178. * Gets the maximum number of attempts.
  179. *
  180. * Null means an unlimited number of attempts.
  181. *
  182. * @return null|int
  183. */
  184. public function getMaxAttempts()
  185. {
  186. return $this->attempts;
  187. }
  188. /**
  189. * Sets a normalizer for the response.
  190. *
  191. * The normalizer can be a callable (a string), a closure or a class implementing __invoke.
  192. *
  193. * @param string|\Closure $normalizer
  194. *
  195. * @return Question The current instance
  196. */
  197. public function setNormalizer($normalizer)
  198. {
  199. $this->normalizer = $normalizer;
  200. return $this;
  201. }
  202. /**
  203. * Gets the normalizer for the response.
  204. *
  205. * The normalizer can ba a callable (a string), a closure or a class implementing __invoke.
  206. *
  207. * @return string|\Closure
  208. */
  209. public function getNormalizer()
  210. {
  211. return $this->normalizer;
  212. }
  213. protected function isAssoc($array)
  214. {
  215. return (bool) count(array_filter(array_keys($array), 'is_string'));
  216. }
  217. }