1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Networking\Serializer\Structure; |
6
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Networking\Structure\AlertDetail; |
8
|
|
|
use BitWasp\Buffertools\BufferInterface; |
9
|
|
|
use BitWasp\Buffertools\Parser; |
10
|
|
|
use BitWasp\Buffertools\Buffer; |
11
|
|
|
use BitWasp\Buffertools\TemplateFactory; |
12
|
|
|
|
13
|
|
|
class AlertDetailSerializer |
14
|
|
|
{ |
15
|
6 |
|
/** |
16
|
|
|
* @return \BitWasp\Buffertools\Template |
17
|
6 |
|
*/ |
18
|
6 |
|
public function getTemplate() |
19
|
6 |
|
{ |
20
|
6 |
|
return (new TemplateFactory()) |
21
|
6 |
|
->uint32le() |
22
|
6 |
|
->uint64le() |
23
|
|
|
->uint64le() |
24
|
6 |
|
->uint32le() |
25
|
6 |
|
->uint32le() |
26
|
6 |
|
->vector(function (Parser $parser) { |
27
|
6 |
|
return $parser->readBytes(4, true)->getInt(); |
28
|
6 |
|
}) |
29
|
6 |
|
->uint32le() |
30
|
6 |
|
->uint32le() |
31
|
6 |
|
->vector(function (Parser $parser) { |
32
|
6 |
|
return $parser->readBytes(4, true)->getInt(); |
33
|
6 |
|
}) |
34
|
6 |
|
->uint32le() |
35
|
|
|
->varstring() |
36
|
|
|
->varstring() |
37
|
6 |
|
->getTemplate(); |
38
|
|
|
} |
39
|
6 |
|
|
40
|
|
|
public function fromParser(Parser $parser) |
41
|
|
|
{ |
42
|
6 |
|
list ($version, $relayUntil, $expiration, |
43
|
|
|
$id, $cancel, $setCancels, $minVer, |
44
|
6 |
|
$maxVer, $setSubVers, $priority, |
45
|
|
|
$comment, $statusBar) = $this->getTemplate()->parse($parser); |
46
|
6 |
|
|
47
|
|
|
return new AlertDetail( |
48
|
6 |
|
(int) $version, |
49
|
|
|
(int) $relayUntil, |
50
|
6 |
|
(int) $expiration, |
51
|
|
|
(int) $id, |
52
|
6 |
|
(int) $cancel, |
53
|
|
|
(int) $minVer, |
54
|
6 |
|
(int) $maxVer, |
55
|
|
|
(int) $priority, |
56
|
6 |
|
$comment, |
57
|
|
|
$statusBar, |
58
|
6 |
|
$setCancels, |
59
|
|
|
$setSubVers |
60
|
6 |
|
); |
61
|
|
|
} |
62
|
6 |
|
|
63
|
|
|
/** |
64
|
6 |
|
* @param BufferInterface $data |
65
|
|
|
* @return AlertDetail |
66
|
6 |
|
*/ |
67
|
4 |
|
public function parse(BufferInterface $data): AlertDetail |
68
|
4 |
|
{ |
69
|
4 |
|
return $this->fromParser(new Parser($data)); |
70
|
4 |
|
} |
71
|
4 |
|
|
72
|
4 |
|
/** |
73
|
4 |
|
* @param AlertDetail $detail |
74
|
4 |
|
* @return BufferInterface |
75
|
4 |
|
*/ |
76
|
4 |
|
public function serialize(AlertDetail $detail): BufferInterface |
77
|
4 |
|
{ |
78
|
|
|
$setCancels = []; |
79
|
4 |
|
foreach ($detail->getSetCancel() as $toCancel) { |
80
|
|
|
$setCancels[] = new Buffer(pack('V', $toCancel)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$setSubVers = []; |
84
|
|
|
foreach ($detail->getSetSubVer() as $subVer) { |
85
|
|
|
$setSubVers[] = new Buffer(pack('V', $subVer)); |
86
|
3 |
|
} |
87
|
|
|
|
88
|
3 |
|
return $this->getTemplate()->write([ |
89
|
|
|
$detail->getVersion(), |
90
|
|
|
$detail->getRelayUntil(), |
91
|
|
|
$detail->getExpiration(), |
92
|
|
|
$detail->getId(), |
93
|
|
|
$detail->getCancel(), |
94
|
|
|
$setCancels, |
95
|
6 |
|
$detail->getMinVer(), |
96
|
|
|
$detail->getMaxVer(), |
97
|
6 |
|
$setSubVers, |
98
|
6 |
|
$detail->getPriority(), |
99
|
6 |
|
$detail->getComment(), |
100
|
6 |
|
$detail->getStatusBar() |
101
|
4 |
|
]); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|