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:
14: /**
15: * Represents a command line argument.
16: *
17: * @author Fabien Potencier <fabien@symfony.com>
18: *
19: * @api
20: */
21: class InputArgument
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:
32: /**
33: * Constructor.
34: *
35: * @param string $name The argument name
36: * @param integer $mode The argument mode: self::REQUIRED or self::OPTIONAL
37: * @param string $description A description text
38: * @param mixed $default The default value (for self::OPTIONAL mode only)
39: *
40: * @throws \InvalidArgumentException When argument mode is not valid
41: *
42: * @api
43: */
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:
59: /**
60: * Returns the argument name.
61: *
62: * @return string The argument name
63: */
64: public function getName()
65: {
66: return $this->name;
67: }
68:
69: /**
70: * Returns true if the argument is required.
71: *
72: * @return Boolean true if parameter mode is self::REQUIRED, false otherwise
73: */
74: public function isRequired()
75: {
76: return self::REQUIRED === (self::REQUIRED & $this->mode);
77: }
78:
79: /**
80: * Returns true if the argument can take multiple values.
81: *
82: * @return Boolean true if mode is self::IS_ARRAY, false otherwise
83: */
84: public function isArray()
85: {
86: return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
87: }
88:
89: /**
90: * Sets the default value.
91: *
92: * @param mixed $default The default value
93: *
94: * @throws \LogicException When incorrect default value is given
95: */
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 Parameter::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:
113: /**
114: * Returns the default value.
115: *
116: * @return mixed The default value
117: */
118: public function getDefault()
119: {
120: return $this->default;
121: }
122:
123: /**
124: * Returns the description text.
125: *
126: * @return string The description text
127: */
128: public function getDescription()
129: {
130: return $this->description;
131: }
132: }
133: