Skip to content

Commit 4e0c281

Browse files
committed
Added EnumMap::isEmpty() as associate of EnumSet::isEmpty()
1 parent 933106d commit 4e0c281

File tree

5 files changed

+67
-37
lines changed

5 files changed

+67
-37
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ $enumSet->has(UserStatus::INACTIVE); // bool
262262
$enumSet->count();
263263
count($enumSet);
264264

265-
// Tests if it has enumerators
265+
// test for elements
266266
$enumSet->isEmpty();
267267

268268
// convert to array
@@ -345,6 +345,8 @@ isset($enumMap[UserStatus::DELETED()]); // false
345345
$enumMap->count();
346346
count($enumMap);
347347

348+
// test for elements
349+
$enumSet->isEmpty();
348350

349351
// support for null aware exists check
350352
$enumMap[UserStatus::NULL] = null;

src/EnumMap.php

+10
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,14 @@ public function count(): int
349349
{
350350
return \count($this->map);
351351
}
352+
353+
/**
354+
* Tests if the map is empty
355+
*
356+
* @return bool
357+
*/
358+
public function isEmpty(): bool
359+
{
360+
return empty($this->map);
361+
}
352362
}

src/EnumSet.php

+14-14
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class EnumSet implements IteratorAggregate, Countable
4848
* Defines private method names to be called depended of how the bitset type was set too.
4949
* ... Integer or binary bitset.
5050
* ... *Int or *Bin method
51-
*
51+
*
5252
* @var string
5353
*/
5454
private $fnDoGetIterator = 'doGetIteratorInt';
@@ -901,6 +901,16 @@ public function isSuperset(EnumSet $other): bool
901901
&& ($this->bitset | $other->bitset) === $this->bitset;
902902
}
903903

904+
/**
905+
* Tests if the set is empty
906+
*
907+
* @return bool
908+
*/
909+
public function isEmpty(): bool
910+
{
911+
return $this->bitset === $this->emptyBitset;
912+
}
913+
904914
/**
905915
* Get ordinal numbers of the defined enumerators as array
906916
* @return int[]
@@ -1008,7 +1018,7 @@ public function getEnumerators(): array
10081018

10091019
/**
10101020
* Get binary bitset in little-endian order
1011-
*
1021+
*
10121022
* @return string
10131023
* @uses doGetBinaryBitsetLeBin()
10141024
* @uses doGetBinaryBitsetLeInt()
@@ -1049,7 +1059,7 @@ private function doGetBinaryBitsetLeInt()
10491059

10501060
/**
10511061
* Get binary bitset in big-endian order
1052-
*
1062+
*
10531063
* @return string
10541064
*/
10551065
public function getBinaryBitsetBe(): string
@@ -1094,7 +1104,7 @@ private function doGetBitBin($ordinal)
10941104
* Get a bit at the given ordinal number.
10951105
*
10961106
* This is the integer bitset implementation.
1097-
*
1107+
*
10981108
* @param int $ordinal Ordinal number of bit to get
10991109
* @return bool
11001110
* @see getBit()
@@ -1104,14 +1114,4 @@ private function doGetBitInt($ordinal)
11041114
{
11051115
return (bool)($this->bitset & (1 << $ordinal));
11061116
}
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-
}
11171117
}

tests/MabeEnumTest/EnumMapTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use InvalidArgumentException;
66
use MabeEnum\EnumMap;
7+
use MabeEnumTest\TestAsset\Enum32;
78
use MabeEnumTest\TestAsset\EnumBasic;
89
use MabeEnumTest\TestAsset\EnumInheritance;
910
use PHPUnit\Framework\TestCase;
@@ -472,6 +473,21 @@ public function testSerializable()
472473
$this->assertFalse($enumMapCopy->offsetExists(EnumBasic::TWO));
473474
}
474475

476+
public function testIsEmpty()
477+
{
478+
$map1 = new EnumMap(Enum32::class, []);
479+
$map2 = new EnumMap(Enum32::class, array_combine(Enum32::getValues(), Enum32::getNames()));
480+
481+
$this->assertTrue($map1->isEmpty());
482+
$this->assertFalse($map2->isEmpty());
483+
484+
$map1->add(Enum32::ONE(), 'first');
485+
$map2->removeIterable(Enum32::getEnumerators());
486+
487+
$this->assertFalse($map1->isEmpty());
488+
$this->assertTrue($map2->isEmpty());
489+
}
490+
475491
/* deprecated */
476492

477493
public function testContains()

tests/MabeEnumTest/EnumSetTest.php

+24-22
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ public function testSetBitOutOfRange()
347347
public function testGetBinaryBitsetLe()
348348
{
349349
$set = new EnumSet(Enum65::class);
350-
350+
351351
$enum1 = Enum65::ONE;
352352
$enum2 = Enum65::TWO;
353353
$enum3 = Enum65::SIXTYFIVE;
@@ -368,20 +368,20 @@ public function testGetBinaryBitsetLe()
368368
$set = $set->with($enum4);
369369
$this->assertSame("\x03\x00\x00\x00\x00\x00\x00\x80\x01", $set->getBinaryBitsetLe());
370370
$this->assertTrue($set->has($enum4));
371-
371+
372372
$this->assertSame(4, $set->count());
373373

374374
$set = $set->without($enum2);
375375
$this->assertSame("\x01\x00\x00\x00\x00\x00\x00\x80\x01", $set->getBinaryBitsetLe());
376376
$this->assertFalse($set->has($enum2));
377-
377+
378378
$this->assertSame(3, $set->count());
379379
}
380380

381381
public function testGetBinaryBitsetBe()
382382
{
383383
$set = new EnumSet(Enum65::class);
384-
384+
385385
$enum1 = Enum65::ONE;
386386
$enum2 = Enum65::TWO;
387387
$enum3 = Enum65::SIXTYFIVE;
@@ -402,13 +402,13 @@ public function testGetBinaryBitsetBe()
402402
$set = $set->with($enum4);
403403
$this->assertSame("\x01\x80\x00\x00\x00\x00\x00\x00\x03", $set->getBinaryBitsetBe());
404404
$this->assertTrue($set->has($enum4));
405-
405+
406406
$this->assertSame(4, $set->count());
407407

408408
$set = $set->without($enum2);
409409
$this->assertSame("\x01\x80\x00\x00\x00\x00\x00\x00\x01", $set->getBinaryBitsetBe());
410410
$this->assertFalse($set->has($enum2));
411-
411+
412412
$this->assertSame(3, $set->count());
413413
}
414414

@@ -720,6 +720,24 @@ public function testIsSupersetWrongInstance()
720720
}
721721
}
722722

723+
/**
724+
* @dataProvider getIntegerEnumerations
725+
*/
726+
public function testIsEmpty($enum)
727+
{
728+
$set1 = new EnumSet($enum, []);
729+
$set2 = new EnumSet($enum, $enum::getValues());
730+
731+
$this->assertTrue($set1->isEmpty());
732+
$this->assertFalse($set2->isEmpty());
733+
734+
$set1->addIterable($enum::getValues());
735+
$set2->removeIterable($enum::getValues());
736+
737+
$this->assertFalse($set1->isEmpty());
738+
$this->assertTrue($set2->isEmpty());
739+
}
740+
723741
public function testGetOrdinalsInt()
724742
{
725743
$set = new EnumSet(EnumBasic::class);
@@ -904,22 +922,6 @@ public function testSetSymDiffThrowsInvalidArgumentException()
904922
$set1->setSymDiff($set2);
905923
}
906924

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-
923925
/* deprecated */
924926

925927
/** @deprecated */

0 commit comments

Comments
 (0)