mirror of
https://github.com/archtechx/enums.git
synced 2025-12-12 15:34:04 +00:00
Code review changes
Altered `::values()` to act like `::names()` for backed enums, code style fixes, general cleanups
This commit is contained in:
parent
c2f0e7faf7
commit
dea16e414c
5 changed files with 22 additions and 13 deletions
|
|
@ -21,7 +21,7 @@ composer require archtechx/enums
|
|||
|
||||
### InvokableCases
|
||||
|
||||
This helper lets you get the value of a backed enum, or the name of a pure enum by "invoking" it — either statically (`MyEnum::FOO()` instead of `MyEnum::FOO`), or as an instance (`$enum()`).
|
||||
This helper lets you get the value of a backed enum, or the name of a pure enum, by "invoking" it — either statically (`MyEnum::FOO()` instead of `MyEnum::FOO`), or as an instance (`$enum()`).
|
||||
|
||||
That way, you can use enums as array keys:
|
||||
```php
|
||||
|
|
@ -122,7 +122,7 @@ Role::names(); // ['ADMINISTRATOR', 'SUBSCRIBER', 'GUEST']
|
|||
|
||||
### Values
|
||||
|
||||
This helper returns a list of case *values* in the enum. _**NB:** pure enums don't have values, so this will only ever return an empty array._
|
||||
This helper returns a list of case *values* for backed enums, or a list of case *names* for pure enums (making this functionally equivalent to [`::names()`](#names) for pure Enums)
|
||||
|
||||
#### Apply the trait on your enum
|
||||
```php
|
||||
|
|
@ -150,12 +150,12 @@ enum Role
|
|||
#### Use the `values()` method
|
||||
```php
|
||||
TaskStatus::values(); // [0, 1, 2]
|
||||
Role::values(); // []
|
||||
Role::values(); // ['ADMINISTRATOR', 'SUBSCRIBER', 'GUEST']
|
||||
```
|
||||
|
||||
### Options
|
||||
|
||||
This helper returns an associative array of case names and values for backed enums, or an array of names for pure enums (making this functionally equivalent to `::names()` for pure Enums).
|
||||
This helper returns an associative array of case names and values for backed enums, or a list of names for pure enums (making this functionally equivalent to [`::names()`](#names) for pure Enums).
|
||||
|
||||
#### Apply the trait on your enum
|
||||
```php
|
||||
|
|
|
|||
|
|
@ -4,12 +4,14 @@ declare(strict_types=1);
|
|||
|
||||
namespace ArchTech\Enums;
|
||||
|
||||
use BackedEnum;
|
||||
|
||||
trait InvokableCases
|
||||
{
|
||||
/** Return the enum's value when it's $invoked(). */
|
||||
public function __invoke()
|
||||
{
|
||||
return $this->value ?? $this->name;
|
||||
return $this instanceof BackedEnum ? $this->value : $this->name;
|
||||
}
|
||||
|
||||
/** Return the enum's value or name when it's called ::STATICALLY(). */
|
||||
|
|
@ -19,7 +21,7 @@ trait InvokableCases
|
|||
|
||||
foreach ($cases as $case) {
|
||||
if ($case->name === $name) {
|
||||
return $case instanceof \BackedEnum ? $case->value : $case->name;
|
||||
return $case instanceof BackedEnum ? $case->value : $case->name;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,16 +4,17 @@ declare(strict_types=1);
|
|||
|
||||
namespace ArchTech\Enums;
|
||||
|
||||
use BackedEnum;
|
||||
|
||||
trait Options
|
||||
{
|
||||
/** Get an associative array of [case name => case value]. */
|
||||
public static function options(): array
|
||||
{
|
||||
$cases = static::cases();
|
||||
if (reset($cases) instanceof \BackedEnum) {
|
||||
return array_column($cases, 'value', 'name');
|
||||
}
|
||||
|
||||
return array_column($cases, 'name');
|
||||
return isset($cases[0]) && $cases[0] instanceof BackedEnum
|
||||
? array_column($cases, 'value', 'name')
|
||||
: array_column($cases, 'name');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,11 +4,17 @@ declare(strict_types=1);
|
|||
|
||||
namespace ArchTech\Enums;
|
||||
|
||||
use BackedEnum;
|
||||
|
||||
trait Values
|
||||
{
|
||||
/** Get an array of case values. */
|
||||
public static function values(): array
|
||||
{
|
||||
return array_column(static::cases(), 'value');
|
||||
$cases = static::cases();
|
||||
|
||||
return isset($cases[0]) && $cases[0] instanceof BackedEnum
|
||||
? array_column($cases, 'value')
|
||||
: array_column($cases, 'name');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,6 @@ it('can return an array of case values from a backed enum')
|
|||
->expect(Status::values())
|
||||
->toBe([0, 1]);
|
||||
|
||||
it('can returns an empty array from a pure enum')
|
||||
it('can return an array of case names from a pure enum')
|
||||
->expect(Role::values())
|
||||
->toBe([]);
|
||||
->toBe(['ADMIN', 'GUEST']);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue