Skip to content

Commit be41ff9

Browse files
authored
Merge pull request #68 from afk11/README-and-examples
build examples as part of tests, don't want them going stale
2 parents 58d7019 + 9d0edde commit be41ff9

7 files changed

Lines changed: 40 additions & 82 deletions

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ script:
1818
- php vendor/bin/phpunit
1919
- vendor/bin/phpstan analyze -l 4 src tests examples
2020
- php vendor/bin/phpcs -n --standard=PSR1,PSR2 --report=full src/ tests/ examples/
21+
- ./validate_examples.sh
2122

2223
after_success:
2324
- wget https://scrutinizer-ci.com/ocular.phar

README.md

Lines changed: 11 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -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+

examples/usingBuffer.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
<?php
22

3-
require "../vendor/autoload.php";
3+
require __DIR__ . "/../vendor/autoload.php";
44

55
use BitWasp\Buffertools\Buffer;
66

77
// Binary data and ASCII can be passed directly to a Buffer
88
$binary = new Buffer('hello world');
9-
echo $binary->getBinary() . PHP_EOL;
10-
echo $binary->getHex() . PHP_EOL;
9+
echo "Buffer 1: binary : " . $binary->getBinary() . PHP_EOL;
10+
echo "Buffer 1: hex : " . $binary->getHex() . PHP_EOL;
11+
echo PHP_EOL;
1112

12-
// BufferHex and BufferInt convert data to binary
13+
// Buffer::hex and Buffer::int convert data to binary
1314
$hex = Buffer::hex('68656c6c6f20776f726c64');
14-
echo $binary->getBinary() . PHP_EOL;
15-
echo $hex->getHex() . PHP_EOL;
15+
echo "Buffer 2: binary : " . $hex->getBinary() . PHP_EOL;
16+
echo "Buffer 2: hex : " . $hex->getHex() . PHP_EOL;
17+
$first = $hex->slice(0, 1);
18+
echo "Buffer 2: 1st char: " . $first->getHex() . PHP_EOL;
19+
echo PHP_EOL;
1620

1721
// All Buffers expose getBinary(), getInt(), getHex()
1822
$int = Buffer::int(65);
19-
echo $int->getBinary() . PHP_EOL;
20-
echo $int->getInt() . PHP_EOL;
21-
echo $int->getHex() . PHP_EOL;
23+
echo "Buffer 3: binary: " . $int->getBinary() . PHP_EOL;
24+
echo "Buffer 3: int : " . $int->getInt() . PHP_EOL;
25+
echo "Buffer 3: hex : " . $int->getHex() . PHP_EOL;
26+
echo PHP_EOL;

examples/usingParser.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22

3-
require "../vendor/autoload.php";
3+
require __DIR__ . "/../vendor/autoload.php";
44

55
use BitWasp\Buffertools\Buffer;
66
use BitWasp\Buffertools\Parser;
77

88
// Parsers read Buffers
9-
$buffer = new Buffer('abc');
9+
$buffer = new Buffer('abcd01020304');
1010
$parser = new Parser($buffer);
1111

1212
// Call readBytes to unpack the data
@@ -15,7 +15,8 @@
1515
$set = [
1616
$parser->readBytes(1),
1717
$parser->readBytes(1),
18-
$parser->readBytes(1)
18+
$parser->readBytes(1),
19+
$parser->readBytes(4)
1920
];
2021

2122
foreach ($set as $item) {

examples/usingTemplates.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
require "../vendor/autoload.php";
3+
require __DIR__ . "/../vendor/autoload.php";
44

55
use BitWasp\Buffertools\Buffer;
66
use BitWasp\Buffertools\Parser;

examples/usingTemplates2.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
require "../vendor/autoload.php";
3+
require __DIR__ . "/../vendor/autoload.php";
44

55
use BitWasp\Buffertools\Buffer;
66
use BitWasp\Buffertools\Parser;

validate_examples.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
for i in examples/*.php; do
3+
php $i > /dev/null
4+
if [ $? -ne 0 ]; then
5+
echo "Error running example code: $i";
6+
exit -1
7+
fi;
8+
done

0 commit comments

Comments
 (0)