PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor_full/symfony/src/Symfony/Component/Console/Input/ArrayInput.php

https://github.com/l3l0/BehatExamples
PHP | 190 lines | 89 code | 24 blank | 77 comment | 15 complexity | 539b226898289e0fbdd6eae9b774bdfb MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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. /**
  12. * ArrayInput represents an input provided as an array.
  13. *
  14. * Usage:
  15. *
  16. * $input = new ArrayInput(array('name' => 'foo', '--bar' => 'foobar'));
  17. *
  18. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  19. */
  20. class ArrayInput extends Input
  21. {
  22. protected $parameters;
  23. /**
  24. * Constructor.
  25. *
  26. * @param array $param An array of parameters
  27. * @param InputDefinition $definition A InputDefinition instance
  28. */
  29. public function __construct(array $parameters, InputDefinition $definition = null)
  30. {
  31. $this->parameters = $parameters;
  32. parent::__construct($definition);
  33. }
  34. /**
  35. * Returns the first argument from the raw parameters (not parsed).
  36. *
  37. * @return string The value of the first argument or null otherwise
  38. */
  39. public function getFirstArgument()
  40. {
  41. foreach ($this->parameters as $key => $value) {
  42. if ($key && '-' === $key[0]) {
  43. continue;
  44. }
  45. return $value;
  46. }
  47. }
  48. /**
  49. * Returns true if the raw parameters (not parsed) contains a value.
  50. *
  51. * This method is to be used to introspect the input parameters
  52. * before it has been validated. It must be used carefully.
  53. *
  54. * @param string|array $value The values to look for in the raw parameters (can be an array)
  55. *
  56. * @return Boolean true if the value is contained in the raw parameters
  57. */
  58. public function hasParameterOption($values)
  59. {
  60. if (!is_array($values)) {
  61. $values = array($values);
  62. }
  63. foreach ($this->parameters as $k => $v) {
  64. if (!is_int($k)) {
  65. $v = $k;
  66. }
  67. if (in_array($v, $values)) {
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. /**
  74. * Returns the value of a raw option (not parsed).
  75. *
  76. * This method is to be used to introspect the input parameters
  77. * before it has been validated. It must be used carefully.
  78. *
  79. * @param string|array $values The value(s) to look for in the raw parameters (can be an array)
  80. * @param mixed $default The default value to return if no result is found
  81. *
  82. * @return mixed The option value
  83. */
  84. public function getParameterOption($values, $default = false)
  85. {
  86. if (!is_array($values)) {
  87. $values = array($values);
  88. }
  89. foreach ($this->parameters as $k => $v) {
  90. if (is_int($k) && in_array($v, $values)) {
  91. return true;
  92. } elseif (in_array($k, $values)) {
  93. return $v;
  94. }
  95. }
  96. return $default;
  97. }
  98. /**
  99. * Processes command line arguments.
  100. */
  101. protected function parse()
  102. {
  103. foreach ($this->parameters as $key => $value) {
  104. if ('--' === substr($key, 0, 2)) {
  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 \RuntimeException When option given doesn't exist
  120. */
  121. protected function addShortOption($shortcut, $value)
  122. {
  123. if (!$this->definition->hasShortcut($shortcut)) {
  124. throw new \InvalidArgumentException(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 \InvalidArgumentException When option given doesn't exist
  135. * @throws \InvalidArgumentException When a required value is missing
  136. */
  137. protected function addLongOption($name, $value)
  138. {
  139. if (!$this->definition->hasOption($name)) {
  140. throw new \InvalidArgumentException(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 \InvalidArgumentException(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. protected 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. }