Skip to content

Commit b378c30

Browse files
author
Marc Bennewitz
committed
Merge pull request #15 from marc-mabe/namespaces
2 parents f687915 + 260c2c6 commit b378c30

File tree

9 files changed

+86
-49
lines changed

9 files changed

+86
-49
lines changed

README.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[![Latest Unstable Version](https://poser.pugx.org/marc-mabe/php-enum/v/unstable.png)](https://packagist.org/packages/marc-mabe/php-enum)
77
[![Dependency Status](https://www.versioneye.com/php/marc-mabe:php-enum/dev-master/badge.png)](https://www.versioneye.com/php/marc-mabe:php-enum/dev-master)
88

9-
This is a native PHP implementation to add enumeration support to PHP 5.x.
9+
This is a native PHP implementation to add enumeration support to PHP >= 5.3
1010
It's an abstract class that needs to be extended to use it.
1111

1212

@@ -32,7 +32,7 @@ It's an abstract class that needs to be extended to use it.
3232

3333
# API
3434

35-
abstract class MabeEnum_Enum
35+
abstract class MabeEnum\Enum
3636
{
3737
/**
3838
* Constructor
@@ -80,7 +80,7 @@ It's an abstract class that needs to be extended to use it.
8080
* @param string $name The name of the constant to instantiate
8181
* @param array $args This should be an empty array (no arguments)
8282
* @throws BadMethodCallException If the called method hasn't the same name as a constant
83-
* @return MabeEnum_Enum The instantiated enum
83+
* @return MabeEnum\Enum The instantiated enum
8484
*/
8585
final public static __callStatic($name, array $args);
8686
}
@@ -129,7 +129,9 @@ It's an abstract class that needs to be extended to use it.
129129

130130
## The way of php-enum:
131131

132-
class UserStatusEnum extends MabeEnum_Enum
132+
use MabeEnum\Enum;
133+
134+
class UserStatusEnum extends Enum
133135
{
134136
const INACTIVE = 0;
135137
const ACTIVE = 1;
@@ -149,23 +151,23 @@ It's an abstract class that needs to be extended to use it.
149151
{
150152
if (!$this->status) {
151153
// init default status
152-
$this->status = new UserStatusEnum(UserStatusEnum::INACTIVE);
154+
$this->status = UserStatusEnum::INACTIVE();
153155
}
154156
return $this->status;
155157
}
156158
}
157159

158160
$user = new User();
159161
echo 'Default user status: ' . $user->getStatus() . '(' . $user->getStatus()->getValue() . ')' . PHP_EOL;
160-
$user->setStatus(new UserStatusEnum(UserStatusEnum::ACTIVE));
162+
$user->setStatus(UserStatusEnum::ACTIVE());
161163
echo 'Changed user status: ' . $user->getStatus() . '(' . $user->getStatus()->getValue() . ')' . PHP_EOL;
162164

163165
**PRINTS**
164166

165167
Default user status: INACTIVE (0)
166168
Changed user status: ACTIVE (1)
167169

168-
* Validation will be already done on basic class ```MabeEnum_Enum```
170+
* Validation will be already done on basic class ```MabeEnum\Enum```
169171
* Using type-hint makes arguments save
170172
* Human readable name of a value is simple accessable
171173

@@ -194,7 +196,9 @@ and extract it.
194196
This example defines the constant ```ONE``` with value ```1``` as default
195197
value.
196198

197-
class MyEnumWithDefaultValue extends MabeEnum_Enum
199+
use MabeEnum\Enum;
200+
201+
class MyEnumWithDefaultValue extends Enum
198202
{
199203
const ONE = 1;
200204
const TWO = 2;
@@ -209,7 +213,9 @@ value.
209213

210214
It's also possible to extend other enumerations.
211215

212-
class MyEnum extends MabeEnum_Enum
216+
use MabeEnum\Enum;
217+
218+
class MyEnum extends Enum
213219
{
214220
const ONE = 1;
215221
const TWO = 2;
@@ -222,10 +228,12 @@ It's also possible to extend other enumerations.
222228

223229
## Simplified instantiation
224230

225-
With PHP >= 5.3 it possible to call one of the defined constants like a method
231+
It's possible to call one of the defined constants like a method
226232
and you will get the instantiated enum as a result.
227233

228-
class MyEnum extends MabeEnum_Enum
234+
use MabeEnum\Enum;
235+
236+
class MyEnum extends Enum
229237
{
230238
const ONE = 1;
231239
const TWO = 2;

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
},
2626
"autoload": {
2727
"psr-0": {
28-
"MabeEnum_": "src/"
28+
"MabeEnum\\": "src/"
2929
}
3030
}
3131
}

src/MabeEnum/Enum.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
<?php
22

3+
namespace MabeEnum;
4+
5+
use ReflectionClass;
6+
use InvalidArgumentException;
7+
use LogicException;
8+
use BadMethodCallException;
9+
310
/**
411
* Class to implement enumerations for PHP 5 (without SplEnum)
512
*
613
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
714
* @copyright Copyright (c) 2012 Marc Bennewitz
815
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
916
*/
10-
abstract class MabeEnum_Enum
17+
abstract class Enum
1118
{
1219
/**
1320
* The selected value
@@ -29,7 +36,7 @@ abstract class MabeEnum_Enum
2936

3037
/**
3138
* Already instantiated enums
32-
* @param array ["$class.$value" => MabeEnum_Enum, ...]
39+
* @param array ["$class.$value" => MabeEnum\Enum, ...]
3340
*/
3441
private static $instances = array();
3542

@@ -117,7 +124,7 @@ final public function getOrdinal()
117124
* Get an enum of the given value
118125
*
119126
* @param scalar $value
120-
* @return MabeEnum_Enum
127+
* @return Enum
121128
* @throws InvalidArgumentException On an unknwon or invalid value
122129
* @throws LogicException On ambiguous constant values
123130
*/

tests/Bootstrap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
function autoload($className)
1313
{
14-
$test = dirname(__FILE__) . '/../src/' . str_replace('_', '/', $className) . '.php';
14+
$test = dirname(__FILE__) . '/../src/' . str_replace('\\', '/', $className) . '.php';
1515
if (file_exists($test)) {
1616
require $test;
1717
}
18-
$test = dirname(__FILE__) . '/' . str_replace('_', '/', $className) . '.php';
18+
$test = dirname(__FILE__) . '/' . str_replace('\\', '/', $className) . '.php';
1919
if (file_exists($test)) {
2020
require $test;
2121
}

tests/MabeEnumTest/EnumTest.php

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,117 @@
11
<?php
22

3+
namespace MabeEnumTest;
4+
5+
use MabeEnumTest\TestAsset\EnumWithoutDefaultValue;
6+
use MabeEnumTest\TestAsset\EnumInheritance;
7+
use MabeEnumTest\TestAsset\EnumAmbiguous;
8+
use PHPUnit_Framework_TestCase as TestCase;
9+
use ReflectionClass;
10+
311
/**
4-
* Unit tests for the class MabeEnum_Enum
12+
* Unit tests for the class MabeEnum\Enum
513
*
614
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
715
* @copyright Copyright (c) 2012 Marc Bennewitz
816
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
917
*/
10-
class MabeEnumTest_EnumTest extends PHPUnit_Framework_TestCase
18+
class EnumTest extends TestCase
1119
{
1220
public function testGetNameReturnsConstantNameOfCurrentValue()
1321
{
14-
$enum = MabeEnumTest_TestAsset_EnumWithoutDefaultValue::get(MabeEnumTest_TestAsset_EnumWithoutDefaultValue::ONE);
22+
$enum = EnumWithoutDefaultValue::get(EnumWithoutDefaultValue::ONE);
1523
$this->assertSame('ONE', $enum->getName());
1624
}
1725

1826
public function testToStringMagicMethodReturnsValueAsString()
1927
{
20-
$enum = MabeEnumTest_TestAsset_EnumWithoutDefaultValue::get(MabeEnumTest_TestAsset_EnumWithoutDefaultValue::ONE);
28+
$enum = EnumWithoutDefaultValue::get(EnumWithoutDefaultValue::ONE);
2129
$this->assertSame('1', $enum->__toString());
2230
}
2331

2432
public function testEnumInheritance()
2533
{
26-
$enum = MabeEnumTest_TestAsset_EnumInheritance::get(MabeEnumTest_TestAsset_EnumInheritance::ONE);
34+
$enum = EnumInheritance::get(EnumInheritance::ONE);
2735
$this->assertSame(array(
2836
'ONE' => 1,
2937
'TWO' => 2,
3038
'INHERITANCE' => 'Inheritance'
3139
), $enum::getConstants());
32-
$this->assertSame(MabeEnumTest_TestAsset_EnumInheritance::ONE, $enum->getValue());
40+
$this->assertSame(EnumInheritance::ONE, $enum->getValue());
3341
$this->assertSame(0, $enum->getOrdinal());
3442

35-
$enum = MabeEnumTest_TestAsset_EnumInheritance::get(MabeEnumTest_TestAsset_EnumInheritance::INHERITANCE);
36-
$this->assertSame(MabeEnumTest_TestAsset_EnumInheritance::INHERITANCE, $enum->getValue());
43+
$enum = EnumInheritance::get(EnumInheritance::INHERITANCE);
44+
$this->assertSame(EnumInheritance::INHERITANCE, $enum->getValue());
3745
$this->assertSame(2, $enum->getOrdinal());
3846
}
3947

4048
public function testConstructorStrictValue()
4149
{
42-
$enum = MabeEnumTest_TestAsset_EnumWithoutDefaultValue::get(MabeEnumTest_TestAsset_EnumWithoutDefaultValue::ONE);
50+
$enum = EnumWithoutDefaultValue::get(EnumWithoutDefaultValue::ONE);
4351
$this->assertSame(1, $enum->getValue());
4452
$this->assertSame(0, $enum->getOrdinal());
4553
}
4654

4755
public function testConstuctorNonStrictValue()
4856
{
49-
$enum = MabeEnumTest_TestAsset_EnumWithoutDefaultValue::get((string)MabeEnumTest_TestAsset_EnumWithoutDefaultValue::TWO);
57+
$enum = EnumWithoutDefaultValue::get((string)EnumWithoutDefaultValue::TWO);
5058
$this->assertSame(2, $enum->getValue());
5159
$this->assertSame(1, $enum->getOrdinal());
5260
}
5361

5462
public function testConstructorInvalidValueThrowsInvalidArgumentException()
5563
{
5664
$this->setExpectedException('InvalidArgumentException');
57-
MabeEnumTest_TestAsset_EnumWithoutDefaultValue::get('unknown');
65+
EnumWithoutDefaultValue::get('unknown');
5866
}
5967

6068
public function testCallingGetOrdinalTwoTimesWillResultTheSameValue()
6169
{
62-
$enum = MabeEnumTest_TestAsset_EnumWithoutDefaultValue::get(MabeEnumTest_TestAsset_EnumWithoutDefaultValue::TWO);
70+
$enum = EnumWithoutDefaultValue::get(EnumWithoutDefaultValue::TWO);
6371
$this->assertSame(1, $enum->getOrdinal());
6472
$this->assertSame(1, $enum->getOrdinal());
6573
}
6674

6775
public function testInstantiateUsingMagicMethod()
6876
{
69-
$enum = MabeEnumTest_TestAsset_EnumInheritance::ONE();
70-
$this->assertInstanceOf('MabeEnumTest_TestAsset_EnumInheritance', $enum);
71-
$this->assertSame(MabeEnumTest_TestAsset_EnumInheritance::ONE, $enum->getValue());
77+
$enum = EnumInheritance::ONE();
78+
$this->assertInstanceOf('MabeEnumTest\TestAsset\EnumInheritance', $enum);
79+
$this->assertSame(EnumInheritance::ONE, $enum->getValue());
7280
}
7381

7482
public function testInstantiateUsingMagicMethodThrowsBadMethodCallException()
7583
{
7684
$this->setExpectedException('BadMethodCallException');
77-
MabeEnumTest_TestAsset_EnumInheritance::UNKNOWN();
85+
EnumInheritance::UNKNOWN();
7886
}
7987

8088
public function testAmbuguousConstantsThrowsLogicException()
8189
{
8290
$this->setExpectedException('LogicException');
83-
MabeEnumTest_TestAsset_EnumAmbiguous::get(MabeEnumTest_TestAsset_EnumAmbiguous::AMBIGUOUS1);
91+
EnumAmbiguous::get(EnumAmbiguous::AMBIGUOUS1);
8492
}
8593

8694
public function testSingleton()
8795
{
88-
$enum1 = MabeEnumTest_TestAsset_EnumWithoutDefaultValue::get(MabeEnumTest_TestAsset_EnumWithoutDefaultValue::ONE);
89-
$enum2 = MabeEnumTest_TestAsset_EnumWithoutDefaultValue::ONE();
96+
$enum1 = EnumWithoutDefaultValue::get(EnumWithoutDefaultValue::ONE);
97+
$enum2 = EnumWithoutDefaultValue::ONE();
9098
$this->assertSame($enum1, $enum2);
9199
}
92100

93101
public function testClear()
94102
{
95-
$enum1 = MabeEnumTest_TestAsset_EnumWithoutDefaultValue::ONE();
96-
MabeEnumTest_TestAsset_EnumWithoutDefaultValue::clear();
97-
$enum2 = MabeEnumTest_TestAsset_EnumWithoutDefaultValue::ONE();
98-
$enum3 = MabeEnumTest_TestAsset_EnumWithoutDefaultValue::ONE();
103+
$enum1 = EnumWithoutDefaultValue::ONE();
104+
EnumWithoutDefaultValue::clear();
105+
$enum2 = EnumWithoutDefaultValue::ONE();
106+
$enum3 = EnumWithoutDefaultValue::ONE();
99107

100108
$this->assertNotSame($enum1, $enum2);
101109
$this->assertSame($enum2, $enum3);
102110
}
103111

104112
public function testCloneNotCallableAndThrowsLogicException()
105113
{
106-
$enum = MabeEnumTest_TestAsset_EnumWithoutDefaultValue::ONE();
114+
$enum = EnumWithoutDefaultValue::ONE();
107115

108116
$reflectionClass = new ReflectionClass($enum);
109117
$reflectionMethod = $reflectionClass->getMethod('__clone');
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<?php
22

3+
namespace MabeEnumTest\TestAsset;
4+
5+
use MabeEnum\Enum;
6+
37
/**
4-
* Unit tests for the class MabeEnum_Enum
8+
* Unit tests for the class MabeEnum\Enum
59
*
610
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
711
* @copyright Copyright (c) 2012 Marc Bennewitz
812
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
913
*/
10-
class MabeEnumTest_TestAsset_EmptyEnum extends MabeEnum_Enum
14+
class EmptyEnum extends Enum
1115
{
1216
}

tests/MabeEnumTest/TestAsset/EnumAmbiguous.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
<?php
22

3+
namespace MabeEnumTest\TestAsset;
4+
5+
use MabeEnum\Enum;
6+
37
/**
4-
* Unit tests for the class MabeEnum_Enum
8+
* Unit tests for the class MabeEnum\Enum
59
*
610
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
711
* @copyright Copyright (c) 2012 Marc Bennewitz
812
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
913
*/
10-
class MabeEnumTest_TestAsset_EnumAmbiguous extends MabeEnum_Enum
14+
class EnumAmbiguous extends Enum
1115
{
1216
const UNIQUE1 = 'unique1';
1317
const AMBIGUOUS1 = 1;
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?php
22

3+
namespace MabeEnumTest\TestAsset;
4+
35
/**
4-
* Unit tests for the class MabeEnum_Enum
6+
* Unit tests for the class MabeEnum\Enum
57
*
68
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
79
* @copyright Copyright (c) 2012 Marc Bennewitz
810
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
911
*/
10-
class MabeEnumTest_TestAsset_EnumInheritance extends MabeEnumTest_TestAsset_EnumWithoutDefaultValue
12+
class EnumInheritance extends EnumWithoutDefaultValue
1113
{
1214
const INHERITANCE = 'Inheritance';
1315
}

tests/MabeEnumTest/TestAsset/EnumWithoutDefaultValue.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
<?php
22

3+
namespace MabeEnumTest\TestAsset;
4+
5+
use MabeEnum\Enum;
6+
37
/**
4-
* Unit tests for the class MabeEnum_Enum
8+
* Unit tests for the class MabeEnum\Enum
59
*
610
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
711
* @copyright Copyright (c) 2012 Marc Bennewitz
812
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
913
*/
10-
class MabeEnumTest_TestAsset_EnumWithoutDefaultValue extends MabeEnum_Enum
14+
class EnumWithoutDefaultValue extends Enum
1115
{
1216
const ONE = 1;
1317
const TWO = 2;

0 commit comments

Comments
 (0)