1 | <?php |
||
16 | abstract class FlaggedEnum extends ReadableEnum |
||
17 | { |
||
18 | const NONE = 0; |
||
19 | |||
20 | /** @var array */ |
||
21 | private static $masks = []; |
||
22 | |||
23 | /** @var int[] */ |
||
24 | protected $flags; |
||
25 | |||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | 30 | public static function accepts($value): bool |
|
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | * |
||
45 | * @param string $separator A delimiter used between each bit flag's readable string |
||
46 | */ |
||
47 | 8 | public static function readableFor($value, string $separator = '; '): string |
|
72 | |||
73 | /** |
||
74 | * Gets the human representation for the none value. |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | 1 | protected static function readableForNone(): string |
|
82 | |||
83 | /** |
||
84 | * Gets an integer value of the possible flags for enumeration. |
||
85 | * |
||
86 | * @throws LogicException If the possibles values are not valid bit flags |
||
87 | * |
||
88 | * @return int |
||
89 | */ |
||
90 | 26 | private static function getBitmask(): int |
|
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | * |
||
115 | * @param string $separator A delimiter used between each bit flag's readable string |
||
116 | */ |
||
117 | 7 | public function getReadable(string $separator = '; '): string |
|
121 | |||
122 | /** |
||
123 | * Gets an array of bit flags of the value. |
||
124 | * |
||
125 | * @return array |
||
126 | */ |
||
127 | 13 | public function getFlags(): array |
|
140 | |||
141 | /** |
||
142 | * Determines whether the specified flag is set in a numeric value. |
||
143 | * |
||
144 | * @param int $bitFlag The bit flag or bit flags |
||
145 | * |
||
146 | * @return bool True if the bit flag or bit flags are also set in the current instance; otherwise, false |
||
147 | */ |
||
148 | 9 | public function hasFlag(int $bitFlag): bool |
|
156 | |||
157 | /** |
||
158 | * Computes a new value with given flags, and returns the corresponding instance. |
||
159 | * |
||
160 | * @param int $flags The bit flag or bit flags |
||
161 | * |
||
162 | * @throws InvalidValueException When $flags is not acceptable for this enumeration type |
||
163 | * |
||
164 | * @return static The enum instance for computed value |
||
165 | */ |
||
166 | 2 | public function withFlags(int $flags): self |
|
167 | { |
||
168 | 2 | if (!static::accepts($flags)) { |
|
169 | 1 | throw new InvalidValueException($flags, static::class); |
|
170 | } |
||
171 | |||
172 | 1 | return static::get($this->value | $flags); |
|
173 | } |
||
174 | |||
175 | /** |
||
176 | * Computes a new value without given flags, and returns the corresponding instance. |
||
177 | * |
||
178 | * @param int $flags The bit flag or bit flags |
||
179 | * |
||
180 | * @throws InvalidValueException When $flags is not acceptable for this enumeration type |
||
181 | * |
||
182 | * @return static The enum instance for computed value |
||
183 | */ |
||
184 | 4 | public function removeFlags(int $flags): self |
|
192 | } |
||
193 |