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

Commit da45936

Browse files
author
Petr Knap
committed
Protected constructor and static items
1 parent a97ffb1 commit da45936

File tree

3 files changed

+10
-39
lines changed

3 files changed

+10
-39
lines changed

src/Enum/AbstractEnum.php

Lines changed: 8 additions & 8 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,9 +19,9 @@ abstract class AbstractEnum
1919
private static $instances;
2020

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

2626
/**
2727
* @var mixed
@@ -37,7 +37,7 @@ abstract class AbstractEnum
3737
* @param mixed $key
3838
* @throws EnumException
3939
*/
40-
public function __construct($key)
40+
protected function __construct($key)
4141
{
4242
$this->key = $key;
4343
$this->value = $this->get($key);
@@ -88,9 +88,9 @@ public function getValue()
8888
/**
8989
* @param mixed[] $items
9090
*/
91-
protected function setItems(array $items)
91+
protected static function setItems(array $items)
9292
{
93-
$this->items = $items;
93+
self::$items[get_called_class()] = $items;
9494
}
9595

9696
/**
@@ -99,7 +99,7 @@ protected function setItems(array $items)
9999
*/
100100
private function exists($key)
101101
{
102-
return array_key_exists($key, $this->items);
102+
return array_key_exists($key, self::$items[get_called_class()]);
103103
}
104104

105105
/**
@@ -120,6 +120,6 @@ private function get($key)
120120
);
121121
}
122122

123-
return $this->items[$key];
123+
return self::$items[get_called_class()][$key];
124124
}
125125
}

tests/Enum/EnumTest.php

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,6 @@ public function wrongKeyProvider()
1717
return [["C"], ["D"]];
1818
}
1919

20-
/**
21-
* @dataProvider goodKeyProvider
22-
* @param string $key
23-
* @param string $value
24-
*/
25-
public function testEnumDirectConstruction_GoodKey($key, $value)
26-
{
27-
$enum = new EnumMock($key);
28-
29-
$this->assertInstanceOf(EnumMock::getClass(), $enum);
30-
$this->assertSame($key, $enum->getKey());
31-
$this->assertSame($value, $enum->getValue());
32-
}
33-
34-
/**
35-
* @dataProvider wrongKeyProvider
36-
* @param string $key
37-
*/
38-
public function testEnumDirectConstruction_WrongKey($key)
39-
{
40-
$this->setExpectedException(
41-
get_class(new EnumException()),
42-
"",
43-
EnumException::OUT_OF_RANGE
44-
);
45-
46-
new EnumMock($key);
47-
}
48-
4920
/**
5021
* @dataProvider goodKeyProvider
5122
* @param string $key

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::setItems([
1616
"A" => "a",
1717
"B" => "b"
1818
]);

0 commit comments

Comments
 (0)