Skip to content

Commit 24657b6

Browse files
committed
Now the $value property is private that minifies checks if the vaue is valid and helps performance
updated README
1 parent 51821ea commit 24657b6

File tree

5 files changed

+90
-120
lines changed

5 files changed

+90
-120
lines changed

README.md

Lines changed: 69 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,61 @@ It's an abstract class that needs to be extended to use it.
2828

2929
* It's not build-in PHP and requires pecl extension
3030
* SplEnum is too much magic under the hod
31-
* SplEnum hasn't strict comparison
3231

3332

3433
# API
3534

36-
MabeEnum_Enum
35+
abstract class MabeEnum_Enum
3736
{
38-
protected $value = null;
39-
public function __construct($value = null);
37+
/**
38+
* Constructor
39+
* @param scalar $value The enum value to select
40+
* @throws InvalidArgumentException On unknwon value
41+
*/
42+
public function __construct($value);
43+
44+
/**
45+
* Returns an assoc array of defined constant names and the values
46+
* @return array
47+
*/
4048
final public function getConstants();
41-
final public function setValue($value);
42-
final public function getValue()
43-
final public function setName($name);
49+
50+
/**
51+
* Get the selected value
52+
* @return scalar
53+
*/
54+
final public function getValue();
55+
56+
/**
57+
* Get the constant name of the selected value
58+
* @return string
59+
*/
4460
final public function getName();
45-
final public function __toString(); // Alias of getValue()
61+
62+
/**
63+
* Get the ordinal number of the selected value
64+
* @return int
65+
*/
66+
final public function getOrdinal();
67+
68+
/**
69+
* Get the selected value as string
70+
* (This will be called automatically on converting into a string)
71+
* @return string
72+
*/
73+
final public function __toString();
74+
75+
/**
76+
* Instantiate a new enum were the selected value
77+
* is the constant name of the called method name
78+
* (This will be called automatically on calling static method)
79+
* NOTE: THIS WILL WORK FOR PHP >= 5.3 ONLY
80+
* @param string $name The name of the constant to instantiate
81+
* @param array $args This should be an empty array (no arguments)
82+
* @throws BadMethodCallException If the called method hasn't the same name as a constant
83+
* @return MabeEnum_Enum The instantiated enum
84+
*/
85+
final public static __callStatic($name, array $args);
4686
}
4787

4888

@@ -94,30 +134,27 @@ It's an abstract class that needs to be extended to use it.
94134
const INACTIVE = 0;
95135
const ACTIVE = 1;
96136
const DELETED = 2;
97-
98-
// default value
99-
protected $value = self::INACTIVE;
100137
}
101-
138+
102139
class User
103140
{
104141
protected $status;
105-
142+
106143
public function setStatus(UserStatusEnum $status)
107144
{
108145
$this->status = $status;
109146
}
110-
147+
111148
public function getStatus()
112149
{
113150
if (!$this->status) {
114151
// init default status
115-
$this->status = new UserStatusEnum();
152+
$this->status = new UserStatusEnum(UserStatusEnum::INACTIVE);
116153
}
117154
return $this->status;
118155
}
119156
}
120-
157+
121158
$user = new User();
122159
echo 'Default user status: ' . $user->getStatus() . '(' . $user->getStatus()->getValue() . ')' . PHP_EOL;
123160
$user->setStatus(new UserStatusEnum(UserStatusEnum::ACTIVE));
@@ -161,63 +198,40 @@ value.
161198
{
162199
const ONE = 1;
163200
const TWO = 2;
164-
protected $value = self::ONE;
201+
202+
public function __construct($value = self::ONE)
203+
{
204+
parent::__construct($value);
205+
}
165206
}
166207

167-
## Enum without a default value
168-
169-
Don't define a ```$value``` to not define a default value if none of your
170-
constant values has ```NULL``` as value.
208+
## Inheritance
171209

172-
That's because ```$value``` was defined as ```NULL``` in the base class and
173-
no constant assignable to the default value.
210+
It's also possible to extend other enumerations.
174211

175-
class MyEnumWithoutDefaultValue extends MabeEnum_Enum
212+
class MyEnum extends MabeEnum_Enum
176213
{
177214
const ONE = 1;
178215
const TWO = 2;
179216
}
180-
181-
* No argument on constructor results in an InvalidArgumentException
182-
183-
## Constant with NULL as value
184-
185-
Because ```$value``` property is defined as ```NULL``` a constant with
186-
```NULL``` as value gets the default value automatically.
187-
188-
class MyEnumWithNullAsDefaultValue extends MabeEnum_Enum
217+
218+
class EnumInheritance extends MyEnum
189219
{
190-
const NONE = null;
191-
const ONE = 1;
192-
const TWO = 2;
220+
const INHERITANCE = 'Inheritance';
193221
}
194222

195-
To disable this behavior simply define ```$value``` to a value not assignable
196-
to a constant.
223+
## Simplified instantiation
197224

198-
class MyEnumWithoutNullAsDefaultValue extends MabeEnum_Enum
199-
{
200-
const NONE = null;
201-
const ONE = 1;
202-
const TWO = 2;
203-
protected $value = -1;
204-
}
205-
206-
## Inheritance
207-
208-
It's also possible to extend other enumerations.
225+
With PHP >= 5.3 it possible to call one of the defined constants like a method
226+
and you will get the instantiated enum as a result.
209227

