ArrayInput.php 5.1 KB

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