1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nasyrov\Laravel\Enums; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
use UnexpectedValueException; |
8
|
|
|
use JsonSerializable; |
9
|
|
|
|
10
|
|
|
abstract class Enum implements JsonSerializable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The enum value. |
14
|
|
|
* |
15
|
|
|
* @var mixed |
16
|
|
|
*/ |
17
|
|
|
protected $value; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The enum constants. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected static $constants = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create a new enum instance. |
28
|
|
|
* |
29
|
|
|
* @param mixed $value |
30
|
|
|
*/ |
31
|
14 |
|
public function __construct($value) |
32
|
|
|
{ |
33
|
14 |
|
if (!static::constants()->contains($value)) { |
34
|
2 |
|
throw new UnexpectedValueException(sprintf( |
35
|
2 |
|
'Value `%s` is not part of the enum %s', |
36
|
2 |
|
$value, |
37
|
2 |
|
static::class |
38
|
|
|
)); |
39
|
|
|
} |
40
|
|
|
|
41
|
14 |
|
$this->value = $value; |
42
|
14 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
2 |
|
public function __toString() |
48
|
|
|
{ |
49
|
2 |
|
return (string)$this->value; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the serialized value. |
54
|
|
|
* |
55
|
|
|
* @return mixed |
56
|
|
|
*/ |
57
|
2 |
|
public function jsonSerialize() |
58
|
|
|
{ |
59
|
2 |
|
return $this->value; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the enum key. |
64
|
|
|
* |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
2 |
|
public function getKey() |
68
|
|
|
{ |
69
|
2 |
|
return static::constants()->search($this->value, true); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the enum value. |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
2 |
|
public function getValue() |
78
|
|
|
{ |
79
|
2 |
|
return $this->value; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the enum keys. |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
2 |
|
public static function keys() |
88
|
|
|
{ |
89
|
2 |
|
return static::constants() |
90
|
2 |
|
->keys() |
91
|
2 |
|
->all(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the enum values. |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
2 |
|
public static function values() |
100
|
|
|
{ |
101
|
2 |
|
return static::constants() |
102
|
2 |
|
->map(function ($value) { |
103
|
2 |
|
return new static($value); |
104
|
2 |
|
}) |
105
|
2 |
|
->all(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the enum constants. |
110
|
|
|
* |
111
|
|
|
* @return \Illuminate\Support\Collection |
112
|
|
|
*/ |
113
|
20 |
|
public static function constants() |
114
|
|
|
{ |
115
|
20 |
|
if (!isset(static::$constants[static::class])) { |
116
|
2 |
|
static::$constants[static::class] = collect( |
117
|
2 |
|
(new ReflectionClass(static::class))->getConstants() |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
20 |
|
return static::$constants[static::class]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns a new enum instance when called statically. |
126
|
|
|
* |
127
|
|
|
* @param string $name |
128
|
|
|
* @param array $arguments |
129
|
|
|
* |
130
|
|
|
* @return static |
131
|
|
|
*/ |
132
|
4 |
|
public static function __callStatic($name, array $arguments) |
133
|
|
|
{ |
134
|
4 |
|
if (static::constants()->has($name)) { |
135
|
2 |
|
return new static(static::constants()->get($name)); |
136
|
|
|
} |
137
|
|
|
|
138
|
2 |
|
throw new BadMethodCallException(sprintf( |
139
|
2 |
|
'No static method or enum constant `%s` in class %s', |
140
|
2 |
|
$name, |
141
|
2 |
|
static::class |
142
|
|
|
)); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|