-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumBase.php
executable file
·52 lines (44 loc) · 1.36 KB
/
EnumBase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
abstract class EnumBase {
private $displayName;
protected function __construct($displayName = '') {
$this->displayName = $displayName;
self::$enumValues[] = $this;
}
private static $enumValues = array();
public static function values() {
$values = array();
$clazz = get_called_class();
foreach (self::$enumValues as $enumValue) {
if ($enumValue instanceof $clazz) {
$values[] = $enumValue;
}
}
return $values;
}
public function getDisplayName() {
if( $this->displayName === ''){
return $this->getName();
}
return $this->displayName;
}
public function getName() {
$reflection = new ReflectionClass(get_class($this));
$staticProperties = $reflection->getStaticProperties();
foreach ($staticProperties as $name => $value) {
if ($value === $this) {
return $name;
}
}
}
public static function valueOf($name) {
$clazz = get_called_class();
if (isset($clazz::$$name)) {
return $clazz::$$name;
}
throw new Exception('そのような名前の enum は存在しません. name['.$name.'] $clazz['.$clazz.']');
}
public function __toString() {
return $this->getName();
}
}