@@ -23,51 +23,58 @@ Encoding
2323Scalars and arrays
2424------------------
2525
26+ .. warning :: *Possibly breaking change in 1.4 and 2.4:*
27+
28+ Before 1.4 and 2.4 ``null `` was encoded as empty string and ``false `` was encoded as 0.
29+ Since bencode spec doesn't have bool and null values, it is not considered a bc break.
30+ Judging by info[private] behavior in BitTorrent spec, the old behavior could be considered as a bug.
31+
2632.. code-block :: php
2733
28- <?php
34+ <?php
2935
30- use SandFox\Bencode\Bencode;
36+ use SandFox\Bencode\Bencode;
3137
32- $encoded = Bencode::encode([ // array will become dictionary
33- 'arr' => [1,2,3,4], // sequential array will become a list
34- 'int' => 123, // integer is stored as is
35- 'float' => 3.1415, // float will become a string
36- 'bool' => true, // bool will be an integer 1 or 0
37- 'string' => "test\0test", // string can contain any binary data
38- ]); // "d3:arrli1ei2ei3ei4ee4:booli1e5:float6:3.14153:inti123e6:string9:test\0teste"
38+ $encoded = Bencode::encode([ // array will become dictionary
39+ 'arr' => [1,2,3,4], // sequential array will become a list
40+ 'int' => 123, // integer is stored as is
41+ 'float' => 3.1415, // float will become a string
42+ 'true' => true, // true will be an integer 1
43+ 'false' => false, // false and null values will be skipped
44+ 'string' => "test\0test", // string can contain any binary data
45+ ]); // "d3:arrli1ei2ei3ei4ee5:float6:3.14153:inti123e6:string9:test\0test4:truei1ee"
3946
4047 Objects
4148-------
4249
4350.. code-block :: php
4451
45- <?php
52+ <?php
4653
47- use SandFox\Bencode\Bencode;
54+ use SandFox\Bencode\Bencode;
4855
49- // traversable objects and stdClass become dictionaries
50- $encoded = Bencode::encode(new ArrayObject([1,2,3])); // "d1:0i1e1:1i2e1:2i3ee"
51- $std = new stdClass();
52- $std->a = '123';
53- $std->b = 456;
54- $encoded = Bencode::encode($std); // "d1:a3:1231:bi456ee"
56+ // traversable objects and stdClass become dictionaries
57+ $encoded = Bencode::encode(new ArrayObject([1,2,3])); // "d1:0i1e1:1i2e1:2i3ee"
58+ $std = new stdClass();
59+ $std->a = '123';
60+ $std->b = 456;
61+ $encoded = Bencode::encode($std); // "d1:a3:1231:bi456ee"
5562
56- // you can force traversable to become a list by wrapping it with ListType
57- // keys will be discarded in that case
58- use SandFox\Bencode\Types\ListType;
59- $encoded = Bencode::encode(new ListType(new ArrayObject([1,2,3]))); // "li1ei2ei3ee"
63+ // you can force traversable to become a list by wrapping it with ListType
64+ // keys will be discarded in that case
65+ use SandFox\Bencode\Types\ListType;
66+ $encoded = Bencode::encode(new ListType(new ArrayObject([1,2,3]))); // "li1ei2ei3ee"
6067
61- // other objects will be converted to string if possible or generate an error if not
62- class ToString
63- {
64- public function __toString()
65- {
66- return 'I am string';
67- }
68- }
68+ // other objects will be converted to string if possible or generate an error if not
69+ class ToString
70+ {
71+ public function __toString()
72+ {
73+ return 'I am string';
74+ }
75+ }
6976
70- $encoded = Bencode::encode(new ToString()); // "11:I am string"
77+ $encoded = Bencode::encode(new ToString()); // "11:I am string"
7178
7279 BencodeSerializable
7380-------------------
@@ -134,12 +141,12 @@ Working with files
134141
135142.. code-block :: php
136143
137- <?php
144+ <?php
138145
139- use SandFox\Bencode\Bencode;
146+ use SandFox\Bencode\Bencode;
140147
141- $data = Bencode::load('testfile.torrent'); // load data from bencoded file
142- Bencode::dump('testfile.torrent', $data); // save data to the bencoded file
148+ $data = Bencode::load('testfile.torrent'); // load data from bencoded file
149+ Bencode::dump('testfile.torrent', $data); // save data to the bencoded file
143150
144151 Upgrade from 1.x
145152================
0 commit comments