@@ -30,7 +30,7 @@ private static function countSetBits(int $bit): int
30
30
31
31
public static function isBit (mixed $ bit ): bool
32
32
{
33
- return self ::isInt ($ bit ) && self ::countSetBits ($ bit ) === 1 ;
33
+ return self ::isInt ($ bit ) && ( self ::countSetBits ($ bit ) === 1 || $ bit === 0 ) ;
34
34
}
35
35
36
36
public static function validateBitmaskCases (string $ enum ): void
@@ -67,18 +67,42 @@ public static function ignoreIntValues(string $enum): bool
67
67
return true ;
68
68
}
69
69
70
+ public static function isModifier (BackedEnum |string $ enum ): bool
71
+ {
72
+ /**
73
+ * @var UnitEnum $enum
74
+ */
75
+
76
+ EnumCheck::check ($ enum );
77
+
78
+ if (self ::ignoreIntValues ($ enum )) {
79
+ return false ;
80
+ }
81
+
82
+ foreach ((new ReflectionClass ($ enum ))->getConstants () as $ constant => $ value ) {
83
+ if (strtolower ($ constant ) === 'bit_modifier ' and is_bool ($ value )) {
84
+ return $ value ;
85
+ }
86
+ }
87
+ return false ;
88
+ }
89
+
70
90
private static function validateBitCases (BackedEnum |string $ enum ): void
71
91
{
92
+ if (self ::isModifier ($ enum )) {
93
+ return ;
94
+ }
95
+
72
96
foreach ($ enum ::cases () as $ case ) {
73
- self ::validateBitCase ($ case );
97
+ if (self ::validateBitCase ($ case )) {
98
+ self ::triggerInvalidBitCase ($ case ::class, $ case );
99
+ }
74
100
}
75
101
}
76
102
77
- private static function validateBitCase (BackedEnum $ case ): void
103
+ private static function validateBitCase (BackedEnum $ case ): bool
78
104
{
79
- if (self ::isInt ($ case ->value ) && !self ::isBit ($ case ->value )) {
80
- self ::triggerInvalidBitCase ($ case ::class, $ case );
81
- }
105
+ return self ::isInt ($ case ->value ) && !self ::isBit ($ case ->value );
82
106
}
83
107
84
108
public static function getBit (UnitEnum $ enum ): int
@@ -88,15 +112,15 @@ public static function getBit(UnitEnum $enum): int
88
112
$ value = EnumValue::value ($ enum );
89
113
90
114
if (self ::ignoreIntValues ($ enum ::class)
91
- || !filter_var ($ value, FILTER_VALIDATE_INT )
115
+ || !is_int ($ value )
92
116
) {
93
117
return pow (
94
118
2 ,
95
119
(int )array_search ($ enum , $ enum ::cases ())
96
120
);
97
121
}
98
122
99
- return ( int ) $ value ;
123
+ return $ value ;
100
124
}
101
125
102
126
public static function getMask (string $ class , UnitEnum |string |int ...$ enums ): Bitmask
@@ -242,8 +266,9 @@ public static function throwMismatch(string $expected, string $given): never
242
266
243
267
protected static function triggerInvalidBitCase (UnitEnum |string $ enum , UnitEnum $ case ): never
244
268
{
269
+ $ enum = is_string ($ enum ) ? $ enum : $ enum ::class;
245
270
trigger_error (
246
- sprintf ('%s::%s is not a valid bit value ' , $ enum::class ?? $ enum , $ case ->name ),
271
+ sprintf ('%s::%s is not a valid bit value ' , $ enum , $ case ->name ),
247
272
E_USER_ERROR
248
273
);
249
274
}
0 commit comments