Skip to content

Commit 37a5b1f

Browse files
committed
Added new methods and changed source structure to be PSR4 compatible
- PSR4 compatible package - new getKey() method (returns the key of the current value on Enum) - new keys() method (returns the names (keys) of all constants in the Enum class) - moved toArray() to values() method (returns all possible values as an array) - flagged toArray() as deprecated for future removal - new isValid() static method (check if tested value is valid on enum set) - new isValidKey() static method (check if tested key is valid on enum set) - new search() method (return key for searched value)
1 parent 27e3b22 commit 37a5b1f

File tree

6 files changed

+207
-40
lines changed

6 files changed

+207
-40
lines changed

.travis.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ php:
44
- 5.3
55
- 5.4
66
- 5.5
7+
- 5.6
8+
- hhvm
79

810
before_script:
9-
- wget http://getcomposer.org/composer.phar
10-
- php composer.phar install
11+
- composer self-update
12+
- composer install --no-interaction --dev
1113

12-
script: phpunit --configuration phpunit.xml tests
14+
script:
15+
- vendor/bin/phpunit
16+
- vendor/bin/phpcs --standard=PSR2 ./src/

composer.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,22 @@
55
"keywords": ["enum"],
66
"homepage": "http://github.com/myclabs/php-enum",
77
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "PHP Enum contributors",
11+
"homepage": "https://github.com/myclabs/php-enum/graphs/contributors"
12+
}
13+
],
814
"autoload": {
9-
"psr-0": {
15+
"psr-4": {
1016
"MyCLabs\\Enum\\": "src/"
1117
}
18+
},
19+
"require": {
20+
"php": ">=5.3"
21+
},
22+
"require-dev": {
23+
"phpunit/phpunit": "4.*",
24+
"squizlabs/php_codesniffer": "1.*"
1225
}
1326
}

phpunit.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212
stopOnFailure="false"
1313
syntaxCheck="false"
1414
bootstrap="./vendor/autoload.php">
15-
1615
<testsuites>
17-
<testsuite name="Enum Test Suite">
18-
<directory>./tests</directory>
16+
<testsuite name="PHP Enum Test Suite">
17+
<directory suffix=".php">./tests</directory>
1918
</testsuite>
2019
</testsuites>
21-
2220
</phpunit>

src/MyCLabs/Enum/Enum.php renamed to src/Enum.php

Lines changed: 92 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,38 @@
1111
*
1212
* Create an enum by implementing this class and adding class constants.
1313
*
14+
* @package MyCLabs\Enum
1415
* @author Matthieu Napoli <[email protected]>
1516
*/
1617
abstract class Enum
1718
{
1819
/**
1920
* Enum value
21+
*
2022
* @var mixed
2123
*/
2224
protected $value;
2325

2426
/**
2527
* Store existing constants in a static cache per object.
28+
*
2629
* @var array
2730
*/
28-
private static $constantsCache = array();
31+
private static $cache = array();
2932

3033
/**
3134
* Creates a new value of some type
35+
*
3236
* @param mixed $value
37+
*
3338
* @throws \UnexpectedValueException if incompatible type is given.
3439
*/
3540
public function __construct($value)
3641
{
37-
$possibleValues = self::toArray();
38-
if (! in_array($value, $possibleValues)) {
42+
if (!in_array($value, self::values())) {
3943
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . get_called_class());
4044
}
45+
4146
$this->value = $value;
4247
}
4348

@@ -49,6 +54,16 @@ public function getValue()
4954
return $this->value;
5055
}
5156

57+
/**
58+
* Returns the key of the current value on Enum
59+
*
60+
* @return mixed
61+
*/
62+
public function getKey()
63+
{
64+
return self::search($this->value);
65+
}
66+
5267
/**
5368
* @return string
5469
*/
@@ -57,24 +72,91 @@ public function __toString()
5772
return (string) $this->value;
5873
}
5974

