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

Commit 07bd7ea

Browse files
author
Petr Knap
committed
AbstractEnum 0.4
2 parents b8222c3 + 2e215db commit 07bd7ea

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/Enum/AbstractEnum.php

Lines changed: 12 additions & 9 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.3
11+
* @version 0.4
1212
* @license https://github.com/petrknap/php-enum/blob/master/LICENSE MIT
1313
*/
1414
abstract class AbstractEnum
@@ -37,9 +37,15 @@ abstract class AbstractEnum
3737
* @param string $memberName
3838
* @throws EnumException
3939
*/
40-
protected function __construct($memberName)
40+
private function __construct($memberName)
4141
{
42-
if(!($memberName === null && !$this->exists(null))) {
42+
$members = &self::$members[get_called_class()];
43+
44+
if (!$members) {
45+
$members = $this->members();
46+
}
47+
48+
if (!($memberName === null && !$this->exists(null))) {
4349
$this->memberName = $memberName;
4450
$this->memberValue = $this->get($memberName);
4551
}
@@ -88,12 +94,9 @@ public function getValue()
8894
}
8995

9096
/**
91-
* @param mixed[] $members
97+
* @return mixed[] [first_name => first_value, second_name => second_value,...]
9298
*/
93-
protected static function setMembers(array $members)
94-
{
95-
self::$members[get_called_class()] = $members;
96-
}
99+
abstract protected function members();
97100

98101
/**
99102
* @return mixed[]
@@ -104,7 +107,7 @@ public static function getMembers()
104107

105108
$members = &self::$members[$className];
106109

107-
if(empty($members)) {
110+
if (empty($members)) {
108111
new $className(null);
109112
}
110113

tests/Enum/EnumTest/EnumMock.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@
1010
*/
1111
class EnumMock extends AbstractEnum
1212
{
13-
protected function __construct($memberName)
13+
protected function members()
1414
{
15-
self::setMembers([
15+
return [
1616
"A" => "a",
1717
"B" => "b"
18-
]);
19-
20-
parent::__construct($memberName);
18+
];
2119
}
2220

2321
/**

0 commit comments

Comments
 (0)