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

Commit 284ea12

Browse files
author
Petr Knap
committed
AbstractEnum 0.3
2 parents 8281d77 + 6a857fb commit 284ea12

File tree

3 files changed

+69
-41
lines changed

3 files changed

+69
-41
lines changed

src/Enum/AbstractEnum.php

Lines changed: 38 additions & 28 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.2
11+
* @version 0.3
1212
* @license https://github.com/petrknap/php-enum/blob/master/LICENSE MIT
1313
*/
1414
abstract class AbstractEnum
@@ -21,36 +21,38 @@ abstract class AbstractEnum
2121
/**
2222
* @var mixed[][]
2323
*/
24-
private static $constants = [];
24+
private static $members = [];
2525

2626
/**
27-
* @var mixed
27+
* @var string
2828
*/
29-
private $constantName;
29+
private $memberName;
3030

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

3636
/**
37-
* @param mixed $constantName
37+
* @param string $memberName
3838
* @throws EnumException
3939
*/
40-
protected function __construct($constantName)
40+
protected function __construct($memberName)
4141
{
42-
$this->constantName = $constantName;
43-
$this->constantValue = $this->get($constantName);
42+
if(!($memberName === null && !$this->exists(null))) {
43+
$this->memberName = $memberName;
44+
$this->memberValue = $this->get($memberName);
45+
}
4446
}
4547

4648
/**
4749
* Creates magical factories for easier access to enum
4850
*
49-
* @param mixed $constantName enum key
51+
* @param string $memberName enum key
5052
* @param array $args ignored
5153
* @return mixed
5254
*/
53-
public static function __callStatic($constantName, array $args)
55+
public static function __callStatic($memberName, array $args)
5456
{
5557
$className = get_called_class();
5658

@@ -60,10 +62,10 @@ public static function __callStatic($constantName, array $args)
6062
$instances = [];
6163
}
6264

63-
$instance = &$instances[$constantName];
65+
$instance = &$instances[$memberName];
6466

6567
if (!($instance instanceof $className)) {
66-
$instance = new $className($constantName);
68+
$instance = new $className($memberName);
6769
}
6870

6971
return $instance;
@@ -74,60 +76,68 @@ public static function __callStatic($constantName, array $args)
7476
*/
7577
public function getName()
7678
{
77-
return $this->constantName;
79+
return $this->memberName;
7880
}
7981

8082
/**
8183
* @return mixed
8284
*/
8385
public function getValue()
8486
{
85-
return $this->constantValue;
87+
return $this->memberValue;
8688
}
8789

8890
/**
89-
* @param mixed[] $constants
91+
* @param mixed[] $members
9092
*/
91-
protected static function setConstants(array $constants)
93+
protected static function setMembers(array $members)
9294
{
93-
self::$constants[get_called_class()] = $constants;
95+
self::$members[get_called_class()] = $members;
9496
}
9597

9698
/**
9799
* @return mixed[]
98100
*/
99-
public static function getConstants()
101+
public static function getMembers()
100102
{
101-
return self::$constants[get_called_class()];
103+
$className = get_called_class();
104+
105+
$members = &self::$members[$className];
106+
107+
if(empty($members)) {
108+
new $className(null);
109+
}
110+
111+
return $members;
102112
}
103113

104114
/**
105-
* @param string $constantName
115+
* @param string $memberName
106116
* @return bool
107117
*/
108-
private function exists($constantName)
118+
private function exists($memberName)
109119
{
110-
return array_key_exists($constantName, self::$constants[get_called_class()]);
120+
return array_key_exists($memberName, self::$members[get_called_class()]);
111121
}
112122

113123
/**
114-
* @param string $constantName
124+
* @param string $memberName
115125
* @return mixed
116126
* @throws EnumException
117127
*/
118-
private function get($constantName)
128+
private function get($memberName)
119129
{
120-
if (!$this->exists($constantName)) {
130+
if (!$this->exists($memberName)) {
121131
throw new EnumException(
122132
sprintf(
123133
"%s does not exist in %s",
124-
$constantName,
134+
$memberName,
125135
get_called_class()
126136
),
127137
EnumException::OUT_OF_RANGE
128138
);
129139
}
130140

131-
return self::$constants[get_called_class()][$constantName];
141+
return self::$members[get_called_class()][$memberName];
132142
}
133143
}

tests/Enum/EnumTest.php

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ public function wrongKeyProvider()
1818
}
1919

2020
/**
21+
* @covers EnumMock::__callStatic
2122
* @dataProvider goodKeyProvider
23+
*
2224
* @param string $name
2325
* @param mixed $value
2426
*/
25-
public function testEnumMagicConstruction_GoodKey($name, $value)
27+
public function testMagicConstruction_GoodKey($name, $value)
2628
{
2729
/** @var EnumMock $enum */
2830
$enum = EnumMock::$name();
@@ -33,10 +35,12 @@ public function testEnumMagicConstruction_GoodKey($name, $value)
3335
}
3436

3537
/**
38+
* @covers EnumMock::__callStatic
3639
* @dataProvider wrongKeyProvider
40+
*
3741
* @param string $name
3842
*/
39-
public function testEnumMagicConstruction_WrongKey($name)
43+
public function testMagicConstruction_WrongKey($name)
4044
{
4145
$this->setExpectedException(
4246
get_class(new EnumException()),
@@ -48,16 +52,30 @@ public function testEnumMagicConstruction_WrongKey($name)
4852
}
4953

5054
/**
51-
* @dataProvider goodKeyProvider
52-
* @param string $name
53-
* @param mixed $value
55+
* @covers EnumMock::__callStatic
56+
*/
57+
public function testComparable()
58+
{
59+
$this->assertSame(EnumMock::A(), EnumMock::A());
60+
$this->assertNotSame(EnumMock::A(), EnumMock::B());
61+
62+
$this->assertTrue(EnumMock::A() == EnumMock::A());
63+
$this->assertFalse(EnumMock::A() == EnumMock::B());
64+
}
65+
66+
/**
67+
* @covers EnumMock::getMembers
68+
* @runInSeparateProcess
5469
*/
55-
public function testGetConstants($name, $value)
70+
public function testGetMembers()
5671
{
57-
$constants = EnumMock::getConstants();
72+
$members = EnumMock::getMembers();
5873

59-
$this->assertInternalType("array", $constants);
60-
$this->assertArrayHasKey($name, $constants);
61-
$this->assertEquals($value, $constants[$name]);
74+
$this->assertInternalType("array", $members);
75+
$this->assertCount(2, $members);
76+
$this->assertArrayHasKey("A", $members);
77+
$this->assertEquals("a", $members["A"]);
78+
$this->assertArrayHasKey("B", $members);
79+
$this->assertEquals("b", $members["B"]);
6280
}
6381
}

tests/Enum/EnumTest/EnumMock.php

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

20-
parent::__construct($key);
20+
parent::__construct($memberName);
2121
}
2222

2323
/**

0 commit comments

Comments
 (0)