You are here

class SymfonyQuestionHelper in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/console/Helper/SymfonyQuestionHelper.php \Symfony\Component\Console\Helper\SymfonyQuestionHelper

Symfony Style Guide compliant question helper.

@author Kevin Bond <kevinbond@gmail.com>

Hierarchy

Expanded class hierarchy of SymfonyQuestionHelper

1 file declares its use of SymfonyQuestionHelper
SymfonyStyle.php in vendor/symfony/console/Style/SymfonyStyle.php

File

vendor/symfony/console/Helper/SymfonyQuestionHelper.php, line 26

Namespace

Symfony\Component\Console\Helper
View source
class SymfonyQuestionHelper extends QuestionHelper {

  /**
   * {@inheritdoc}
   */
  public function ask(InputInterface $input, OutputInterface $output, Question $question) {
    $validator = $question
      ->getValidator();
    $question
      ->setValidator(function ($value) use ($validator) {
      if (null !== $validator && is_callable($validator)) {
        $value = $validator($value);
      }

      // make required
      if (!is_array($value) && !is_bool($value) && 0 === strlen($value)) {
        throw new \Exception('A value is required.');
      }
      return $value;
    });
    return parent::ask($input, $output, $question);
  }

  /**
   * {@inheritdoc}
   */
  protected function writePrompt(OutputInterface $output, Question $question) {
    $text = $question
      ->getQuestion();
    $default = $question
      ->getDefault();
    switch (true) {
      case null === $default:
        $text = sprintf(' <info>%s</info>:', $text);
        break;
      case $question instanceof ConfirmationQuestion:
        $text = sprintf(' <info>%s (yes/no)</info> [<comment>%s</comment>]:', $text, $default ? 'yes' : 'no');
        break;
      case $question instanceof ChoiceQuestion:
        $choices = $question
          ->getChoices();
        $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $choices[$default]);
        break;
      default:
        $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $default);
    }
    $output
      ->writeln($text);
    if ($question instanceof ChoiceQuestion) {
      $width = max(array_map('strlen', array_keys($question
        ->getChoices())));
      foreach ($question
        ->getChoices() as $key => $value) {
        $output
          ->writeln(sprintf("  [<comment>%-{$width}s</comment>] %s", $key, $value));
      }
    }
    $output
      ->write(' > ');
  }

  /**
   * {@inheritdoc}
   */
  protected function writeError(OutputInterface $output, \Exception $error) {
    if ($output instanceof SymfonyStyle) {
      $output
        ->newLine();
      $output
        ->error($error
        ->getMessage());
      return;
    }
    parent::writeError($output, $error);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Helper::$helperSet protected property
Helper::formatMemory public static function
Helper::formatTime public static function
Helper::getHelperSet public function Gets the helper set associated with this helper. Overrides HelperInterface::getHelperSet
Helper::setHelperSet public function Sets the helper set associated with this helper. Overrides HelperInterface::setHelperSet
Helper::strlen public static function Returns the length of a string, using mb_strwidth if it is available.
Helper::strlenWithoutDecoration public static function
QuestionHelper::$inputStream private property
QuestionHelper::$shell private static property
QuestionHelper::$stty private static property
QuestionHelper::autocomplete private function Autocompletes a question.
QuestionHelper::doAsk public function Asks the question to the user.
QuestionHelper::getHiddenResponse private function Gets a hidden response from user.
QuestionHelper::getInputStream public function Returns the helper's input stream.
QuestionHelper::getName public function Returns the canonical name of this helper. Overrides HelperInterface::getName
QuestionHelper::getShell private function Returns a valid unix shell.
QuestionHelper::hasSttyAvailable private function Returns whether Stty is available or not.
QuestionHelper::setInputStream public function Sets the input stream to read from when interacting with the user.
QuestionHelper::validateAttempts private function Validates an attempt.
SymfonyQuestionHelper::ask public function Asks a question to the user. Overrides QuestionHelper::ask
SymfonyQuestionHelper::writeError protected function Outputs an error message. Overrides QuestionHelper::writeError
SymfonyQuestionHelper::writePrompt protected function Outputs the question prompt. Overrides QuestionHelper::writePrompt