1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Nate Brunette. |
4
|
|
|
* Distributed under the MIT License (http://opensource.org/licenses/MIT) |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Tebru\Enum; |
8
|
|
|
|
9
|
|
|
use BadMethodCallException; |
10
|
|
|
use RuntimeException; |
11
|
|
|
|
12
|
|
|
/**v |
13
|
|
|
* Class AbstractEnum |
14
|
|
|
* |
15
|
|
|
* @author Nate Brunette <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractEnum implements EnumInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $value; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* An array of created instances to ensure singleton |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private static $instances = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor |
33
|
|
|
* |
34
|
|
|
* @param string $value |
35
|
|
|
* @throws RuntimeException If the value is not valid |
36
|
|
|
*/ |
37
|
6 |
|
final private function __construct($value) |
38
|
|
|
{ |
39
|
6 |
|
if (!static::exists($value)) { |
40
|
1 |
|
throw new RuntimeException(sprintf('%s is not a valid value for this enum.', $value)); |
41
|
|
|
} |
42
|
|
|
|
43
|
5 |
|
$this->value = $value; |
44
|
5 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Return the value as a string |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
4 |
|
public function __toString() |
52
|
|
|
{ |
53
|
4 |
|
return (string) $this->value; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Do a comparison to check if the enum values are equal |
58
|
|
|
* |
59
|
|
|
* @param string $value |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
5 |
|
public function equals($value) |
63
|
|
|
{ |
64
|
5 |
|
return $this->value === $value; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Create a new instance of the enum |
69
|
|
|
* |
70
|
|
|
* @param string $value |
71
|
|
|
* @return AbstractEnum |
72
|
|
|
* @throws RuntimeException If the value is not valid |
73
|
|
|
*/ |
74
|
26 |
|
public static function create($value) |
75
|
|
|
{ |
76
|
26 |
|
$class = get_called_class(); |
77
|
26 |
|
if (isset(self::$instances[$class][$value])) { |
78
|
21 |
|
return self::$instances[$class][$value]; |
79
|
|
|
} |
80
|
|
|
|
81
|
6 |
|
$instance = new static($value); |
82
|
5 |
|
self::$instances[$class][$value] = $instance; |
83
|
|
|
|
84
|
5 |
|
return $instance; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Return true if the value is valid for the enum |
89
|
|
|
* |
90
|
|
|
* @param string $value |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
10 |
|
public static function exists($value) |
94
|
|
|
{ |
95
|
10 |
|
return in_array($value, static::values(), true); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get an array of the enum values |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
12 |
|
public static function values() |
104
|
|
|
{ |
105
|
12 |
|
return array_values(static::getConstants()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Return enum as an array with keys matching values |
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
1 |
|
public static function toArray() |
114
|
|
|
{ |
115
|
1 |
|
return array_combine(self::values(), self::values()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get the current enum value |
120
|
|
|
* |
121
|
|
|
* @return mixed |
122
|
|
|
*/ |
123
|
13 |
|
public function getValue() |
124
|
|
|
{ |
125
|
13 |
|
return $this->value; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Allow enum instantiation by magic method |
130
|
|
|
* |
131
|
|
|
* @param string $name |
132
|
|
|
* @param $arguments |
133
|
|
|
* @return static |
134
|
|
|
* @throws RuntimeException If the value is not valid |
135
|
|
|
* @throws BadMethodCallException If the constant doesn't exist in the class |
136
|
|
|
*/ |
137
|
4 |
|
public static function __callStatic($name, $arguments) |
138
|
|
|
{ |
139
|
4 |
|
$constant = @constant('static::' . $name); |
140
|
|
|
|
141
|
4 |
|
if (null === $constant) { |
142
|
1 |
|
throw new BadMethodCallException(sprintf('Could not find constant "%s" for class "%s"', $name, static::class)); |
143
|
|
|
} |
144
|
|
|
|
145
|
3 |
|
return self::create($constant); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|