75+
/**
76+
* Returns the names (keys) of all constants in the Enum class
77+
*
78+
* @return array
79+
*/
80+
public static function keys()
81+
{
82+
return array_keys(static::values());
83+
}
84+
6085
/**
6186
* Returns all possible values as an array
87+
*
6288
* @return array Constant name in key, constant value in value
6389
*/
64-
public static function toArray()
90+
public static function values()
6591
{
66-
$calledClass = get_called_class();
67-
if(!array_key_exists($calledClass, self::$constantsCache)) {
68-
$reflection = new \ReflectionClass($calledClass);
69-
self::$constantsCache[$calledClass] = $reflection->getConstants();
92+
$class = get_called_class();
93+
if (!array_key_exists($class, self::$cache)) {
94+
$reflection = new \ReflectionClass($class);
95+
self::$cache[$class] = $reflection->getConstants();
7096
}
71-
return self::$constantsCache[$calledClass];
97+
98+
return self::$cache[$class];
99+
}
100+
101+
/**
102+
* An alias method for values()
103+
*
104+
* @return array
105+
* @deprecated
106+
*/
107+
public static function toArray()
108+
{
109+
return self::values();
110+
}
111+
112+
/**
113+
* Check if is valid enum value
114+
*
115+
* @static
116+
*
117+
* @param $value
118+
*
119+
* @return bool
120+
*/
121+
public static function isValid($value)
122+
{
123+
return in_array($value, self::values());
124+
}
125+
126+
/**
127+
* Check if is valid enum key
128+
*
129+
* @static
130+
*
131+
* @param $key
132+
*
133+
* @return bool
134+
*/
135+
public static function isValidKey($key)
136+
{
137+
return in_array($key, self::keys());
138+
}
139+
140+
/**
141+
* Return key for value
142+
*
143+
* @static
144+
*
145+
* @param $value
146+
*
147+
* @return mixed
148+
*/
149+
public static function search($value)
150+
{
151+
return array_search($value, array_combine(self::keys(), self::values()));
72152
}
73153

74154
/**
75155
* Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant
156+
*
76157
* @param string $name
77158
* @param array $arguments
159+
*
78160
* @return static
79161
* @throws \BadMethodCallException
80162
*/
@@ -83,6 +165,7 @@ public static function __callStatic($name, $arguments)
83165
if (defined("static::$name")) {
84166
return new static(constant("static::$name"));
85167
}
168+
86169
throw new \BadMethodCallException("No static method or enum constant '$name' in class " . get_called_class());
87170
}
88171
}

tests/EnumFixture.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* @link http://github.com/myclabs/php-enum
4+
* @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
5+
*/
6+
7+
namespace MyCLabs\Enum;
8+
9+
/**
10+
* Class EnumFixture
11+
*
12+
* @package MyCLabs\Enum
13+
* @method static EnumFixture FOO()
14+
* @method static EnumFixture BAR()
15+
* @method static EnumFixture NUMBER()
16+
* @author Daniel Costa <[email protected]
17+
*/
18+
class EnumFixture extends Enum
19+
{
20+
const FOO = "foo";
21+
const BAR = "bar";
22+
const NUMBER = 42;
23+
}

tests/EnumTest.php

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
* @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
55
*/
66

7-
namespace UnitTest\MyCLabs\Enum\Enum;
8-
9-
use MyCLabs\Enum\Enum;
7+
namespace MyCLabs\Enum;
108

119
/**
1210
* Enum test
1311
*
12+
* @package MyCLabs\Enum
1413
* @author Matthieu Napoli <[email protected]>
1514
*/
1615
class EnumTest extends \PHPUnit_Framework_TestCase
@@ -30,26 +29,36 @@ public function testGetValue()
3029
$this->assertEquals(EnumFixture::NUMBER, $value->getValue());
3130
}
3231

