1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DavidIanBonner\Enumerated; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Support\Facades\Lang; |
8
|
|
|
use ReflectionClass; |
9
|
|
|
|
10
|
|
|
abstract class Enum |
11
|
|
|
{ |
12
|
|
|
/** @var string */ |
13
|
|
|
private $value; |
14
|
|
|
|
15
|
|
|
/** @var array */ |
16
|
|
|
private static $loaded = []; |
17
|
|
|
|
18
|
|
|
/** @var array */ |
19
|
|
|
protected static $values = []; |
20
|
|
|
|
21
|
|
|
abstract public function langKey(): string; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $value |
25
|
|
|
*/ |
26
|
12 |
|
public function __construct($value) |
27
|
|
|
{ |
28
|
12 |
|
$this->validateValue($value); |
29
|
9 |
|
$this->value = $value; |
30
|
9 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Return the enumerators values. |
34
|
|
|
* |
35
|
|
|
* @param bool $keys |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
30 |
|
public static function allValues($keys = false): array |
39
|
|
|
{ |
40
|
30 |
|
$caller = get_called_class(); |
41
|
|
|
|
42
|
30 |
|
if (!isset(static::$values[$caller])) { |
43
|
12 |
|
static::$values[$caller] = static::getDeclaredConstants(); |
44
|
|
|
} |
45
|
|
|
|
46
|
30 |
|
return $keys ? static::$values[$caller] : array_values(static::$values[$caller]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return a collection of the declared values. |
51
|
|
|
* |
52
|
|
|
* @param bool $keys |
53
|
|
|
* @return Illuminate\Support\Collection |
54
|
|
|
*/ |
55
|
9 |
|
public static function collect($keys = false): Collection |
56
|
|
|
{ |
57
|
9 |
|
return Collection::make(static::allValues($keys)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Return an instance of a desired value. |
62
|
|
|
* |
63
|
|
|
* @param string $value |
64
|
|
|
* @return DavidIanBonner\Enumerated\Enum |
65
|
|
|
*/ |
66
|
15 |
|
public static function ofType($value): self |
67
|
|
|
{ |
68
|
15 |
|
$key = get_called_class() . ':' . $value; |
69
|
|
|
|
70
|
15 |
|
if (!isset(self::$loaded[$key])) { |
71
|
12 |
|
self::$loaded[$key] = new static($value); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// We can safely return the instance from the loaded array as |
75
|
|
|
// validation is carried out in the constructor and the |
76
|
|
|
// $loaded property is private. |
77
|
|
|
|
78
|
12 |
|
return self::$loaded[$key]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return the value. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
12 |
|
public function value(): string |
87
|
|
|
{ |
88
|
12 |
|
return $this->value; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Validate the value. |
93
|
|
|
* |
94
|
|
|
* @param mixed $value |
95
|
|
|
* @throws DavidIanBonner\Enumerated\EnumNotValidException |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
18 |
|
public static function validateValue($value) |
99
|
|
|
{ |
100
|
18 |
|
if (!in_array($value, static::allValues())) { |
101
|
6 |
|
throw new EnumNotValidException("The value [{$value}] is not a valid type."); |
102
|
|
|
} |
103
|
15 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Check the value is valid and return a bool. |
107
|
|
|
* |
108
|
|
|
* @param mixed $value |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
3 |
|
public static function isValid($value): bool |
112
|
|
|
{ |
113
|
|
|
try { |
114
|
3 |
|
static::validateValue($value); |
115
|
3 |
|
} catch (Exception $e) { |
116
|
3 |
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
3 |
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get the declared constants. |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
9 |
|
protected static function getDeclaredConstants(): array |
128
|
|
|
{ |
129
|
9 |
|
$reflection = new ReflectionClass(get_called_class()); |
130
|
|
|
|
131
|
9 |
|
return (array) $reflection->getConstants(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
6 |
|
public static function toSelect(): array |
138
|
|
|
{ |
139
|
6 |
|
return static::collect() |
140
|
6 |
|
->mapWithKeys(function ($key) { |
141
|
6 |
|
$outcome = self::ofType($key); |
142
|
|
|
|
143
|
6 |
|
return [$outcome->value() => $outcome->line()]; |
144
|
6 |
|
}) |
145
|
6 |
|
->toArray(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
9 |
|
public function line(): string |
152
|
|
|
{ |
153
|
9 |
|
return Lang::get( |
154
|
9 |
|
implode('.', [ |
155
|
9 |
|
$this->langKeyPrefix() . 'enum', |
156
|
9 |
|
$this->langKey(), |
157
|
9 |
|
str_replace('_', '-', $this->value()), |
158
|
|
|
]) |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
6 |
|
public function langKeyPrefix(): string |
163
|
|
|
{ |
164
|
6 |
|
return ''; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|