Skip to content

Commit 3596ccd

Browse files
committed
fixed #4
- implemented setOrdinal() - removed public setValue() and setName()
1 parent 193e60a commit 3596ccd

File tree

3 files changed

+66
-71
lines changed

3 files changed

+66
-71
lines changed

src/MabeEnum/Enum.php

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

18+
/**
19+
* The ordinal number of the value
20+
* @var null|int
21+
*/
22+
private $ordinal = null;
23+
1824
/**
1925
* An array of available constants
2026
* @var array
@@ -30,13 +36,28 @@ abstract class MabeEnum_Enum
3036
public function __construct($value = null)
3137
{
3238
$reflectionClass = new ReflectionClass($this);
33-
$this->constants = $reflectionClass->getConstants();
39+
$constants = $reflectionClass->getConstants();
40+
41+
// This is required to make sure that constants of base classes will be the first
42+
while ( ($reflectionClass = $reflectionClass->getParentClass()) ) {
43+
$constants = $reflectionClass->getConstants() + $constants;
44+
}
45+
$this->constants = $constants;
46+
47+
// TODO: Check that constant values are equal (non strict comparison)
48+
49+
// use the default value
50+
if (func_num_args() == 0) {
51+
$value = $this->value;
52+
}
3453

35-
if (func_num_args() > 0) {
36-
$this->setValue($value);
37-
} elseif (!in_array($this->value, $this->constants, true)) {
38-
throw new InvalidArgumentException("No value given and no default value defined");
54+
// find and set the given value
55+
// set the defined value because of non strict comparison
56+
$const = array_search($value, $this->constants);
57+
if ($const === false) {
58+
throw new InvalidArgumentException("Unknown value '{$value}'");
3959
}
60+
$this->value = $this->constants[$const];
4061
}
4162

4263
/**
@@ -48,19 +69,6 @@ final public function getConstants()
4869
return $this->constants;
4970
}
5071

51-
/**
52-
* Select a new value
53-
* @param mixed $value
54-
* @throws InvalidArgumentException
55-
*/
56-
final public function setValue($value)
57-
{
58-
if (!in_array($value, $this->constants, true)) {
59-
throw new InvalidArgumentException("Unknown value '{$value}'");
60-
}
61-
$this->value = $value;
62-
}
63-
6472
/**
6573
* Get the current selected value
6674
* @return mixed
@@ -70,19 +78,6 @@ final public function getValue()
7078
return $this->value;
7179
}
7280

73-
/**
74-
* Select a new value by constant name
75-
* @param string $name
76-
* @throws InvalidArgumentException
77-
*/
78-
final public function setName($name)
79-
{
80-
if (!array_key_exists($name, $this->constants)) {
81-
throw new InvalidArgumentException("Unknown name '{$name}'");
82-
}
83-
$this->value = $this->constants[$name];
84-
}
85-
8681
/**
8782
* Get the current selected constant name
8883
* @return string
@@ -92,6 +87,28 @@ final public function getName()
9287
return array_search($this->value, $this->constants, true);
9388
}
9489

90+
final public function getOrdinal()
91+
{
92+
if ($this->ordinal !== null) {
93+
return $this->ordinal;
94+
}
95+
96+
// detect ordinal
97+
$ordinal = 0;
98+
$value = $this->value;
99+
foreach ($this->constants as $constValue) {
100+
if ($value === $constValue) {
101+
$this->ordinal = $ordinal;
102+
return $ordinal;
103+
}
104+
++$ordinal;
105+
}
106+
107+
throw new RuntimeException(
108+
"Current value '{$value}' isn't defined within this '" . get_class($this) . "'"
109+
);
110+
}
111+
95112
/**
96113
* Get the current selected constant name
97114
* @return string

tests/MabeEnumTest/EnumTest.php

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function testEnumWithDefaultValue()
2525
$this->assertSame('1', $enum->__toString());
2626

2727
$this->assertSame('ONE', $enum->getName());
28+
$this->assertSame(0, $enum->getOrdinal());
2829
}
2930

3031
public function testGetNameReturnsConstantNameOfCurrentValue()
@@ -61,53 +62,30 @@ public function testEnumWithoutDefaultValue()
6162
public function testEnumInheritance()
6263
{
6364
$enum = new MabeEnumTest_TestAsset_EnumInheritance(MabeEnumTest_TestAsset_EnumInheritance::ONE);
65+
$this->assertSame(array(
66+
'ONE' => 1,
67+
'TWO' => 2,
68+
'INHERITANCE' => 'Inheritance'
69+
), $enum->getConstants());
6470
$this->assertSame(MabeEnumTest_TestAsset_EnumInheritance::ONE, $enum->getValue());
71+
$this->assertSame(0, $enum->getOrdinal());
6572

66-
$enum = new MabeEnumTest_TestAsset_EnumInheritance(MabeEnumTest_TestAsset_EnumInheritance::INHERITACE);
67-
$this->assertSame(MabeEnumTest_TestAsset_EnumInheritance::INHERITACE, $enum->getValue());
73+
$enum = new MabeEnumTest_TestAsset_EnumInheritance(MabeEnumTest_TestAsset_EnumInheritance::INHERITANCE);
74+
$this->assertSame(MabeEnumTest_TestAsset_EnumInheritance::INHERITANCE, $enum->getValue());
75+
$this->assertSame(2, $enum->getOrdinal());
6876
}
6977

70-
public function testChangeValueOnConstructor()
78+
public function testConstructorStrictValue()
7179
{
72-
$enum = new MabeEnumTest_TestAsset_EnumWithoutDefaultValue(1);
80+
$enum = new MabeEnumTest_TestAsset_EnumWithoutDefaultValue(MabeEnumTest_TestAsset_EnumWithoutDefaultValue::ONE);
7381
$this->assertSame(1, $enum->getValue());
82+
$this->assertSame(0, $enum->getOrdinal());
7483
}
7584

76-
public function testChangeValueOnConstructorThrowsInvalidArgumentExceptionOnStrictComparison()
85+
public function testConstuctorNonStrictValue()
7786
{
78-
$this->setExpectedException('InvalidArgumentException');
79-
$enum = new MabeEnumTest_TestAsset_EnumWithoutDefaultValue('1');
80-
}
81-
82-
public function testSetValue()
83-
{
84-
$enum = new MabeEnumTest_TestAsset_EnumWithDefaultValue();
85-
$enum->setValue(2);
86-
87+
$enum = new MabeEnumTest_TestAsset_EnumWithoutDefaultValue((string)MabeEnumTest_TestAsset_EnumWithoutDefaultValue::TWO);
8788
$this->assertSame(2, $enum->getValue());
88-
}
89-
90-
public function testSetValueThrowsInvalidArgumentExceptionOnStrictComparison()
91-
{
92-
$this->setExpectedException('InvalidArgumentException');
93-
$enum = new MabeEnumTest_TestAsset_EnumWithDefaultValue();
94-
$enum->setValue('2');
95-
}
96-
97-
public function testSetName()
98-
{
99-
$enum = new MabeEnumTest_TestAsset_EnumWithDefaultValue();
100-
$enum->setValue(2);
101-
$enum->setName('ONE');
102-
$this->assertEquals(1, $enum->getValue());
103-
}
104-
105-
/**
106-
* @expectedException InvalidArgumentException
107-
*/
108-
public function testSetInvalidName()
109-
{
110-
$enum = new MabeEnumTest_TestAsset_EnumWithDefaultValue();
111-
$enum->setName('FOO');
89+
$this->assertSame(1, $enum->getOrdinal());
11290
}
11391
}

tests/MabeEnumTest/TestAsset/EnumInheritance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
*/
1010
class MabeEnumTest_TestAsset_EnumInheritance extends MabeEnumTest_TestAsset_EnumWithoutDefaultValue
1111
{
12-
const INHERITACE = 'Inheritance';
12+
const INHERITANCE = 'Inheritance';
1313
}

0 commit comments

Comments
 (0)