Guest User

Untitled

a guest
Nov 19th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. <?php
  2.  
  3. trait EnumValuesTrait
  4. {
  5. /** @var static[] */
  6. private static $values = [];
  7.  
  8. /**
  9. * @return static[]
  10. */
  11. public static function values()
  12. {
  13. if (empty(self::$values)) {
  14. self::$values = self::initValues();
  15. }
  16. return self::$values;
  17. }
  18. }
  19.  
  20. class SomeStrategy
  21. {
  22. use EnumValuesTrait;
  23.  
  24. private static function initValues()
  25. {
  26. return [
  27. 'TRAVERSE' => new self(1, 'Traverse'),
  28. 'DIRECT' => new self(2, 'Direct'),
  29. ];
  30. }
  31.  
  32. /** @var int */
  33. private $id;
  34.  
  35. /** @var string */
  36. private $title;
  37.  
  38. /**
  39. * Private constructor avoids extrernal creation of the instances.
  40. * @param int $id
  41. * @param string $title
  42. */
  43. private function __construct($id, $title)
  44. {
  45. $this->id = $id;
  46. $this->title = $title;
  47. }
  48.  
  49. /**
  50. * @return SomeStrategy
  51. */
  52. public static function TRAVERSE()
  53. {
  54. return self::values()['TRAVERSE'];
  55. }
  56.  
  57. /**
  58. * @return SomeStrategy
  59. */
  60. public static function DIRECT()
  61. {
  62. return self::values()['DIRECT'];
  63. }
  64.  
  65. /**
  66. * @param string $title
  67. * @return null|SomeStrategy
  68. */
  69. public static function getByTitle(string $title)
  70. {
  71. $title2upper = mb_strtoupper($title);
  72.  
  73. foreach (self::values() as $value) {
  74. if (mb_strtoupper($value->title) === $title2upper) {
  75. return $value;
  76. }
  77. }
  78. return null;
  79. }
  80.  
  81. // Helper methods...
  82. /**
  83. * @return bool
  84. */
  85. public function isTraverse()
  86. {
  87. return $this === self::TRAVERSE();
  88. }
  89.  
  90. /**
  91. * @return bool
  92. */
  93. public function isDirect()
  94. {
  95. return $this === self::DIRECT();
  96. }
  97.  
  98. // Getters...
  99. /**
  100. * @return int
  101. */
  102. public function getId(): int
  103. {
  104. return $this->id;
  105. }
  106.  
  107. /**
  108. * @return string
  109. */
  110. public function getTitle(): string
  111. {
  112. return $this->title;
  113. }
  114. }
  115.  
  116. class UsageExample {
  117.  
  118. public function lookMa()
  119. {
  120. // direct pointer
  121. $strategy1 = SomeStrategy::TRAVERSE();
  122. // dinamyc pointer
  123. $strategy2 = SomeStrategy::getByTitle('traverse');
  124.  
  125. // calling regular methods
  126. if ($strategy1->isTraverse() && $strategy2->isTraverse()) {
  127. echo $strategy1->getId() . ': ' . $strategy2->getTitle();
  128. }
  129.  
  130. // comparing pointers
  131. echo $strategy1 === $strategy2 ? 'true' : 'false';
  132.  
  133. // iterating over the values with type hinting
  134. foreach (SomeStrategy::values() as $value) {
  135. echo $value->getId() . PHP_EOL;
  136. }
  137.  
  138. // using in switch cases
  139. switch ($strategy1) {
  140. case SomeStrategy::TRAVERSE():
  141. echo 'Traverse';
  142. break;
  143. case SomeStrategy::DIRECT():
  144. echo 'Direct';
  145. break;
  146. }
  147. }
  148. }
Add Comment
Please, Sign In to add comment