mirror of
https://github.com/archtechx/enums.git
synced 2025-12-12 15:34:04 +00:00
add names() and values() methods, resolve #1
This commit is contained in:
parent
e01edd128f
commit
b50d001374
5 changed files with 41 additions and 1 deletions
14
src/Names.php
Normal file
14
src/Names.php
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
namespace ArchTech\Enums;
|
||||
|
||||
trait Names
|
||||
{
|
||||
/** Get an array of case names. */
|
||||
public static function names(): array
|
||||
{
|
||||
return array_map(function ($case) {
|
||||
return $case->name;
|
||||
}, static::cases());
|
||||
}
|
||||
}
|
||||
14
src/Values.php
Normal file
14
src/Values.php
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
namespace ArchTech\Enums;
|
||||
|
||||
trait Values
|
||||
{
|
||||
/** Get an array of case values. */
|
||||
public static function values(): array
|
||||
{
|
||||
return array_map(function ($case) {
|
||||
return $case->value;
|
||||
}, static::cases());
|
||||
}
|
||||
}
|
||||
|
|
@ -12,7 +12,9 @@
|
|||
*/
|
||||
|
||||
use ArchTech\Enums\InvokableCases;
|
||||
use ArchTech\Enums\Names;
|
||||
use ArchTech\Enums\Options;
|
||||
use ArchTech\Enums\Values;
|
||||
|
||||
uses(ArchTech\Enums\Tests\TestCase::class)->in('Pest');
|
||||
|
||||
|
|
@ -49,7 +51,7 @@ function something()
|
|||
|
||||
enum Status: int
|
||||
{
|
||||
use InvokableCases, Options;
|
||||
use InvokableCases, Options, Names, Values;
|
||||
|
||||
case PENDING = 0;
|
||||
case DONE = 1;
|
||||
|
|
|
|||
5
tests/Pest/NamesTest.php
Normal file
5
tests/Pest/NamesTest.php
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
|
||||
it('can return an array of case names')
|
||||
->expect(Status::names())
|
||||
->toBe(['PENDING', 'DONE']);
|
||||
5
tests/Pest/ValuesTest.php
Normal file
5
tests/Pest/ValuesTest.php
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
|
||||
it('can return an array of case values')
|
||||
->expect(Status::values())
|
||||
->toBe([0, 1]);
|
||||
Loading…
Add table
Add a link
Reference in a new issue