Skip to content

Commit 7ce9fc7

Browse files
committed
Added BencodeSerializable interface
1 parent a71bdfe commit 7ce9fc7

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ print_r(Bencode::decode('d3:bar4:spam3:fooi42ee'));
2323
```
2424
ArrayObject Object
2525
(
26-
[bar] => spam
27-
[foo] => 42
26+
[storage:ArrayObject:private] => Array
27+
(
28+
[bar] => spam
29+
[foo] => 42
30+
)
31+
2832
)
2933
```
3034

@@ -38,6 +42,33 @@ print_r(Bencode::encode(['foo' => 42, 'bar' => 'spam']));
3842
d3:bar4:spam3:fooi42ee
3943
```
4044

45+
Supported types are as follow:
46+
47+
- `array`, `int`, and `string` values are encoded natively.
48+
- `float` values that can be losslessly converted to integers are coerced to `int`.
49+
- `bool` values are coerced to `int`.
50+
- An object that implements `s9e\Bencode\BencodeSerializable` is encoded as the value returned by its `bencodeSerialize()` method.
51+
- The properties of an `stdClass` object are encoded in a dictionary.
52+
- An instance of `ArrayObject` is treated as an array.
53+
54+
```php
55+
use s9e\Bencode\Bencode;
56+
use s9e\Bencode\BencodeSerializable;
57+
58+
$bencodable = new class implements BencodeSerializable
59+
{
60+
public function bencodeSerialize(): array|int|string
61+
{
62+
return 42;
63+
}
64+
};
65+
66+
print_r(Bencode::encode($bencodable));
67+
```
68+
```
69+
i42e
70+
```
71+
4172
#### Handle exceptions
4273

4374
```php

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
},
1111
"require-dev": {
1212
"phpunit/phpunit": ">=10.0",
13-
"nikic/php-fuzzer": "*"
13+
"nikic/php-fuzzer": "*",
14+
"s9e/repdoc": "dev-wip"
1415
},
1516
"autoload": {
1617
"psr-4": {

src/BencodeSerializable.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* @package s9e\Bencode
5+
* @copyright Copyright (c) The s9e authors
6+
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
7+
*/
8+
namespace s9e\Bencode;
9+
10+
interface BencodeSerializable
11+
{
12+
/**
13+
* Serialize this object that can be encoded with Bencode::encode()
14+
*
15+
* @return array|int|string
16+
*/
17+
public function bencodeSerialize(): array|int|string;
18+
}

src/Encoder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ protected static function encodeIndexedArray(array $array): string
9191

9292
protected static function encodeObject(object $value): string
9393
{
94+
if ($value instanceof BencodeSerializable)
95+
{
96+
return static::encode($value->bencodeSerialize());
97+
}
9498
if ($value instanceof ArrayObject)
9599
{
96100
return static::encodeAssociativeArray($value->getArrayCopy());

tests/EncoderTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPUnit\Framework\Attributes\DataProvider;
77
use PHPUnit\Framework\TestCase;
88
use TypeError;
9+
use s9e\Bencode\BencodeSerializable;
910
use s9e\Bencode\Encoder;
1011
use s9e\Bencode\Exceptions\EncodingException;
1112
use stdClass;
@@ -60,6 +61,16 @@ public static function getEncodeTests()
6061
'de',
6162
new class extends stdClass {}
6263
],
64+
[
65+
'i42e',
66+
new class implements BencodeSerializable
67+
{
68+
public function bencodeSerialize(): array|int|string
69+
{
70+
return 42;
71+
}
72+
}
73+
],
6374
[
6475
'd3:fooi1ee',
6576
['foo' => 1]

0 commit comments

Comments
 (0)