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