@@ -23,83 +23,26 @@ This library provides a `Buffer` and `Parser` class to make dealing with binary
2323## Examples
2424
2525 Buffer's are immutable classes to store binary data.
26- BufferHex will convert the provided data to binary, as will BufferInt.
26+ Buffer::hex can be used to initialize from hex
27+ Buffer::int can be used to initialize from a positive decimal integer (int|string)
28+
2729 Buffer's main methods are:
2830 - getBinary()
2931 - getHex()
3032 - getInt()
31-
33+
3234 Parser will read Buffers.
3335 Parser's main methods are:
3436 - readBytes()
35- - getArray()
36- - getVarInt()
37- - getString()
3837 - writeBytes()
38+ - readArray()
39+ - writeArray()
3940
4041 In most cases, the interface offered by Parser should not be used directly.
4142 Instead, Templates expose read/write access to larger serialized structures.
4243
43- ### Using Parser to read binary data:
44- ``` php
45- use BitWasp\Buffertools\Buffer;
46- use BitWasp\Buffertools\Parser;
47-
48- // Parsers read Buffers
49- $buffer = new Buffer('abc');
50- $parser = new Parser($buffer);
51-
52- // Call readBytes to unpack the data
53- /** @var Buffer[] $set */
54- $set = [
55- $parser->readBytes(1),
56- $parser->readBytes(1),
57- $parser->readBytes(1)
58- ];
59-
60- foreach ($set as $item) {
61- echo $item->getBinary() . PHP_EOL;
62- }
63- ```
64-
65- ### Using Templates
66- ``` php
67-
68- use BitWasp\Buffertools\Buffer;
69- use BitWasp\Buffertools\Parser;
70- use BitWasp\Buffertools\TemplateFactory;
71-
72- $structure = (object) [
73- 'hash' => hash('sha256', 'abc'),
74- 'message_id' => 9123,
75- 'message' => "Hi there! What's up?"
76- ];
77-
78- // Templates are read/write
79- $template = (new TemplateFactory)
80- ->bytestring(32)
81- ->uint32()
82- ->varstring()
83- ->getTemplate();
84-
85- // Write the structure
86- $binary = $template->write([
87- Buffer::hex($structure->hash),
88- $structure->message_id,
89- new Buffer($structure->message)
90- ]);
91-
92- echo $binary->getHex() . "\n";
93-
94- // Use the template to read resulting Buffer
95- $parsed = $template->parse(new Parser($binary));
96-
97- $p = (object) [
98- 'hash' => $parsed[0]->getHex(),
99- 'message_id' => $parsed[1],
100- 'message' => $parsed[2]->getBinary()
101- ];
102-
103- print_r($p);
104- ```
105-
44+ - [ Example 1: Using buffer to wrap binary data] ( ./examples/usingBuffer.php )
45+ - [ Example 2: Using parser to extract binary data] ( ./examples/usingParser.php )
46+ - [ Example 3: Using templates to read multiple elements from a parser] ( ./examples/usingTemplates.php )
47+ - [ Example 4: Using templates to read/write structured messages] ( ./examples/usingTemplates2.php )
48+
0 commit comments