Skip to content

Commit 1f005ff

Browse files
authored
Merge pull request #126 from Lctrs/enumset-isempty
Add EnumSet::isEmpty() to know if sets has enumerators
2 parents fa98114 + e5088ff commit 1f005ff

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ $enumSet->has(UserStatus::INACTIVE); // bool
262262
$enumSet->count();
263263
count($enumSet);
264264

265+
// Tests if it has enumerators
266+
$enumSet->isEmpty();
265267

266268
// convert to array
267269
$enumSet->getValues(); // List of enumerator values

src/EnumSet.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ class EnumSet implements IteratorAggregate, Countable
3737
*/
3838
private $bitset = 0;
3939

40+
/**
41+
* Integer or binary (little endian) empty bitset
42+
*
43+
* @var int|string
44+
*/
45+
private $emptyBitset = 0;
46+
4047
/**#@+
4148
* Defines private method names to be called depended of how the bitset type was set too.
4249
* ... Integer or binary bitset.
@@ -79,7 +86,7 @@ public function __construct(string $enumeration, iterable $enumerators = null)
7986
// we will switch this into a binary bitset
8087
if ($this->enumerationCount > \PHP_INT_SIZE * 8) {
8188
// init binary bitset with zeros
82-
$this->bitset = \str_repeat("\0", (int)\ceil($this->enumerationCount / 8));
89+
$this->bitset = $this->emptyBitset = \str_repeat("\0", (int)\ceil($this->enumerationCount / 8));
8390

8491
// switch internal binary bitset functions
8592
$this->fnDoGetIterator = 'doGetIteratorBin';
@@ -1097,4 +1104,14 @@ private function doGetBitInt($ordinal)
10971104
{
10981105
return (bool)($this->bitset & (1 << $ordinal));
10991106
}
1107+
1108+
/**
1109+
* Return true if the set is empty, false otherwise.
1110+
*
1111+
* @return bool
1112+
*/
1113+
public function isEmpty(): bool
1114+
{
1115+
return $this->bitset === $this->emptyBitset;
1116+
}
11001117
}

tests/MabeEnumTest/EnumSetTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,22 @@ public function testSetSymDiffThrowsInvalidArgumentException()
904904
$set1->setSymDiff($set2);
905905
}
906906

907+
/**
908+
* @dataProvider getIntegerEnumerations
909+
*/
910+
public function testIsEmpty($enum)
911+
{
912+
$set1 = new EnumSet($enum, []);
913+
$set2 = new EnumSet($enum, $enum::getValues());
914+
915+
$this->assertTrue($set1->isEmpty());
916+
$this->assertFalse($set2->isEmpty());
917+
918+
$set2->removeIterable($enum::getValues());
919+
920+
$this->assertTrue($set2->isEmpty());
921+
}
922+
907923
/* deprecated */
908924

909925
/** @deprecated */

0 commit comments

Comments
 (0)