forked from ProtonMail/buffertools-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usingTemplates.php
38 lines (29 loc) · 880 Bytes
/
usingTemplates.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
require __DIR__ . "/../vendor/autoload.php";
use BitWasp\Buffertools\Buffer;
use BitWasp\Buffertools\Parser;
use BitWasp\Buffertools\TemplateFactory;
// Parsers read Buffers
$setBuffer = new Buffer('aaabccdeee');
$setParser = new Parser($setBuffer);
// Read data into $set
$set = (new TemplateFactory())
->bytestring(3)
->bytestring(1)
->bytestring(2)
->bytestring(1)
->bytestring(3)
->getTemplate()
->parse($setParser);
print_r($set);
// We can serialize a set:
// data starts with the size of the set
// write each member as a buffer
// a structure-specific parser reads the internal structure (not needed for writing)
$vector = (new TemplateFactory())
->vector(function () {
}) // can be null, since we're writing
->getTemplate()
->write([$set]);
echo $vector->getHex() . PHP_EOL;
echo $vector->getBinary() . PHP_EOL;