Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Console\Question; Chris@0: Chris@0: /** Chris@0: * Represents a yes/no question. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class ConfirmationQuestion extends Question Chris@0: { Chris@0: private $trueAnswerRegex; Chris@0: Chris@0: /** Chris@0: * @param string $question The question to ask to the user Chris@0: * @param bool $default The default answer to return, true or false Chris@0: * @param string $trueAnswerRegex A regex to match the "yes" answer Chris@0: */ Chris@0: public function __construct($question, $default = true, $trueAnswerRegex = '/^y/i') Chris@0: { Chris@0: parent::__construct($question, (bool) $default); Chris@0: Chris@0: $this->trueAnswerRegex = $trueAnswerRegex; Chris@0: $this->setNormalizer($this->getDefaultNormalizer()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the default answer normalizer. Chris@0: * Chris@0: * @return callable Chris@0: */ Chris@0: private function getDefaultNormalizer() Chris@0: { Chris@0: $default = $this->getDefault(); Chris@0: $regex = $this->trueAnswerRegex; Chris@0: Chris@0: return function ($answer) use ($default, $regex) { Chris@17: if (\is_bool($answer)) { Chris@0: return $answer; Chris@0: } Chris@0: Chris@0: $answerIsTrue = (bool) preg_match($regex, $answer); Chris@0: if (false === $default) { Chris@0: return $answer && $answerIsTrue; Chris@0: } Chris@0: Chris@17: return '' === $answer || $answerIsTrue; Chris@0: }; Chris@0: } Chris@0: }