Skip to content

Commit 1bdf6a9

Browse files
committed
Enum new politics & code clean
1 parent 270051a commit 1bdf6a9

File tree

1 file changed

+41
-17
lines changed

1 file changed

+41
-17
lines changed

core/Base/Enum.class.php

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,44 @@ public static function create($id)
3535

3636
public function __construct($id)
3737
{
38-
$this->setId($id);
38+
$this->setInternalId($id);
3939
}
4040

41-
/// prevent's serialization of names' array
42-
//@{
41+
/**
42+
* @param $id
43+
* @return Enum
44+
* @throws MissingElementException
45+
*/
46+
protected function setInternalId($id)
47+
{
48+
$names = static::$names;
49+
50+
if (isset($names[$id])) {
51+
$this->id = $id;
52+
$this->name = $names[$id];
53+
} else
54+
throw new MissingElementException(
55+
'knows nothing about such id == '.$id
56+
);
57+
58+
return $this;
59+
}
60+
61+
/**
62+
* @return string
63+
*/
4364
public function serialize()
4465
{
4566
return (string) $this->id;
4667
}
4768

69+
/**
70+
* @param $serialized
71+
*/
4872
public function unserialize($serialized)
4973
{
50-
$this->setId($serialized);
74+
$this->setInternalId($serialized);
5175
}
52-
//@}
5376

5477
/**
5578
* Array of object
@@ -68,13 +91,16 @@ public static function getList()
6891
/**
6992
* must return any existent ID
7093
* 1 should be ok for most enumerations
94+
* @return integer
7195
**/
7296
public static function getAnyId()
7397
{
7498
return 1;
7599
}
76100

77-
/// parent's getId() is too complex in our case
101+
/**
102+
* @return null|integer
103+
*/
78104
public function getId()
79105
{
80106
return $this->id;
@@ -92,11 +118,19 @@ public static function getObjectList()
92118
return static::getList();
93119
}
94120

121+
/**
122+
* @return string
123+
*/
95124
public function toString()
96125
{
97126
return $this->name;
98127
}
99128

129+
/**
130+
* Plain list
131+
* @static
132+
* @return array
133+
*/
100134
public static function getNameList()
101135
{
102136
return static::$names;
@@ -107,17 +141,7 @@ public static function getNameList()
107141
**/
108142
public function setId($id)
109143
{
110-
$names = static::$names;
111-
112-
if (isset($names[$id])) {
113-
$this->id = $id;
114-
$this->name = $names[$id];
115-
} else
116-
throw new MissingElementException(
117-
'knows nothing about such id == '.$id
118-
);
119-
120-
return $this;
144+
throw new UnsupportedMethodException('You can not change id here, because it is politics for Enum!');
121145
}
122146
}
123147
?>

0 commit comments

Comments
 (0)