Option.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 Option
  13. {
  14. const VALUE_NONE = 1;
  15. const VALUE_REQUIRED = 2;
  16. const VALUE_OPTIONAL = 4;
  17. const VALUE_IS_ARRAY = 8;
  18. private $name;
  19. private $shortcut;
  20. private $mode;
  21. private $default;
  22. private $description;
  23. /**
  24. * 构造方法
  25. * @param string $name 选项名
  26. * @param string|array $shortcut 短名称,多个用|隔开或者使用数组
  27. * @param int $mode 选项类型(可选类型为 self::VALUE_*)
  28. * @param string $description 描述
  29. * @param mixed $default 默认值 (类型为 self::VALUE_REQUIRED 或者 self::VALUE_NONE 的时候必须为null)
  30. * @throws \InvalidArgumentException
  31. */
  32. public function __construct($name, $shortcut = null, $mode = null, $description = '', $default = null)
  33. {
  34. if (0 === strpos($name, '--')) {
  35. $name = substr($name, 2);
  36. }
  37. if (empty($name)) {
  38. throw new \InvalidArgumentException('An option name cannot be empty.');
  39. }
  40. if (empty($shortcut)) {
  41. $shortcut = null;
  42. }
  43. if (null !== $shortcut) {
  44. if (is_array($shortcut)) {
  45. $shortcut = implode('|', $shortcut);
  46. }
  47. $shortcuts = preg_split('{(\|)-?}', ltrim($shortcut, '-'));
  48. $shortcuts = array_filter($shortcuts);
  49. $shortcut = implode('|', $shortcuts);
  50. if (empty($shortcut)) {
  51. throw new \InvalidArgumentException('An option shortcut cannot be empty.');
  52. }
  53. }
  54. if (null === $mode) {
  55. $mode = self::VALUE_NONE;
  56. } elseif (!is_int($mode) || $mode > 15 || $mode < 1) {
  57. throw new \InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode));
  58. }
  59. $this->name = $name;
  60. $this->shortcut = $shortcut;
  61. $this->mode = $mode;
  62. $this->description = $description;
  63. if ($this->isArray() && !$this->acceptValue()) {
  64. throw new \InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.');
  65. }
  66. $this->setDefault($default);
  67. }
  68. /**
  69. * 获取短名称
  70. * @return string
  71. */
  72. public function getShortcut()
  73. {
  74. return $this->shortcut;
  75. }
  76. /**
  77. * 获取选项名
  78. * @return string
  79. */
  80. public function getName()
  81. {
  82. return $this->name;
  83. }
  84. /**
  85. * 是否可以设置值
  86. * @return bool 类型不是 self::VALUE_NONE 的时候返回true,其他均返回false
  87. */
  88. public function acceptValue()
  89. {
  90. return $this->isValueRequired() || $this->isValueOptional();
  91. }
  92. /**
  93. * 是否必须
  94. * @return bool 类型是 self::VALUE_REQUIRED 的时候返回true,其他均返回false
  95. */
  96. public function isValueRequired()
  97. {
  98. return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode);
  99. }
  100. /**
  101. * 是否可选
  102. * @return bool 类型是 self::VALUE_OPTIONAL 的时候返回true,其他均返回false
  103. */
  104. public function isValueOptional()
  105. {
  106. return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode);
  107. }
  108. /**
  109. * 选项值是否接受数组
  110. * @return bool 类型是 self::VALUE_IS_ARRAY 的时候返回true,其他均返回false
  111. */
  112. public function isArray()
  113. {
  114. return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode);
  115. }
  116. /**
  117. * 设置默认值
  118. * @param mixed $default 默认值
  119. * @throws \LogicException
  120. */
  121. public function setDefault($default = null)
  122. {
  123. if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) {
  124. throw new \LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.');
  125. }
  126. if ($this->isArray()) {
  127. if (null === $default) {
  128. $default = [];
  129. } elseif (!is_array($default)) {
  130. throw new \LogicException('A default value for an array option must be an array.');
  131. }
  132. }
  133. $this->default = $this->acceptValue() ? $default : false;
  134. }
  135. /**
  136. * 获取默认值
  137. * @return mixed
  138. */
  139. public function getDefault()
  140. {
  141. return $this->default;
  142. }
  143. /**
  144. * 获取描述文字
  145. * @return string
  146. */
  147. public function getDescription()
  148. {
  149. return $this->description;
  150. }
  151. /**
  152. * 检查所给选项是否是当前这个
  153. * @param Option $option
  154. * @return bool
  155. */
  156. public function equals(Option $option)
  157. {
  158. return $option->getName() === $this->getName()
  159. && $option->getShortcut() === $this->getShortcut()
  160. && $option->getDefault() === $this->getDefault()
  161. && $option->isArray() === $this->isArray()
  162. && $option->isValueRequired() === $this->isValueRequired()
  163. && $option->isValueOptional() === $this->isValueOptional();
  164. }
  165. }