|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MabeEnum; |
|
4
|
|
|
|
|
5
|
|
|
use SplObjectStorage; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* A map of enumerator keys of the given enumeration (EnumMap<T>) |
|
10
|
|
|
* based on SplObjectStorage |
|
11
|
|
|
* |
|
12
|
|
|
* @link http://github.com/marc-mabe/php-enum for the canonical source repository |
|
13
|
|
|
* @copyright Copyright (c) 2017 Marc Bennewitz |
|
14
|
|
|
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License |
|
15
|
|
|
*/ |
|
16
|
|
|
class EnumMap extends SplObjectStorage |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* The classname of the enumeration type |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private $enumeration; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Constructor |
|
26
|
|
|
* @param string $enumeration The classname of the enumeration type |
|
27
|
|
|
* @throws InvalidArgumentException |
|
28
|
|
|
*/ |
|
29
|
16 |
|
public function __construct($enumeration) |
|
30
|
|
|
{ |
|
31
|
16 |
|
if (!is_subclass_of($enumeration, Enum::class)) { |
|
32
|
2 |
|
throw new InvalidArgumentException(sprintf( |
|
33
|
2 |
|
"This EnumMap can handle subclasses of '%s' only", |
|
34
|
1 |
|
Enum::class |
|
35
|
1 |
|
)); |
|
36
|
|
|
} |
|
37
|
14 |
|
$this->enumeration = $enumeration; |
|
38
|
14 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get the classname of the enumeration |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
2 |
|
public function getEnumeration() |
|
45
|
|
|
{ |
|
46
|
2 |
|
return $this->enumeration; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Attach a new enumerator or overwrite an existing one |
|
51
|
|
|
* @param Enum|null|boolean|int|float|string $enumerator |
|
52
|
|
|
* @param mixed $data |
|
53
|
|
|
* @return void |
|
54
|
|
|
* @throws InvalidArgumentException On an invalid given enumerator |
|
55
|
|
|
*/ |
|
56
|
6 |
|
public function attach($enumerator, $data = null) |
|
57
|
|
|
{ |
|
58
|
6 |
|
$enumeration = $this->enumeration; |
|
59
|
6 |
|
parent::attach($enumeration::get($enumerator), $data); |
|
60
|
6 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Test if the given enumerator exists |
|
64
|
|
|
* @param Enum|null|boolean|int|float|string $enumerator |
|
65
|
|
|
* @return boolean |
|
66
|
|
|
*/ |
|
67
|
10 |
|
public function contains($enumerator) |
|
68
|
|
|
{ |
|
69
|
|
|
try { |
|
70
|
10 |
|
$enumeration = $this->enumeration; |
|
71
|
10 |
|
return parent::contains($enumeration::get($enumerator)); |
|
72
|
2 |
|
} catch (InvalidArgumentException $e) { |
|
73
|
|
|
// On an InvalidArgumentException the given argument can't be contained in this map |
|
74
|
2 |
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Detach an enumerator |
|
80
|
|
|
* @param Enum|null|boolean|int|float|string $enumerator |
|
81
|
|
|
* @return void |
|
82
|
|
|
* @throws InvalidArgumentException On an invalid given enumerator |
|
83
|
|
|
*/ |
|
84
|
4 |
|
public function detach($enumerator) |
|
85
|
|
|
{ |
|
86
|
4 |
|
$enumeration = $this->enumeration; |
|
87
|
4 |
|
parent::detach($enumeration::get($enumerator)); |
|
88
|
4 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Test if the given enumerator exists |
|
92
|
|
|
* @param Enum|null|boolean|int|float|string $enumerator |
|
93
|
|
|
* @return boolean |
|
94
|
|
|
* @see contains() |
|
95
|
|
|
*/ |
|
96
|
6 |
|
public function offsetExists($enumerator) |
|
97
|
|
|
{ |
|
98
|
6 |
|
return $this->contains($enumerator); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get mapped data for the given enumerator |
|
103
|
|
|
* @param Enum|null|boolean|int|float|string $enumerator |
|
104
|
|
|
* @return mixed |
|
105
|
|
|
* @throws InvalidArgumentException On an invalid given enumerator |
|
106
|
|
|
*/ |
|
107
|
8 |
|
public function offsetGet($enumerator) |
|
108
|
|
|
{ |
|
109
|
8 |
|
$enumeration = $this->enumeration; |
|
110
|
8 |
|
return parent::offsetGet($enumeration::get($enumerator)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Attach a new enumerator or overwrite an existing one |
|
115
|
|
|
* @param Enum|null|boolean|int|float|string $enumerator |
|
116
|
|
|
* @param mixed $data |
|
117
|
|
|
* @return void |
|
118
|
|
|
* @throws InvalidArgumentException On an invalid given enumerator |
|
119
|
|
|
* @see attach() |
|
120
|
|
|
*/ |
|
121
|
6 |
|
public function offsetSet($enumerator, $data = null) |
|
122
|
|
|
{ |
|
123
|
6 |
|
$enumeration = $this->enumeration; |
|
124
|
6 |
|
parent::offsetSet($enumeration::get($enumerator), $data); |
|
125
|
4 |
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Detach an existing enumerator |
|
129
|
|
|
* @param Enum|null|boolean|int|float|string $enumerator |
|
130
|
|
|
* @return void |
|
131
|
|
|
* @throws InvalidArgumentException On an invalid given enumerator |
|
132
|
|
|
* @see detach() |
|
133
|
|
|
*/ |
|
134
|
4 |
|
public function offsetUnset($enumerator) |
|
135
|
|
|
{ |
|
136
|
4 |
|
$enumeration = $this->enumeration; |
|
137
|
4 |
|
parent::offsetUnset($enumeration::get($enumerator)); |
|
138
|
4 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Get the current value |
|
142
|
|
|
* @return mixed |
|
143
|
|
|
*/ |
|
144
|
2 |
|
public function current() |
|
145
|
|
|
{ |
|
146
|
2 |
|
return parent::getInfo(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Get the current key |
|
151
|
|
|
* @return Enum|null |
|
|
|
|
|
|
152
|
|
|
*/ |
|
153
|
2 |
|
public function key() |
|
154
|
|
|
{ |
|
155
|
2 |
|
return parent::current(); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.