Skip to content

Commit 98f3b8e

Browse files
committed
Add quick access to enum value via Enum::CONSTANT_NAME().
1 parent c5bcdd6 commit 98f3b8e

File tree

3 files changed

+67
-32
lines changed

3 files changed

+67
-32
lines changed

Enum.php

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
abstract class Enum
1717
{
18-
protected static array $cacheItems = [];
18+
protected static array $cacheData = [];
1919
protected static array $cacheInstances = [];
2020

2121
/**
@@ -76,27 +76,10 @@ public static function isValid($id, array $filter = []): bool
7676
*/
7777
public static function toArray(array $filter = []): array
7878
{
79-
$class = get_called_class();
80-
if (!array_key_exists($class, static::$cacheItems)) {
81-
$reflection = new \ReflectionClass($class);
82-
if (is_callable([$class, 'items'])) {
83-
/** @noinspection PhpUndefinedMethodInspection */
84-
$items = $class::items();
85-
array_walk($items, function (&$item) {
86-
$item = is_array($item) ? $item : ['name' => $item];
87-
});
88-
} else {
89-
$items = array_fill_keys($reflection->getConstants(), []);
90-
}
91-
foreach ($reflection->getConstants() as $constant) {
92-
if (!isset($items[$constant]['name'])) {
93-
$items[$constant]['name'] = $constant;
94-
}
95-
$items[$constant]['id'] = $constant;
96-
}
97-
static::$cacheItems[$class] = $items;
98-
}
99-
$items = array_filter(static::$cacheItems[$class], function ($item) use ($class, $filter) {
79+
$items = array_map(function (array $data) {
80+
return $data['item'];
81+
}, static::getData());
82+
return array_filter($items, function ($item) use ($filter) {
10083
foreach ($filter as $key => $filterItem) {
10184
if (is_int($key)) {
10285
$operator = $filterItem[0];
@@ -146,7 +129,7 @@ public static function toArray(array $filter = []): array
146129
}
147130
} else {
148131
return call_user_func_array(
149-
[$class, $operator],
132+
[get_called_class(), $operator],
150133
array_merge([$item], array_slice($filterItem, 1))
151134
);
152135
}
@@ -159,7 +142,6 @@ public static function toArray(array $filter = []): array
159142
}
160143
return true;
161144
});
162-
return $items;
163145
}
164146

165147
/**
@@ -207,6 +189,21 @@ public static function toObjects(array $filter = []): array
207189
return $objects;
208190
}
209191

192+
public static function __callStatic($name, $arguments)
193+
{
194+
if ($name === 'items') {
195+
return [];
196+
}
197+
198+
foreach (static::getData() as $id => $data) {
199+
if ($data['constantName'] === $name) {
200+
return static::get($id);
201+
}
202+
}
203+
204+
throw new \RuntimeException();
205+
}
206+
210207
/**
211208
* @param string $name
212209
* @return mixed
@@ -223,11 +220,42 @@ public function __get($name)
223220
throw new LogicException('Getting unknown property: ' . get_class($this) . '::' . $name);
224221
}
225222

226-
/**
227-
* @return string
228-
*/
229-
public function __toString()
223+
public function __toString(): string
230224
{
231225
return (string)$this->id;
232226
}
227+
228+
private static function getData(): array
229+
{
230+
$class = get_called_class();
231+
if (!array_key_exists($class, static::$cacheData)) {
232+
$reflection = new \ReflectionClass($class);
233+
234+
$items = call_user_func([$class, 'items']);
235+
236+
$data = [];
237+
foreach ($reflection->getConstants() as $constantName => $id) {
238+
if (array_key_exists($id, $items)) {
239+
$item = is_array($items[$id]) ? $items[$id] : [
240+
'name' => $items[$id],
241+
];
242+
} else {
243+
$item = [];
244+
}
245+
246+
$item['id'] = $id;
247+
if (!array_key_exists('name', $item)) {
248+
$item['name'] = $id;
249+
}
250+
251+
$data[$id] = [
252+
'constantName' => $constantName,
253+
'item' => $item,
254+
];
255+
}
256+
257+
static::$cacheData[$class] = $data;
258+
}
259+
return static::$cacheData[$class];
260+
}
233261
}

tests/BaseTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,11 @@ public function testGetName(): void
4040
$this->assertEquals('Foo Name', $this->withName->name);
4141
$this->assertEquals('One', $this->withData->name);
4242
}
43+
44+
public function testCreate(): void
45+
{
46+
$this->assertSame(1, (new Pure(Pure::ONE))->id);
47+
$this->assertSame(1, Pure::get(Pure::ONE)->id);
48+
$this->assertSame(1, Pure::ONE()->id);
49+
}
4350
}

tests/PureTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,20 @@ public function testToArray(): void
6666
{
6767
$this->assertSame([
6868
Pure::FOO => [
69-
'name' => Pure::FOO,
7069
'id' => Pure::FOO,
70+
'name' => Pure::FOO,
7171
],
7272
Pure::BAR => [
73-
'name' => Pure::BAR,
7473
'id' => Pure::BAR,
74+
'name' => Pure::BAR,
7575
],
7676
Pure::ONE => [
77-
'name' => Pure::ONE,
7877
'id' => Pure::ONE,
78+
'name' => Pure::ONE,
7979
],
8080
Pure::TWO => [
81-
'name' => Pure::TWO,
8281
'id' => Pure::TWO,
82+
'name' => Pure::TWO,
8383
],
8484
], Pure::toArray());
8585
}

0 commit comments

Comments
 (0)