Skip to content
This repository was archived by the owner on Dec 26, 2023. It is now read-only.

Commit a77fdfe

Browse files
author
Petr Knap
committed
AbstractEnum 0.2
2 parents 63accf3 + e36e37c commit a77fdfe

File tree

3 files changed

+55
-62
lines changed

3 files changed

+55
-62
lines changed

src/Enum/AbstractEnum.php

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @author Petr Knap <dev@petrknap.cz>
99
* @since 2016-01-23
1010
* @package PetrKnap\Php\Enum
11-
* @version 0.1
11+
* @version 0.2
1212
* @license https://github.com/petrknap/php-enum/blob/master/LICENSE MIT
1313
*/
1414
abstract class AbstractEnum
@@ -19,38 +19,38 @@ abstract class AbstractEnum
1919
private static $instances;
2020

2121
/**
22-
* @var mixed[]
22+
* @var mixed[][]
2323
*/
24-
private $items = [];
24+
private static $constants = [];
2525

2626
/**
2727
* @var mixed
2828
*/
29-
private $key;
29+
private $constantName;
3030

3131
/**
3232
* @var mixed
3333
*/
34-
private $value;
34+
private $constantValue;
3535

3636
/**
37-
* @param mixed $key
37+
* @param mixed $constantName
3838
* @throws EnumException
3939
*/
40-
public function __construct($key)
40+
protected function __construct($constantName)
4141
{
42-
$this->key = $key;
43-
$this->value = $this->get($key);
42+
$this->constantName = $constantName;
43+
$this->constantValue = $this->get($constantName);
4444
}
4545

4646
/**
4747
* Creates magical factories for easier access to enum
4848
*
49-
* @param mixed $key enum key
49+
* @param mixed $constantName enum key
5050
* @param array $args ignored
5151
* @return mixed
5252
*/
53-
public static function __callStatic($key, array $args)
53+
public static function __callStatic($constantName, array $args)
5454
{
5555
$className = get_called_class();
5656

@@ -60,66 +60,74 @@ public static function __callStatic($key, array $args)
6060
$instances = [];
6161
}
6262

63-
$instance = &$instances[$key];
63+
$instance = &$instances[$constantName];
6464

6565
if (!($instance instanceof $className)) {
66-
$instance = new $className($key);
66+
$instance = new $className($constantName);
6767
}
6868

6969
return $instance;
7070
}
7171

7272
/**
73-
* @return mixed
73+
* @return string
7474
*/
75-
public function getKey()
75+
public function getName()
7676
{
77-
return $this->key;
77+
return $this->constantName;
7878
}
7979

8080
/**
8181
* @return mixed
8282
*/
8383
public function getValue()
8484
{
85-
return $this->value;
85+
return $this->constantValue;
86+
}
87+
88+
/**
89+
* @param mixed[] $constants
90+
*/
91+
protected static function setConstants(array $constants)
92+
{
93+
self::$constants[get_called_class()] = $constants;
8694
}
8795

8896
/**
89-
* @param mixed[] $items
97+
* @return mixed[]
9098
*/
91-
protected function setItems(array $items)
99+
public static function getConstants()
92100
{
93-
$this->items = $items;
101+
return self::$constants[get_called_class()];
94102
}
95103

96104
/**
97-
* @param mixed $key
105+
* @param string $constantName
98106
* @return bool
99107
*/
100-
private function exists($key)
108+
private function exists($constantName)
101109
{
102-
return array_key_exists($key, $this->items);
110+
return array_key_exists($constantName, self::$constants[get_called_class()]);
103111
}
104112

105113
/**
106-
* @param mixed $key
114+
* @param string $constantName
107115
* @return mixed
108116
* @throws EnumException
109117
*/
110-
private function get($key)
118+
private function get($constantName)
111119
{
112-
if (!$this->exists($key)) {
120+
if (!$this->exists($constantName)) {
113121
throw new EnumException(
114122
sprintf(
115-
"%s does not exists in %s",
116-
$key,
123+
"%s does not exist in %s",
124+
$constantName,
117125
get_called_class()
118126
),
119127
EnumException::OUT_OF_RANGE
120128
);
121129
}
122130

123-
return $this->items[$key];
131+
return self::$constants[get_called_class()][$constantName];
124132
}
125133
}

tests/Enum/EnumTest.php

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,60 +19,45 @@ public function wrongKeyProvider()
1919

2020
/**
2121
* @dataProvider goodKeyProvider
22-
* @param string $key
23-
* @param string $value
22+
* @param string $name
23+
* @param mixed $value
2424
*/
25-
public function testEnumDirectConstruction_GoodKey($key, $value)
25+
public function testEnumMagicConstruction_GoodKey($name, $value)
2626
{
27-
$enum = new EnumMock($key);
27+
/** @var EnumMock $enum */
28+
$enum = EnumMock::$name();
2829

2930
$this->assertInstanceOf(EnumMock::getClass(), $enum);
30-
$this->assertSame($key, $enum->getKey());
31+
$this->assertSame($name, $enum->getName());
3132
$this->assertSame($value, $enum->getValue());
3233
}
3334

3435
/**
3536
* @dataProvider wrongKeyProvider
36-
* @param string $key
37+
* @param string $name
3738
*/
38-
public function testEnumDirectConstruction_WrongKey($key)
39+
public function testEnumMagicConstruction_WrongKey($name)
3940
{
4041
$this->setExpectedException(
4142
get_class(new EnumException()),
4243
"",
4344
EnumException::OUT_OF_RANGE
4445
);
4546

46-
new EnumMock($key);
47+
EnumMock::$name();
4748
}
4849

4950
/**
5051
* @dataProvider goodKeyProvider
51-
* @param string $key
52-
* @param string $value
53-
*/
54-
public function testEnumMagicConstruction_GoodKey($key, $value)
55-
{
56-
/** @var EnumMock $enum */
57-
$enum = EnumMock::$key();
58-
59-
$this->assertInstanceOf(EnumMock::getClass(), $enum);
60-
$this->assertSame($key, $enum->getKey());
61-
$this->assertSame($value, $enum->getValue());
62-
}
63-
64-
/**
65-
* @dataProvider wrongKeyProvider
66-
* @param string $key
52+
* @param string $name
53+
* @param mixed $value
6754
*/
68-
public function testEnumMagicConstruction_WrongKey($key)
55+
public function testGetConstants($name, $value)
6956
{
70-
$this->setExpectedException(
71-
get_class(new EnumException()),
72-
"",
73-
EnumException::OUT_OF_RANGE
74-
);
57+
$constants = EnumMock::getConstants();
7558

76-
EnumMock::$key();
59+
$this->assertInternalType("array", $constants);
60+
$this->assertArrayHasKey($name, $constants);
61+
$this->assertEquals($value, $constants[$name]);
7762
}
7863
}

tests/Enum/EnumTest/EnumMock.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
*/
1111
class EnumMock extends AbstractEnum
1212
{
13-
public function __construct($key)
13+
protected function __construct($key)
1414
{
15-
$this->setItems([
15+
self::setConstants([
1616
"A" => "a",
1717
"B" => "b"
1818
]);

0 commit comments

Comments
 (0)