Skip to content

Commit a99cb1d

Browse files
committed
add EnumMapping
1 parent 30754e4 commit a99cb1d

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/EnumMapping.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Warengo\Enum;
4+
5+
final class EnumMapping
6+
{
7+
8+
/** @var string[] */
9+
private array $mapping = [];
10+
11+
private string $class;
12+
13+
/**
14+
* @param bool[] $mapping
15+
*/
16+
public function __construct(string $class, array $mapping)
17+
{
18+
$this->class = $class;
19+
foreach ($mapping as $key => $bool) {
20+
$this->mapping[strtoupper($key)] = $key;
21+
}
22+
}
23+
24+
public function getClass(): string
25+
{
26+
return $this->class;
27+
}
28+
29+
public function callableGetter(): callable
30+
{
31+
return [$this->getClass(), 'get'];
32+
}
33+
34+
/**
35+
* @return string[]
36+
*/
37+
public function getMapping(): array
38+
{
39+
return $this->mapping;
40+
}
41+
42+
}

src/EnumStatic.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ abstract class EnumStatic
2222
*/
2323
abstract protected static function getEnums(): array;
2424

25+
final public static function createMapping(): EnumMapping {
26+
return new EnumMapping(static::class, self::getEnumsCached());
27+
}
28+
2529
/**
2630
* @param mixed[] $arguments
2731
*/

0 commit comments

Comments
 (0)