Enum.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * The Enum base class for Enum functionality
  4. *
  5. * PHP version 5.3
  6. *
  7. * @category PHPPasswordLib
  8. * @package Core
  9. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  10. * @copyright 2011 The Authors
  11. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  12. * @version Build @@version@@
  13. */
  14. namespace SecurityLib;
  15. use \ReflectionObject;
  16. /**
  17. * The Enum base class for Enum functionality
  18. *
  19. * This is based off of the SplEnum class implementation (which is only available
  20. * as a PECL extension in 5.3)
  21. *
  22. * @see http://www.php.net/manual/en/class.splenum.php
  23. * @category PHPPasswordLib
  24. * @package Core
  25. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  26. */
  27. abstract class Enum {
  28. /**
  29. * A default value of null is provided. Override this to set your own default
  30. */
  31. const __DEFAULT = null;
  32. /**
  33. * @var string The name of the constant this instance is using
  34. */
  35. protected $name = '';
  36. /**
  37. * @var scalar The value of the constant this instance is using.
  38. */
  39. protected $value = '';
  40. /**
  41. * Creates a new value of the Enum type
  42. *
  43. * @param mixed $value The value this instance represents
  44. * @param boolean $strict Not Implemented at this time
  45. *
  46. * @return void
  47. * @throws UnexpectedValueException If the value is not a constant
  48. */
  49. public function __construct($value = null, $strict = false) {
  50. if (is_null($value)) {
  51. $value = static::__DEFAULT;
  52. }
  53. $validValues = $this->getConstList();
  54. $this->name = array_search($value, $validValues);
  55. if (!$this->name) {
  56. throw new \UnexpectedValueException(
  57. 'Value not a const in enum ' . get_class($this)
  58. );
  59. }
  60. $this->value = $value;
  61. }
  62. /**
  63. * Cast the current object to a string and return its value
  64. *
  65. * @return mixed the current value of the instance
  66. */
  67. public function __toString() {
  68. return (string) $this->value;
  69. }
  70. /**
  71. * Compare two enums using numeric comparison
  72. *
  73. * @param Enum $arg The enum to compare this instance to
  74. *
  75. * @return int 0 if same, 1 if the argument is greater, -1 else
  76. */
  77. public function compare(Enum $arg) {
  78. if ($this->value == $arg->value) {
  79. return 0;
  80. } elseif ($this->value > $arg->value) {
  81. return -1;
  82. } else {
  83. return 1;
  84. }
  85. }
  86. /**
  87. * Returns all constants (including values) as an associative array
  88. *
  89. * @param boolean $include_default Include the __default magic value?
  90. *
  91. * @return array All of the constants found against this instance
  92. */
  93. public function getConstList($include_default = false) {
  94. static $constCache = array();
  95. $class = get_class($this);
  96. if (!isset($constCache[$class])) {
  97. $reflector = new ReflectionObject($this);
  98. $constCache[$class] = $reflector->getConstants();
  99. }
  100. if (!$include_default) {
  101. $constants = $constCache[$class];
  102. unset($constants['__DEFAULT']);
  103. return $constants;
  104. }
  105. return $constCache[$class];
  106. }
  107. }