1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- <?php
- namespace Symfony\Component\Console\Question;
- class ConfirmationQuestion extends Question
- {
- private $trueAnswerRegex;
-
- public function __construct(string $question, bool $default = true, string $trueAnswerRegex = '/^y/i')
- {
- parent::__construct($question, $default);
- $this->trueAnswerRegex = $trueAnswerRegex;
- $this->setNormalizer($this->getDefaultNormalizer());
- }
-
- 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;
- };
- }
- }
|