Skip to content

Commit 1534d31

Browse files
committed
Make constructor private and use EnumManager for all static methods
1 parent 0b29079 commit 1534d31

File tree

3 files changed

+64
-59
lines changed

3 files changed

+64
-59
lines changed

src/Enum.php

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
namespace MyCLabs\Enum;
88

99
use BadMethodCallException;
10-
use UnexpectedValueException;
1110

1211
/**
1312
* Base Enum class
@@ -21,32 +20,27 @@
2120
abstract class Enum
2221
{
2322
/**
24-
* Enum value
23+
* Enum name
2524
*
26-
* @var mixed
25+
* @var string
2726
*/
28-
protected $value;
27+
private $name;
2928

3029
/**
31-
* Store existing constants in a static cache per object.
30+
* Enum value
3231
*
33-
* @var array
32+
* @var mixed
3433
*/
35-
protected static $cache = array();
34+
private $value;
3635

3736
/**
3837
* Creates a new value of some type
3938
*
4039
* @param mixed $value
41-
*
42-
* @throws UnexpectedValueException if incompatible type is given.
4340
*/
44-
public function __construct($value)
41+
final private function __construct($name, $value)
4542
{
46-
if (!static::isValid($value)) {
47-
throw new UnexpectedValueException("Value '$value' is not part of the enum " . get_called_class());
48-
}
49-
43+
$this->name = $name;
5044
$this->value = $value;
5145
}
5246

@@ -65,7 +59,7 @@ public function getValue()
6559
*/
6660
public function getKey()
6761
{
68-
return static::search($this->value);
62+
return $this->name;
6963
}
7064

7165
/**
@@ -107,8 +101,8 @@ public static function values()
107101
{
108102
$values = array();
109103

110-
foreach (static::toArray() as $key => $value) {
111-
$values[$key] = new static($value);
104+
foreach (static::toArray() as $name => $value) {
105+
$values[$name] = EnumManager::get(new static($name, $value));
112106
}
113107

114108
return $values;
@@ -121,13 +115,7 @@ public static function values()
121115
*/
122116
public static function toArray()
123117
{
124-
$class = get_called_class();
125-
if (!array_key_exists($class, static::$cache)) {
126-
$reflection = new \ReflectionClass($class);
127-
static::$cache[$class] = $reflection->getConstants();
128-
}
129-
130-
return static::$cache[$class];
118+
return EnumManager::constants(new static(null, null));
131119
}
132120

133121
/**
@@ -168,6 +156,21 @@ public static function search($value)
168156
return array_search($value, static::toArray(), true);
169157
}
170158

159+
/**
160+
* Returns Enum by value
161+
*
162+
* @return static
163+
*/
164+
public static function fromValue($value)
165+
{
166+
$name = static::search($value);
167+
if ($name === false) {
168+
return null;
169+
}
170+
171+
return EnumManager::get(new static($name, $value));
172+
}
173+
171174
/**
172175
* Returns Enum by key
173176
*
@@ -176,8 +179,8 @@ public static function search($value)
176179
public static function fromKey($name)
177180
{
178181
$array = static::toArray();
179-
if (array_key_exists($name, $array)) {
180-
return EnumManager::get(new static($array[$name]));
182+
if (isset($array[$name]) || array_key_exists($name, $array)) {
183+
return EnumManager::get(new static($name, $array[$name]));
181184
}
182185

183186
return null;

src/EnumManager.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,16 @@ public static function get(Enum $enum)
4040
self::$instances[$class][$name] = $enum;
4141
return $enum;
4242
}
43+
44+
/**
45+
* Returns all possible values as an array
46+
*
47+
* @return array Constant name in key, constant value in value
48+
*/
49+
public static function constants(Enum $enum)
50+
{
51+
$reflection = new ReflectionObject($enum);
52+
$result = $reflection->getConstants();
53+
return $result;
54+
}
4355
}

tests/EnumTest.php

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ class EnumTest extends \PHPUnit\Framework\TestCase
1818
*/
1919
public function testGetValue()
2020
{
21-
$value = new EnumFixture(EnumFixture::FOO);
21+
$value = EnumFixture::FOO();
2222
$this->assertEquals(EnumFixture::FOO, $value->getValue());
2323

24-
$value = new EnumFixture(EnumFixture::BAR);
24+
$value = EnumFixture::BAR();
2525
$this->assertEquals(EnumFixture::BAR, $value->getValue());
2626

27-
$value = new EnumFixture(EnumFixture::NUMBER);
27+
$value = EnumFixture::NUMBER();
2828
$this->assertEquals(EnumFixture::NUMBER, $value->getValue());
2929
}
3030

@@ -33,21 +33,11 @@ public function testGetValue()
3333
*/
3434
public function testGetKey()
3535
{
36-
$value = new EnumFixture(EnumFixture::FOO);
36+
$value = EnumFixture::FOO();
3737
$this->assertEquals('FOO', $value->getKey());
3838
$this->assertNotEquals('BA', $value->getKey());
3939
}
4040

