Code

< 40 %
40-60 %
> 60 %
1
<?php
2
3
/**
4
 * Part of SplTypes package.
5
 *
6
 * (c) Adrien Loyant <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Ducks\Component\SplTypes;
15
16
/**
17
 * SplEnumBackedTrait expose magic method in order to use an SplEnum class which implements SplBackedEnum.
18
 *
19
 * @template T
20
 *
21
 * @phpstan-require-extends SplEnum
22
 * @phpstan-require-implements SplBackedEnum
23
 *
24
 * @psalm-api
25
 */
26
trait SplEnumBackedTrait
27
{
28
    use SplEnumUnitTrait {
29
        SplEnumUnitTrait::__serialize as private __unitSerialize;
30
        SplEnumUnitTrait::__unserialize as private __unitUnserialize;
31
        SplEnumUnitTrait::__set_state as private __unitSetState;
32
        SplEnumUnitTrait::__debugInfo as private __unitDebugInfo;
33
    }
34
35
    /**
36
     * Serialize object.
37
     *
38
     * @return mixed[]
39
     *
40
     * @phpstan-return array<string,mixed>
41
     *
42
     * @psalm-suppress LessSpecificImplementedReturnType
43
     */
44 1
    public function __serialize(): array
45
    {
46 1
        $result = $this->__unitSerialize();
47
48 1
        return $result;
49
    }
50
51
    /**
52
     * Unserialize object.
53
     *
54
     * @param mixed[] $data
55
     *
56
     * @return void
57
     *
58
     * @phpstan-param array<string,mixed> $data
59
     * @phpstan-return void
60
     *
61
     * @psalm-suppress UnsupportedPropertyReferenceUsage
62
     */
63 1
    public function __unserialize(array $data): void
64
    {
65 1
        $this->__unitUnserialize($data);
66
67 1
        $this->value = &$this->__default;
68
    }
69
70
    /**
71
     * Instanciate an exported object.
72
     *
73
     * @param mixed[] $properties
74
     *
75
     * @return static
76
     *
77
     * @phpstan-param array<string,mixed> $properties
78
     * @phpstan-return static
79
     *
80
     * @psalm-suppress UnsafeInstantiation
81
     */
82
    #[\ReturnTypeWillChange]
83
    public static function __set_state(array $properties): SplBackedEnum
84
    {
85
        /** @phpstan-var T $value */
86
        $value = $properties['value'];
87
88
        $object = self::__unitSetState($properties);
89
        $object->value = $value;
90
91
        /** @var static $object */
92
        return $object;
93
    }
94
95
    /**
96
     * Dumping object.
97
     *
98
     * @return mixed[]
99
     *
100
     * @phpstan-return array<string,mixed>
101
     *
102
     * @psalm-suppress LessSpecificImplementedReturnType
103
     *
104
     * @codeCoverageIgnore
105
     */
106
    public function __debugInfo(): array
107
    {
108
        /** @phpstan-var T $value */
109
        $value = $this->value;
110
111
        $result = $this->__unitDebugInfo();
112
        $result['value'] = $value;
113
114
        return $result;
115
    }
116
}
117