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 | * SplEnumUnitTrait expose magic method in order to use an SplEnum class which implements SplUnitEnum. |
||
18 | * |
||
19 | * @phpstan-require-extends SplEnum |
||
20 | * @phpstan-require-implements SplUnitEnum |
||
21 | * |
||
22 | * @psalm-api |
||
23 | */ |
||
24 | trait SplEnumUnitTrait |
||
25 | { |
||
26 | /** |
||
27 | * Serialize object. |
||
28 | * |
||
29 | * @return mixed[] |
||
30 | * |
||
31 | * @phpstan-return array<string,mixed> |
||
32 | */ |
||
33 | 1 | public function __serialize(): array |
|
34 | { |
||
35 | 1 | $result = parent::__serialize(); |
|
36 | 1 | $result['name'] = $this->name; |
|
37 | |||
38 | 1 | return $result; |
|
39 | } |
||
40 | |||
41 | /** |
||
42 | * Unserialize object. |
||
43 | * |
||
44 | * @param mixed[] $data |
||
45 | * |
||
46 | * @return void |
||
47 | * |
||
48 | * @phpstan-param array<string,mixed> $data |
||
49 | * @phpstan-return void |
||
50 | */ |
||
51 | 1 | public function __unserialize(array $data): void |
|
52 | { |
||
53 | 1 | parent::__unserialize($data); |
|
54 | |||
55 | 1 | $this->name = (string) $data['name']; |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * Instanciate an exported object. |
||
60 | * |
||
61 | * @param mixed[] $properties |
||
62 | * |
||
63 | * @return static |
||
64 | * |
||
65 | * @phpstan-param array<string,mixed> $properties |
||
66 | * @phpstan-return static |
||
67 | * |
||
68 | * @psalm-suppress UnsafeInstantiation |
||
69 | */ |
||
70 | public static function __set_state(array $properties): SplUnitEnum |
||
71 | { |
||
72 | /** |
||
73 | * @var static $object |
||
74 | */ |
||
75 | // @phpstan-ignore-next-line |
||
76 | $object = /** @scrutinizer ignore-call */ new static(); |
||
77 | $object->name = (string) $properties['name']; |
||
78 | |||
79 | return $object; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Dumping object. |
||
84 | * |
||
85 | * @return string[] |
||
86 | * |
||
87 | * @phpstan-return array<string,mixed> |
||
88 | * |
||
89 | * @codeCoverageIgnore |
||
90 | */ |
||
91 | public function __debugInfo(): array |
||
92 | { |
||
93 | $result = parent::__debugInfo(); |
||
94 | $result['name'] = $this->name; |
||
95 | |||
96 | return $result; |
||
97 | } |
||
98 | } |
||
99 |