1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MabeEnum; |
4
|
|
|
|
5
|
|
|
use RuntimeException; |
6
|
|
|
use LogicException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Trait to make enumerations serializable |
10
|
|
|
* |
11
|
|
|
* This trait is a workaround to make enumerations serializable. |
12
|
|
|
* |
13
|
|
|
* Please note that this feature breaks singleton behaviour of your enumerations |
14
|
|
|
* if an enumeration will be unserialized after it was instantiated already. |
15
|
|
|
* |
16
|
|
|
* @link https://github.com/marc-mabe/php-enum/issues/52 for further information about this feature |
17
|
|
|
* @link http://github.com/marc-mabe/php-enum for the canonical source repository |
18
|
|
|
* @copyright Copyright (c) 2015 Marc Bennewitz |
19
|
|
|
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License |
20
|
|
|
*/ |
21
|
|
|
trait EnumSerializableTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The method will be defined by MabeEnum\Enum |
25
|
|
|
* @return null|bool|int|float|string |
26
|
|
|
*/ |
27
|
|
|
abstract public function getValue(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Serialized the value of the enumeration |
31
|
|
|
* This will be called automatically on `serialize()` if the enumeration implements the `Serializable` interface |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
2 |
|
public function serialize() |
35
|
|
|
{ |
36
|
2 |
|
return serialize($this->getValue()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Unserializes a given serialized value and push it into the current instance |
41
|
|
|
* This will be called automatically on `unserialize()` if the enumeration implements the `Serializable` interface |
42
|
|
|
* @param string $serialized |
43
|
|
|
* @throws RuntimeException On an unknown or invalid value |
44
|
|
|
* @throws LogicException On changing numeration value by calling this directly |
45
|
|
|
*/ |
46
|
5 |
|
public function unserialize($serialized) |
47
|
|
|
{ |
48
|
5 |
|
$value = unserialize($serialized); |
49
|
5 |
|
$constants = self::getConstants(); |
50
|
5 |
|
$name = array_search($value, $constants, true); |
51
|
5 |
View Code Duplication |
if ($name === false) { |
52
|
2 |
|
$message = is_scalar($value) |
53
|
2 |
|
? 'Unknown value ' . var_export($value, true) |
54
|
2 |
|
: 'Invalid value of type ' . (is_object($value) ? get_class($value) : gettype($value)); |
55
|
2 |
|
throw new RuntimeException($message); |
56
|
|
|
} |
57
|
|
|
|
58
|
3 |
|
$class = get_class($this); |
59
|
3 |
|
$enumerator = $this; |
60
|
3 |
|
$closure = function () use ($class, $name, $value, $enumerator) { |
61
|
3 |
|
if ($this->value !== null && $value !== null) { |
62
|
1 |
|
throw new LogicException('Do not call this directly - please use unserialize($enum) instead'); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
$this->value = $value; |
66
|
|
|
|
67
|
2 |
|
if (!isset(self::$instances[$class][$name])) { |
68
|
1 |
|
self::$instances[$class][$name] = $enumerator; |
69
|
1 |
|
} |
70
|
3 |
|
}; |
71
|
3 |
|
$closure = $closure->bindTo($this, 'MabeEnum\Enum'); |
72
|
3 |
|
$closure(); |
73
|
2 |
|
} |
74
|
|
|
} |
75
|
|
|
|