1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace drupol\valuewrapper\Object; |
||
6 | |||
7 | use Closure; |
||
8 | use SuperClosure\Serializer; |
||
9 | |||
10 | use function call_user_func_array; |
||
11 | use function func_get_args; |
||
12 | use function is_string; |
||
13 | |||
14 | /** |
||
15 | * Class ClosureObject. |
||
16 | */ |
||
17 | class ClosureObject extends ObjectValue |
||
18 | { |
||
19 | /** |
||
20 | * @var \SuperClosure\Serializer |
||
21 | */ |
||
22 | private $serializer; |
||
23 | |||
24 | /** |
||
25 | * ClosureObject constructor. |
||
26 | * |
||
27 | * @param Closure $value |
||
28 | */ |
||
29 | 9 | public function __construct(Closure $value) |
|
30 | { |
||
31 | 9 | parent::__construct($value); |
|
32 | |||
33 | 9 | $this->serializer = new Serializer(); |
|
34 | 9 | } |
|
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | 2 | public function __invoke() |
|
40 | { |
||
41 | 2 | return call_user_func_array($this->value(), func_get_args()); |
|
42 | } |
||
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | 2 | public function hash(): string |
|
48 | { |
||
49 | 2 | return $this->doHash($this->type() . $this->serializer->serialize($this->value())); |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | 1 | public function serialize() |
|
56 | { |
||
57 | 1 | return serialize([ |
|
58 | 1 | 'value' => base64_encode($this->serializer->serialize($this->value())), |
|
59 | ]); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | 1 | public function unserialize($serialized) |
|
66 | { |
||
67 | 1 | $unserialized = unserialize($serialized); |
|
68 | |||
69 | 1 | $decoded = base64_decode($unserialized['value'], true); |
|
70 | |||
71 | 1 | if (is_string($decoded)) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
72 | 1 | $this->set( |
|
73 | 1 | $this->serializer->unserialize($decoded) |
|
74 | ); |
||
75 | } |
||
76 | 1 | } |
|
77 | } |
||
78 |