Skip to content

Commit

Permalink
Allow Enum::all() with any property values
Browse files Browse the repository at this point in the history
  • Loading branch information
grachevko committed Nov 15, 2019
1 parent 68a5a47 commit bdf3bf2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,9 @@ $drive = DriveWheel::fromCode('FWD');
$drive = DriveWheel::from('code', 'FWD');

// Array instances
$ids = [1, 2];
// or
$ids = [DriveWheel::front(), DriveWheel::rear()];

DriveWheel::all(); // [DriveWheel::front(), DriveWheel::rear(), DriveWheel::allDrive()]
DriveWheel::all($ids); // [DriveWheel::front(), DriveWheel::rear()]
DriveWheel::all($ids, $reverse = true); // [DriveWheel::allDrive()]
DriveWheel::all(['FWD', 'RWD'], $reverse = false, $property = 'code'); // [DriveWheel::front(), DriveWheel::rear()]
DriveWheel::all([1, 2], $reverse = true); // [DriveWheel::allDrive()]

// Methods
$drive->getId(); // 1
Expand Down
26 changes: 13 additions & 13 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,29 +163,29 @@ final public function get(string $property)
}

/**
* @param Enum[]|int[]|string[] $ids
* @param int[]|string[] $values
*
* @throws ReflectionException
*
* @return static[]
*/
final public static function all(array $ids = [], bool $reverse = false): array
final public static function all(array $values = [], bool $reverse = false, string $property = 'id'): array
{
$ids = array_map(static function ($id) {
return $id instanceof Enum ? $id->getId() : (int) $id;
}, $ids);

$all = array_values(self::getReflection()->getConstants());
if ('id' === $property) {
$all = array_values(self::getReflection()->getConstants());
} else {
$all = array_values(self::$properties[static::class][$property]->getValue());
}

if ([] === $ids) {
$ids = $all;
if ([] === $values) {
$values = $all;
} else {
$ids = $reverse ? array_diff($all, $ids) : $ids;
$values = $reverse ? array_values(array_diff($all, $values)) : $values;
}

return array_map(static function (int $id) {
return static::create($id);
}, $ids);
return array_map(static function ($id) use ($property) {
return 'id' === $property ? static::create($id) : static::from($property, $id);
}, $values);
}

final public function eq(Enum $enum): bool
Expand Down
16 changes: 13 additions & 3 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ public function testAllMethod(): void
self::assertSame([TestEnum::one()], TestEnum::all([1]));
self::assertSame([TestEnum::two()], TestEnum::all([2]));
self::assertSame([TestEnum::one()], TestEnum::all(['1']));
self::assertSame([TestEnum::two()], TestEnum::all(['2']));
self::assertSame([TestEnum::one()], TestEnum::all([TestEnum::one()]));
self::assertSame([TestEnum::two()], TestEnum::all([TestEnum::two()]));
self::assertSame([TestEnum::two()], TestEnum::all(['1'], true));
self::assertSame([TestEnum::one()], TestEnum::all(['2'], true));

self::assertSame([TestEnum::one(), TestEnum::two()], TestEnum::all(['uno', 'duo'], false, 'identifier'));
self::assertSame([TestEnum::two()], TestEnum::all(['uno'], true, 'identifier'));
}

public function testReadableName(): void
Expand Down Expand Up @@ -126,6 +128,14 @@ class TestEnum extends Enum
self::TWO => 'Double yo',
];

/**
* @var array
*/
private static $identifier = [
self::ONE => 'uno',
self::TWO => 'duo',
];

/**
* @var array
*/
Expand Down

0 comments on commit bdf3bf2

Please sign in to comment.