1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MabeEnum; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
use LogicException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Trait to make enumerations serializable |
12
|
|
|
* |
13
|
|
|
* This trait is a workaround to make enumerations serializable. |
14
|
|
|
* |
15
|
|
|
* Please note that this feature breaks singleton behaviour of your enumerations |
16
|
|
|
* if an enumeration will be unserialized after it was instantiated already. |
17
|
|
|
* |
18
|
|
|
* @copyright 2019 Marc Bennewitz |
19
|
|
|
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License |
20
|
|
|
* @link http://github.com/marc-mabe/php-enum for the canonical source repository |
21
|
|
|
* @link https://github.com/marc-mabe/php-enum/issues/52 for further information about this feature |
22
|
|
|
*/ |
23
|
|
|
trait EnumSerializableTrait |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The method will be defined by MabeEnum\Enum |
27
|
|
|
* @return null|bool|int|float|string|array |
28
|
|
|
*/ |
29
|
|
|
abstract public function getValue(); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Returns an array of data to be serialized. |
33
|
|
|
* This magic method will be called by serialize() in PHP >= 7.4 |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
5 |
|
public function __serialize(): array |
|
|
|
|
38
|
|
|
{ |
39
|
5 |
|
return ['value' => $this->getValue()]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Receives an array of data to be unserialized on a new instance without constructor. |
44
|
|
|
* This magic method will be called in PHP >= 7.4 is the data where serialized with PHP >= 7.4. |
45
|
|
|
* |
46
|
|
|
* @throws RuntimeException On missing, unknown or invalid value |
|
|
|
|
47
|
|
|
* @throws LogicException On calling this method on an already initialized enumerator |
|
|
|
|
48
|
|
|
* |
49
|
|
|
* @param array $data |
|
|
|
|
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
8 |
|
public function __unserialize(array $data): void |
|
|
|
|
53
|
|
|
{ |
54
|
8 |
|
if (!\array_key_exists('value', $data)) { |
55
|
|
|
throw new RuntimeException('Missing array key "value"'); |
56
|
|
|
} |
57
|
|
|
|
58
|
8 |
|
$value = $data['value']; |
59
|
8 |
|
$constants = self::getConstants(); |
60
|
8 |
|
$name = \array_search($value, $constants, true); |
61
|
8 |
|
if ($name === false) { |
62
|
2 |
|
$message = \is_scalar($value) |
63
|
1 |
|
? 'Unknown value ' . \var_export($value, true) |
64
|
2 |
|
: 'Invalid value of type ' . (\is_object($value) ? \get_class($value) : \gettype($value)); |
65
|
2 |
|
throw new RuntimeException($message); |
66
|
|
|
} |
67
|
|
|
|
68
|
6 |
|
$class = static::class; |
69
|
6 |
|
$enumerator = $this; |
70
|
|
|
$closure = function () use ($class, $name, $value, $enumerator) { |
71
|
6 |
|
if ($value !== null && $this->value !== null) { |
72
|
1 |
|
throw new LogicException('Do not call this directly - please use unserialize($enum) instead'); |
73
|
|
|
} |
74
|
|
|
|
75
|
5 |
|
$this->value = $value; |
|
|
|
|
76
|
|
|
|
77
|
5 |
|
if (!isset(self::$instances[$class][$name])) { |
78
|
2 |
|
self::$instances[$class][$name] = $enumerator; |
|
|
|
|
79
|
|
|
} |
80
|
6 |
|
}; |
81
|
6 |
|
$closure->bindTo($this, Enum::class)(); |
82
|
5 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Serialize the value of the enumeration |
86
|
|
|
* This will be called automatically on `serialize()` if the enumeration implements the `Serializable` interface |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
* @deprecated Since PHP 7.4 |
90
|
|
|
*/ |
91
|
|
|
public function serialize(): string |
92
|
|
|
{ |
93
|
|
|
return \serialize($this->getValue()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Unserializes a given serialized value and push it into the current instance |
98
|
|
|
* This will be called automatically on `unserialize()` if the enumeration implements the `Serializable` interface |
99
|
|
|
* @param string $serialized |
|
|
|
|
100
|
|
|
* @return void |
101
|
|
|
* @throws RuntimeException On an unknown or invalid value |
|
|
|
|
102
|
|
|
* @throws LogicException On calling this method on an already initialized enumerator |
|
|
|
|
103
|
|
|
* @deprecated Since PHP 7.4 |
104
|
|
|
*/ |
105
|
3 |
|
public function unserialize($serialized): void |
|
|
|
|
106
|
|
|
{ |
107
|
3 |
|
$this->__unserialize(['value' => \unserialize($serialized)]); |
108
|
1 |
|
} |
109
|
|
|
} |
110
|
|
|
|