Skip to content

Commit e01c1d0

Browse files
committed
Merge pull request #5 from danielcosta/feature/psr-4-and-more-features
Added new methods and changed source structure to be PSR4 compatible
2 parents 27e3b22 + b3280d2 commit e01c1d0

File tree

7 files changed

+190
-37
lines changed

7 files changed

+190
-37
lines changed

.travis.yml

+7-3
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/

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ function setAction(Action $action) {
5656
- `__construct()` The constructor checks that the value exist in the enum
5757
- `__toString()` You can `echo $myValue`, it will display the enum value (value of the constant)
5858
- `getValue()` Returns the current value of the enum
59-
- `toArray()` (static) Returns an array of all possible values (constant name in key, constant value in value)
59+
- `getKey()` Returns the key of the current value on Enum
60+
- `keys()` (@static) Returns the names (keys) of all constants in the Enum class
61+
- `toArray()` (@static) method Returns all possible values as an array (constant name in key, constant value in value)
62+
- `isValid()` (@static) Check if tested value is valid on enum set
63+
- `isValidKey()` (@static) Check if tested key is valid on enum set
64+
- `search()` Return key for searched value
6065

6166
### Static methods
6267

composer.json

+14-1
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

+2-4
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

+80-8
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::toArray())) {
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,80 @@ 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::toArray());
83+
}
84+
6085
/**
6186
* Returns all possible values as an array
87+
*
6288
* @return array Constant name in key, constant value in value
6389
*/
6490
public static function toArray()
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+
* Check if is valid enum value
103+
*
104+
* @static
105+
*
106+
* @param $value
107+
*
108+
* @return bool
109+
*/
110+
public static function isValid($value)
111+
{
112+
return in_array($value, self::toArray());
113+
}
114+
115+
/**
116+
* Check if is valid enum key
117+
*
118+
* @static
119+
*
120+
* @param $key
121+
*
122+
* @return bool
123+
*/
124+
public static function isValidKey($key)
125+
{
126+
return in_array($key, self::keys());
127+
}
128+
129+
/**
130+
* Return key for value
131+
*
132+
* @static
133+
*
134+
* @param $value
135+
*
136+
* @return mixed
137+
*/
138+
public static function search($value)
139+
{
140+
return array_search($value, array_combine(self::keys(), self::toArray()));
72141
}
73142

74143
/**
75144
* Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant
145+
*
76146
* @param string $name
77147
* @param array $arguments
148+
*
78149
* @return static
79150
* @throws \BadMethodCallException
80151
*/
@@ -83,6 +154,7 @@ public static function __callStatic($name, $arguments)
83154
if (defined("static::$name")) {
84155
return new static(constant("static::$name"));
85156
}
157+
86158
throw new \BadMethodCallException("No static method or enum constant '$name' in class " . get_called_class());
87159
}
88160
}

tests/EnumFixture.php

+23
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

+58-20
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
}
@@ -69,6 +78,21 @@ public function testToString()
6978
$this->assertEquals((string) EnumFixture::NUMBER, (string) $value);
7079
}
7180

81+
/**
82+
* keys()
83+
*/
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+
7296
/**
7397
* toArray()
7498
*/
@@ -96,24 +120,38 @@ public function testStaticAccess()
96120

97121
/**
98122
* @expectedException \BadMethodCallException
99-
* @expectedExceptionMessage No static method or enum constant 'UNKNOWN' in class UnitTest\MyCLabs\Enum\Enum\EnumFixture
123+
* @expectedExceptionMessage No static method or enum constant 'UNKNOWN' in class
124+
* UnitTest\MyCLabs\Enum\Enum\EnumFixture
100125
*/
101126
public function testBadStaticAccess()
102127
{
103128
EnumFixture::UNKNOWN();
104129
}
105-
}
106130

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;
131+
/**
132+
* isValid()
133+
*/
134+
public function testIsValid()
135+
{
136+
$this->assertTrue(EnumFixture::isValid('foo'));
137+
$this->assertFalse(EnumFixture::isValid('baz'));
138+
}
139+
140+
/**
141+
* ssValidKey()
142+
*/
143+
public function testIsValidKey()
144+
{
145+
$this->assertTrue(EnumFixture::isValidKey('FOO'));
146+
$this->assertFalse(EnumFixture::isValidKey('BAZ'));
147+
}
148+
149+
/**
150+
* search()
151+
*/
152+
public function testSearch()
153+
{
154+
$this->assertEquals('FOO', EnumFixture::search('foo'));
155+
$this->assertNotEquals('FOO', EnumFixture::isValidKey('baz'));
156+
}
119157
}

0 commit comments

Comments
 (0)