mirror of
https://github.com/archtechx/enums.git
synced 2025-12-12 09:24:03 +00:00
add options() method, resolve #2
This commit is contained in:
parent
7c02bbaa73
commit
e01edd128f
4 changed files with 34 additions and 9 deletions
16
src/Options.php
Normal file
16
src/Options.php
Normal 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;
|
||||||
|
}, []);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -11,6 +11,9 @@
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use ArchTech\Enums\InvokableCases;
|
||||||
|
use ArchTech\Enums\Options;
|
||||||
|
|
||||||
uses(ArchTech\Enums\Tests\TestCase::class)->in('Pest');
|
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;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use ArchTech\Enums\Exceptions\UndefinedCaseError;
|
use ArchTech\Enums\Exceptions\UndefinedCaseError;
|
||||||
use ArchTech\Enums\InvokableCases;
|
|
||||||
|
|
||||||
it('can be used as a static method', function () {
|
it('can be used as a static method', function () {
|
||||||
expect(Status::PENDING())->toBe(0);
|
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 () {
|
it('throws an error when a nonexistent case is being used', function () {
|
||||||
Status::INVALID();
|
Status::INVALID();
|
||||||
})->expectException(UndefinedCaseError::class);
|
})->expectException(UndefinedCaseError::class);
|
||||||
|
|
||||||
enum Status: int
|
|
||||||
{
|
|
||||||
use InvokableCases;
|
|
||||||
|
|
||||||
case PENDING = 0;
|
|
||||||
case DONE = 1;
|
|
||||||
}
|
|
||||||
7
tests/Pest/OptionsTest.php
Normal file
7
tests/Pest/OptionsTest.php
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
it('can return an associative array of options')
|
||||||
|
->expect(Status::options())->toBe([
|
||||||
|
'PENDING' => 0,
|
||||||
|
'DONE' => 1,
|
||||||
|
]);
|
||||||
Loading…
Add table
Add a link
Reference in a new issue