/src/think/console/input/Option.php

https://github.com/top-think/framework · PHP · 221 lines · 105 code · 28 blank · 88 comment · 21 complexity · 3d6829b73299c87f94d320388525a06e MD5 · raw file

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