Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. StrictConfirmationQuestion.php

class StrictConfirmationQuestion

Represents a yes/no question Enforces strict responses rather than non-standard answers counting as default Based on Symfony\Component\Console\Question\ConfirmationQuestion

@author Theo Tonge <theo@theotonge.co.uk>

Hierarchy

  • class \Symfony\Component\Console\Question\Question
    • class \Composer\Question\StrictConfirmationQuestion extends \Symfony\Component\Console\Question\Question

Expanded class hierarchy of StrictConfirmationQuestion

1 file declares its use of StrictConfirmationQuestion
ConsoleIO.php in vendor/composer/composer/src/Composer/IO/ConsoleIO.php

File

vendor/composer/composer/src/Composer/Question/StrictConfirmationQuestion.php, line 26

Namespace

Composer\Question
View source
class StrictConfirmationQuestion extends Question {
    
    /** @var non-empty-string */
    private $trueAnswerRegex;
    
    /** @var non-empty-string */
    private $falseAnswerRegex;
    
    /**
     * Constructor.s
     *
     * @param string $question         The question to ask to the user
     * @param bool   $default          The default answer to return, true or false
     * @param non-empty-string $trueAnswerRegex  A regex to match the "yes" answer
     * @param non-empty-string $falseAnswerRegex A regex to match the "no" answer
     */
    public function __construct(string $question, bool $default = true, string $trueAnswerRegex = '/^y(?:es)?$/i', string $falseAnswerRegex = '/^no?$/i') {
        parent::__construct($question, $default);
        $this->trueAnswerRegex = $trueAnswerRegex;
        $this->falseAnswerRegex = $falseAnswerRegex;
        $this->setNormalizer($this->getDefaultNormalizer());
        $this->setValidator($this->getDefaultValidator());
    }
    
    /**
     * Returns the default answer normalizer.
     */
    private function getDefaultNormalizer() : callable {
        $default = $this->getDefault();
        $trueRegex = $this->trueAnswerRegex;
        $falseRegex = $this->falseAnswerRegex;
        return static function ($answer) use ($default, $trueRegex, $falseRegex) {
            if (is_bool($answer)) {
                return $answer;
            }
            if (empty($answer) && !empty($default)) {
                return $default;
            }
            if (Preg::isMatch($trueRegex, $answer)) {
                return true;
            }
            if (Preg::isMatch($falseRegex, $answer)) {
                return false;
            }
            return null;
        };
    }
    
    /**
     * Returns the default answer validator.
     */
    private function getDefaultValidator() : callable {
        return static function ($answer) : bool {
            if (!is_bool($answer)) {
                throw new InvalidArgumentException('Please answer yes, y, no, or n.');
            }
            return $answer;
        };
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
Question::$attempts private property
Question::$autocompleterCallback private property
Question::$hidden private property
Question::$hiddenFallback private property
Question::$multiline private property
Question::$normalizer private property
Question::$trimmable private property
Question::$validator private property
Question::getAutocompleterCallback public function Gets the callback function used for the autocompleter.
Question::getAutocompleterValues public function Gets values for the autocompleter.
Question::getDefault public function Returns the default answer.
Question::getMaxAttempts public function Gets the maximum number of attempts.
Question::getNormalizer public function Gets the normalizer for the response.
Question::getQuestion public function Returns the question.
Question::getValidator public function Gets the validator for the question.
Question::isAssoc protected function
Question::isHidden public function Returns whether the user response must be hidden.
Question::isHiddenFallback public function In case the response cannot be hidden, whether to fallback on non-hidden question or not.
Question::isMultiline public function Returns whether the user response accepts newline characters.
Question::isTrimmable public function
Question::setAutocompleterCallback public function Sets the callback function used for the autocompleter.
Question::setAutocompleterValues public function Sets values for the autocompleter.
Question::setHidden public function Sets whether the user response must be hidden or not.
Question::setHiddenFallback public function Sets whether to fallback on non-hidden question if the response cannot be hidden.
Question::setMaxAttempts public function Sets the maximum number of attempts.
Question::setMultiline public function Sets whether the user response should accept newline characters.
Question::setNormalizer public function Sets a normalizer for the response.
Question::setTrimmable public function
Question::setValidator public function Sets a validator for the question.
StrictConfirmationQuestion::$falseAnswerRegex private property @var non-empty-string
StrictConfirmationQuestion::$trueAnswerRegex private property @var non-empty-string
StrictConfirmationQuestion::getDefaultNormalizer private function Returns the default answer normalizer.
StrictConfirmationQuestion::getDefaultValidator private function Returns the default answer validator.
StrictConfirmationQuestion::__construct public function Constructor.s Overrides Question::__construct

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal