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 | namespace Ducks\Component\SplTypes; |
||
13 | |||
14 | /** |
||
15 | * Trait used for magic. |
||
16 | * |
||
17 | * @template T |
||
18 | * |
||
19 | * @phpstan-require-extends SplType |
||
20 | * |
||
21 | * @psalm-api |
||
22 | */ |
||
23 | trait SplTypeTrait |
||
24 | { |
||
25 | /** |
||
26 | * Internal enum value. |
||
27 | * |
||
28 | * @var mixed |
||
29 | * |
||
30 | * @phpstan-var T |
||
31 | */ |
||
32 | // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore |
||
33 | protected $__default = null; |
||
34 | |||
35 | /** |
||
36 | * Specify data which should be serialized to JSON. |
||
37 | * |
||
38 | * @return mixed |
||
39 | * |
||
40 | * @phpstan-return T |
||
41 | */ |
||
42 | #[\ReturnTypeWillChange] |
||
43 | 5 | public function jsonSerialize() |
|
44 | { |
||
45 | 5 | return $this->__default; |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * Serialize object. |
||
50 | * |
||
51 | * @return mixed[] |
||
52 | * |
||
53 | * @phpstan-return array<string,T> |
||
54 | */ |
||
55 | 5 | public function __serialize(): array |
|
56 | { |
||
57 | 5 | return [ |
|
58 | 5 | '__default' => $this->__default, |
|
59 | 5 | ]; |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * Unserialize object. |
||
64 | * |
||
65 | * @param mixed[] $data |
||
66 | * |
||
67 | * @return void |
||
68 | * |
||
69 | * @phpstan-param array<string,mixed> $data |
||
70 | * @phpstan-return void |
||
71 | */ |
||
72 | 5 | public function __unserialize(array $data): void |
|
73 | { |
||
74 | /** @phpstan-var T $data['__default'] */ |
||
75 | 5 | $this->__default = $data['__default']; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * Method called when a script tries to call an object as a function. |
||
80 | * |
||
81 | * @return mixed |
||
82 | * |
||
83 | * @phpstan-return T |
||
84 | */ |
||
85 | #[\ReturnTypeWillChange] |
||
86 | 2 | public function &__invoke() |
|
87 | { |
||
88 | 2 | return $this->__default; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Stringify object. |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | 1 | final public function __toString(): string |
|
97 | { |
||
98 | 1 | return (string) $this->__default; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Instanciate an exported object. |
||
103 | * |
||
104 | * @param mixed[] $properties |
||
105 | * |
||
106 | * @return static |
||
107 | * |
||
108 | * @phpstan-param array<string,T> $properties |
||
109 | * @phpstan-return static |
||
110 | * |
||
111 | * @psalm-suppress UnsafeInstantiation |
||
112 | * @psalm-suppress UndefinedDocblockClass |
||
113 | */ |
||
114 | public static function __set_state(array $properties): object |
||
115 | { |
||
116 | // @phpstan-ignore-next-line |
||
117 | return /** @scrutinizer ignore-call */ new static($properties['__default'] ?? null); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Dumping object. |
||
122 | * |
||
123 | * @return mixed[] |
||
124 | * |
||
125 | * @phpstan-return array<string,T> |
||
126 | * |
||
127 | * @codeCoverageIgnore |
||
128 | */ |
||
129 | public function __debugInfo(): array |
||
130 | { |
||
131 | return [ |
||
132 | '__default' => $this->__default, |
||
133 | ]; |
||
134 | } |
||
135 | } |
||
136 |