Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / console / Input / InputArgument.php
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 use Symfony\Component\Console\Exception\InvalidArgumentException;
15 use Symfony\Component\Console\Exception\LogicException;
16
17 /**
18  * Represents a command line argument.
19  *
20  * @author Fabien Potencier <fabien@symfony.com>
21  */
22 class InputArgument
23 {
24     const REQUIRED = 1;
25     const OPTIONAL = 2;
26     const IS_ARRAY = 4;
27
28     private $name;
29     private $mode;
30     private $default;
31     private $description;
32
33     /**
34      * Constructor.
35      *
36      * @param string $name        The argument name
37      * @param int    $mode        The argument mode: self::REQUIRED or self::OPTIONAL
38      * @param string $description A description text
39      * @param mixed  $default     The default value (for self::OPTIONAL mode only)
40      *
41      * @throws InvalidArgumentException When argument mode is not valid
42      */
43     public function __construct($name, $mode = null, $description = '', $default = null)
44     {
45         if (null === $mode) {
46             $mode = self::OPTIONAL;
47         } elseif (!is_int($mode) || $mode > 7 || $mode < 1) {
48             throw new InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode));
49         }
50
51         $this->name = $name;
52         $this->mode = $mode;
53         $this->description = $description;
54
55         $this->setDefault($default);
56     }
57
58     /**
59      * Returns the argument name.
60      *
61      * @return string The argument name
62      */
63     public function getName()
64     {
65         return $this->name;
66     }
67
68     /**
69      * Returns true if the argument is required.
70      *
71      * @return bool true if parameter mode is self::REQUIRED, false otherwise
72      */
73     public function isRequired()
74     {
75         return self::REQUIRED === (self::REQUIRED & $this->mode);
76     }
77
78     /**
79      * Returns true if the argument can take multiple values.
80      *
81      * @return bool true if mode is self::IS_ARRAY, false otherwise
82      */
83     public function isArray()
84     {
85         return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
86     }
87
88     /**
89      * Sets the default value.
90      *
91      * @param mixed $default The default value
92      *
93      * @throws LogicException When incorrect default value is given
94      */
95     public function setDefault($default = null)
96     {
97         if (self::REQUIRED === $this->mode && null !== $default) {
98             throw new LogicException('Cannot set a default value except for InputArgument::OPTIONAL mode.');
99         }
100
101         if ($this->isArray()) {
102             if (null === $default) {
103                 $default = array();
104             } elseif (!is_array($default)) {
105                 throw new LogicException('A default value for an array argument must be an array.');
106             }
107         }
108
109         $this->default = $default;
110     }
111
112     /**
113      * Returns the default value.
114      *
115      * @return mixed The default value
116      */
117     public function getDefault()
118     {
119         return $this->default;
120     }
121
122     /**
123      * Returns the description text.
124      *
125      * @return string The description text
126      */
127     public function getDescription()
128     {
129         return $this->description;
130     }
131 }