1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the miBadger package. |
5
|
|
|
* |
6
|
|
|
* @author Michael Webbers <[email protected]> |
7
|
|
|
* @license http://opensource.org/licenses/Apache-2.0 Apache v2 License |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace miBadger\Enum; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The enum class. |
14
|
|
|
* |
15
|
|
|
* @see http://tools.ietf.org/html/rfc5424 |
16
|
|
|
* @since 1.0.0 |
17
|
|
|
*/ |
18
|
|
|
class Enum |
19
|
|
|
{ |
20
|
|
|
/** @var int|double|string|boolean The value. */ |
21
|
|
|
private $value; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Construct a enum with the given value. |
25
|
|
|
* |
26
|
|
|
* @param int|double|string|boolean $value |
27
|
|
|
* @throws \UnexpectedValueException |
28
|
|
|
*/ |
29
|
8 |
|
public function __construct($value) |
30
|
|
|
{ |
31
|
8 |
|
if (!in_array($value, static::toArray())) { |
32
|
1 |
|
throw new \UnexpectedValueException($value . ' is not a valid enum value.'); |
33
|
|
|
} |
34
|
|
|
|
35
|
8 |
|
$this->value = $value; |
36
|
8 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns the string representation of the enum object. |
40
|
|
|
* |
41
|
|
|
* @return string the string representation of the enum object. |
42
|
|
|
*/ |
43
|
1 |
|
public function __toString() |
44
|
|
|
{ |
45
|
1 |
|
return $this->getName(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns the ordinal. |
50
|
|
|
* |
51
|
|
|
* @return int the ordinal. |
52
|
|
|
*/ |
53
|
1 |
|
public function getOrdinal() |
54
|
|
|
{ |
55
|
1 |
|
$ordinal = array_search($this->value, array_values(static::toArray())); |
56
|
|
|
|
57
|
1 |
|
return $ordinal !== false ? $ordinal : -1; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns the name. |
62
|
|
|
* |
63
|
|
|
* @return string the name. |
64
|
|
|
*/ |
65
|
2 |
|
public function getName() |
66
|
|
|
{ |
67
|
2 |
|
$name = array_search($this->value, static::toArray()); |
68
|
|
|
|
69
|
2 |
|
return $name !== false ? $name : ''; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns the value. |
74
|
|
|
* |
75
|
|
|
* @return mixed the value; |
76
|
|
|
*/ |
77
|
1 |
|
public function getValue() |
78
|
|
|
{ |
79
|
1 |
|
return $this->value; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns the enum objects. |
84
|
|
|
* |
85
|
|
|
* @return array<string,integer|double|string|boolean> the enum objects. |
86
|
|
|
*/ |
87
|
8 |
|
public static function toArray() |
88
|
|
|
{ |
89
|
8 |
|
return (new \ReflectionClass(\get_called_class()))->getConstants(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns the enum object with the specified name. |
94
|
|
|
* |
95
|
|
|
* @param string $name |
96
|
|
|
* @return Enum the enum object with the specified name. |
97
|
|
|
* @throws \UnexpectedValueException |
98
|
|
|
*/ |
99
|
2 |
|
public static function valueOf($name) |
100
|
|
|
{ |
101
|
2 |
|
if (!defined('static::' . $name)) { |
102
|
1 |
|
throw new \UnexpectedValueException($name . ' is not a valid enum name.'); |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
return new static(constant('static::' . $name)); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|