diff --git a/Info.php b/Info.php index e0e7de5..de649f1 100644 --- a/Info.php +++ b/Info.php @@ -6,6 +6,8 @@ use ReflectionEnum; +use UnitEnum; + use function array_column; use function array_search; use function count; @@ -66,4 +68,34 @@ public static function nameOf(string|int $value): ?string default => $res, }; } + + /** + * Returns the list of cases not including the enum instance passed to the method call. + * + * @return array + */ + public static function except(UnitEnum ...$values): array + { + return array_values( + array_filter( + static::cases(), + fn (UnitEnum $enum) => !in_array($enum, $values, true) + ) + ); + } + + /** + * Returns the list of cases including only the enum listed in the method call if they exist. + * + * @return array + */ + public static function only(UnitEnum ...$values): array + { + return array_values( + array_filter( + static::cases(), + fn (UnitEnum $enum) => in_array($enum, $values, true) + ) + ); + } } diff --git a/InfoTest.php b/InfoTest.php index e70b302..56945d4 100644 --- a/InfoTest.php +++ b/InfoTest.php @@ -18,6 +18,8 @@ public function it_can_get_information_from_a_pure_enumeration(): void self::assertSame(['Top', 'Down', 'Left', 'Right'], Direction::names()); self::assertSame([], Direction::values()); self::assertNull(Direction::nameOf('Up')); + self::assertSame([Direction::Down], Direction::only(Direction::Down)); + self::assertSame([Direction::Left, Direction::Right], Direction::except(Direction::Top, Direction::Down)); } #[Test]