Question.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\console\output;
  12. class Question
  13. {
  14. private $question;
  15. private $attempts;
  16. private $hidden = false;
  17. private $hiddenFallback = true;
  18. private $autocompleterValues;
  19. private $validator;
  20. private $default;
  21. private $normalizer;
  22. /**
  23. * 构造方法
  24. * @param string $question 问题
  25. * @param mixed $default 默认答案
  26. */
  27. public function __construct($question, $default = null)
  28. {
  29. $this->question = $question;
  30. $this->default = $default;
  31. }
  32. /**
  33. * 获取问题
  34. * @return string
  35. */
  36. public function getQuestion()
  37. {
  38. return $this->question;
  39. }
  40. /**
  41. * 获取默认答案
  42. * @return mixed
  43. */
  44. public function getDefault()
  45. {
  46. return $this->default;
  47. }
  48. /**
  49. * 是否隐藏答案
  50. * @return bool
  51. */
  52. public function isHidden()
  53. {
  54. return $this->hidden;
  55. }
  56. /**
  57. * 隐藏答案
  58. * @param bool $hidden
  59. * @return Question
  60. */
  61. public function setHidden($hidden)
  62. {
  63. if ($this->autocompleterValues) {
  64. throw new \LogicException('A hidden question cannot use the autocompleter.');
  65. }
  66. $this->hidden = (bool) $hidden;
  67. return $this;
  68. }
  69. /**
  70. * 不能被隐藏是否撤销
  71. * @return bool
  72. */
  73. public function isHiddenFallback()
  74. {
  75. return $this->hiddenFallback;
  76. }
  77. /**
  78. * 设置不能被隐藏的时候的操作
  79. * @param bool $fallback
  80. * @return Question
  81. */
  82. public function setHiddenFallback($fallback)
  83. {
  84. $this->hiddenFallback = (bool) $fallback;
  85. return $this;
  86. }
  87. /**
  88. * 获取自动完成
  89. * @return null|array|\Traversable
  90. */
  91. public function getAutocompleterValues()
  92. {
  93. return $this->autocompleterValues;
  94. }
  95. /**
  96. * 设置自动完成的值
  97. * @param null|array|\Traversable $values
  98. * @return Question
  99. * @throws \InvalidArgumentException
  100. * @throws \LogicException
  101. */
  102. public function setAutocompleterValues($values)
  103. {
  104. if (is_array($values) && $this->isAssoc($values)) {
  105. $values = array_merge(array_keys($values), array_values($values));
  106. }
  107. if (null !== $values && !is_array($values)) {
  108. if (!$values instanceof \Traversable || $values instanceof \Countable) {
  109. throw new \InvalidArgumentException('Autocompleter values can be either an array, `null` or an object implementing both `Countable` and `Traversable` interfaces.');
  110. }
  111. }
  112. if ($this->hidden) {
  113. throw new \LogicException('A hidden question cannot use the autocompleter.');
  114. }
  115. $this->autocompleterValues = $values;
  116. return $this;
  117. }
  118. /**
  119. * 设置答案的验证器
  120. * @param null|callable $validator
  121. * @return Question The current instance
  122. */
  123. public function setValidator($validator)
  124. {
  125. $this->validator = $validator;
  126. return $this;
  127. }
  128. /**
  129. * 获取验证器
  130. * @return null|callable
  131. */
  132. public function getValidator()
  133. {
  134. return $this->validator;
  135. }
  136. /**
  137. * 设置最大重试次数
  138. * @param null|int $attempts
  139. * @return Question
  140. * @throws \InvalidArgumentException
  141. */
  142. public function setMaxAttempts($attempts)
  143. {
  144. if (null !== $attempts && $attempts < 1) {
  145. throw new \InvalidArgumentException('Maximum number of attempts must be a positive value.');
  146. }
  147. $this->attempts = $attempts;
  148. return $this;
  149. }
  150. /**
  151. * 获取最大重试次数
  152. * @return null|int
  153. */
  154. public function getMaxAttempts()
  155. {
  156. return $this->attempts;
  157. }
  158. /**
  159. * 设置响应的回调
  160. * @param string|\Closure $normalizer
  161. * @return Question
  162. */
  163. public function setNormalizer($normalizer)
  164. {
  165. $this->normalizer = $normalizer;
  166. return $this;
  167. }
  168. /**
  169. * 获取响应回调
  170. * The normalizer can ba a callable (a string), a closure or a class implementing __invoke.
  171. * @return string|\Closure
  172. */
  173. public function getNormalizer()
  174. {
  175. return $this->normalizer;
  176. }
  177. protected function isAssoc($array)
  178. {
  179. return (bool) count(array_filter(array_keys($array), 'is_string'));
  180. }
  181. }