You are here

abstract class Input in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/symfony/console/Input/Input.php \Symfony\Component\Console\Input\Input

Input is the base class for all concrete Input classes.

Three concrete classes are provided by default:

  • `ArgvInput`: The input comes from the CLI arguments (argv)
  • `StringInput`: The input is provided as a string
  • `ArrayInput`: The input is provided as an array

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

Expanded class hierarchy of Input

2 string references to 'Input'
AbstractProcessTest::methodProvider in vendor/symfony/process/Tests/AbstractProcessTest.php
provides default method names for simple getter/setter.
SessionTestForm::buildForm in core/modules/system/tests/modules/session_test/src/Form/SessionTestForm.php
Form constructor.

File

vendor/symfony/console/Input/Input.php, line 25

Namespace

Symfony\Component\Console\Input
View source
abstract class Input implements InputInterface {

  /**
   * @var InputDefinition
   */
  protected $definition;
  protected $options = array();
  protected $arguments = array();
  protected $interactive = true;

  /**
   * Constructor.
   *
   * @param InputDefinition $definition A InputDefinition instance
   */
  public function __construct(InputDefinition $definition = null) {
    if (null === $definition) {
      $this->definition = new InputDefinition();
    }
    else {
      $this
        ->bind($definition);
      $this
        ->validate();
    }
  }

  /**
   * Binds the current Input instance with the given arguments and options.
   *
   * @param InputDefinition $definition A InputDefinition instance
   */
  public function bind(InputDefinition $definition) {
    $this->arguments = array();
    $this->options = array();
    $this->definition = $definition;
    $this
      ->parse();
  }

  /**
   * Processes command line arguments.
   */
  protected abstract function parse();

  /**
   * Validates the input.
   *
   * @throws \RuntimeException When not enough arguments are given
   */
  public function validate() {
    $definition = $this->definition;
    $givenArguments = $this->arguments;
    $missingArguments = array_filter(array_keys($definition
      ->getArguments()), function ($argument) use ($definition, $givenArguments) {
      return !array_key_exists($argument, $givenArguments) && $definition
        ->getArgument($argument)
        ->isRequired();
    });
    if (count($missingArguments) > 0) {
      throw new \RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments)));
    }
  }

  /**
   * Checks if the input is interactive.
   *
   * @return bool Returns true if the input is interactive
   */
  public function isInteractive() {
    return $this->interactive;
  }

  /**
   * Sets the input interactivity.
   *
   * @param bool $interactive If the input should be interactive
   */
  public function setInteractive($interactive) {
    $this->interactive = (bool) $interactive;
  }

  /**
   * Returns the argument values.
   *
   * @return array An array of argument values
   */
  public function getArguments() {
    return array_merge($this->definition
      ->getArgumentDefaults(), $this->arguments);
  }

  /**
   * Returns the argument value for a given argument name.
   *
   * @param string $name The argument name
   *
   * @return mixed The argument value
   *
   * @throws \InvalidArgumentException When argument given doesn't exist
   */
  public function getArgument($name) {
    if (!$this->definition
      ->hasArgument($name)) {
      throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
    }
    return isset($this->arguments[$name]) ? $this->arguments[$name] : $this->definition
      ->getArgument($name)
      ->getDefault();
  }

  /**
   * Sets an argument value by name.
   *
   * @param string $name  The argument name
   * @param string $value The argument value
   *
   * @throws \InvalidArgumentException When argument given doesn't exist
   */
  public function setArgument($name, $value) {
    if (!$this->definition
      ->hasArgument($name)) {
      throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
    }
    $this->arguments[$name] = $value;
  }

  /**
   * Returns true if an InputArgument object exists by name or position.
   *
   * @param string|int $name The InputArgument name or position
   *
   * @return bool true if the InputArgument object exists, false otherwise
   */
  public function hasArgument($name) {
    return $this->definition
      ->hasArgument($name);
  }

  /**
   * Returns the options values.
   *
   * @return array An array of option values
   */
  public function getOptions() {
    return array_merge($this->definition
      ->getOptionDefaults(), $this->options);
  }

  /**
   * Returns the option value for a given option name.
   *
   * @param string $name The option name
   *
   * @return mixed The option value
   *
   * @throws \InvalidArgumentException When option given doesn't exist
   */
  public function getOption($name) {
    if (!$this->definition
      ->hasOption($name)) {
      throw new \InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
    }
    return isset($this->options[$name]) ? $this->options[$name] : $this->definition
      ->getOption($name)
      ->getDefault();
  }

  /**
   * Sets an option value by name.
   *
   * @param string      $name  The option name
   * @param string|bool $value The option value
   *
   * @throws \InvalidArgumentException When option given doesn't exist
   */
  public function setOption($name, $value) {
    if (!$this->definition
      ->hasOption($name)) {
      throw new \InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
    }
    $this->options[$name] = $value;
  }

  /**
   * Returns true if an InputOption object exists by name.
   *
   * @param string $name The InputOption name
   *
   * @return bool true if the InputOption object exists, false otherwise
   */
  public function hasOption($name) {
    return $this->definition
      ->hasOption($name);
  }

  /**
   * Escapes a token through escapeshellarg if it contains unsafe chars.
   *
   * @param string $token
   *
   * @return string
   */
  public function escapeToken($token) {
    return preg_match('{^[\\w-]+$}', $token) ? $token : escapeshellarg($token);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Input::$arguments protected property
Input::$definition protected property
Input::$interactive protected property
Input::$options protected property
Input::bind public function Binds the current Input instance with the given arguments and options. Overrides InputInterface::bind
Input::escapeToken public function Escapes a token through escapeshellarg if it contains unsafe chars.
Input::getArgument public function Returns the argument value for a given argument name. Overrides InputInterface::getArgument
Input::getArguments public function Returns the argument values. Overrides InputInterface::getArguments
Input::getOption public function Returns the option value for a given option name. Overrides InputInterface::getOption
Input::getOptions public function Returns the options values. Overrides InputInterface::getOptions
Input::hasArgument public function Returns true if an InputArgument object exists by name or position. Overrides InputInterface::hasArgument
Input::hasOption public function Returns true if an InputOption object exists by name. Overrides InputInterface::hasOption
Input::isInteractive public function Checks if the input is interactive. Overrides InputInterface::isInteractive
Input::parse abstract protected function Processes command line arguments. 2
Input::setArgument public function Sets an argument value by name. Overrides InputInterface::setArgument
Input::setInteractive public function Sets the input interactivity. Overrides InputInterface::setInteractive
Input::setOption public function Sets an option value by name. Overrides InputInterface::setOption
Input::validate public function Validates the input. Overrides InputInterface::validate
Input::__construct public function Constructor. 2
InputInterface::getFirstArgument public function Returns the first argument from the raw parameters (not parsed). 2
InputInterface::getParameterOption public function Returns the value of a raw option (not parsed). 2
InputInterface::hasParameterOption public function Returns true if the raw parameters (not parsed) contain a value. 2