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

add options() method, resolve #2

This commit is contained in:
Samuel Štancl 2022-02-20 21:50:53 +01:00
parent 7c02bbaa73
commit e01edd128f
4 changed files with 34 additions and 9 deletions

16
src/Options.php Normal file
View file

@ -0,0 +1,16 @@
<?php
namespace ArchTech\Enums;
trait Options
{
/** Get an associative array of [case name => case value]. */
public static function options(): array
{
return array_reduce(static::cases(), function ($options, $case) {
$options[$case->name] = $case->value;
return $options;
}, []);
}
}

View file

@ -11,6 +11,9 @@
|
*/
use ArchTech\Enums\InvokableCases;
use ArchTech\Enums\Options;
uses(ArchTech\Enums\Tests\TestCase::class)->in('Pest');
/*
@ -43,3 +46,11 @@ function something()
{
// ..
}
enum Status: int
{
use InvokableCases, Options;
case PENDING = 0;
case DONE = 1;
}

View file

@ -1,7 +1,6 @@
<?php
use ArchTech\Enums\Exceptions\UndefinedCaseError;
use ArchTech\Enums\InvokableCases;
it('can be used as a static method', function () {
expect(Status::PENDING())->toBe(0);
@ -18,11 +17,3 @@ it('can be invoked as an instance', function () {
it('throws an error when a nonexistent case is being used', function () {
Status::INVALID();
})->expectException(UndefinedCaseError::class);
enum Status: int
{
use InvokableCases;
case PENDING = 0;
case DONE = 1;
}

View file

@ -0,0 +1,7 @@
<?php
it('can return an associative array of options')
->expect(Status::options())->toBe([
'PENDING' => 0,
'DONE' => 1,
]);