InputArgument.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Symfony\Component\Console\Input;
  3. /*
  4. * This file is part of the Symfony framework.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. /**
  12. * Represents a command line argument.
  13. *
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. */
  16. class InputArgument
  17. {
  18. const REQUIRED = 1;
  19. const OPTIONAL = 2;
  20. const IS_ARRAY = 4;
  21. protected $name;
  22. protected $mode;
  23. protected $default;
  24. protected $description;
  25. /**
  26. * Constructor.
  27. *
  28. * @param string $name The argument name
  29. * @param integer $mode The argument mode: self::REQUIRED or self::OPTIONAL
  30. * @param string $description A description text
  31. * @param mixed $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. } else if (is_string($mode) || $mode > 7) {
  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 Boolean 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 Boolean 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 mixed $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 Parameter::OPTIONAL mode.');
  85. }
  86. if ($this->isArray()) {
  87. if (null === $default) {
  88. $default = array();
  89. } else if (!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 mixed 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. }