|
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
|
|
|
|
|
31
|
|
|
abstract class Enum |
|
32
|
|
|
{ |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string[] |
|
36
|
|
|
*/ |
|
37
|
|
|
protected static $values = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var $this[] |
|
41
|
|
|
*/ |
|
42
|
|
|
protected static $instances = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $value; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* PROTECTED!! Not callable directly. |
|
51
|
|
|
* @see static::instance() |
|
52
|
|
|
* @see static::__callStatic() |
|
53
|
|
|
* @internal |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $value |
|
56
|
|
|
*/ |
|
57
|
7 |
|
protected function __construct($value) |
|
58
|
|
|
{ |
|
59
|
7 |
|
if (empty(static::$values)) { |
|
60
|
1 |
|
throw new InvalidEnumSetupException('Incorrect setup! Enum '.get_called_class().' doesn\'t have its static::$values set.'); |
|
61
|
|
|
} |
|
62
|
6 |
|
if (!in_array($value, static::$values)) { |
|
63
|
3 |
|
throw new InvalidEnumValueException('Enum '.get_called_class().' does not contain value '.$value.'. Possible values are: '.implode(', ', static::$values)); |
|
64
|
|
|
} |
|
65
|
3 |
|
$this->value = $value; |
|
66
|
3 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* String representation |
|
70
|
|
|
*/ |
|
71
|
48 |
|
public function __toString() |
|
72
|
|
|
{ |
|
73
|
48 |
|
return $this->value; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $method |
|
78
|
|
|
* @param array $args |
|
79
|
|
|
* |
|
80
|
|
|
* @return \SpareParts\Enum\Enum |
|
81
|
|
|
*/ |
|
82
|
56 |
|
public static function __callStatic($method, $args) |
|
83
|
|
|
{ |
|
84
|
56 |
|
if ($method[0] === '_') { |
|
85
|
7 |
|
$method = substr($method, 1); |
|
86
|
|
|
} |
|
87
|
56 |
|
return static::instance($method); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $value |
|
92
|
|
|
* |
|
93
|
|
|
* @return $this |
|
94
|
|
|
*/ |
|
95
|
62 |
|
public static function instance($value) |
|
96
|
|
|
{ |
|
97
|
62 |
|
if (!isset(static::$instances[get_called_class()][$value])) { |
|
98
|
7 |
|
static::$instances[get_called_class()][$value] = new static($value); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
58 |
|
return static::$instances[get_called_class()][$value]; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
public function __clone() |
|
105
|
|
|
{ |
|
106
|
1 |
|
throw new OperationNotAllowedException('Singleton cannot be cloned.'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
1 |
|
public function __sleep() |
|
110
|
|
|
{ |
|
111
|
1 |
|
throw new OperationNotAllowedException('Singleton cannot be serialized.'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
public function __wakeup() |
|
115
|
|
|
{ |
|
116
|
1 |
|
throw new OperationNotAllowedException('Singleton cannot be serialized'); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|