Skip to content

Commit 3090b04

Browse files
authored
Merge pull request #65 from marc-mabe/enumset2array
EnumSet: added getOrdinals(), getEnumerators(), getNames() and getValues()
2 parents 6af8018 + 094f999 commit 3090b04

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

src/EnumSet.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,70 @@ public function isSuperset(EnumSet $other)
240240
return ($thisBitset | $other->getBinaryBitsetLe()) === $thisBitset;
241241
}
242242

243+
/**
244+
* Get ordinal numbers of the defined enumerators as array
245+
* @return int[]
246+
*/
247+
public function getOrdinals()
248+
{
249+
$ordinals = array();
250+
$byteLen = strlen($this->bitset);
251+
252+
for ($bytePos = 0; $bytePos < $byteLen; ++$bytePos) {
253+
if ($this->bitset[$bytePos] === "\0") {
254+
continue; // fast skip null byte
255+
}
256+
257+
for ($bitPos = 0; $bitPos < 8; ++$bitPos) {
258+
if ((ord($this->bitset[$bytePos]) & (1 << $bitPos)) !== 0) {
259+
$ordinals[] = $bytePos * 8 + $bitPos;
260+
}
261+
}
262+
}
263+
264+
return $ordinals;
265+
}
266+
267+
/**
268+
* Get values of the defined enumerators as array
269+
* @return null[]|bool[]|int[]|float[]|string[]
270+
*/
271+
public function getValues()
272+
{
273+
$values = array();
274+
foreach ($this->getEnumerators() as $enumerator) {
275+
$values[] = $enumerator->getValue();
276+
}
277+
return $values;
278+
}
279+
280+
/**
281+
* Get names of the defined enumerators as array
282+
* @return string[]
283+
*/
284+
public function getNames()
285+
{
286+
$names = array();
287+
foreach ($this->getEnumerators() as $enumerator) {
288+
$names[] = $enumerator->getName();
289+
}
290+
return $names;
291+
}
292+
293+
/**
294+
* Get the defined enumerators as array
295+
* @return Enum[]
296+
*/
297+
public function getEnumerators()
298+
{
299+
$enumeration = $this->enumeration;
300+
$enumerators = array();
301+
foreach ($this->getOrdinals() as $ord) {
302+
$enumerators[] = $enumeration::getByOrdinal($ord);
303+
}
304+
return $enumerators;
305+
}
306+
243307
/**
244308
* Get binary bitset in little-endian order
245309
*

tests/MabeEnumTest/EnumSetTest.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,4 +566,96 @@ public function testIsSupersetWrongInstance()
566566
$this->assertFalse($set1->isSuperset($set2));
567567
}
568568
}
569+
570+
public function testGetOrdinals()
571+
{
572+
$set = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
573+
$this->assertSame(array(), $set->getOrdinals());
574+
575+
foreach (EnumBasic::getConstants() as $value) {
576+
$set->attach($value);
577+
}
578+
579+
$this->assertSame(range(0, count(EnumBasic::getConstants()) - 1), $set->getOrdinals());
580+
}
581+
582+
public function testGetOrdinalsDoesNotEffectIteratorPosition()
583+
{
584+
$set = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
585+
$set->attach(EnumBasic::ONE);
586+
$set->attach(EnumBasic::TWO);
587+
$set->next();
588+
589+
$set->getOrdinals();
590+
$this->assertSame(EnumBasic::TWO, $set->current()->getValue());
591+
}
592+
593+
public function testGetEnumerators()
594+
{
595+
$set = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
596+
$this->assertSame(array(), $set->getEnumerators());
597+
598+
foreach (EnumBasic::getConstants() as $value) {
599+
$set->attach($value);
600+
}
601+
602+
$this->assertSame(EnumBasic::getEnumerators(), $set->getEnumerators());
603+
}
604+
605+
public function testGetEnumeratorsDoesNotEffectIteratorPosition()
606+
{
607+
$set = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
608+
$set->attach(EnumBasic::ONE);
609+
$set->attach(EnumBasic::TWO);
610+
$set->next();
611+
612+
$set->getEnumerators();
613+
$this->assertSame(EnumBasic::TWO, $set->current()->getValue());
614+
}
615+
616+
public function testGetValues()
617+
{
618+
$set = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
619+
$this->assertSame(array(), $set->getValues());
620+
621+
foreach (EnumBasic::getConstants() as $value) {
622+
$set->attach($value);
623+
}
624+
625+
$this->assertSame(array_values(EnumBasic::getConstants()), $set->getValues());
626+
}
627+
628+
public function testGetValuesDoesNotEffectIteratorPosition()
629+
{
630+
$set = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
631+
$set->attach(EnumBasic::ONE);
632+
$set->attach(EnumBasic::TWO);
633+
$set->next();
634+
635+
$set->getValues();
636+
$this->assertSame(EnumBasic::TWO, $set->current()->getValue());
637+
}
638+
639+
public function testGetNames()
640+
{
641+
$set = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
642+
$this->assertSame(array(), $set->getNames());
643+
644+
foreach (EnumBasic::getConstants() as $value) {
645+
$set->attach($value);
646+
}
647+
648+
$this->assertSame(array_keys(EnumBasic::getConstants()), $set->getNames());
649+
}
650+
651+
public function testGetNamesDoesNotEffectIteratorPosition()
652+
{
653+
$set = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
654+
$set->attach(EnumBasic::ONE);
655+
$set->attach(EnumBasic::TWO);
656+
$set->next();
657+
658+
$set->getNames();
659+
$this->assertSame(EnumBasic::TWO, $set->current()->getValue());
660+
}
569661
}

0 commit comments

Comments
 (0)