2015-08-17 17:00:26 -07:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\Console\Input ;
2016-04-20 09:56:34 -07:00
use Symfony\Component\Console\Exception\InvalidArgumentException ;
use Symfony\Component\Console\Exception\InvalidOptionException ;
2015-08-17 17:00:26 -07:00
/**
* ArrayInput represents an input provided as an array .
*
* Usage :
*
* $input = new ArrayInput ( array ( 'name' => 'foo' , '--bar' => 'foobar' ));
*
* @ author Fabien Potencier < fabien @ symfony . com >
*/
class ArrayInput extends Input
{
private $parameters ;
public function __construct ( array $parameters , InputDefinition $definition = null )
{
$this -> parameters = $parameters ;
parent :: __construct ( $definition );
}
/**
2017-02-02 16:28:38 -08:00
* { @ inheritdoc }
2015-08-17 17:00:26 -07:00
*/
public function getFirstArgument ()
{
foreach ( $this -> parameters as $key => $value ) {
if ( $key && '-' === $key [ 0 ]) {
continue ;
}
return $value ;
}
}
/**
2017-02-02 16:28:38 -08:00
* { @ inheritdoc }
2015-08-17 17:00:26 -07:00
*/
2018-11-23 12:29:20 +00:00
public function hasParameterOption ( $values , $onlyParams = false )
2015-08-17 17:00:26 -07:00
{
$values = ( array ) $values ;
foreach ( $this -> parameters as $k => $v ) {
2018-11-23 12:29:20 +00:00
if ( ! \is_int ( $k )) {
2015-08-17 17:00:26 -07:00
$v = $k ;
}
2018-11-23 12:29:20 +00:00
if ( $onlyParams && '--' === $v ) {
return false ;
}
if ( \in_array ( $v , $values )) {
2015-08-17 17:00:26 -07:00
return true ;
}
}
return false ;
}
/**
2017-02-02 16:28:38 -08:00
* { @ inheritdoc }
2015-08-17 17:00:26 -07:00
*/
2018-11-23 12:29:20 +00:00
public function getParameterOption ( $values , $default = false , $onlyParams = false )
2015-08-17 17:00:26 -07:00
{
$values = ( array ) $values ;
foreach ( $this -> parameters as $k => $v ) {
2018-11-23 12:29:20 +00:00
if ( $onlyParams && ( '--' === $k || ( \is_int ( $k ) && '--' === $v ))) {
return $default ;
}
if ( \is_int ( $k )) {
if ( \in_array ( $v , $values )) {
2015-08-17 17:00:26 -07:00
return true ;
}
2018-11-23 12:29:20 +00:00
} elseif ( \in_array ( $k , $values )) {
2015-08-17 17:00:26 -07:00
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 ]) {
2018-11-23 12:29:20 +00:00
if ( \is_array ( $val )) {
foreach ( $val as $v ) {
$params [] = $param . ( '' != $v ? '=' . $this -> escapeToken ( $v ) : '' );
}
} else {
$params [] = $param . ( '' != $val ? '=' . $this -> escapeToken ( $val ) : '' );
}
2015-08-17 17:00:26 -07:00
} else {
2018-11-23 12:29:20 +00:00
$params [] = \is_array ( $val ) ? implode ( ' ' , array_map ( array ( $this , 'escapeToken' ), $val )) : $this -> escapeToken ( $val );
2015-08-17 17:00:26 -07:00
}
}
return implode ( ' ' , $params );
}
/**
2017-02-02 16:28:38 -08:00
* { @ inheritdoc }
2015-08-17 17:00:26 -07:00
*/
protected function parse ()
{
foreach ( $this -> parameters as $key => $value ) {
2018-11-23 12:29:20 +00:00
if ( '--' === $key ) {
return ;
}
2015-08-17 17:00:26 -07:00
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
*
2016-04-20 09:56:34 -07:00
* @ throws InvalidOptionException When option given doesn ' t exist
2015-08-17 17:00:26 -07:00
*/
private function addShortOption ( $shortcut , $value )
{
if ( ! $this -> definition -> hasShortcut ( $shortcut )) {
2016-04-20 09:56:34 -07:00
throw new InvalidOptionException ( sprintf ( 'The "-%s" option does not exist.' , $shortcut ));
2015-08-17 17:00:26 -07:00
}
$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
*
2016-04-20 09:56:34 -07:00
* @ throws InvalidOptionException When option given doesn ' t exist
* @ throws InvalidOptionException When a required value is missing
2015-08-17 17:00:26 -07:00
*/
private function addLongOption ( $name , $value )
{
if ( ! $this -> definition -> hasOption ( $name )) {
2016-04-20 09:56:34 -07:00
throw new InvalidOptionException ( sprintf ( 'The "--%s" option does not exist.' , $name ));
2015-08-17 17:00:26 -07:00
}
$option = $this -> definition -> getOption ( $name );
if ( null === $value ) {
if ( $option -> isValueRequired ()) {
2016-04-20 09:56:34 -07:00
throw new InvalidOptionException ( sprintf ( 'The "--%s" option requires a value.' , $name ));
2015-08-17 17:00:26 -07:00
}
2018-11-23 12:29:20 +00:00
if ( ! $option -> isValueOptional ()) {
$value = true ;
}
2015-08-17 17:00:26 -07:00
}
$this -> options [ $name ] = $value ;
}
/**
* Adds an argument value .
*
* @ param string $name The argument name
* @ param mixed $value The value for the argument
*
2016-04-20 09:56:34 -07:00
* @ throws InvalidArgumentException When argument given doesn ' t exist
2015-08-17 17:00:26 -07:00
*/
private function addArgument ( $name , $value )
{
if ( ! $this -> definition -> hasArgument ( $name )) {
2016-04-20 09:56:34 -07:00
throw new InvalidArgumentException ( sprintf ( 'The "%s" argument does not exist.' , $name ));
2015-08-17 17:00:26 -07:00
}
$this -> arguments [ $name ] = $value ;
}
}