Skip to content

Commit

Permalink
Addimg except and only for Enum
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Mar 5, 2024
1 parent 82cc526 commit 08b8bce
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use ReflectionEnum;

use UnitEnum;

use function array_column;
use function array_search;
use function count;
Expand Down Expand Up @@ -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<UnitEnum>
*/
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<UnitEnum>
*/
public static function only(UnitEnum ...$values): array
{
return array_values(
array_filter(
static::cases(),
fn (UnitEnum $enum) => in_array($enum, $values, true)
)
);
}
}
2 changes: 2 additions & 0 deletions InfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 08b8bce

Please sign in to comment.