1
0
Fork 0
mirror of https://github.com/archtechx/enums.git synced 2025-12-12 15:34:04 +00:00

Use iterable type instead of unnecessary checks

This commit is contained in:
Mark 2024-12-29 07:07:26 +01:00
parent e6f788fd9f
commit 050057a708
No known key found for this signature in database
GPG key ID: 79CFF7869BD39873

View file

@ -20,21 +20,9 @@ trait Comparable
return ! $this->is($enum); return ! $this->is($enum);
} }
public function in(array|object $enums): bool public function in(iterable $enums): bool
{ {
$iterator = $enums; foreach ($enums as $item) {
if (! is_array($enums)) {
if ($enums instanceof Iterator) {
$iterator = $enums;
} elseif ($enums instanceof IteratorAggregate) {
$iterator = $enums->getIterator();
} else {
throw new Exception('in() expects an iterable value');
}
}
foreach ($iterator as $item) {
if ($item === $this) { if ($item === $this) {
return true; return true;
} }
@ -43,7 +31,7 @@ trait Comparable
return false; return false;
} }
public function notIn(array|object $enums): bool public function notIn(iterable $enums): bool
{ {
return ! $this->in($enums); return ! $this->in($enums);
} }