Skip to content

Commit

Permalink
Added BencodeSerializable interface
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshyPHP committed Oct 16, 2023
1 parent a71bdfe commit 7ce9fc7
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ print_r(Bencode::decode('d3:bar4:spam3:fooi42ee'));
```
ArrayObject Object
(
[bar] => spam
[foo] => 42
[storage:ArrayObject:private] => Array
(
[bar] => spam
[foo] => 42
)
)
```

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

Supported types are as follow:

- `array`, `int`, and `string` values are encoded natively.
- `float` values that can be losslessly converted to integers are coerced to `int`.
- `bool` values are coerced to `int`.
- An object that implements `s9e\Bencode\BencodeSerializable` is encoded as the value returned by its `bencodeSerialize()` method.
- The properties of an `stdClass` object are encoded in a dictionary.
- An instance of `ArrayObject` is treated as an array.

```php
use s9e\Bencode\Bencode;
use s9e\Bencode\BencodeSerializable;

$bencodable = new class implements BencodeSerializable
{
public function bencodeSerialize(): array|int|string
{
return 42;
}
};

print_r(Bencode::encode($bencodable));
```
```
i42e
```

#### Handle exceptions

```php
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
},
"require-dev": {
"phpunit/phpunit": ">=10.0",
"nikic/php-fuzzer": "*"
"nikic/php-fuzzer": "*",
"s9e/repdoc": "dev-wip"
},
"autoload": {
"psr-4": {
Expand Down
18 changes: 18 additions & 0 deletions src/BencodeSerializable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

/**
* @package s9e\Bencode
* @copyright Copyright (c) The s9e authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\Bencode;

interface BencodeSerializable
{
/**
* Serialize this object that can be encoded with Bencode::encode()
*
* @return array|int|string
*/
public function bencodeSerialize(): array|int|string;
}
4 changes: 4 additions & 0 deletions src/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ protected static function encodeIndexedArray(array $array): string

protected static function encodeObject(object $value): string
{
if ($value instanceof BencodeSerializable)
{
return static::encode($value->bencodeSerialize());
}
if ($value instanceof ArrayObject)
{
return static::encodeAssociativeArray($value->getArrayCopy());
Expand Down
11 changes: 11 additions & 0 deletions tests/EncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use TypeError;
use s9e\Bencode\BencodeSerializable;
use s9e\Bencode\Encoder;
use s9e\Bencode\Exceptions\EncodingException;
use stdClass;
Expand Down Expand Up @@ -60,6 +61,16 @@ public static function getEncodeTests()
'de',
new class extends stdClass {}
],
[
'i42e',
new class implements BencodeSerializable
{
public function bencodeSerialize(): array|int|string
{
return 42;
}
}
],
[
'd3:fooi1ee',
['foo' => 1]
Expand Down

0 comments on commit 7ce9fc7

Please sign in to comment.