forked from BenSampo/laravel-enum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlaggedEnum.php
250 lines (217 loc) · 5.27 KB
/
FlaggedEnum.php
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
namespace BenSampo\Enum;
use BenSampo\Enum\Exceptions\InvalidEnumMemberException;
abstract class FlaggedEnum extends Enum
{
const None = 0;
/**
* Construct a FlaggedEnum instance.
*
* @param int[]|\BenSampo\Enum\Enum[] $flags
* @return void
*/
public function __construct($flags = [])
{
$this->key = null;
$this->description = null;
if (is_array($flags)) {
$this->setFlags($flags);
} else {
try {
parent::__construct($flags);
} catch (InvalidEnumMemberException $exception) {
$this->value = $flags;
}
}
}
/**
* Attempt to instantiate a new Enum using the given key or value.
*
* @param mixed $enumKeyOrValue
* @return static|null
*/
public static function coerce($enumKeyOrValue): ?Enum
{
if (is_integer($enumKeyOrValue)) {
return static::fromValue($enumKeyOrValue);
}
return parent::coerce($enumKeyOrValue);
}
/**
* Return a FlaggedEnum instance with defined flags.
*
* @param int[]|\BenSampo\Enum\Enum[] $flags
* @return void
*/
public static function flags($flags): self
{
return static::fromValue($flags);
}
/**
* Set the flags for the enum to the given array of flags.
*
* @param int[]|\BenSampo\Enum\Enum[] $flags
* @return self
*/
public function setFlags(array $flags): self
{
$this->value = array_reduce($flags, function ($carry, $flag) {
return $carry | static::fromValue($flag)->value;
}, 0);
return $this;
}
/**
* Add the given flag to the enum.
*
* @param int|\BenSampo\Enum\Enum $flag
* @return self
*/
public function addFlag($flag): self
{
$this->value |= static::fromValue($flag)->value;
return $this;
}
/**
* Add the given flags to the enum.
*
* @param int[]|\BenSampo\Enum\Enum[] $flags
* @return self
*/
public function addFlags(array $flags): self
{
array_map(function ($flag) {
$this->addFlag($flag);
}, $flags);
return $this;
}
/**
* Add all flags to the enum.
*
* @return self
*/
public function addAllFlags(): self
{
return (new static)->addFlags(self::getValues());
}
/**
* Remove the given flag from the enum.
*
* @param int|\BenSampo\Enum\Enum $flag
* @return self
*/
public function removeFlag($flag): self
{
$this->value &= ~static::fromValue($flag)->value;
return $this;
}
/**
* Remove the given flags from the enum.
*
* @param int[]|\BenSampo\Enum\Enum[] $flags
* @return self
*/
public function removeFlags(array $flags): self
{
array_map(function ($flag) {
$this->removeFlag($flag);
}, $flags);
return $this;
}
/**
* Remove all flags from the enum.
*
* @return self
*/
public function removeAllFlags(): self
{
return static::None();
}
/**
* Check if the enum has the specified flag.
*
* @param int|\BenSampo\Enum\Enum $flag
* @return bool
*/
public function hasFlag($flag): bool
{
$flagValue = static::fromValue($flag)->value;
if ($flagValue === 0) {
return false;
}
return ($flagValue & $this->value) === $flagValue;
}
/**
* Check if the enum has all of the specified flags.
*
* @param int[]|\BenSampo\Enum\Enum[] $flags
* @return bool
*/
public function hasFlags(array $flags): bool
{
foreach ($flags as $flag) {
if (! static::hasFlag($flag)) {
return false;
}
}
return true;
}
/**
* Check if the enum does not have the specified flag.
*
* @param int|\BenSampo\Enum\Enum $flag
* @return bool
*/
public function notHasFlag($flag): bool
{
return ! $this->hasFlag($flag);
}
/**
* Check if the enum doesn't have any of the specified flags.
*
* @param int[]|\BenSampo\Enum\Enum[] $flags
* @return bool
*/
public function notHasFlags(array $flags): bool
{
foreach ($flags as $flag) {
if (! static::notHasFlag($flag)) {
return false;
}
}
return true;
}
/**
* Return the flags as an array of instances.
*
* @return \BenSampo\Enum\Enum[]
*/
public function getFlags(): array
{
$members = static::getInstances();
$flags = [];
foreach ($members as $member) {
if ($this->hasFlag($member)) {
$flags[] = $member;
}
}
return $flags;
}
/**
* Check if there are multiple flags set on the enum.
*
* @return bool
*/
public function hasMultipleFlags(): bool
{
return ($this->value & ($this->value - 1)) !== 0;
}
/**
* Get the bitmask for the enum.
*
* @return int
*/
public function getBitmask(): int
{
return (int) decbin($this->value);
}
}