8
8
* @author Petr Knap <dev@petrknap.cz>
9
9
* @since 2016-01-23
10
10
* @package PetrKnap\Php\Enum
11
- * @version 0.2
11
+ * @version 0.3
12
12
* @license https://github.com/petrknap/php-enum/blob/master/LICENSE MIT
13
13
*/
14
14
abstract class AbstractEnum
@@ -21,36 +21,38 @@ abstract class AbstractEnum
21
21
/**
22
22
* @var mixed[][]
23
23
*/
24
- private static $ constants = [];
24
+ private static $ members = [];
25
25
26
26
/**
27
- * @var mixed
27
+ * @var string
28
28
*/
29
- private $ constantName ;
29
+ private $ memberName ;
30
30
31
31
/**
32
32
* @var mixed
33
33
*/
34
- private $ constantValue ;
34
+ private $ memberValue ;
35
35
36
36
/**
37
- * @param mixed $constantName
37
+ * @param string $memberName
38
38
* @throws EnumException
39
39
*/
40
- protected function __construct ($ constantName )
40
+ protected function __construct ($ memberName )
41
41
{
42
- $ this ->constantName = $ constantName ;
43
- $ this ->constantValue = $ this ->get ($ constantName );
42
+ if (!($ memberName === null && !$ this ->exists (null ))) {
43
+ $ this ->memberName = $ memberName ;
44
+ $ this ->memberValue = $ this ->get ($ memberName );
45
+ }
44
46
}
45
47
46
48
/**
47
49
* Creates magical factories for easier access to enum
48
50
*
49
- * @param mixed $constantName enum key
51
+ * @param string $memberName enum key
50
52
* @param array $args ignored
51
53
* @return mixed
52
54
*/
53
- public static function __callStatic ($ constantName , array $ args )
55
+ public static function __callStatic ($ memberName , array $ args )
54
56
{
55
57
$ className = get_called_class ();
56
58
@@ -60,10 +62,10 @@ public static function __callStatic($constantName, array $args)
60
62
$ instances = [];
61
63
}
62
64
63
- $ instance = &$ instances [$ constantName ];
65
+ $ instance = &$ instances [$ memberName ];
64
66
65
67
if (!($ instance instanceof $ className )) {
66
- $ instance = new $ className ($ constantName );
68
+ $ instance = new $ className ($ memberName );
67
69
}
68
70
69
71
return $ instance ;
@@ -74,60 +76,68 @@ public static function __callStatic($constantName, array $args)
74
76
*/
75
77
public function getName ()
76
78
{
77
- return $ this ->constantName ;
79
+ return $ this ->memberName ;
78
80
}
79
81
80
82
/**
81
83
* @return mixed
82
84
*/
83
85
public function getValue ()
84
86
{
85
- return $ this ->constantValue ;
87
+ return $ this ->memberValue ;
86
88
}
87
89
88
90
/**
89
- * @param mixed[] $constants
91
+ * @param mixed[] $members
90
92
*/
91
- protected static function setConstants (array $ constants )
93
+ protected static function setMembers (array $ members )
92
94
{
93
- self ::$ constants [get_called_class ()] = $ constants ;
95
+ self ::$ members [get_called_class ()] = $ members ;
94
96
}
95
97
96
98
/**
97
99
* @return mixed[]
98
100
*/
99
- public static function getConstants ()
101
+ public static function getMembers ()
100
102
{
101
- return self ::$ constants [get_called_class ()];
103
+ $ className = get_called_class ();
104
+
105
+ $ members = &self ::$ members [$ className ];
106
+
107
+ if (empty ($ members )) {
108
+ new $ className (null );
109
+ }
110
+
111
+ return $ members ;
102
112
}
103
113
104
114
/**
105
- * @param string $constantName
115
+ * @param string $memberName
106
116
* @return bool
107
117
*/
108
- private function exists ($ constantName )
118
+ private function exists ($ memberName )
109
119
{
110
- return array_key_exists ($ constantName , self ::$ constants [get_called_class ()]);
120
+ return array_key_exists ($ memberName , self ::$ members [get_called_class ()]);
111
121
}
112
122
113
123
/**
114
- * @param string $constantName
124
+ * @param string $memberName
115
125
* @return mixed
116
126
* @throws EnumException
117
127
*/
118
- private function get ($ constantName )
128
+ private function get ($ memberName )
119
129
{
120
- if (!$ this ->exists ($ constantName )) {
130
+ if (!$ this ->exists ($ memberName )) {
121
131
throw new EnumException (
122
132
sprintf (
123
133
"%s does not exist in %s " ,
124
- $ constantName ,
134
+ $ memberName ,
125
135
get_called_class ()
126
136
),
127
137
EnumException::OUT_OF_RANGE
128
138
);
129
139
}
130
140
131
- return self ::$ constants [get_called_class ()][$ constantName ];
141
+ return self ::$ members [get_called_class ()][$ memberName ];
132
142
}
133
143
}
0 commit comments