123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace Symfony\Component\Console\Input;
- use Symfony\Component\Console\Exception\InvalidArgumentException;
- use Symfony\Component\Console\Exception\LogicException;
- class InputArgument
- {
- const REQUIRED = 1;
- const OPTIONAL = 2;
- const IS_ARRAY = 4;
- private $name;
- private $mode;
- private $default;
- private $description;
-
- public function __construct($name, $mode = null, $description = '', $default = null)
- {
- if (null === $mode) {
- $mode = self::OPTIONAL;
- } elseif (!\is_int($mode) || $mode > 7 || $mode < 1) {
- throw new InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode));
- }
- $this->name = $name;
- $this->mode = $mode;
- $this->description = $description;
- $this->setDefault($default);
- }
-
- public function getName()
- {
- return $this->name;
- }
-
- public function isRequired()
- {
- return self::REQUIRED === (self::REQUIRED & $this->mode);
- }
-
- public function isArray()
- {
- return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
- }
-
- public function setDefault($default = null)
- {
- if (self::REQUIRED === $this->mode && null !== $default) {
- throw new LogicException('Cannot set a default value except for InputArgument::OPTIONAL mode.');
- }
- if ($this->isArray()) {
- if (null === $default) {
- $default = array();
- } elseif (!\is_array($default)) {
- throw new LogicException('A default value for an array argument must be an array.');
- }
- }
- $this->default = $default;
- }
-
- public function getDefault()
- {
- return $this->default;
- }
-
- public function getDescription()
- {
- return $this->description;
- }
- }
|