@@ -11,10 +11,16 @@ abstract class MabeEnum_Enum
11
11
{
12
12
/**
13
13
* The current selected value
14
- * @var mixed
14
+ * @var scalar
15
15
*/
16
16
protected $ value = null ;
17
17
18
+ /**
19
+ * The ordinal number of the value
20
+ * @var null|int
21
+ */
22
+ private $ ordinal = null ;
23
+
18
24
/**
19
25
* An array of available constants
20
26
* @var array
@@ -30,13 +36,28 @@ abstract class MabeEnum_Enum
30
36
public function __construct ($ value = null )
31
37
{
32
38
$ 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
+ }
34
53
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 }' " );
39
59
}
60
+ $ this ->value = $ this ->constants [$ const ];
40
61
}
41
62
42
63
/**
@@ -48,19 +69,6 @@ final public function getConstants()
48
69
return $ this ->constants ;
49
70
}
50
71
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
-
64
72
/**
65
73
* Get the current selected value
66
74
* @return mixed
@@ -70,19 +78,6 @@ final public function getValue()
70
78
return $ this ->value ;
71
79
}
72
80
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
-
86
81
/**
87
82
* Get the current selected constant name
88
83
* @return string
@@ -92,6 +87,28 @@ final public function getName()
92
87
return array_search ($ this ->value , $ this ->constants , true );
93
88
}
94
89
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
+
95
112
/**
96
113
* Get the current selected constant name
97
114
* @return string
0 commit comments