Skip to content

Commit

Permalink
Add has() method
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbero90 committed Oct 28, 2024
1 parent 4e357c2 commit ce404f8
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/CasesCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CasesCollection implements Countable, IteratorAggregate, JsonSerializable,
*
* @param array<array-key, TValue> $cases
*/
final public function __construct(protected array $cases)
final public function __construct(protected readonly array $cases)
{
$this->enumIsBacked = reset($cases) instanceof BackedEnum;
}
Expand Down Expand Up @@ -79,6 +79,20 @@ public function all(): array
return $this->cases;
}

/**
* Determine whether the collection contains the given case.
*/
public function has(mixed $case): bool
{
foreach ($this->cases as $instance) {
if ($instance->is($case)) {
return true;
}
}

return false;
}

/**
* Retrieve all the cases as a plain array recursively.
*
Expand All @@ -89,7 +103,7 @@ public function toArray(): array
$array = [];

foreach ($this->cases as $key => $value) {
$array[$key] = $value instanceof self ? $value->toArray() : $value;
$array[$key] = $value instanceof static ? $value->toArray() : $value;
}

return $array;
Expand Down

0 comments on commit ce404f8

Please sign in to comment.