Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
21 / 21 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| EnumTrait | |
100.00% |
21 / 21 |
|
100.00% |
6 / 6 |
9 | |
100.00% |
1 / 1 |
| equals | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isOneOf | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| values | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| names | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| options | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| tryFromName | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace Phexium\Domain; |
| 11 | |
| 12 | trait EnumTrait |
| 13 | { |
| 14 | public function equals(EnumInterface $other): bool |
| 15 | { |
| 16 | return $this === $other; |
| 17 | } |
| 18 | |
| 19 | public function isOneOf(EnumInterface ...$values): bool |
| 20 | { |
| 21 | return array_any( |
| 22 | $values, |
| 23 | fn ($value) => $this->equals($value) |
| 24 | ); |
| 25 | } |
| 26 | |
| 27 | public static function values(): array |
| 28 | { |
| 29 | return array_map( |
| 30 | static fn (self $case): string => $case->value, |
| 31 | self::cases() |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | public static function names(): array |
| 36 | { |
| 37 | return array_map( |
| 38 | static fn (self $case): string => $case->name, |
| 39 | self::cases() |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | public static function options(): array |
| 44 | { |
| 45 | $options = []; |
| 46 | |
| 47 | foreach (self::cases() as $case) { |
| 48 | $options[$case->name] = $case->label(); |
| 49 | } |
| 50 | |
| 51 | return $options; |
| 52 | } |
| 53 | |
| 54 | public static function tryFromName(string $name): ?self |
| 55 | { |
| 56 | foreach (self::cases() as $case) { |
| 57 | if (strcasecmp($case->name, $name) === 0) { |
| 58 | return $case; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return null; |
| 63 | } |
| 64 | } |