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