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