ConfirmationQuestion.php
51 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace Matomo\Dependencies\Symfony\Component\Console\Question; |
| 12 | |
| 13 | /** |
| 14 | * Represents a yes/no question. |
| 15 | * |
| 16 | * @author Fabien Potencier <fabien@symfony.com> |
| 17 | */ |
| 18 | class ConfirmationQuestion extends Question |
| 19 | { |
| 20 | private $trueAnswerRegex; |
| 21 | /** |
| 22 | * @param string $question The question to ask to the user |
| 23 | * @param bool $default The default answer to return, true or false |
| 24 | * @param string $trueAnswerRegex A regex to match the "yes" answer |
| 25 | */ |
| 26 | public function __construct(string $question, bool $default = \true, string $trueAnswerRegex = '/^y/i') |
| 27 | { |
| 28 | parent::__construct($question, $default); |
| 29 | $this->trueAnswerRegex = $trueAnswerRegex; |
| 30 | $this->setNormalizer($this->getDefaultNormalizer()); |
| 31 | } |
| 32 | /** |
| 33 | * Returns the default answer normalizer. |
| 34 | */ |
| 35 | private function getDefaultNormalizer() : callable |
| 36 | { |
| 37 | $default = $this->getDefault(); |
| 38 | $regex = $this->trueAnswerRegex; |
| 39 | return function ($answer) use($default, $regex) { |
| 40 | if (\is_bool($answer)) { |
| 41 | return $answer; |
| 42 | } |
| 43 | $answerIsTrue = (bool) preg_match($regex, $answer); |
| 44 | if (\false === $default) { |
| 45 | return $answer && $answerIsTrue; |
| 46 | } |
| 47 | return '' === $answer || $answerIsTrue; |
| 48 | }; |
| 49 | } |
| 50 | } |
| 51 |