Skip to content

Commit

Permalink
bitmask instance should be treated as a whole
Browse files Browse the repository at this point in the history
  • Loading branch information
henzeb committed Jan 5, 2023
1 parent 5e8b151 commit b7575e0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@
"machine",
"transition",
"validation",
"rules"
"rules",
"bitmask",
"bitmasks",
"macros",
"macroable"
],
"homepage": "https://github.com/henzeb/enumhancer",
"license": "AGPL-3.0-only",
Expand Down
10 changes: 8 additions & 2 deletions src/Helpers/Bitmasks/Bitmask.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ public function any(self|UnitEnum|string|int ...$bits): bool
return true;
}

return ($this->value() & $mask) > 0;
foreach ($bits as $bit) {
if ($this->has($bit)) {
return true;
}
}

return false;
}

public function xor(self|UnitEnum|string|int ...$bits): bool
Expand Down Expand Up @@ -75,7 +81,7 @@ public function none(self|UnitEnum|string|int ...$bits): bool
return true;
}

return ($this->value() & $mask) === 0;
return !$this->any(...$bits);
}

public function value(): int
Expand Down
6 changes: 3 additions & 3 deletions src/Laravel/Mixins/RulesMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
use Henzeb\Enumhancer\Contracts\TransitionHook;
use Henzeb\Enumhancer\Laravel\Rules\EnumBitmask;
use Henzeb\Enumhancer\Laravel\Rules\EnumTransition;
use Henzeb\Enumhancer\Laravel\Rules\IsEnum as UnitEnumRule;
use Henzeb\Enumhancer\Laravel\Rules\IsEnum;
use UnitEnum;

class RulesMixin
{
public function isEnum(): Closure
{
return function (string $type, Mapper|string|array|null ...$mappers): UnitEnumRule {
return new UnitEnumRule($type, ...$mappers);
return function (string $type, Mapper|string|array|null ...$mappers): IsEnum {
return new IsEnum($type, ...$mappers);
};
}

Expand Down
13 changes: 10 additions & 3 deletions tests/Unit/Helpers/Bitmasks/BitmaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Henzeb\Enumhancer\Tests\Fixtures\BackedEnums\Bitmasks\BitmasksIncorrectIntEnum;
use Henzeb\Enumhancer\Tests\Fixtures\BackedEnums\Bitmasks\BitmasksIntEnum;
use Henzeb\Enumhancer\Tests\Fixtures\EnhancedUnitEnum;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

class BitmaskTest extends TestCase
Expand Down Expand Up @@ -100,8 +99,8 @@ public function testAny()
$this->assertTrue($bitmask->any(new Bitmask(BitmasksIntEnum::class, 0)));
$this->assertTrue($bitmask->any(new Bitmask(BitmasksIntEnum::class, 24)));
$this->assertFalse($bitmask->any(new Bitmask(BitmasksIntEnum::class, 32)));
$this->assertTrue($bitmask->any(new Bitmask(BitmasksIntEnum::class, 40)));
$this->assertTrue($bitmask->any(new Bitmask(BitmasksIntEnum::class, 56)));
$this->assertFalse($bitmask->any(new Bitmask(BitmasksIntEnum::class, 40)));
$this->assertFalse($bitmask->any(new Bitmask(BitmasksIntEnum::class, 56)));

$this->assertTrue($bitmask->any());
$this->assertTrue($bitmask->any(8, 16));
Expand Down Expand Up @@ -183,6 +182,14 @@ public function testNone()
$this->assertTrue($bitmask->none('Write'));
$this->assertTrue($bitmask->none('Read', 'Write'));
$this->assertFalse($bitmask->none('Execute', 'Read'));

$bitmask = new Bitmask(BitmasksIntEnum::class, 40);

$this->assertTrue(
$bitmask->none(
new Bitmask(BitmasksIntEnum::class, 24),
)
);
}

public function testCases()
Expand Down

0 comments on commit b7575e0

Please sign in to comment.