41-
/**
42-
* @dataProvider invalidValueProvider
43-
* @expectedException UnexpectedValueException
44-
* @expectedExceptionMessage is not part of the enum MyCLabs\Tests\Enum\EnumFixture
45-
*/
46-
public function testCreatingEnumWithInvalidValue($value)
47-
{
48-
new EnumFixture($value);
49-
}
50-
5141
/**
5242
* Contains values not existing in EnumFixture
5343
* @return array
@@ -70,9 +60,9 @@ public function testToString($expected, $enumObject)
7060

7161
public function toStringProvider() {
7262
return array(
73-
array(EnumFixture::FOO, new EnumFixture(EnumFixture::FOO)),
74-
array(EnumFixture::BAR, new EnumFixture(EnumFixture::BAR)),
75-
array((string) EnumFixture::NUMBER, new EnumFixture(EnumFixture::NUMBER)),
63+
array(EnumFixture::FOO, EnumFixture::FOO()),
64+
array(EnumFixture::BAR, EnumFixture::BAR()),
65+
array((string) EnumFixture::NUMBER, EnumFixture::NUMBER()),
7666
);
7767
}
7868

@@ -102,13 +92,13 @@ public function testValues()
10292
{
10393
$values = EnumFixture::values();
10494
$expectedValues = array(
105-
"FOO" => new EnumFixture(EnumFixture::FOO),
106-
"BAR" => new EnumFixture(EnumFixture::BAR),
107-
"NUMBER" => new EnumFixture(EnumFixture::NUMBER),
108-
"PROBLEMATIC_NUMBER" => new EnumFixture(EnumFixture::PROBLEMATIC_NUMBER),
109-
"PROBLEMATIC_NULL" => new EnumFixture(EnumFixture::PROBLEMATIC_NULL),
110-
"PROBLEMATIC_EMPTY_STRING" => new EnumFixture(EnumFixture::PROBLEMATIC_EMPTY_STRING),
111-
"PROBLEMATIC_BOOLEAN_FALSE" => new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE),
95+
"FOO" => EnumFixture::FOO(),
96+
"BAR" => EnumFixture::BAR(),
97+
"NUMBER" => EnumFixture::NUMBER(),
98+
"PROBLEMATIC_NUMBER" => EnumFixture::PROBLEMATIC_NUMBER(),
99+
"PROBLEMATIC_NULL" => EnumFixture::PROBLEMATIC_NULL(),
100+
"PROBLEMATIC_EMPTY_STRING" => EnumFixture::PROBLEMATIC_EMPTY_STRING(),
101+
"PROBLEMATIC_BOOLEAN_FALSE" => EnumFixture::PROBLEMATIC_BOOLEAN_FALSE(),
112102
);
113103

114104
$this->assertEquals($expectedValues, $values);
@@ -138,9 +128,9 @@ public function testToArray()
138128
*/
139129
public function testStaticAccess()
140130
{
141-
$this->assertEquals(new EnumFixture(EnumFixture::FOO), EnumFixture::FOO());
142-
$this->assertEquals(new EnumFixture(EnumFixture::BAR), EnumFixture::BAR());
143-
$this->assertEquals(new EnumFixture(EnumFixture::NUMBER), EnumFixture::NUMBER());
131+
$this->assertEquals(EnumFixture::FOO(), EnumFixture::FOO());
132+
$this->assertEquals(EnumFixture::BAR(), EnumFixture::BAR());
133+
$this->assertEquals(EnumFixture::NUMBER(), EnumFixture::NUMBER());
144134
}
145135

146136
/**
@@ -216,9 +206,9 @@ public function searchProvider() {
216206
*/
217207
public function testEquals()
218208
{
219-
$foo = new EnumFixture(EnumFixture::FOO);
220-
$number = new EnumFixture(EnumFixture::NUMBER);
221-
$anotherFoo = new EnumFixture(EnumFixture::FOO);
209+
$foo = EnumFixture::FOO();
210+
$number = EnumFixture::NUMBER();
211+
$anotherFoo = EnumFixture::FOO();
222212

223213
$this->assertTrue($foo->equals($foo));
224214
$this->assertFalse($foo->equals($number));
@@ -241,9 +231,9 @@ public function testSameInstance()
241231
*/
242232
public function testEqualsComparesProblematicValuesProperly()
243233
{
244-
$false = new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE);
245-
$emptyString = new EnumFixture(EnumFixture::PROBLEMATIC_EMPTY_STRING);
246-
$null = new EnumFixture(EnumFixture::PROBLEMATIC_NULL);
234+
$false = EnumFixture::PROBLEMATIC_BOOLEAN_FALSE();
235+
$emptyString = EnumFixture::PROBLEMATIC_EMPTY_STRING();
236+
$null = EnumFixture::PROBLEMATIC_NULL();
247237

248238
$this->assertTrue($false->equals($false));
249239
$this->assertFalse($false->equals($emptyString));

0 commit comments

Comments
 (0)