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;
24 * @param string $question The question to ask to the user
25 * @param bool $default The default answer to return, true or false
26 * @param string $trueAnswerRegex A regex to match the "yes" answer
28 public function __construct($question, $default = true, $trueAnswerRegex = '/^y/i')
30 parent::__construct($question, (bool) $default);
32 $this->trueAnswerRegex = $trueAnswerRegex;
33 $this->setNormalizer($this->getDefaultNormalizer());
37 * Returns the default answer normalizer.
41 private function getDefaultNormalizer()
43 $default = $this->getDefault();
44 $regex = $this->trueAnswerRegex;
46 return function ($answer) use ($default, $regex) {
47 if (\is_bool($answer)) {
51 $answerIsTrue = (bool) preg_match($regex, $answer);
52 if (false === $default) {
53 return $answer && $answerIsTrue;
56 return !$answer || $answerIsTrue;