32+
/**
33+
* getKey()
34+
*/
35+
public function testGetKey()
36+
{
37+
$value = new EnumFixture(EnumFixture::FOO);
38+
$this->assertEquals('FOO', $value->getKey());
39+
$this->assertNotEquals('BA', $value->getKey());
40+
}
41+
3342
/**
3443
* @expectedException \UnexpectedValueException
3544
*/
36-
public function testInvalidValue1()
45+
public function testInvalidValueString()
3746
{
3847
new EnumFixture("test");
3948
}
4049

4150
/**
4251
* @expectedException \UnexpectedValueException
4352
*/
44-
public function testInvalidValue2()
53+
public function testInvalidValueInt()
4554
{
4655
new EnumFixture(1234);
4756
}
4857

4958
/**
5059
* @expectedException \UnexpectedValueException
5160
*/
52-
public function testInvalidValue3()
61+
public function testInvalidValueEmpty()
5362
{
5463
new EnumFixture(null);
5564
}
@@ -70,11 +79,26 @@ public function testToString()
7079
}
7180

7281
/**
73-
* toArray()
82+
* keys()
7483
*/
75-
public function testToArray()
84+
public function testKeys()
85+
{
86+
$values = EnumFixture::keys();
87+
$this->assertInternalType("array", $values);
88+
$expectedValues = array(
89+
"FOO",
90+
"BAR",
91+
"NUMBER",
92+
);
93+
$this->assertEquals($expectedValues, $values);
94+
}
95+
96+
/**
97+
* values()
98+
*/
99+
public function testValues()
76100
{
77-
$values = EnumFixture::toArray();
101+
$values = EnumFixture::values();
78102
$this->assertInternalType("array", $values);
79103
$expectedValues = array(
80104
"FOO" => EnumFixture::FOO,
@@ -84,6 +108,14 @@ public function testToArray()
84108
$this->assertEquals($expectedValues, $values);
85109
}
86110

111+
/**
112+
* toArray()
113+
*/
114+
public function testToArray()
115+
{
116+
$this->assertEquals(EnumFixture::values(), EnumFixture::toArray());
117+
}
118+
87119
/**
88120
* __callStatic()
89121
*/
@@ -96,24 +128,38 @@ public function testStaticAccess()
96128

97129
/**
98130
* @expectedException \BadMethodCallException
99-
* @expectedExceptionMessage No static method or enum constant 'UNKNOWN' in class UnitTest\MyCLabs\Enum\Enum\EnumFixture
131+
* @expectedExceptionMessage No static method or enum constant 'UNKNOWN' in class
132+
* UnitTest\MyCLabs\Enum\Enum\EnumFixture
100133
*/
101134
public function testBadStaticAccess()
102135
{
103136
EnumFixture::UNKNOWN();
104137
}
105-
}
106138

107-
/**
108-
* Fixture class
109-
*
110-
* @method static EnumFixture FOO()
111-
* @method static EnumFixture BAR()
112-
* @method static EnumFixture NUMBER()
113-
*/
114-
class EnumFixture extends Enum
115-
{
116-
const FOO = "foo";
117-
const BAR = "bar";
118-
const NUMBER = 42;
139+
/**
140+
* isValid()
141+
*/
142+
public function testIsValid()
143+
{
144+
$this->assertTrue(EnumFixture::isValid('foo'));
145+
$this->assertFalse(EnumFixture::isValid('baz'));
146+
}
147+
148+
/**
149+
* ssValidKey()
150+
*/
151+
public function testIsValidKey()
152+
{
153+
$this->assertTrue(EnumFixture::isValidKey('FOO'));
154+
$this->assertFalse(EnumFixture::isValidKey('BAZ'));
155+
}
156+
157+
/**
158+
* search()
159+
*/
160+
public function testSearch()
161+
{
162+
$this->assertEquals('FOO', EnumFixture::search('foo'));
163+
$this->assertNotEquals('FOO', EnumFixture::isValidKey('baz'));
164+
}
119165
}

0 commit comments

Comments
 (0)