Skip to content

Commit 33f9204

Browse files
committed
enum
1 parent 1325f77 commit 33f9204

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

src/Enum.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
class Enum implements EnumInterface
1919
{
2020
/** @var array */
21-
private static array $constants;
21+
private static array $constants = [];
22+
23+
protected static array $instances = [];
2224

2325
/** @var mixed */
2426
private mixed $value;
@@ -33,11 +35,10 @@ class Enum implements EnumInterface
3335
*/
3436
final public function __construct(mixed $value)
3537
{
36-
static::$constants = static::constants();
37-
3838
$value = ($value instanceof EnumInterface) ? $value->getValue() : $value;
3939

4040
$this->key = static::searchNameByValue($value);
41+
4142
$this->value = $value;
4243
}
4344

@@ -46,9 +47,15 @@ final public function __construct(mixed $value)
4647
*/
4748
private static function constants(): array
4849
{
50+
if (isset(static::$constants[static::class])) {
51+
return static::$constants[static::class];
52+
}
53+
4954
$reflection = new ReflectionClass(static::class);
5055

51-
return $reflection->getConstants();
56+
static::$constants[static::class] = $reflection->getConstants();
57+
58+
return static::$constants[static::class];
5259
}
5360

5461
/**
@@ -65,7 +72,7 @@ public function getValue(): mixed
6572
*/
6673
private static function searchNameByValue(mixed $value): string
6774
{
68-
$name = array_search($value, static::$constants, true);
75+
$name = array_search($value, static::constants(), true);
6976
if (false === $name) {
7077
throw new InvalidArgumentException(sprintf('Constant value [%s] not found', $value));
7178
}
@@ -80,27 +87,32 @@ private static function searchNameByValue(mixed $value): string
8087
*/
8188
final public static function __callStatic(string $name, array $arguments): self
8289
{
83-
if (!array_key_exists($name, static::$constants)) {
90+
if (!array_key_exists($name, static::constants())) {
8491
throw new InvalidArgumentException(sprintf('Constant [%s] is not defined', $name));
8592
}
93+
if (isset(static::$instances[static::class][$name])) {
94+
return static::$instances[static::class][$name];
95+
}
96+
97+
static::$instances[static::class][$name] = new static(static::constants()[$name]);
8698

87-
return new static(static::$constants[$name]);
99+
return static::$instances[static::class][$name];
88100
}
89101

90102
/**
91103
* @return mixed[]
92104
*/
93105
public function getValues(): array
94106
{
95-
return array_values(static::$constants);
107+
return array_values(static::$constants[static::class]);
96108
}
97109

98110
/**
99111
* @return string[]
100112
*/
101113
public function getKeys(): array
102114
{
103-
return array_keys(static::$constants);
115+
return array_keys(static::$constants[static::class]);
104116
}
105117

106118
/**
@@ -116,7 +128,7 @@ public function getKey(): string
116128
*/
117129
public function toArray(): array
118130
{
119-
return static::$constants;
131+
return static::$constants[static::class];
120132
}
121133

122134
/**

tests/EnumTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function invalidMethod(): void
5353
public function keysValues(): void
5454
{
5555
$this->enum = new DummyEnum(5);
56-
static::assertEquals(["VALUE", "OTHER", "PRODUCT"], $this->enum->getKeys());
56+
static::assertEquals(["VALUE1", "VALUE2", "VALUE5"], $this->enum->getKeys());
5757
static::assertEquals([0 => 1, 1=> 2, 2=> 5], $this->enum->getValues());
5858
}
5959

@@ -66,22 +66,22 @@ public function callDataProvider(): array
6666

6767
return [
6868
[
69-
"1", $this->enum::VALUE()
69+
"1", $this->enum::VALUE1()
7070
],
7171
[
72-
"2", $this->enum::OTHER(),
72+
"2", $this->enum::VALUE2(),
7373
],
7474
[
7575
"5", new DummyEnum(new DummyEnum(5)),
7676
],
7777
[
78-
5, $this->enum->getValue(),
78+
"5", $this->enum->getValue(),
7979
],
8080
[
81-
"PRODUCT", $this->enum->getKey(),
81+
"VALUE5", $this->enum->getKey(),
8282
],
8383
[
84-
["VALUE" => 1, "OTHER" => 2, "PRODUCT" => 5], $this->enum->toArray(),
84+
["VALUE1" => 1, "VALUE2" => 2, "VALUE5" => 5], $this->enum->toArray(),
8585
]
8686
];
8787
}
@@ -96,9 +96,9 @@ public function callDataProvider(): array
9696
*/
9797
class DummyEnum extends Enum
9898
{
99-
public const VALUE = 1;
99+
public const VALUE1 = 1;
100100

101-
public const OTHER = 2;
101+
public const VALUE2 = 2;
102102

103-
public const PRODUCT = 5;
103+
public const VALUE5 = 5;
104104
}

0 commit comments

Comments
 (0)