TYPO3  7.6
InputArgument.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Symfony\Component\Console\Input;
13 
22 {
23  const REQUIRED = 1;
24  const OPTIONAL = 2;
25  const IS_ARRAY = 4;
26 
27  private $name;
28  private $mode;
29  private $default;
30  private $description;
31 
44  public function __construct($name, $mode = null, $description = '', $default = null)
45  {
46  if (null === $mode) {
47  $mode = self::OPTIONAL;
48  } elseif (!is_int($mode) || $mode > 7 || $mode < 1) {
49  throw new \InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode));
50  }
51 
52  $this->name = $name;
53  $this->mode = $mode;
54  $this->description = $description;
55 
56  $this->setDefault($default);
57  }
58 
64  public function getName()
65  {
66  return $this->name;
67  }
68 
74  public function isRequired()
75  {
76  return self::REQUIRED === (self::REQUIRED & $this->mode);
77  }
78 
84  public function isArray()
85  {
86  return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
87  }
88 
96  public function setDefault($default = null)
97  {
98  if (self::REQUIRED === $this->mode && null !== $default) {
99  throw new \LogicException('Cannot set a default value except for InputArgument::OPTIONAL mode.');
100  }
101 
102  if ($this->isArray()) {
103  if (null === $default) {
104  $default = array();
105  } elseif (!is_array($default)) {
106  throw new \LogicException('A default value for an array argument must be an array.');
107  }
108  }
109 
110  $this->default = $default;
111  }
112 
118  public function getDefault()
119  {
120  return $this->default;
121  }
122 
128  public function getDescription()
129  {
130  return $this->description;
131  }
132 }