Argument.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\console\input;
  12. class Argument
  13. {
  14. const REQUIRED = 1;
  15. const OPTIONAL = 2;
  16. const IS_ARRAY = 4;
  17. private $name;
  18. private $mode;
  19. private $default;
  20. private $description;
  21. /**
  22. * 构造方法
  23. * @param string $name 参数名
  24. * @param int $mode 参数类型: self::REQUIRED 或者 self::OPTIONAL
  25. * @param string $description 描述
  26. * @param mixed $default 默认值 (仅 self::OPTIONAL 类型有效)
  27. * @throws \InvalidArgumentException
  28. */
  29. public function __construct($name, $mode = null, $description = '', $default = null)
  30. {
  31. if (null === $mode) {
  32. $mode = self::OPTIONAL;
  33. } elseif (!is_int($mode) || $mode > 7 || $mode < 1) {
  34. throw new \InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode));
  35. }
  36. $this->name = $name;
  37. $this->mode = $mode;
  38. $this->description = $description;
  39. $this->setDefault($default);
  40. }
  41. /**
  42. * 获取参数名
  43. * @return string
  44. */
  45. public function getName()
  46. {
  47. return $this->name;
  48. }
  49. /**
  50. * 是否必须
  51. * @return bool
  52. */
  53. public function isRequired()
  54. {
  55. return self::REQUIRED === (self::REQUIRED & $this->mode);
  56. }
  57. /**
  58. * 该参数是否接受数组
  59. * @return bool
  60. */
  61. public function isArray()
  62. {
  63. return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
  64. }
  65. /**
  66. * 设置默认值
  67. * @param mixed $default 默认值
  68. * @throws \LogicException
  69. */
  70. public function setDefault($default = null)
  71. {
  72. if (self::REQUIRED === $this->mode && null !== $default) {
  73. throw new \LogicException('Cannot set a default value except for InputArgument::OPTIONAL mode.');
  74. }
  75. if ($this->isArray()) {
  76. if (null === $default) {
  77. $default = [];
  78. } elseif (!is_array($default)) {
  79. throw new \LogicException('A default value for an array argument must be an array.');
  80. }
  81. }
  82. $this->default = $default;
  83. }
  84. /**
  85. * 获取默认值
  86. * @return mixed
  87. */
  88. public function getDefault()
  89. {
  90. return $this->default;
  91. }
  92. /**
  93. * 获取描述
  94. * @return string
  95. */
  96. public function getDescription()
  97. {
  98. return $this->description;
  99. }
  100. }