12
12
/**
13
13
* @ingroup Primitives
14
14
**/
15
- class PrimitiveEnum extends IdentifiablePrimitive
15
+ class PrimitiveEnum extends IdentifiablePrimitive implements ListedPrimitive
16
16
{
17
17
public function getList ()
18
18
{
19
19
if ($ this ->value )
20
- return ClassUtils::callStaticMethod (get_class ($ this ->value ).'::getObjectList ' );
20
+ return ClassUtils::callStaticMethod (get_class ($ this ->value ).'::getList ' );
21
21
elseif ($ this ->default )
22
- return ClassUtils::callStaticMethod (get_class ($ this ->default ).'::getObjectList ' );
22
+ return ClassUtils::callStaticMethod (get_class ($ this ->default ).'::getList ' );
23
23
else {
24
24
$ object = new $ this ->className (
25
25
ClassUtils::callStaticMethod ($ this ->className .'::getAnyId ' )
@@ -60,16 +60,11 @@ public function importValue(/* Identifiable */ $value)
60
60
61
61
public function import ($ scope )
62
62
{
63
- if (!$ this ->className )
64
- throw new WrongStateException (
65
- "no class defined for PrimitiveEnum ' {$ this ->name }' "
66
- );
67
-
68
63
$ result = parent ::import ($ scope );
69
64
70
65
if ($ result === true ) {
71
66
try {
72
- $ this ->value = new $ this ->className ($ this ->value );
67
+ $ this ->value = $ this ->makeEnumById ($ this ->value );
73
68
} catch (MissingElementException $ e ) {
74
69
$ this ->value = null ;
75
70
@@ -81,5 +76,57 @@ public function import($scope)
81
76
82
77
return $ result ;
83
78
}
79
+
80
+ /**
81
+ * @param $list
82
+ * @throws UnsupportedMethodException
83
+ */
84
+ public function setList ($ list )
85
+ {
86
+ throw new UnsupportedMethodException ('you cannot set list here, it is impossible, because list getted from enum classes ' );
87
+ }
88
+
89
+ /**
90
+ * @return null|string
91
+ */
92
+ public function getChoiceValue ()
93
+ {
94
+ if (
95
+ ($ value = $ this ->getValue () ) &&
96
+ $ value instanceof Enum
97
+ )
98
+ return $ value ->getName ();
99
+
100
+ return null ;
101
+ }
102
+
103
+
104
+ /**
105
+ * @return Enum|mixed|null
106
+ */
107
+ public function getActualChoiceValue ()
108
+ {
109
+ if (
110
+ !$ this ->getChoiceValue () &&
111
+ $ this ->getDefault ()
112
+ )
113
+ return $ this ->getDefault ()->getName ();
114
+
115
+ return null ;
116
+ }
117
+
118
+ /**
119
+ * @param $id
120
+ * @return Enum|mixed
121
+ */
122
+ protected function makeEnumById ($ id )
123
+ {
124
+ if (!$ this ->className )
125
+ throw new WrongStateException (
126
+ "no class defined for PrimitiveEnum ' {$ this ->name }' "
127
+ );
128
+
129
+ return new $ this ->className ($ id );
130
+ }
84
131
}
85
132
?>
0 commit comments