Guest User

Untitled

a guest
Jan 23rd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. <?php
  2.  
  3. use InvalidArgumentException;
  4.  
  5. abstract class Enum
  6. {
  7. protected $value;
  8.  
  9. /**
  10. * The constructor is protected: use the static create method instead
  11. *
  12. * @param mixed $value
  13. */
  14. protected function __construct($value)
  15. {
  16. $this->value = $value;
  17. }
  18.  
  19. /**
  20. * Instantiates a new Enum
  21. *
  22. * @param mixed $value
  23. * @return Enum
  24. */
  25. public static function create($value)
  26. {
  27. static::checkIsAcceptableValue($value);
  28.  
  29. return new static($value);
  30. }
  31.  
  32. /**
  33. * Instantiates a new Enum with the default value
  34. *
  35. * @param mixed $value
  36. * @return Enum
  37. */
  38. public static function createWithDefault()
  39. {
  40. return new static(static::getDefaultValue());
  41. }
  42.  
  43. /**
  44. * Gets an array of the possible values
  45. *
  46. * @return array
  47. */
  48. public static function getPossibleValues()
  49. {
  50. throw new \Exception('This method should be implemented');
  51. }
  52.  
  53. /**
  54. * Gets an array of the human representations indexed by possible values
  55. *
  56. * @return array
  57. */
  58. public static function getReadables()
  59. {
  60. throw new \Exception('This method should be implemented');
  61. }
  62.  
  63. /**
  64. * Gets a default value
  65. *
  66. * @return value
  67. */
  68. public static function getDefaultValue()
  69. {
  70. return null;
  71. }
  72.  
  73. /**
  74. * Gets the raw value
  75. *
  76. * @return mixed
  77. **/
  78. public function getValue()
  79. {
  80. return $this->value;
  81. }
  82.  
  83. /**
  84. * Returns the human representation of the value
  85. *
  86. * @return string
  87. **/
  88. public function getReadable()
  89. {
  90. return static::getReadableFor($this->getValue());
  91. }
  92.  
  93. /**
  94. * Tells is this value is acceptable
  95. *
  96. * @param mixed $value
  97. * @return boolean
  98. */
  99. public static function isAcceptableValue($value)
  100. {
  101. return in_array($value, static::getPossibleValues());
  102. }
  103.  
  104. /**
  105. * Checks if this value is acceptable
  106. *
  107. * @throws InvalidArgumentException
  108. * @param mixed $value
  109. */
  110. public static function checkIsAcceptableValue($value)
  111. {
  112. if (!static::isAcceptableValue($value)) {
  113. throw new InvalidArgumentException($value.' is not an acceptable value');
  114. }
  115. }
  116.  
  117. /**
  118. * Gets the human representation for a given value
  119. *
  120. * @return string
  121. **/
  122. public static function getReadableFor($value)
  123. {
  124. static::checkIsAcceptableValue($value);
  125. $humanRepresentations = static::getReadables();
  126.  
  127. return array_search($value, $humanRepresentations);
  128. }
  129.  
  130. /**
  131. * Converts to the human representation of the current value
  132. *
  133. * @return string
  134. **/
  135. public function __toString()
  136. {
  137. return (string) $this->getReadable();
  138. }
  139. }
Add Comment
Please, Sign In to add comment