-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
FlagBag.php
291 lines (245 loc) · 7.48 KB
/
FlagBag.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
declare(strict_types=1);
/*
* This file is part of the "elao/enum" package.
*
* Copyright (C) Elao
*
* @author Elao <[email protected]>
*/
namespace Elao\Enum;
use Elao\Enum\Exception\InvalidArgumentException;
use Elao\Enum\Exception\LogicException;
/**
* A bag of {@see \BackedEnum} allowing to perform bit operations.
*
* @final
*
* @template T of \BackedEnum
*/
class FlagBag
{
public const NONE = 0;
/** @var array<class-string<T>, int> */
private static array $masks = [];
/** @var int[] */
private array $bits;
/** @var class-string<T> */
private string $type;
/**
* @param class-string<T> $enumType
*/
public function __construct(string $enumType, int ...$bits)
{
self::checkIntBackedEnumType($enumType);
$this->type = $enumType;
$bits = self::encodeBits($bits);
if (!static::accepts($enumType, $bits)) {
throw new InvalidArgumentException(sprintf('%d is not a valid bits combination for "%s"', $bits, $this->type));
}
$this->bits = static::decodeBits($bits);
}
/**
* @param class-string $enumType
*
* @throws InvalidArgumentException If $enumType is not an int backed enum
*/
private static function checkIntBackedEnumType(string $enumType): void
{
if (!is_a($enumType, \BackedEnum::class, true)) {
throw new InvalidArgumentException(sprintf('"%s" is not a backed enum', $enumType));
}
if ('int' !== (string) (new \ReflectionEnum($enumType))->getBackingType()) {
throw new InvalidArgumentException(sprintf('"%s" is not an int backed enum', $enumType));
}
}
/**
* @param class-string<T> $enumType
*
* @return FlagBag<T>
*/
public static function fromAll(string $enumType): FlagBag
{
return new FlagBag($enumType, static::getBitmask($enumType));
}
/**
* @param class-string<T>|T $enumOrType
*/
public static function from(string|\BackedEnum $enumOrType, \BackedEnum ...$flags): static
{
if ($enumOrType instanceof \BackedEnum) {
if ('int' !== (string) (new \ReflectionEnum($enumOrType))->getBackingType()) {
throw new InvalidArgumentException(sprintf('"%s" is not an int backed enum', $enumOrType::class));
}
$type = $enumOrType::class;
$flags[] = $enumOrType;
} else {
if (!is_a($enumOrType, \BackedEnum::class, true)) {
throw new InvalidArgumentException(sprintf('"%s" is not a backed enum', $enumOrType));
}
$type = $enumOrType;
}
return new static($type, static::encodeBits(array_map(static fn (\BackedEnum $flag) => $flag->value, $flags)));
}
/**
* @param class-string<T> $enumType
*/
public static function accepts(string $enumType, int $value): bool
{
if ($value === self::NONE) {
return true;
}
return $value === ($value & self::getBitmask($enumType));
}
/**
* E.g: 43 => [1, 2, 8, 32]
*
* @return int[]
*/
private static function decodeBits(int $flags): array
{
/** @var string[] $bits */
$bits = array_reverse(str_split(decbin($flags)));
return array_values(
array_filter(
array_map(
static fn ($k, $v) => $v === '1' ? (1 << $k) : null,
array_keys($bits),
array_values($bits),
)
)
);
}
/**
* E.g: [1, 2, 8, 32] => 43
*
* @param int[] $bits
*/
private static function encodeBits(array $bits): int
{
return array_sum(array_unique($bits));
}
/**
* Gets an integer value of the possible flags for enumeration.
*
* @param class-string<\BackedEnum> $enumType
*
* @throws LogicException If the possibles values are not valid bit flags
* @throws InvalidArgumentException If $enumType is not an int backed enum
*/
public static function getBitmask(string $enumType): int
{
if (!isset(self::$masks[$enumType])) {
self::checkIntBackedEnumType($enumType);
/** @var \BackedEnum[] $cases */
$cases = $enumType::cases();
$mask = 0;
foreach ($cases as $case) {
$value = $case->value;
if ($value < 1 || ($value > 1 && ($value % 2) !== 0)) {
throw new LogicException(sprintf(
'Possible value %s of the enumeration "%s" is not a bit flag.',
$value,
$enumType
));
}
$mask |= $value;
}
self::$masks[$enumType] = $mask;
}
return self::$masks[$enumType];
}
/**
* @return int[]
*/
public function getBits(): array
{
return $this->bits;
}
/**
* The encoded bits value.
*/
public function getValue(): int
{
return self::encodeBits($this->bits);
}
/**
* @return array<T>
*/
public function getFlags(): array
{
return array_map(fn (int $bit) => $this->type::from($bit), $this->bits);
}
/**
* True if the bit flag(s) are also present in the current bag.
*/
public function hasBits(int $bits): bool
{
if ($bits >= 1) {
return $bits === ($bits & self::encodeBits($this->bits));
}
return false;
}
/**
* True if the all the flags are also present in the current bag.
*
* @param T ...$flags
*/
public function hasFlags(\BackedEnum ...$flags): bool
{
$bits = static::encodeBits(array_map(static fn (\BackedEnum $flag) => $flag->value, $flags));
if ($bits >= 1) {
return $bits === ($bits & self::encodeBits($this->bits));
}
return false;
}
/**
* Computes a new value with given flags, and returns the corresponding instance.
*/
public function withBits(int ...$bits): static
{
$mask = self::encodeBits($bits);
if (!static::accepts($this->type, $mask)) {
throw new InvalidArgumentException(sprintf('"%d" is not a valid flags combination for "%s"', $mask, $this->type));
}
return new static($this->type, self::encodeBits($this->bits) | $mask);
}
/**
* @param T ...$flags
*/
public function withFlags(\BackedEnum ...$flags): static
{
return $this->withBits(...$this->flagsToBits(...$flags));
}
/**
* @param T ...$flags
*/
public function withoutFlags(\BackedEnum ...$flags): static
{
return $this->withoutBits(...$this->flagsToBits(...$flags));
}
public function withoutBits(int ...$bits): static
{
$mask = self::encodeBits($bits);
if (!static::accepts($this->type, $mask)) {
throw new InvalidArgumentException(sprintf('"%d" is not a valid flags combination for "%s"', $mask, $this->type));
}
return new static($this->type, self::encodeBits($this->bits) & ~$mask);
}
/**
* @return class-string<T>
*/
public function getType(): string
{
return $this->type;
}
/**
* @param T ...$flags
*
* @return int[]
*/
private function flagsToBits(\BackedEnum ...$flags): array
{
return array_map(static fn (\BackedEnum $flag) => $flag->value, $flags);
}
}