1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ZingleCom\Enum; |
4
|
|
|
|
5
|
|
|
use ZingleCom\Enum\Exception\InvalidValueException; |
6
|
|
|
use ZingleCom\Enum\Meta\GeneratorFactory; |
7
|
|
|
use ZingleCom\Enum\Meta\GeneratorInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AbstractEnum |
11
|
|
|
*/ |
12
|
|
|
abstract class AbstractEnum implements EnumInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var GeneratorInterface |
16
|
|
|
*/ |
17
|
|
|
private static $generator; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $constantName; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var mixed |
26
|
|
|
*/ |
27
|
|
|
private $value; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* AbstractEnum constructor. |
32
|
|
|
* @param mixed $value |
33
|
|
|
* @throws Exception\EnumException |
34
|
|
|
* @throws Exception\MissingValueException |
35
|
|
|
* @throws InvalidValueException |
36
|
|
|
*/ |
37
|
|
|
public function __construct($value) |
38
|
|
|
{ |
39
|
|
|
$meta = self::getMeta(); |
40
|
|
|
|
41
|
|
|
if (!$meta->isValid($value)) { |
42
|
|
|
throw new InvalidValueException($value); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->constantName = $meta->getConstantName($value); |
46
|
|
|
$this->value = $value; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function getConstantName(): string |
53
|
|
|
{ |
54
|
|
|
return $this->constantName; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
|
|
public function getValue() |
61
|
|
|
{ |
62
|
|
|
return $this->value; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function getDisplayName(): string |
69
|
|
|
{ |
70
|
|
|
return mb_convert_case( |
71
|
|
|
strtolower(str_replace('_', ' ', $this->constantName)), |
72
|
|
|
MB_CASE_TITLE, |
73
|
|
|
'UTF-8' |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param mixed $value |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function is($value): bool |
82
|
|
|
{ |
83
|
|
|
return $value === $this->getValue(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function __toString(): string |
90
|
|
|
{ |
91
|
|
|
return strval($this->getValue()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return GeneratorInterface |
96
|
|
|
*/ |
97
|
|
|
private static function getGenerator(): GeneratorInterface |
98
|
|
|
{ |
99
|
|
|
if (null === self::$generator) { |
100
|
|
|
self::$generator = GeneratorFactory::create(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return self::$generator; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return MetaInterface |
108
|
|
|
*/ |
109
|
|
|
private static function getMeta(): MetaInterface |
110
|
|
|
{ |
111
|
|
|
return self::getGenerator()->get(get_called_class()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Makes this compatible with Symfony |
116
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
public static function getOptions(): array |
120
|
|
|
{ |
121
|
|
|
return array_flip(self::getMeta()->getConstants()->toArray()); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|