Skip to content

Commit bdf3bf2

Browse files
committed
Allow Enum::all() with any property values
1 parent 68a5a47 commit bdf3bf2

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,9 @@ $drive = DriveWheel::fromCode('FWD');
5151
$drive = DriveWheel::from('code', 'FWD');
5252

5353
// Array instances
54-
$ids = [1, 2];
55-
// or
56-
$ids = [DriveWheel::front(), DriveWheel::rear()];
57-
5854
DriveWheel::all(); // [DriveWheel::front(), DriveWheel::rear(), DriveWheel::allDrive()]
59-
DriveWheel::all($ids); // [DriveWheel::front(), DriveWheel::rear()]
60-
DriveWheel::all($ids, $reverse = true); // [DriveWheel::allDrive()]
55+
DriveWheel::all(['FWD', 'RWD'], $reverse = false, $property = 'code'); // [DriveWheel::front(), DriveWheel::rear()]
56+
DriveWheel::all([1, 2], $reverse = true); // [DriveWheel::allDrive()]
6157

6258
// Methods
6359
$drive->getId(); // 1

src/Enum.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -163,29 +163,29 @@ final public function get(string $property)
163163
}
164164

165165
/**
166-
* @param Enum[]|int[]|string[] $ids
166+
* @param int[]|string[] $values
167167
*
168168
* @throws ReflectionException
169169
*
170170
* @return static[]
171171
*/
172-
final public static function all(array $ids = [], bool $reverse = false): array
172+
final public static function all(array $values = [], bool $reverse = false, string $property = 'id'): array
173173
{
174-
$ids = array_map(static function ($id) {
175-
return $id instanceof Enum ? $id->getId() : (int) $id;
176-
}, $ids);
177-
178-
$all = array_values(self::getReflection()->getConstants());
174+
if ('id' === $property) {
175+
$all = array_values(self::getReflection()->getConstants());
176+
} else {
177+
$all = array_values(self::$properties[static::class][$property]->getValue());
178+
}
179179

180-
if ([] === $ids) {
181-
$ids = $all;
180+
if ([] === $values) {
181+
$values = $all;
182182
} else {
183-
$ids = $reverse ? array_diff($all, $ids) : $ids;
183+
$values = $reverse ? array_values(array_diff($all, $values)) : $values;
184184
}
185185

186-
return array_map(static function (int $id) {
187-
return static::create($id);
188-
}, $ids);
186+
return array_map(static function ($id) use ($property) {
187+
return 'id' === $property ? static::create($id) : static::from($property, $id);
188+
}, $values);
189189
}
190190

191191
final public function eq(Enum $enum): bool

tests/EnumTest.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@ public function testAllMethod(): void
5656
self::assertSame([TestEnum::one()], TestEnum::all([1]));
5757
self::assertSame([TestEnum::two()], TestEnum::all([2]));
5858
self::assertSame([TestEnum::one()], TestEnum::all(['1']));
59-
self::assertSame([TestEnum::two()], TestEnum::all(['2']));
60-
self::assertSame([TestEnum::one()], TestEnum::all([TestEnum::one()]));
61-
self::assertSame([TestEnum::two()], TestEnum::all([TestEnum::two()]));
59+
self::assertSame([TestEnum::two()], TestEnum::all(['1'], true));
60+
self::assertSame([TestEnum::one()], TestEnum::all(['2'], true));
61+
62+
self::assertSame([TestEnum::one(), TestEnum::two()], TestEnum::all(['uno', 'duo'], false, 'identifier'));
63+
self::assertSame([TestEnum::two()], TestEnum::all(['uno'], true, 'identifier'));
6264
}
6365

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

131+
/**
132+
* @var array
133+
*/
134+
private static $identifier = [
135+
self::ONE => 'uno',
136+
self::TWO => 'duo',
137+
];
138+
129139
/**
130140
* @var array
131141
*/

0 commit comments

Comments
 (0)