Skip to content

Commit 094f999

Browse files
committed
EnumSet: get as array methods
- added getOrdinals() - optimized getEnumerators(), getNames() and getValues() - added tests for all get*() methods
1 parent 50b3b65 commit 094f999

File tree

2 files changed

+121
-4
lines changed

2 files changed

+121
-4
lines changed

src/EnumSet.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,38 @@ 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+
243267
/**
244268
* Get values of the defined enumerators as array
245269
* @return null[]|bool[]|int[]|float[]|string[]
246270
*/
247271
public function getValues()
248272
{
249273
$values = array();
250-
foreach ($this as $enumerator) {
274+
foreach ($this->getEnumerators() as $enumerator) {
251275
$values[] = $enumerator->getValue();
252276
}
253277
return $values;
@@ -260,7 +284,7 @@ public function getValues()
260284
public function getNames()
261285
{
262286
$names = array();
263-
foreach ($this as $enumerator) {
287+
foreach ($this->getEnumerators() as $enumerator) {
264288
$names[] = $enumerator->getName();
265289
}
266290
return $names;
@@ -272,9 +296,10 @@ public function getNames()
272296
*/
273297
public function getEnumerators()
274298
{
299+
$enumeration = $this->enumeration;
275300
$enumerators = array();
276-
foreach ($this as $enumerator) {
277-
$enumerators[] = $enumerator->getName();
301+
foreach ($this->getOrdinals() as $ord) {
302+
$enumerators[] = $enumeration::getByOrdinal($ord);
278303
}
279304
return $enumerators;
280305
}

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)