$question
$question :
setHidden(boolean $hidden) : \think\console\output\Question
隐藏答案
boolean | $hidden |
setHiddenFallback(boolean $fallback) : \think\console\output\Question
设置不能被隐藏的时候的操作
boolean | $fallback |
setAutocompleterValues(null|array|\Traversable $values) : \think\console\output\Question
设置自动完成的值
null|array|\Traversable | $values |
setValidator(null|callable $validator) : \think\console\output\Question
设置答案的验证器
null|callable | $validator |
The current instance
setMaxAttempts(null|integer $attempts) : \think\console\output\Question
设置最大重试次数
null|integer | $attempts |
setNormalizer(string|\Closure $normalizer) : \think\console\output\Question
设置响应的回调
string|\Closure | $normalizer |
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace think\console\output\question;
use think\console\output\Question;
class Confirmation extends Question
{
private $trueAnswerRegex;
/**
* 构造方法
* @param string $question 问题
* @param bool $default 默认答案
* @param string $trueAnswerRegex 验证正则
*/
public function __construct($question, $default = true, $trueAnswerRegex = '/^y/i')
{
parent::__construct($question, (bool) $default);
$this->trueAnswerRegex = $trueAnswerRegex;
$this->setNormalizer($this->getDefaultNormalizer());
}
/**
* 获取默认的答案回调
* @return callable
*/
private function getDefaultNormalizer()
{
$default = $this->getDefault();
$regex = $this->trueAnswerRegex;
return function ($answer) use ($default, $regex) {
if (is_bool($answer)) {
return $answer;
}
$answerIsTrue = (bool) preg_match($regex, $answer);
if (false === $default) {
return $answer && $answerIsTrue;
}
return !$answer || $answerIsTrue;
};
}
}