diff --git a/src/Util/Bitmap.php b/src/Util/Bitmap.php index 286e9b6a30..1d5956dde2 100644 --- a/src/Util/Bitmap.php +++ b/src/Util/Bitmap.php @@ -24,11 +24,11 @@ use App\Util\Exception\ServerException; abstract class Bitmap { /** - * Convert an to or from an integer and an array of constants for - * each bit. If $instance, return an object with the corresponding + * Convert from an integer to an onject or an array of constants for + * set each bit. If $instance, return an object with the corresponding * properties set */ - private static function _do(int $r, bool $instance) + private static function _to(int $r, bool $instance) { $init = $r; $class = get_called_class(); @@ -38,8 +38,12 @@ abstract class Bitmap $vals = []; } - $consts = (new \ReflectionClass($class))->getConstants(); - unset($consts['PREFIX']); + $consts = (new \ReflectionClass($class))->getConstants(); + $have_prefix = false; + if (isset($consts['PREFIX'])) { + $have_prefix = true; + unset($consts['PREFIX']); + } foreach ($consts as $c => $v) { $b = ($r & $v) !== 0; @@ -50,7 +54,7 @@ abstract class Bitmap if ($b) { $r -= $v; if (!$instance) { - $vals[] = $class::PREFIX . $c; + $vals[] = ($have_prefix ? $class::PREFIX : '') . $c; } } } @@ -69,11 +73,11 @@ abstract class Bitmap public static function create(int $r): self { - return self::_do($r, true); + return self::_to($r, true); } public static function toArray(int $r): array { - return self::_do($r, false); + return self::_to($r, false); } } diff --git a/tests/Util/BitmapTest.php b/tests/Util/BitmapTest.php new file mode 100644 index 0000000000..2178df2b5d --- /dev/null +++ b/tests/Util/BitmapTest.php @@ -0,0 +1,59 @@ +. +// }}} + +namespace App\Tests\Util; + +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; + +class FooBitmap extends \App\Util\Bitmap +{ + public const FOO = 1; + public const BAR = 2; + public const QUUX = 4; +} + +class BarBitmap extends \App\Util\Bitmap +{ + public const HYDROGEN = 1; + public const HELIUM = 2; + public const PREFIX = 'BAR_'; +} + +class BitmapTest extends KernelTestCase +{ + public function testObj() + { + $a = FooBitmap::create(FooBitmap::FOO | FooBitmap::BAR); + static::assertTrue($a->foo); + static::assertTrue($a->bar); + static::assertFalse($a->quux); + } + + public function testArray() + { + $b = FooBitmap::toArray(FooBitmap::FOO | FooBitmap::QUUX); + static::assertSame(['FOO', 'QUUX'], $b); + } + + public function testPrefix() + { + $b = BarBitmap::toArray(BarBitmap::HYDROGEN | BarBitmap::HELIUM); + static::assertSame(['BAR_HYDROGEN', 'BAR_HELIUM'], $b); + } +}