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 ;
/**
* Constructor .
*
2017-02-02 16:28:38 -08:00
* @ param array $parameters An array of parameters
* @ param InputDefinition | null $definition A InputDefinition instance
2015-08-17 17:00:26 -07:00
*/
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
*/
public function hasParameterOption ( $values )
{
$values = ( array ) $values ;
foreach ( $this -> parameters as $k => $v ) {
if ( ! is_int ( $k )) {
$v = $k ;
}
if ( in_array ( $v , $values )) {
return true ;
}
}
return false ;
}
/**
2017-02-02 16:28:38 -08:00
* { @ inheritdoc }
2015-08-17 17:00:26 -07:00
*/
public function getParameterOption ( $values , $default = false )
{
$values = ( array ) $values ;
foreach ( $this -> parameters as $k => $v ) {
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 .
*
* @ return string
*/
public function __toString ()
{
$params = array ();
foreach ( $this -> parameters as $param => $val ) {
if ( $param && '-' === $param [ 0 ]) {
$params [] = $param . ( '' != $val ? '=' . $this -> escapeToken ( $val ) : '' );
} else {
$params [] = $this -> escapeToken ( $val );
}
}
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 ) {
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
}
$value = $option -> isValueOptional () ? $option -> getDefault () : true ;
}
$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 ;
}
}