Skip to content

Commit f32c2a4

Browse files
committed
Make constructor private and use EnumManager for all static methods
1 parent 90981eb commit f32c2a4

File tree

3 files changed

+63
-71
lines changed

3 files changed

+63
-71
lines changed

src/Enum.php

Lines changed: 28 additions & 24 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+
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
/**
@@ -108,7 +102,7 @@ public static function values()
108102
$values = array();
109103

110104
foreach (static::toArray() as $key => $value) {
111-
$values[$key] = new static($value);
105+
$values[$key] = EnumManager::get(new static($key, $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,22 @@ 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+
foreach (static::toArray() as $constname => $constvalue) {
167+
if ($constvalue === $value) {
168+
return EnumManager::get(new static($constname, $constvalue));
169+
}
170+
}
171+
172+
return null;
173+
}
174+
171175
/**
172176
* Returns Enum by key
173177
*
@@ -177,7 +181,7 @@ public static function fromKey($name)
177181
{
178182
$array = static::toArray();
179183
if (array_key_exists($name, $array)) {
180-
return EnumManager::get(new static($array[$name]));
184+
return EnumManager::get(new static($name, $array[$name]));
181185
}
182186

183187
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 & 47 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,35 +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-
*/
44-
public function testCreatingEnumWithInvalidValue($value)
45-
{
46-
$this->setExpectedException(
47-
'\UnexpectedValueException',
48-
'Value \'' . $value . '\' is not part of the enum MyCLabs\Tests\Enum\EnumFixture'
49-
);
50-
51-
new EnumFixture($value);
52-
}
53-
54-
/**
55-
* Contains values not existing in EnumFixture
56-
* @return array
57-
*/
58-
public function invalidValueProvider() {
59-
return array(
60-
"string" => array('test'),
61-
"int" => array(1234),
62-
);
63-
}
64-
6541
/**
6642
* __toString()
6743
* @dataProvider toStringProvider
@@ -73,9 +49,9 @@ public function testToString($expected, $enumObject)
7349

7450
public function toStringProvider() {
7551
return array(
76-
array(EnumFixture::FOO, new EnumFixture(EnumFixture::FOO)),
77-
array(EnumFixture::BAR, new EnumFixture(EnumFixture::BAR)),
78-
array((string) EnumFixture::NUMBER, new EnumFixture(EnumFixture::NUMBER)),
52+
array(EnumFixture::FOO, EnumFixture::FOO()),
53+
array(EnumFixture::BAR, EnumFixture::BAR()),
54+
array((string) EnumFixture::NUMBER, EnumFixture::NUMBER()),
7955
);
8056
}
8157

@@ -105,13 +81,13 @@ public function testValues()
10581
{
10682
$values = EnumFixture::values();
10783
$expectedValues = array(
108-
"FOO" => new EnumFixture(EnumFixture::FOO),
109-
"BAR" => new EnumFixture(EnumFixture::BAR),
110-
"NUMBER" => new EnumFixture(EnumFixture::NUMBER),
111-
"PROBLEMATIC_NUMBER" => new EnumFixture(EnumFixture::PROBLEMATIC_NUMBER),
112-
"PROBLEMATIC_NULL" => new EnumFixture(EnumFixture::PROBLEMATIC_NULL),
113-
"PROBLEMATIC_EMPTY_STRING" => new EnumFixture(EnumFixture::PROBLEMATIC_EMPTY_STRING),
114-
"PROBLEMATIC_BOOLEAN_FALSE" => new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE),
84+
"FOO" => EnumFixture::FOO(),
85+
"BAR" => EnumFixture::BAR(),
86+
"NUMBER" => EnumFixture::NUMBER(),
87+
"PROBLEMATIC_NUMBER" => EnumFixture::PROBLEMATIC_NUMBER(),
88+
"PROBLEMATIC_NULL" => EnumFixture::PROBLEMATIC_NULL(),
89+
"PROBLEMATIC_EMPTY_STRING" => EnumFixture::PROBLEMATIC_EMPTY_STRING(),
90+
"PROBLEMATIC_BOOLEAN_FALSE" => EnumFixture::PROBLEMATIC_BOOLEAN_FALSE(),
11591
);
11692

11793
$this->assertEquals($expectedValues, $values);
@@ -141,9 +117,9 @@ public function testToArray()
141117
*/
142118
public function testStaticAccess()
143119
{
144-
$this->assertEquals(new EnumFixture(EnumFixture::FOO), EnumFixture::FOO());
145-
$this->assertEquals(new EnumFixture(EnumFixture::BAR), EnumFixture::BAR());
146-
$this->assertEquals(new EnumFixture(EnumFixture::NUMBER), EnumFixture::NUMBER());
120+
$this->assertEquals(EnumFixture::FOO(), EnumFixture::FOO());
121+
$this->assertEquals(EnumFixture::BAR(), EnumFixture::BAR());
122+
$this->assertEquals(EnumFixture::NUMBER(), EnumFixture::NUMBER());
147123
}
148124

149125
/**
@@ -219,9 +195,9 @@ public function searchProvider() {
219195
*/
220196
public function testEquals()
221197
{
222-
$foo = new EnumFixture(EnumFixture::FOO);
223-
$number = new EnumFixture(EnumFixture::NUMBER);
224-
$anotherFoo = new EnumFixture(EnumFixture::FOO);
198+
$foo = EnumFixture::FOO();
199+
$number = EnumFixture::NUMBER();
200+
$anotherFoo = EnumFixture::FOO();
225201

226202
$this->assertTrue($foo->equals($foo));
227203
$this->assertFalse($foo->equals($number));
@@ -244,9 +220,9 @@ public function testSameInstance()
244220
*/
245221
public function testEqualsComparesProblematicValuesProperly()
246222
{
247-
$false = new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE);
248-
$emptyString = new EnumFixture(EnumFixture::PROBLEMATIC_EMPTY_STRING);
249-
$null = new EnumFixture(EnumFixture::PROBLEMATIC_NULL);
223+
$false = EnumFixture::PROBLEMATIC_BOOLEAN_FALSE();
224+
$emptyString = EnumFixture::PROBLEMATIC_EMPTY_STRING();
225+
$null = EnumFixture::PROBLEMATIC_NULL();
250226

251227
$this->assertTrue($false->equals($false));
252228
$this->assertFalse($false->equals($emptyString));

0 commit comments

Comments
 (0)