1 | <?php |
||
16 | trait EnumTrait |
||
17 | { |
||
18 | /** |
||
19 | * Cached array of enum instances by enum type (FQCN). |
||
20 | * This cache is used in order to make single enums values act as singletons. |
||
21 | * This means you'll always get the exact same instance for a same enum value. |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | private static $instances; |
||
26 | |||
27 | /** @var mixed */ |
||
28 | protected $value; |
||
29 | |||
30 | /** |
||
31 | * The constructor is private and cannot be overridden: use the static get method instead. |
||
32 | * |
||
33 | * @param mixed $value The raw value of an enumeration |
||
34 | */ |
||
35 | final private function __construct($value) |
||
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | * |
||
58 | * @return static The enum instance for given value |
||
59 | */ |
||
60 | public static function get($value): EnumInterface |
||
73 | |||
74 | /** |
||
75 | * Instantiates a new enumeration. |
||
76 | * |
||
77 | * @param string $name The name of a particular enumerated constant |
||
78 | * @param array $arguments |
||
79 | * |
||
80 | * @throws \BadMethodCallException On invalid constant name |
||
81 | * |
||
82 | * @return static When $name is an existing constant for this enumeration type |
||
83 | */ |
||
84 | public static function __callStatic($name, $arguments = []): EnumInterface |
||
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | public static function accepts($value): bool |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | public static function instances(): array |
||
114 | |||
115 | private static function getCachedInstance($value) |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function getValue() |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | public function equals(EnumInterface $enum): bool |
||
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | public function is($value): bool |
||
146 | } |
||
147 |