2010-04-08 08:03:04 +04:00
|
|
|
<?php
|
|
|
|
|
2010-08-23 10:21:41 +04:00
|
|
|
namespace Symfony\Component\Console\Input;
|
2010-04-08 08:03:04 +04:00
|
|
|
|
|
|
|
/*
|
2010-10-30 15:24:50 +04:00
|
|
|
* This file is part of the Symfony framework.
|
2010-04-08 08:03:04 +04:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a command line argument.
|
|
|
|
*
|
2010-10-30 15:24:50 +04:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
2010-04-08 08:03:04 +04:00
|
|
|
*/
|
|
|
|
class InputArgument
|
|
|
|
{
|
2010-10-30 15:24:50 +04:00
|
|
|
const REQUIRED = 1;
|
|
|
|
const OPTIONAL = 2;
|
|
|
|
const IS_ARRAY = 4;
|
2010-04-08 08:03:04 +04:00
|
|
|
|
2010-10-30 15:24:50 +04:00
|
|
|
protected $name;
|
|
|
|
protected $mode;
|
|
|
|
protected $default;
|
|
|
|
protected $description;
|
2010-04-08 08:03:04 +04:00
|
|
|
|
2010-10-30 15:24:50 +04:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param string $name The argument name
|
|
|
|
* @param integer $mode The argument mode: self::REQUIRED or self::OPTIONAL
|
|
|
|
* @param string $description A description text
|
|
|
|
* @param mixed $default The default value (for self::OPTIONAL mode only)
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException When argument mode is not valid
|
|
|
|
*/
|
|
|
|
public function __construct($name, $mode = null, $description = '', $default = null)
|
2010-04-08 08:03:04 +04:00
|
|
|
{
|
2010-10-30 15:24:50 +04:00
|
|
|
if (null === $mode) {
|
|
|
|
$mode = self::OPTIONAL;
|
|
|
|
} else if (is_string($mode) || $mode > 7) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->name = $name;
|
|
|
|
$this->mode = $mode;
|
|
|
|
$this->description = $description;
|
|
|
|
|
|
|
|
$this->setDefault($default);
|
2010-04-08 08:03:04 +04:00
|
|
|
}
|
2010-10-30 15:24:50 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the argument name.
|
|
|
|
*
|
|
|
|
* @return string The argument name
|
|
|
|
*/
|
|
|
|
public function getName()
|
2010-04-08 08:03:04 +04:00
|
|
|
{
|
2010-10-30 15:24:50 +04:00
|
|
|
return $this->name;
|
2010-04-08 08:03:04 +04:00
|
|
|
}
|
|
|
|
|
2010-10-30 15:24:50 +04:00
|
|
|
/**
|
|
|
|
* Returns true if the argument is required.
|
|
|
|
*
|
|
|
|
* @return Boolean true if parameter mode is self::REQUIRED, false otherwise
|
|
|
|
*/
|
|
|
|
public function isRequired()
|
|
|
|
{
|
|
|
|
return self::REQUIRED === (self::REQUIRED & $this->mode);
|
|
|
|
}
|
2010-04-08 08:03:04 +04:00
|
|
|
|
2010-10-30 15:24:50 +04:00
|
|
|
/**
|
|
|
|
* Returns true if the argument can take multiple values.
|
|
|
|
*
|
|
|
|
* @return Boolean true if mode is self::IS_ARRAY, false otherwise
|
|
|
|
*/
|
|
|
|
public function isArray()
|
|
|
|
{
|
|
|
|
return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
|
|
|
|
}
|
2010-04-08 08:03:04 +04:00
|
|
|
|
2010-10-30 15:24:50 +04:00
|
|
|
/**
|
|
|
|
* Sets the default value.
|
|
|
|
*
|
|
|
|
* @param mixed $default The default value
|
|
|
|
*
|
|
|
|
* @throws \LogicException When incorrect default value is given
|
|
|
|
*/
|
|
|
|
public function setDefault($default = null)
|
|
|
|
{
|
|
|
|
if (self::REQUIRED === $this->mode && null !== $default) {
|
|
|
|
throw new \LogicException('Cannot set a default value except for Parameter::OPTIONAL mode.');
|
|
|
|
}
|
2010-04-08 08:03:04 +04:00
|
|
|
|
2010-10-30 15:24:50 +04:00
|
|
|
if ($this->isArray()) {
|
|
|
|
if (null === $default) {
|
|
|
|
$default = array();
|
|
|
|
} else if (!is_array($default)) {
|
|
|
|
throw new \LogicException('A default value for an array argument must be an array.');
|
|
|
|
}
|
|
|
|
}
|
2010-04-08 08:03:04 +04:00
|
|
|
|
2010-10-30 15:24:50 +04:00
|
|
|
$this->default = $default;
|
|
|
|
}
|
2010-04-08 08:03:04 +04:00
|
|
|
|
2010-10-30 15:24:50 +04:00
|
|
|
/**
|
|
|
|
* Returns the default value.
|
|
|
|
*
|
|
|
|
* @return mixed The default value
|
|
|
|
*/
|
|
|
|
public function getDefault()
|
2010-04-08 08:03:04 +04:00
|
|
|
{
|
2010-10-30 15:24:50 +04:00
|
|
|
return $this->default;
|
2010-04-08 08:03:04 +04:00
|
|
|
}
|
|
|
|
|
2010-10-30 15:24:50 +04:00
|
|
|
/**
|
|
|
|
* Returns the description text.
|
|
|
|
*
|
|
|
|
* @return string The description text
|
|
|
|
*/
|
|
|
|
public function getDescription()
|
2010-04-08 08:03:04 +04:00
|
|
|
{
|
2010-10-30 15:24:50 +04:00
|
|
|
return $this->description;
|
2010-04-08 08:03:04 +04:00
|
|
|
}
|
|
|
|
}
|