|
1
|
|
|
<?php |
|
2
|
|
|
namespace Robusto\Enum; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Trait for enumerations. |
|
6
|
|
|
* |
|
7
|
|
|
* @author Jarddel Antunes |
|
8
|
|
|
* @package Robusto\Enum |
|
9
|
|
|
* @copyright 2017 Robusto Enum |
|
10
|
|
|
*/ |
|
11
|
|
|
trait EnumTrait |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* |
|
15
|
|
|
* @var int Represents the value of the enumerative instance. |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $value; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* |
|
21
|
|
|
* @var string Represents the description of the enumerative instance. |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $description; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* |
|
27
|
|
|
* @var array Represents possible values according to the enumerative class. |
|
28
|
|
|
*/ |
|
29
|
|
|
protected static $values = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* |
|
33
|
|
|
* @var array Represents the possible descriptive values according to the enumerative class. |
|
34
|
|
|
*/ |
|
35
|
|
|
protected static $descriptions = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* |
|
39
|
|
|
* @var array Represents the names of the constants of the enumerative class. |
|
40
|
|
|
*/ |
|
41
|
|
|
protected static $constants = []; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Gets all possible values for the enumerative class. |
|
45
|
|
|
* |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function getValues(): array |
|
49
|
|
|
{ |
|
50
|
|
|
static::assignValues(); |
|
51
|
|
|
|
|
52
|
|
|
return static::$values; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Gets all possible descriptions for the enumerative class. |
|
57
|
|
|
* |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function getDescriptions(): array |
|
61
|
|
|
{ |
|
62
|
|
|
static::assignValues(); |
|
63
|
|
|
|
|
64
|
|
|
return static::$descriptions; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get value of the enumerative instance. |
|
69
|
|
|
* |
|
70
|
|
|
* @return int |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getValue(): int |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->value; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get description of the enumerative instance. |
|
79
|
|
|
* |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getDescription(): string |
|
83
|
|
|
{ |
|
84
|
|
|
return (string) $this->description; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $name |
|
90
|
|
|
* @param array $arguments |
|
91
|
|
|
* @return Enum |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function __callStatic(string $name, array $arguments): EnumInterface |
|
94
|
|
|
{ |
|
95
|
|
|
static::assignConstants(); |
|
96
|
|
|
static::assignValues(); |
|
97
|
|
|
|
|
98
|
|
|
return static::getEnumByConstant($name); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Gets the descriptive value that represents the enumerative instance. |
|
103
|
|
|
* |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
public function __toString(): string |
|
107
|
|
|
{ |
|
108
|
|
|
static::assignValues(); |
|
109
|
|
|
$description = static::$descriptions[array_search($this->value, static::$values)] ?? $this->value; |
|
110
|
|
|
|
|
111
|
|
|
return (string) $description; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get the instance of the enumerative class |
|
116
|
|
|
* |
|
117
|
|
|
* @param int $value |
|
118
|
|
|
* @param string $description |
|
119
|
|
|
* @return EnumInterface |
|
120
|
|
|
*/ |
|
121
|
|
|
abstract protected static function getInstance(int $value, string $description = null): EnumInterface; |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Get the instance of the enumerative class by constant |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $constant |
|
127
|
|
|
* @throws \InvalidArgumentException |
|
128
|
|
|
* @return EnumInterface |
|
129
|
|
|
*/ |
|
130
|
|
|
protected static function getEnumByConstant(string $constant): EnumInterface |
|
131
|
|
|
{ |
|
132
|
|
|
static::assignConstants(); |
|
133
|
|
|
static::assignValues(); |
|
134
|
|
|
|
|
135
|
|
|
try { |
|
136
|
|
|
return static::getEnumByValue($constant, static::$constants); |
|
137
|
|
|
} catch(\InvalidArgumentException $exception) { |
|
138
|
|
|
throw $exception; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Get the instance of the enumerative class by description |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $constant |
|
146
|
|
|
* @throws \InvalidArgumentException |
|
147
|
|
|
* @return EnumInterface |
|
148
|
|
|
*/ |
|
149
|
|
|
protected static function getEnumByDescription(string $constant): EnumInterface |
|
150
|
|
|
{ |
|
151
|
|
|
static::assignValues(); |
|
152
|
|
|
|
|
153
|
|
|
try { |
|
154
|
|
|
return self::getEnumByValue($constant, static::$descriptions); |
|
155
|
|
|
} catch(\InvalidArgumentException $exception) { |
|
156
|
|
|
throw $exception; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Get the instance of the enumerative class by value, if it's in values. |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $value |
|
164
|
|
|
* @param array $values |
|
165
|
|
|
* @throws \InvalidArgumentException |
|
166
|
|
|
* @return EnumInterface |
|
167
|
|
|
*/ |
|
168
|
|
|
protected static function getEnumByValue(string $value, array $values): EnumInterface |
|
169
|
|
|
{ |
|
170
|
|
|
$index = array_search($value, $values); |
|
171
|
|
|
|
|
172
|
|
|
if (false !== $index) { |
|
173
|
|
|
$description = static::$descriptions[array_search(static::$values[$index], static::$values)] ?? ''; |
|
174
|
|
|
$enum = static::getInstance(static::$values[$index], $description); |
|
175
|
|
|
|
|
176
|
|
|
return $enum; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
throw new \InvalidArgumentException("Invalid Value! Only enumerative values of the type:". static::class); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Assigns the possible values according to the enumerative class. |
|
184
|
|
|
*/ |
|
185
|
|
|
protected static function assignValues() |
|
186
|
|
|
{ |
|
187
|
|
|
static::$values = array_values((new \ReflectionClass(static::class))->getConstants()); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Assigns the constants of the enumerative class. |
|
192
|
|
|
*/ |
|
193
|
|
|
protected static function assignConstants() |
|
194
|
|
|
{ |
|
195
|
|
|
static::$constants = array_keys((new \ReflectionClass(static::class))->getConstants()); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|