class ArrayInput
ArrayInput represents an input provided as an array.
Usage:
$input = new ArrayInput(['command' => 'foo:bar', 'foo' => 'bar', '--bar' => 'foobar']);
@author Fabien Potencier <fabien@symfony.com>
Hierarchy
- class \Symfony\Component\Console\Input\Input implements \Symfony\Component\Console\Input\InputInterface, \Symfony\Component\Console\Input\StreamableInputInterface
- class \Symfony\Component\Console\Input\ArrayInput extends \Symfony\Component\Console\Input\Input
Expanded class hierarchy of ArrayInput
7 files declare their use of ArrayInput
- Application.php in vendor/
symfony/ console/ Application.php - ApplicationTester.php in vendor/
symfony/ console/ Tester/ ApplicationTester.php - CliDescriptor.php in vendor/
symfony/ var-dumper/ Command/ Descriptor/ CliDescriptor.php - CommandTester.php in vendor/
symfony/ console/ Tester/ CommandTester.php - InitCommand.php in vendor/
composer/ composer/ src/ Composer/ Command/ InitCommand.php
File
-
vendor/
symfony/ console/ Input/ ArrayInput.php, line 26
Namespace
Symfony\Component\Console\InputView source
class ArrayInput extends Input {
public function __construct(array $parameters, ?InputDefinition $definition = null) {
parent::__construct($definition);
}
public function getFirstArgument() : ?string {
foreach ($this->parameters as $param => $value) {
if ($param && \is_string($param) && '-' === $param[0]) {
continue;
}
return $value;
}
return null;
}
public function hasParameterOption(string|array $values, bool $onlyParams = false) : bool {
$values = (array) $values;
foreach ($this->parameters as $k => $v) {
if (!\is_int($k)) {
$v = $k;
}
if ($onlyParams && '--' === $v) {
return false;
}
if (\in_array($v, $values)) {
return true;
}
}
return false;
}
public function getParameterOption(string|array $values, string|bool|int|float|array|null $default = false, bool $onlyParams = false) : mixed {
$values = (array) $values;
foreach ($this->parameters as $k => $v) {
if ($onlyParams && ('--' === $k || \is_int($k) && '--' === $v)) {
return $default;
}
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.
*/
public function __toString() : string {
$params = [];
foreach ($this->parameters as $param => $val) {
if ($param && \is_string($param) && '-' === $param[0]) {
$glue = '-' === $param[1] ? '=' : ' ';
if (\is_array($val)) {
foreach ($val as $v) {
$params[] = $param . ('' != $v ? $glue . $this->escapeToken($v) : '');
}
}
else {
$params[] = $param . ('' != $val ? $glue . $this->escapeToken($val) : '');
}
}
else {
$params[] = \is_array($val) ? implode(' ', array_map($this->escapeToken(...), $val)) : $this->escapeToken($val);
}
}
return implode(' ', $params);
}
protected function parse() : void {
foreach ($this->parameters as $key => $value) {
if ('--' === $key) {
return;
}
if (str_starts_with($key, '--')) {
$this->addLongOption(substr($key, 2), $value);
}
elseif (str_starts_with($key, '-')) {
$this->addShortOption(substr($key, 1), $value);
}
else {
$this->addArgument($key, $value);
}
}
}
/**
* Adds a short option value.
*
* @throws InvalidOptionException When option given doesn't exist
*/
private function addShortOption(string $shortcut, mixed $value) : void {
if (!$this->definition
->hasShortcut($shortcut)) {
throw new InvalidOptionException(\sprintf('The "-%s" option does not exist.', $shortcut));
}
$this->addLongOption($this->definition
->getOptionForShortcut($shortcut)
->getName(), $value);
}
/**
* Adds a long option value.
*
* @throws InvalidOptionException When option given doesn't exist
* @throws InvalidOptionException When a required value is missing
*/
private function addLongOption(string $name, mixed $value) : void {
if (!$this->definition
->hasOption($name)) {
if (!$this->definition
->hasNegation($name)) {
throw new InvalidOptionException(\sprintf('The "--%s" option does not exist.', $name));
}
$optionName = $this->definition
->negationToName($name);
$this->options[$optionName] = false;
return;
}
$option = $this->definition
->getOption($name);
if (null === $value) {
if ($option->isValueRequired()) {
throw new InvalidOptionException(\sprintf('The "--%s" option requires a value.', $name));
}
if (!$option->isValueOptional()) {
$value = true;
}
}
$this->options[$name] = $value;
}
/**
* Adds an argument value.
*
* @throws InvalidArgumentException When argument given doesn't exist
*/
private function addArgument(string|int $name, mixed $value) : void {
if (!$this->definition
->hasArgument($name)) {
throw new InvalidArgumentException(\sprintf('The "%s" argument does not exist.', $name));
}
$this->arguments[$name] = $value;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
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 | Overrides Input::__construct | ||
ArrayInput::__toString | public | function | Returns a stringified representation of the args passed to the command. | Overrides InputInterface::__toString | |
Input::$arguments | protected | property | |||
Input::$definition | protected | property | |||
Input::$interactive | protected | property | |||
Input::$options | protected | property | |||
Input::$stream | protected | property | @var resource | ||
Input::bind | public | function | Binds the current Input instance with the given arguments and options. | Overrides InputInterface::bind | 1 |
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 all the given arguments merged with the default 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 all the given options merged with the default values. | Overrides InputInterface::getOptions | |
Input::getStream | public | function | Overrides StreamableInputInterface::getStream | ||
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 | Is this input means 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::setStream | public | function | Overrides StreamableInputInterface::setStream | ||
Input::validate | public | function | Validates the input. | Overrides InputInterface::validate |