InputArgument.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. const REQUIRED = 1;
  21. const OPTIONAL = 2;
  22. const IS_ARRAY = 4;
  23. private $name;
  24. private $mode;
  25. private $default;
  26. private $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|string[]|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($name, $mode = null, $description = '', $default = null)
  36. {
  37. if (null === $mode) {
  38. $mode = self::OPTIONAL;
  39. } elseif (!\is_int($mode) || $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. * @return string The argument name
  51. */
  52. public function getName()
  53. {
  54. return $this->name;
  55. }
  56. /**
  57. * Returns true if the argument is required.
  58. *
  59. * @return bool true if parameter mode is self::REQUIRED, false otherwise
  60. */
  61. public function isRequired()
  62. {
  63. return self::REQUIRED === (self::REQUIRED & $this->mode);
  64. }
  65. /**
  66. * Returns true if the argument can take multiple values.
  67. *
  68. * @return bool true if mode is self::IS_ARRAY, false otherwise
  69. */
  70. public function isArray()
  71. {
  72. return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
  73. }
  74. /**
  75. * Sets the default value.
  76. *
  77. * @param string|string[]|null $default The default value
  78. *
  79. * @throws LogicException When incorrect default value is given
  80. */
  81. public function setDefault($default = null)
  82. {
  83. if (self::REQUIRED === $this->mode && null !== $default) {
  84. throw new LogicException('Cannot set a default value except for InputArgument::OPTIONAL mode.');
  85. }
  86. if ($this->isArray()) {
  87. if (null === $default) {
  88. $default = array();
  89. } elseif (!\is_array($default)) {
  90. throw new LogicException('A default value for an array argument must be an array.');
  91. }
  92. }
  93. $this->default = $default;
  94. }
  95. /**
  96. * Returns the default value.
  97. *
  98. * @return string|string[]|null The default value
  99. */
  100. public function getDefault()
  101. {
  102. return $this->default;
  103. }
  104. /**
  105. * Returns the description text.
  106. *
  107. * @return string The description text
  108. */
  109. public function getDescription()
  110. {
  111. return $this->description;
  112. }
  113. }