Completed
Pull Request — master (#58)
by Danny
11:05
created

EnumSerializableTrait::unserialize()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 28
Code Lines 19

Duplication

Lines 6
Ratio 21.43 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 6
loc 28
ccs 0
cts 23
cp 0
rs 6.7272
cc 7
eloc 19
nc 5
nop 1
crap 56
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
    public function serialize()
35
    {
36
        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
    public function unserialize($serialized)
47
    {
48
        $value     = unserialize($serialized);
49
        $constants = self::getConstants();
50
        $name      = array_search($value, $constants, true);
51 View Code Duplication
        if ($name === false) {
52
            $message = is_scalar($value)
53
                ? 'Unknown value ' . var_export($value, true)
54
                : 'Invalid value of type ' . (is_object($value) ? get_class($value) : gettype($value));
55
            throw new RuntimeException($message);
56
        }
57
58
        $class      = get_class($this);
59
        $enumerator = $this;
60
        $closure    = function () use ($class, $name, $value, $enumerator) {
61
            if ($this->value !== null && $value !== null) {
62
                throw new LogicException('Do not call this directly - please use unserialize($enum) instead');
63
            }
64
65
            $this->value = $value;
66
67
            if (!isset(self::$instances[$class][$name])) {
68
                self::$instances[$class][$name] = $enumerator;
69
            }
70
        };
71
        $closure = $closure->bindTo($this, 'MabeEnum\Enum');
72
        $closure();
73
    }
74
}
75