Skip to content

Commit 717ca54

Browse files
committed
Explain BencodeSerializable in README
1 parent 09aceb3 commit 717ca54

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

README.md

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,45 @@ $encoded = Bencode::encode([ // array will become dictionary
3131
// objects
3232

3333
// traversable objects and stdClass become dictionaries
34-
Bencode::encode(new ArrayObject([1,2,3])); // "d1:0i1e1:1i2e1:2i3ee"
34+
$encoded = Bencode::encode(new ArrayObject([1,2,3])); // "d1:0i1e1:1i2e1:2i3ee"
3535
$std = new stdClass();
3636
$std->a = '123';
3737
$std->b = 456;
38-
Bencode::encode($std); // "d1:a3:1231:bi456ee"
38+
$encoded = Bencode::encode($std); // "d1:a3:1231:bi456ee"
3939

4040
// you can force traversable to become a list by wrapping it with SandFoxMe\Bencode\Types\ListType
4141
// keys will be discarded in that case
4242
use SandFoxMe\Bencode\Types\ListType;
43-
Bencode::encode(new ListType(new ArrayObject([1,2,3]))); // "li1ei2ei3ee"
43+
$encoded = Bencode::encode(new ListType(new ArrayObject([1,2,3]))); // "li1ei2ei3ee"
4444

4545
// other objects will be converted to string if possible or generate an error if not
46-
Bencode::encode(new class { function __toString() { return 'I am string'; } }); // "11:I am string"
46+
$encoded = Bencode::encode(new class { function __toString() { return 'I am string'; } }); // "11:I am string"
47+
```
48+
49+
### BencodeSerializable
50+
51+
You can also force object representation by implementing BencodeSerializable interface.
52+
This will work exactly like JsonSerializable interface.
53+
```php
54+
<?php
55+
56+
use SandFoxMe\Bencode\Bencode;
57+
use SandFoxMe\Bencode\Types\BencodeSerializable;
58+
59+
class MyFile implements BencodeSerializable
60+
{
61+
public function bencodeSerialize() {
62+
return [
63+
'class' => static::class,
64+
'name' => 'myfile.torrent',
65+
'size' => 5 * 1024 * 1024,
66+
];
67+
}
68+
}
69+
70+
$file = new MyFile;
71+
72+
$encoded = Bencode::encode($file); // "d5:class6:MyFile4:name14:myfile.torrent4:sizei5242880ee"
4773
```
4874

4975
## Decoding
@@ -54,7 +80,7 @@ Bencode::encode(new class { function __toString() { return 'I am string'; } });
5480
use SandFoxMe\Bencode\Bencode;
5581

5682
// simple decoding, lists and dictionaries will be arrays
57-
Bencode::decode("d3:arrli1ei2ei3ei4ee4:booli1e5:float6:3.14153:inti123e6:string9:test\0teste");
83+
$data = Bencode::decode("d3:arrli1ei2ei3ei4ee4:booli1e5:float6:3.14153:inti123e6:string9:test\0teste");
5884
// [
5985
// "arr" => [1,2,3,4],
6086
// "bool" => 1,
@@ -64,7 +90,7 @@ Bencode::decode("d3:arrli1ei2ei3ei4ee4:booli1e5:float6:3.14153:inti123e6:string9
6490
// ]
6591

6692
// You can control lists and dictionaries types with options
67-
Bencode::decode("...", [
93+
$data = Bencode::decode("...", [
6894
'dictionaryType' => ArrayObject::class, // pass class name, new $type($array) will be created
6995
'listType' => function ($array) { // or callback for greater flexibility
7096
return new ArrayObject($array, ArrayObject::ARRAY_AS_PROPS);
@@ -91,12 +117,12 @@ Add this to your `composer.json`:
91117
```json
92118
{
93119
"require": {
94-
"sandfoxme/bencode": "^1.0.0"
120+
"sandfoxme/bencode": "^1.2.0"
95121
}
96122
}
97123
```
98124

99-
or run `composer require 'sandfoxme/bencode:^1.0.0'`.
125+
or run `composer require 'sandfoxme/bencode:^1.2.0'`.
100126

101127
## License
102128

0 commit comments

Comments
 (0)