Skip to content

Commit

Permalink
Merge pull request #10 from webfox/feature/support-direct-value-compa…
Browse files Browse the repository at this point in the history
…rison

Add support for direct value comparisons
  • Loading branch information
Jim-Webfox authored Feb 27, 2023
2 parents a4c5c09 + 5ed0255 commit 15321a2
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/IsBackedEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

/**
* @implements \Webfox\LaravelBackedEnums\BackedEnum<string,string>
* @mixin \BackedEnum<string,string>
*/
trait IsBackedEnum
{


protected static function ensureImplementsInterface(): void
{
throw_unless(class_implements(static::class, BackedEnum::class), new \Exception(sprintf('Enum %s must implement BackedEnum', static::class)));
Expand Down Expand Up @@ -100,7 +100,7 @@ public function toJson($options = 0): array
public function isA($value): bool
{
static::ensureImplementsInterface();
return $this == $value;
return $this->isAny([$value]);
}

public function isAn(string $value): bool
Expand All @@ -118,6 +118,12 @@ public function isNot(string $value): bool
public function isAny(array $values): bool
{
static::ensureImplementsInterface();

if (empty($values)) {
return false;
}

$values = array_map(fn($value) => $value instanceof static ? $value : static::from($value), $values);
return in_array($this, $values);
}

Expand Down

0 comments on commit 15321a2

Please sign in to comment.