sfCommandArgument.class.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Represents a command line argument.
  11. *
  12. * @package symfony
  13. * @subpackage command
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfCommandArgument.class.php 17858 2009-05-01 21:22:50Z FabianLange $
  16. */
  17. class sfCommandArgument
  18. {
  19. const REQUIRED = 1;
  20. const OPTIONAL = 2;
  21. const IS_ARRAY = 4;
  22. protected
  23. $name = null,
  24. $mode = null,
  25. $default = null,
  26. $help = '';
  27. /**
  28. * Constructor.
  29. *
  30. * @param string $name The argument name
  31. * @param integer $mode The argument mode: self::REQUIRED or self::OPTIONAL
  32. * @param string $help A help text
  33. * @param mixed $default The default value (for self::OPTIONAL mode only)
  34. */
  35. public function __construct($name, $mode = null, $help = '', $default = null)
  36. {
  37. if (is_null($mode))
  38. {
  39. $mode = self::OPTIONAL;
  40. }
  41. else if (is_string($mode) || $mode > 7)
  42. {
  43. throw new sfCommandException(sprintf('Argument mode "%s" is not valid.', $mode));
  44. }
  45. $this->name = $name;
  46. $this->mode = $mode;
  47. $this->help = $help;
  48. $this->setDefault($default);
  49. }
  50. /**
  51. * Returns the argument name.
  52. *
  53. * @return string The argument name
  54. */
  55. public function getName()
  56. {
  57. return $this->name;
  58. }
  59. /**
  60. * Returns true if the argument is required.
  61. *
  62. * @return Boolean true if parameter mode is self::REQUIRED, false otherwise
  63. */
  64. public function isRequired()
  65. {
  66. return self::REQUIRED === (self::REQUIRED & $this->mode);
  67. }
  68. /**
  69. * Returns true if the argument can take multiple values.
  70. *
  71. * @return Boolean true if mode is self::IS_ARRAY, false otherwise
  72. */
  73. public function isArray()
  74. {
  75. return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
  76. }
  77. /**
  78. * Sets the default value.
  79. *
  80. * @param mixed $default The default value
  81. */
  82. public function setDefault($default = null)
  83. {
  84. if (self::REQUIRED === $this->mode && !is_null($default))
  85. {
  86. throw new sfCommandException('Cannot set a default value except for sfCommandParameter::OPTIONAL mode.');
  87. }
  88. if ($this->isArray())
  89. {
  90. if (is_null($default))
  91. {
  92. $default = array();
  93. }
  94. else if (!is_array($default))
  95. {
  96. throw new sfCommandException('A default value for an array argument must be an array.');
  97. }
  98. }
  99. $this->default = $default;
  100. }
  101. /**
  102. * Returns the default value.
  103. *
  104. * @return mixed The default value
  105. */
  106. public function getDefault()
  107. {
  108. return $this->default;
  109. }
  110. /**
  111. * Returns the help text.
  112. *
  113. * @return string The help text
  114. */
  115. public function getHelp()
  116. {
  117. return $this->help;
  118. }
  119. }