InputArgument.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Input;
  11. use Symfony\Component\Console\Exception\InvalidArgumentException;
  12. use Symfony\Component\Console\Exception\LogicException;
  13. /**
  14. * Represents a command line argument.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class InputArgument
  19. {
  20. public const REQUIRED = 1;
  21. public const OPTIONAL = 2;
  22. public const IS_ARRAY = 4;
  23. private string $name;
  24. private int $mode;
  25. private string|int|bool|array|null|float $default;
  26. private string $description;
  27. /**
  28. * @param string $name The argument name
  29. * @param int|null $mode The argument mode: self::REQUIRED or self::OPTIONAL
  30. * @param string $description A description text
  31. * @param string|bool|int|float|array|null $default The default value (for self::OPTIONAL mode only)
  32. *
  33. * @throws InvalidArgumentException When argument mode is not valid
  34. */
  35. public function __construct(string $name, int $mode = null, string $description = '', string|bool|int|float|array $default = null)
  36. {
  37. if (null === $mode) {
  38. $mode = self::OPTIONAL;
  39. } elseif ($mode > 7 || $mode < 1) {
  40. throw new InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode));
  41. }
  42. $this->name = $name;
  43. $this->mode = $mode;
  44. $this->description = $description;
  45. $this->setDefault($default);
  46. }
  47. /**
  48. * Returns the argument name.
  49. */
  50. public function getName(): string
  51. {
  52. return $this->name;
  53. }
  54. /**
  55. * Returns true if the argument is required.
  56. *
  57. * @return bool true if parameter mode is self::REQUIRED, false otherwise
  58. */
  59. public function isRequired(): bool
  60. {
  61. return self::REQUIRED === (self::REQUIRED & $this->mode);
  62. }
  63. /**
  64. * Returns true if the argument can take multiple values.
  65. *
  66. * @return bool true if mode is self::IS_ARRAY, false otherwise
  67. */
  68. public function isArray(): bool
  69. {
  70. return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
  71. }
  72. /**
  73. * Sets the default value.
  74. *
  75. * @throws LogicException When incorrect default value is given
  76. */
  77. public function setDefault(string|bool|int|float|array $default = null)
  78. {
  79. if (self::REQUIRED === $this->mode && null !== $default) {
  80. throw new LogicException('Cannot set a default value except for InputArgument::OPTIONAL mode.');
  81. }
  82. if ($this->isArray()) {
  83. if (null === $default) {
  84. $default = [];
  85. } elseif (!\is_array($default)) {
  86. throw new LogicException('A default value for an array argument must be an array.');
  87. }
  88. }
  89. $this->default = $default;
  90. }
  91. /**
  92. * Returns the default value.
  93. */
  94. public function getDefault(): string|bool|int|float|array|null
  95. {
  96. return $this->default;
  97. }
  98. /**
  99. * Returns the description text.
  100. */
  101. public function getDescription(): string
  102. {
  103. return $this->description;
  104. }
  105. }