You are here

class ArrayInput in Zircon Profile 8

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

ArrayInput represents an input provided as an array.

Usage:

$input = new ArrayInput(array('name' => 'foo', '--bar' => 'foobar'));

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

Expanded class hierarchy of ArrayInput

7 files declare their use of ArrayInput
Application.php in vendor/symfony/console/Application.php
ApplicationTest.php in vendor/symfony/console/Tests/ApplicationTest.php
ApplicationTester.php in vendor/symfony/console/Tester/ApplicationTester.php
ArrayInputTest.php in vendor/symfony/console/Tests/Input/ArrayInputTest.php
CommandTester.php in vendor/symfony/console/Tester/CommandTester.php

... See full list

File

vendor/symfony/console/Input/ArrayInput.php, line 23

Namespace

Symfony\Component\Console\Input
View source
class ArrayInput extends Input {
  private $parameters;

  /**
   * Constructor.
   *
   * @param array           $parameters An array of parameters
   * @param InputDefinition $definition A InputDefinition instance
   */
  public function __construct(array $parameters, InputDefinition $definition = null) {
    $this->parameters = $parameters;
    parent::__construct($definition);
  }

  /**
   * Returns the first argument from the raw parameters (not parsed).
   *
   * @return string The value of the first argument or null otherwise
   */
  public function getFirstArgument() {
    foreach ($this->parameters as $key => $value) {
      if ($key && '-' === $key[0]) {
        continue;
      }
      return $value;
    }
  }

  /**
   * Returns true if the raw parameters (not parsed) contain a value.
   *
   * This method is to be used to introspect the input parameters
   * before they have been validated. It must be used carefully.
   *
   * @param string|array $values The values to look for in the raw parameters (can be an array)
   *
   * @return bool true if the value is contained in the raw parameters
   */
  public function hasParameterOption($values) {
    $values = (array) $values;
    foreach ($this->parameters as $k => $v) {
      if (!is_int($k)) {
        $v = $k;
      }
      if (in_array($v, $values)) {
        return true;
      }
    }
    return false;
  }

  /**
   * Returns the value of a raw option (not parsed).
   *
   * This method is to be used to introspect the input parameters
   * before they have been validated. It must be used carefully.
   *
   * @param string|array $values  The value(s) to look for in the raw parameters (can be an array)
   * @param mixed        $default The default value to return if no result is found
   *
   * @return mixed The option value
   */
  public function getParameterOption($values, $default = false) {
    $values = (array) $values;
    foreach ($this->parameters as $k => $v) {
      if (is_int($k)) {
        if (in_array($v, $values)) {
          return true;
        }
      }
      elseif (in_array($k, $values)) {
        return $v;
      }
    }
    return $default;
  }

  /**
   * Returns a stringified representation of the args passed to the command.
   *
   * @return string
   */
  public function __toString() {
    $params = array();
    foreach ($this->parameters as $param => $val) {
      if ($param && '-' === $param[0]) {
        $params[] = $param . ('' != $val ? '=' . $this
          ->escapeToken($val) : '');
      }
      else {
        $params[] = $this
          ->escapeToken($val);
      }
    }
    return implode(' ', $params);
  }

  /**
   * Processes command line arguments.
   */
  protected function parse() {
    foreach ($this->parameters as $key => $value) {
      if (0 === strpos($key, '--')) {
        $this
          ->addLongOption(substr($key, 2), $value);
      }
      elseif ('-' === $key[0]) {
        $this
          ->addShortOption(substr($key, 1), $value);
      }
      else {
        $this
          ->addArgument($key, $value);
      }
    }
  }

  /**
   * Adds a short option value.
   *
   * @param string $shortcut The short option key
   * @param mixed  $value    The value for the option
   *
   * @throws \InvalidArgumentException When option given doesn't exist
   */
  private function addShortOption($shortcut, $value) {
    if (!$this->definition
      ->hasShortcut($shortcut)) {
      throw new \InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
    }
    $this
      ->addLongOption($this->definition
      ->getOptionForShortcut($shortcut)
      ->getName(), $value);
  }

  /**
   * Adds a long option value.
   *
   * @param string $name  The long option key
   * @param mixed  $value The value for the option
   *
   * @throws \InvalidArgumentException When option given doesn't exist
   * @throws \InvalidArgumentException When a required value is missing
   */
  private function addLongOption($name, $value) {
    if (!$this->definition
      ->hasOption($name)) {
      throw new \InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
    }
    $option = $this->definition
      ->getOption($name);
    if (null === $value) {
      if ($option
        ->isValueRequired()) {
        throw new \InvalidArgumentException(sprintf('The "--%s" option requires a value.', $name));
      }
      $value = $option
        ->isValueOptional() ? $option
        ->getDefault() : true;
    }
    $this->options[$name] = $value;
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
ArrayInput::$parameters private property
ArrayInput::addArgument private function Adds an argument value.
ArrayInput::addLongOption private function Adds a long option value.
ArrayInput::addShortOption private function Adds a short option value.
ArrayInput::getFirstArgument public function Returns the first argument from the raw parameters (not parsed). Overrides InputInterface::getFirstArgument
ArrayInput::getParameterOption public function Returns the value of a raw option (not parsed). Overrides InputInterface::getParameterOption
ArrayInput::hasParameterOption public function Returns true if the raw parameters (not parsed) contain a value. Overrides InputInterface::hasParameterOption
ArrayInput::parse protected function Processes command line arguments. Overrides Input::parse
ArrayInput::__construct public function Constructor. Overrides Input::__construct
ArrayInput::__toString public function Returns a stringified representation of the args passed to the command.
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::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