|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by gerk on 08.09.16 16:39 |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace PeekAndPoke\Types; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @api |
|
10
|
|
|
* |
|
11
|
|
|
* @author Karsten J. Gerber <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class Enumerated implements ValueHolder |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var string */ |
|
16
|
|
|
private $value; |
|
17
|
|
|
/** @var boolean */ |
|
18
|
|
|
private $valid = true; |
|
19
|
|
|
|
|
20
|
3 |
|
protected function __construct() |
|
21
|
|
|
{ |
|
22
|
|
|
// noop |
|
23
|
3 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @return string |
|
27
|
|
|
*/ |
|
28
|
7 |
|
public function getValue() |
|
29
|
|
|
{ |
|
30
|
7 |
|
return $this->value; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return boolean |
|
35
|
|
|
*/ |
|
36
|
5 |
|
public function isValid() |
|
37
|
|
|
{ |
|
38
|
5 |
|
return $this->valid; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
4 |
|
final public function __toString() |
|
45
|
|
|
{ |
|
46
|
|
|
/** @noinspection UnnecessaryCastingInspection */ |
|
47
|
4 |
|
return (string) $this->value; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* static initializer to set up all enum values |
|
52
|
|
|
*/ |
|
53
|
1 |
|
final public static function init() |
|
54
|
|
|
{ |
|
55
|
1 |
|
$reflect = self::getReflection(); |
|
56
|
1 |
|
$props = $reflect->getStaticProperties(); |
|
57
|
|
|
|
|
58
|
1 |
|
foreach ($props as $name => $val) { |
|
59
|
1 |
|
$inst = new static(); |
|
60
|
1 |
|
$inst->value = (string) $name; |
|
61
|
1 |
|
$reflect->setStaticPropertyValue($name, $inst); |
|
62
|
|
|
} |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get an instance of the enum with a value of 'null'. |
|
67
|
|
|
* |
|
68
|
|
|
* This might be handy if we need an instance of the Enum to work with but |
|
69
|
|
|
* we do not really care about a real value being set. |
|
70
|
|
|
* |
|
71
|
|
|
* @return static |
|
72
|
|
|
*/ |
|
73
|
1 |
|
final public static function void() |
|
74
|
|
|
{ |
|
75
|
1 |
|
return self::from(null); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get Enum for the given input |
|
80
|
|
|
* |
|
81
|
|
|
* @param string|mixed $value |
|
82
|
|
|
* |
|
83
|
|
|
* @return static |
|
84
|
|
|
*/ |
|
85
|
5 |
|
final public static function from($value) |
|
86
|
|
|
{ |
|
87
|
5 |
|
$isString = is_scalar($value) || (\is_object($value) && \method_exists($value, '__toString')); |
|
88
|
5 |
|
$value = $isString ? (string) $value : ''; |
|
89
|
|
|
|
|
90
|
|
|
// first we try to find the REAL values |
|
91
|
|
|
|
|
92
|
|
|
/** @var array|Enumerated[] $enumerated */ |
|
93
|
5 |
|
$enumerated = self::enumerateProps(); |
|
94
|
|
|
|
|
95
|
5 |
|
if (isset($enumerated[$value]) && $enumerated[$value] instanceof static) { |
|
96
|
3 |
|
return $enumerated[$value]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// then we keep track of INVALID values. |
|
100
|
2 |
|
static $invalid = []; |
|
101
|
|
|
// take care of the called class |
|
102
|
2 |
|
$cls = static::class; |
|
103
|
|
|
|
|
104
|
2 |
|
if (! isset($invalid[$cls][$value])) { |
|
105
|
|
|
|
|
106
|
2 |
|
$inst = new static(); |
|
107
|
2 |
|
$inst->value = $value; |
|
108
|
2 |
|
$inst->valid = false; |
|
109
|
|
|
|
|
110
|
2 |
|
$invalid[$cls][$value] = $inst; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
2 |
|
return $invalid[$cls][$value]; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return string[] |
|
118
|
|
|
*/ |
|
119
|
1 |
|
final public static function enumerateValues() |
|
120
|
|
|
{ |
|
121
|
1 |
|
return array_keys(self::enumerateProps()); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return array|Enumerated[] |
|
126
|
|
|
*/ |
|
127
|
6 |
|
final public static function enumerateProps() |
|
128
|
|
|
{ |
|
129
|
6 |
|
return self::getReflection()->getStaticProperties(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return \ReflectionClass |
|
134
|
|
|
*/ |
|
135
|
7 |
|
final public static function getReflection() |
|
136
|
|
|
{ |
|
137
|
|
|
/** @noinspection ExceptionsAnnotatingAndHandlingInspection */ |
|
138
|
7 |
|
return new \ReflectionClass(static::class); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|