4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Question;
15 * Represents a yes/no question.
17 * @author Fabien Potencier <fabien@symfony.com>
19 class ConfirmationQuestion extends Question
21 private $trueAnswerRegex;
26 * @param string $question The question to ask to the user
27 * @param bool $default The default answer to return, true or false
28 * @param string $trueAnswerRegex A regex to match the "yes" answer
30 public function __construct($question, $default = true, $trueAnswerRegex = '/^y/i')
32 parent::__construct($question, (bool) $default);
34 $this->trueAnswerRegex = $trueAnswerRegex;
35 $this->setNormalizer($this->getDefaultNormalizer());
39 * Returns the default answer normalizer.
43 private function getDefaultNormalizer()
45 $default = $this->getDefault();
46 $regex = $this->trueAnswerRegex;
48 return function ($answer) use ($default, $regex) {
49 if (is_bool($answer)) {
53 $answerIsTrue = (bool) preg_match($regex, $answer);
54 if (false === $default) {
55 return $answer && $answerIsTrue;
58 return !$answer || $answerIsTrue;