210228
class MyEnum extends MabeEnum_Enum
211229
{
212230
const ONE = 1;
213231
const TWO = 2;
214232
}
215-
216-
class EnumInheritance extends MyEnum
217-
{
218-
const INHERITACE = 'Inheritance';
219-
}
220-
233+
234+
$enum = MyEnum::ONE();
221235

222236
# New BSD License
223237

src/MabeEnum/Enum.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,29 @@ abstract class MabeEnum_Enum
1111
{
1212
/**
1313
* The current selected value
14-
* @var scalar
14+
* @var null|scalar
1515
*/
16-
protected $value = null;
16+
private $value;
1717

1818
/**
1919
* The ordinal number of the value
2020
* @var null|int
2121
*/
22-
private $ordinal = null;
22+
private $ordinal;
2323

2424
/**
2525
* An array of available constants
26-
* @var array
26+
* @var null|array
2727
*/
28-
private $constants = null;
28+
private $constants;
2929

3030
/**
3131
* Constructor
3232
*
33-
* @param mixed $value The value to select
33+
* @param scalar $value The value to select
3434
* @throws InvalidArgumentException
3535
*/
36-
public function __construct($value = null)
36+
public function __construct($value)
3737
{
3838
$reflectionClass = new ReflectionClass($this);
3939
$constants = $reflectionClass->getConstants();
@@ -46,11 +46,6 @@ public function __construct($value = null)
4646

4747
// TODO: Check that constant values are equal (non strict comparison)
4848

49-
// use the default value
50-
if (func_num_args() == 0) {
51-
$value = $this->value;
52-
}
53-
5449
// find and set the given value
5550
// set the defined value because of non strict comparison
5651
$const = array_search($value, $this->constants);
@@ -98,15 +93,13 @@ final public function getOrdinal()
9893
$value = $this->value;
9994
foreach ($this->constants as $constValue) {
10095
if ($value === $constValue) {
101-
$this->ordinal = $ordinal;
102-
return $ordinal;
96+
break;
10397
}
10498
++$ordinal;
10599
}
106100

107-
throw new RuntimeException(
108-
"Current value '{$value}' isn't defined within this '" . get_class($this) . "'"
109-
);
101+
$this->ordinal = $ordinal;
102+
return $ordinal;
110103
}
111104

112105
/**

tests/MabeEnumTest/EnumTest.php

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,6 @@ public function testToStringMagicMethodReturnsValueAsString()
4040
$this->assertSame('1', $enum->__toString());
4141
}
4242

43-
public function testEnumWithNullAsDefaultValue()
44-
{
45-
$enum = new MabeEnumTest_TestAsset_EnumWithNullAsDefaultValue();
46-
47-
$this->assertSame(array(
48-
'NONE' => null,
49-
'ONE' => 1,
50-
'TWO' => 2,
51-
), $enum->getConstants());
52-
53-
$this->assertNull($enum->getValue());
54-
}
55-
56-
public function testEnumWithoutDefaultValue()
57-
{
58-
$this->setExpectedException('InvalidArgumentException');
59-
new MabeEnumTest_TestAsset_EnumWithoutDefaultValue();
60-
}
61-
6243
public function testEnumInheritance()
6344
{
6445
$enum = new MabeEnumTest_TestAsset_EnumInheritance(MabeEnumTest_TestAsset_EnumInheritance::ONE);
@@ -89,26 +70,19 @@ public function testConstuctorNonStrictValue()
8970
$this->assertSame(1, $enum->getOrdinal());
9071
}
9172

73+
public function testConstructorInvalidValueThrowsInvalidArgumentException()
74+
{
75+
$this->setExpectedException('InvalidArgumentException');
76+
new MabeEnumTest_TestAsset_EnumWithoutDefaultValue('unknown');
77+
}
78+
9279
public function testCallingGetOrdinalTwoTimesWillResultTheSameValue()
9380
{
9481
$enum = new MabeEnumTest_TestAsset_EnumWithoutDefaultValue(MabeEnumTest_TestAsset_EnumWithoutDefaultValue::TWO);
9582
$this->assertSame(1, $enum->getOrdinal());
9683
$this->assertSame(1, $enum->getOrdinal());
9784
}
9885

99-
public function testGetOrdinalThrowsRuntimeExceptionOnUnknwonValue()
100-
{
101-
$enum = new MabeEnumTest_TestAsset_EnumWithDefaultValue();
102-
103-
// change the protected value property to an unknwon value
104-
$reflectionValue = new ReflectionProperty($enum, 'value');
105-
$reflectionValue->setAccessible(true);
106-
$reflectionValue->setValue($enum, 'unknwonValue');
107-
108-
$this->setExpectedException('RuntimeException');
109-
$enum->getOrdinal();
110-
}
111-
11286
public function testInstantiateUsingMagicMethod()
11387
{
11488
if (version_compare(PHP_VERSION, '5.3', '<')) {

tests/MabeEnumTest/TestAsset/EnumWithDefaultValue.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,9 @@ class MabeEnumTest_TestAsset_EnumWithDefaultValue extends MabeEnum_Enum
1111
{
1212
const ONE = 1;
1313
const TWO = 2;
14-
protected $value = 1;
14+
15+
public function __construct($value = self::ONE)
16+
{
17+
parent::__construct($value);
18+
}
1519
}

tests/MabeEnumTest/TestAsset/EnumWithNullAsDefaultValue.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)