[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/console/Input/ -> ArrayInput.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\Console\Input;
  13  
  14  use Symfony\Component\Console\Exception\InvalidArgumentException;
  15  use Symfony\Component\Console\Exception\InvalidOptionException;
  16  
  17  /**
  18   * ArrayInput represents an input provided as an array.
  19   *
  20   * Usage:
  21   *
  22   *     $input = new ArrayInput(array('name' => 'foo', '--bar' => 'foobar'));
  23   *
  24   * @author Fabien Potencier <fabien@symfony.com>
  25   */
  26  class ArrayInput extends Input
  27  {
  28      private $parameters;
  29  
  30      public function __construct(array $parameters, InputDefinition $definition = null)
  31      {
  32          $this->parameters = $parameters;
  33  
  34          parent::__construct($definition);
  35      }
  36  
  37      /**
  38       * {@inheritdoc}
  39       */
  40      public function getFirstArgument()
  41      {
  42          foreach ($this->parameters as $key => $value) {
  43              if ($key && '-' === $key[0]) {
  44                  continue;
  45              }
  46  
  47              return $value;
  48          }
  49      }
  50  
  51      /**
  52       * {@inheritdoc}
  53       */
  54      public function hasParameterOption($values)
  55      {
  56          $values = (array) $values;
  57  
  58          foreach ($this->parameters as $k => $v) {
  59              if (!\is_int($k)) {
  60                  $v = $k;
  61              }
  62  
  63              if (\in_array($v, $values)) {
  64                  return true;
  65              }
  66          }
  67  
  68          return false;
  69      }
  70  
  71      /**
  72       * {@inheritdoc}
  73       */
  74      public function getParameterOption($values, $default = false)
  75      {
  76          $values = (array) $values;
  77  
  78          foreach ($this->parameters as $k => $v) {
  79              if (\is_int($k)) {
  80                  if (\in_array($v, $values)) {
  81                      return true;
  82                  }
  83              } elseif (\in_array($k, $values)) {
  84                  return $v;
  85              }
  86          }
  87  
  88          return $default;
  89      }
  90  
  91      /**
  92       * Returns a stringified representation of the args passed to the command.
  93       *
  94       * @return string
  95       */
  96      public function __toString()
  97      {
  98          $params = array();
  99          foreach ($this->parameters as $param => $val) {
 100              if ($param && '-' === $param[0]) {
 101                  if (\is_array($val)) {
 102                      foreach ($val as $v) {
 103                          $params[] = $param.('' != $v ? '='.$this->escapeToken($v) : '');
 104                      }
 105                  } else {
 106                      $params[] = $param.('' != $val ? '='.$this->escapeToken($val) : '');
 107                  }
 108              } else {
 109                  $params[] = \is_array($val) ? implode(' ', array_map(array($this, 'escapeToken'), $val)) : $this->escapeToken($val);
 110              }
 111          }
 112  
 113          return implode(' ', $params);
 114      }
 115  
 116      /**
 117       * {@inheritdoc}
 118       */
 119      protected function parse()
 120      {
 121          foreach ($this->parameters as $key => $value) {
 122              if (0 === strpos($key, '--')) {
 123                  $this->addLongOption(substr($key, 2), $value);
 124              } elseif ('-' === $key[0]) {
 125                  $this->addShortOption(substr($key, 1), $value);
 126              } else {
 127                  $this->addArgument($key, $value);
 128              }
 129          }
 130      }
 131  
 132      /**
 133       * Adds a short option value.
 134       *
 135       * @param string $shortcut The short option key
 136       * @param mixed  $value    The value for the option
 137       *
 138       * @throws InvalidOptionException When option given doesn't exist
 139       */
 140      private function addShortOption($shortcut, $value)
 141      {
 142          if (!$this->definition->hasShortcut($shortcut)) {
 143              throw new InvalidOptionException(sprintf('The "-%s" option does not exist.', $shortcut));
 144          }
 145  
 146          $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
 147      }
 148  
 149      /**
 150       * Adds a long option value.
 151       *
 152       * @param string $name  The long option key
 153       * @param mixed  $value The value for the option
 154       *
 155       * @throws InvalidOptionException When option given doesn't exist
 156       * @throws InvalidOptionException When a required value is missing
 157       */
 158      private function addLongOption($name, $value)
 159      {
 160          if (!$this->definition->hasOption($name)) {
 161              throw new InvalidOptionException(sprintf('The "--%s" option does not exist.', $name));
 162          }
 163  
 164          $option = $this->definition->getOption($name);
 165  
 166          if (null === $value) {
 167              if ($option->isValueRequired()) {
 168                  throw new InvalidOptionException(sprintf('The "--%s" option requires a value.', $name));
 169              }
 170  
 171              $value = $option->isValueOptional() ? $option->getDefault() : true;
 172          }
 173  
 174          $this->options[$name] = $value;
 175      }
 176  
 177      /**
 178       * Adds an argument value.
 179       *
 180       * @param string $name  The argument name
 181       * @param mixed  $value The value for the argument
 182       *
 183       * @throws InvalidArgumentException When argument given doesn't exist
 184       */
 185      private function addArgument($name, $value)
 186      {
 187          if (!$this->definition->hasArgument($name)) {
 188              throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
 189          }
 190  
 191          $this->arguments[$name] = $value;
 192      }
 193  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1