-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnum.php
More file actions
147 lines (128 loc) · 3.01 KB
/
Enum.php
File metadata and controls
147 lines (128 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
declare(strict_types=1);
/**
* @author Damian Glinkowski <damian@d0niek.pl>
*/
abstract class Enum implements \JsonSerializable
{
/**
* Map enum's names and values
*
* @var array<string, mixed>
*/
const VALUES = [];
/**
* Actual enum name
*
* @var string
*/
private $_name;
/**
* Actual enum value
*
* @var mixed
*/
private $_value;
/**
* Already created enums
*
* @var array<string, \Enum>
*/
private static $_values = [];
/**
* Constructor \Enum
*
* @param string $name Enum's name
*
* @throws \RuntimeException
*/
private function __construct(string $name)
{
$isAllowValue = static::VALUES[$name] ?? null;
if (!$isAllowValue) {
throw new \RuntimeException(
"Can not create enum with name {$name}"
);
}
$this->_name = $name;
$this->_value = static::VALUES[$name];
}
/**
* Create enum object
*
* @param string $name Specific enum name
* @param array $args
*
* @return self
* @throws \RuntimeException
*/
public static function __callStatic(string $name, array $args): self
{
$key = self::generateKey($name);
$isEnumCreated = \array_key_exists($key, self::$_values);
if (!$isEnumCreated) {
self::$_values[$key] = new static($name);
self::creataEnumsWithSameValue(self::$_values[$key]);
}
return self::$_values[$key];
}
private static function creataEnumsWithSameValue(\Enum $enum): void
{
foreach (static::VALUES as $name => $value) {
if ($value === $enum()) {
$key = self::generateKey($name);
self::$_values[$key] = $enum;
}
}
}
private static function generateKey(string $name): string
{
return \get_called_class() . $name;
}
/**
* Create enum object from value
*
* @param mixed $value Enum's value
*
* @return self
* @throws \RuntimeException
*/
public static function fromValue($value): self
{
$enumName = \array_search($value, static::VALUES, true);
if ($enumName === false) {
throw new \RuntimeException("Unknow enum value \"{$value}\"");
}
return self::__callStatic($enumName, []);
}
/**
* Gets enum object value
*
* @return mixed
*/
public function __invoke()
{
return $this->_value;
}
/**
* Allow extract value from enum in string
*
* @return string
*/
public function __toString(): string
{
return (string) $this->_value;
}
/**
* Specify data which should be serialized to JSON
*
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
return [
'name' => $this->_name,
'value' => $this->_value
];
}
}