Enum.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace loyalsoft;
  3. /**
  4. * PHP中的枚举类型,
  5. * Abstract class that enables creation of PHP enums. All you
  6. * have to do is extend this class and define some constants.
  7. * Enum is an object with value from on of those constants
  8. * (or from on of superclass if any). There is also
  9. * __default constat that enables you creation of object
  10. * without passing enum value.
  11. *
  12. * @refer Marijan Šuflaj <msufflaj32@gmail.com&gt
  13. * @author gwang<mail@wanggangzero.cn>
  14. * @version
  15. * 2.0.0 修改代码去掉后期绑定的特定用法,改用5.3.0支持的语法. gwang 2017.03.17
  16. * 1.0.0 参考了互联网上的资源,比如上面refer中提到的作者,源码的主体部分来自于他,
  17. * 结合了其他作者的功能, 调整了源码中的部分bug, 最终合并出来这个代码 gwang 2017.3.6
  18. */
  19. abstract class Enum
  20. {
  21. /**
  22. * Constant with default value for creating enum object
  23. */
  24. const __default = null;
  25. private $value;
  26. private $strict;
  27. private static $constants = array();
  28. /**
  29. * 获取预定义常量
  30. *
  31. * @param bool $includeDefault If true, default value is included into return
  32. * @return array Array with constant values
  33. */
  34. public function getConstList($includeDefault = false)
  35. {
  36. $class = get_class($this);
  37. if (!array_key_exists($class, self::$constants)) {
  38. $this->populateConstants();
  39. }
  40. return $includeDefault ? array_merge(self::$constants[$class], array(
  41. "__default" => self::__default
  42. )) : self::$constants[$class];
  43. }
  44. /**
  45. * Creates new enum object. If child class overrides __construct(),
  46. * it is required to call parent::__construct() in order for this
  47. * class to work as expected.
  48. *
  49. * @param mixed $initialValue Any value that is exists in defined constants
  50. * @param bool $strict If set to true, type and value must be equal
  51. * @throws UnexpectedValueException If value is not valid enum value
  52. */
  53. public function __construct($initialValue = null, $strict = true)
  54. {
  55. $class = get_class($this);
  56. if (!array_key_exists($class, self::$constants)) {
  57. $this->populateConstants();
  58. }
  59. if ($initialValue === null) {
  60. $initialValue = self::$constants[$class]["__default"];
  61. }
  62. if ($initialValue instanceof Enum) {
  63. $initialValue = $initialValue->value;
  64. }
  65. $temp = self::$constants[$class];
  66. if (!in_array($initialValue, $temp, $strict)) {
  67. throw new UnexpectedValueException("Value is not in enum " . $class);
  68. }
  69. $this->value = $initialValue;
  70. $this->strict = $strict;
  71. }
  72. private function populateConstants()
  73. {
  74. if (self::$constants == NULL) {
  75. self::$constants = array();
  76. }
  77. $class = get_class($this);
  78. $r = new \ReflectionClass($class);
  79. self::$constants [$class] = $r->getConstants();
  80. }
  81. /**
  82. * 重载魔术方法: 返回一个枚举值的字符串表示. 如果value的类型是string, 默认返回value, 否则 返回其名称.
  83. * @return string
  84. */
  85. public function __toString()
  86. {
  87. if (is_string($this->value)) {
  88. return $this->value;
  89. }
  90. $constants = $this->getConstList();
  91. $str = array_search($this->value, $constants);
  92. if (false === $str) {
  93. return $this->value;
  94. }
  95. return $str;
  96. }
  97. public function isValidName($name, $strict = false)
  98. {
  99. $constants = $this->getConstList();
  100. if ($strict) {
  101. return array_key_exists($name, $constants);
  102. }
  103. $keys = array_map('strtolower', array_keys($constants));
  104. return in_array(strtolower($name), $keys);
  105. }
  106. public function isValidValue($value, $strict = true)
  107. {
  108. $values = array_values($this->getConstList());
  109. return in_array($value, $values, $strict);
  110. }
  111. }