Sha256: c26f008946dbdd3553d54db5abdb73c0a10b5da3e0bfce128d132b20cfe02112

Contents?: true

Size: 1.55 KB

Versions: 16

Compression:

Stored size: 1.55 KB

Contents

<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Console\Question;

/**
 * Represents a yes/no question.
 *
 * @author Fabien Potencier <fabien@symfony.com>
 */
class ConfirmationQuestion extends Question
{
    private $trueAnswerRegex;

    /**
     * @param string $question        The question to ask to the user
     * @param bool   $default         The default answer to return, true or false
     * @param string $trueAnswerRegex A regex to match the "yes" answer
     */
    public function __construct(string $question, bool $default = true, string $trueAnswerRegex = '/^y/i')
    {
        parent::__construct($question, $default);

        $this->trueAnswerRegex = $trueAnswerRegex;
        $this->setNormalizer($this->getDefaultNormalizer());
    }

    /**
     * Returns the default answer normalizer.
     *
     * @return callable
     */
    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;
        };
    }
}

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
dependabot-composer-0.112.1 helpers/vendor/symfony/console/Question/ConfirmationQuestion.php
dependabot-composer-0.111.57 helpers/vendor/symfony/console/Question/ConfirmationQuestion.php
dependabot-composer-0.111.56 helpers/vendor/symfony/console/Question/ConfirmationQuestion.php
dependabot-composer-0.111.52 helpers/vendor/symfony/console/Question/ConfirmationQuestion.php
dependabot-composer-0.111.50 helpers/vendor/symfony/console/Question/ConfirmationQuestion.php
dependabot-composer-0.111.25 helpers/vendor/symfony/console/Question/ConfirmationQuestion.php
dependabot-composer-0.111.17 helpers/vendor/symfony/console/Question/ConfirmationQuestion.php
dependabot-composer-0.111.15 helpers/vendor/symfony/console/Question/ConfirmationQuestion.php
dependabot-composer-0.110.13 helpers/vendor/symfony/console/Question/ConfirmationQuestion.php
dependabot-composer-0.108.11 helpers/vendor/symfony/console/Question/ConfirmationQuestion.php
dependabot-composer-0.108.8 helpers/vendor/symfony/console/Question/ConfirmationQuestion.php
dependabot-composer-0.107.47 helpers/vendor/symfony/console/Question/ConfirmationQuestion.php
dependabot-composer-0.107.39 helpers/vendor/symfony/console/Question/ConfirmationQuestion.php
dependabot-composer-0.107.36 helpers/vendor/symfony/console/Question/ConfirmationQuestion.php
dependabot-composer-0.107.28 helpers/vendor/symfony/console/Question/ConfirmationQuestion.php
dependabot-composer-0.107.14 helpers/vendor/symfony/console/Question/ConfirmationQuestion.php