1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
MIT License |
4
|
|
|
|
5
|
|
|
Copyright (c) 2017 Ondrej Hatala |
6
|
|
|
|
7
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
in the Software without restriction, including without limitation the rights |
10
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
furnished to do so, subject to the following conditions: |
13
|
|
|
|
14
|
|
|
The above copyright notice and this permission notice shall be included in all |
15
|
|
|
copies or substantial portions of the Software. |
16
|
|
|
|
17
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23
|
|
|
SOFTWARE. |
24
|
|
|
*/ |
25
|
|
|
namespace SpareParts\Enum; |
26
|
|
|
|
27
|
|
|
use SpareParts\Enum\Exception\InvalidEnumSetupException; |
28
|
|
|
use SpareParts\Enum\Exception\InvalidEnumValueException; |
29
|
|
|
use SpareParts\Enum\Exception\OperationNotAllowedException; |
30
|
|
|
use SpareParts\Enum\Mapping\Annotations; |
31
|
|
|
use SpareParts\Enum\Set\ISet; |
32
|
|
|
|
33
|
|
|
abstract class Enum |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
/** @var string[] */ |
37
|
|
|
protected static $values = []; |
38
|
|
|
|
39
|
|
|
/** @var string[][] */ |
40
|
|
|
private static $innerValues = []; |
41
|
|
|
|
42
|
|
|
/** @var self[][] */ |
43
|
|
|
protected static $instances = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $value; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* PROTECTED!! Not callable directly. |
52
|
|
|
* @see static::instance() |
53
|
|
|
* @see static::__callStatic() |
54
|
|
|
* @internal |
55
|
|
|
*/ |
56
|
8 |
|
protected function __construct(string $value) |
57
|
|
|
{ |
58
|
8 |
|
if (!isset(self::$innerValues[get_class($this)])) { |
59
|
|
|
// first, try to fetch correct values |
60
|
4 |
|
if (empty(static::$values)) { |
61
|
|
|
// try to use annotations |
62
|
2 |
|
self::$innerValues[get_class($this)] = Annotations::loadEnumValues(get_class($this)); |
63
|
|
|
} else { |
64
|
2 |
|
self::$innerValues[get_class($this)] = static::$values; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// then check we did it in a constructive way |
68
|
4 |
|
if (empty(self::$innerValues[get_class($this)])) { |
69
|
1 |
|
throw new InvalidEnumSetupException('Incorrect setup! Enum '.get_called_class().' doesn\'t have its static::$values set.'); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// does this $value exist in current Enum |
74
|
7 |
|
if (!in_array($value, self::$innerValues[get_class($this)])) { |
75
|
3 |
|
throw new InvalidEnumValueException('Enum '.get_called_class().' does not contain value '.$value.'. Possible values are: '.implode(', ', static::$values)); |
76
|
|
|
} |
77
|
4 |
|
$this->value = $value; |
78
|
4 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* String representation |
82
|
|
|
*/ |
83
|
50 |
|
public function __toString(): string |
84
|
|
|
{ |
85
|
50 |
|
return $this->value; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** @return static */ |
89
|
62 |
|
public static function __callStatic(string $method, array $args): self |
90
|
|
|
{ |
91
|
62 |
|
if ($method[0] === '_') { |
92
|
7 |
|
$method = substr($method, 1); |
93
|
|
|
} |
94
|
62 |
|
return static::instance($method); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** @return static */ |
98
|
68 |
|
public static function instance(string $value): self |
99
|
|
|
{ |
100
|
68 |
|
if (!isset(static::$instances[get_called_class()][$value])) { |
101
|
8 |
|
static::$instances[get_called_class()][$value] = new static($value); |
102
|
|
|
} |
103
|
|
|
|
104
|
64 |
|
return static::$instances[get_called_class()][$value]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* |
109
|
|
|
* |
110
|
|
|
* @param iterable|self[] $enumList |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
4 |
|
public function isAnyOf(iterable $enumList): bool |
114
|
|
|
{ |
115
|
4 |
|
foreach ($enumList as $enum) { |
116
|
3 |
|
if ($enum === $this) { |
117
|
3 |
|
return true; |
118
|
|
|
} |
119
|
|
|
} |
120
|
3 |
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
public function __clone() |
124
|
|
|
{ |
125
|
1 |
|
throw new OperationNotAllowedException('Singleton cannot be cloned.'); |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
public function __sleep() |
129
|
|
|
{ |
130
|
1 |
|
throw new OperationNotAllowedException('Singleton cannot be serialized.'); |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
public function __wakeup() |
134
|
|
|
{ |
135
|
1 |
|
throw new OperationNotAllowedException('Singleton cannot be serialized'); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|