Commit b6b767ee authored by Anton Smirnov's avatar Anton Smirnov
Browse files

Backport useJsonSerializable

parent fcf7d23f
Loading
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@
### 1.next

* Decoder and Encoder are backported from 3.x
* `DictType` backported from 3.x
* `useJsonSerializable` backported from 3.x

### 1.6.2

+1 −0
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@
    },
    "require-dev": {
        "ext-gmp": "*",
        "ext-json": "*",
        "brick/math": "*",
        "mikey179/vfsstream": "^1.6",
        "pear/math_biginteger": "^1.0",
+4 −4
Original line number Diff line number Diff line
@@ -55,7 +55,7 @@ final class Bencode
     */
    public static function encode($data, array $options = []): string
    {
        return (new Encoder())->encode($data);
        return (new Encoder($options))->encode($data);
    }

    /**
@@ -65,9 +65,9 @@ final class Bencode
     * @param resource|null $writeStream Write capable stream. If null, a new php://temp will be created
     * @return resource Original or created stream
     */
    public static function encodeToStream($data, $writeStream = null)
    public static function encodeToStream($data, $writeStream = null, array $options = [])
    {
        return (new Encoder())->encodeToStream($data, $writeStream);
        return (new Encoder($options))->encodeToStream($data, $writeStream);
    }

    /**
@@ -80,6 +80,6 @@ final class Bencode
     */
    public static function dump(string $filename, $data, array $options = []): bool
    {
        return (new Encoder())->dump($data, $filename);
        return (new Encoder($options))->dump($data, $filename);
    }
}
+5 −2
Original line number Diff line number Diff line
@@ -6,8 +6,11 @@ namespace SandFox\Bencode;

final class Encoder
{
    public function __construct()
    private $options;

    public function __construct(array $options = [])
    {
        $this->options = $options;
    }

    /**
@@ -23,7 +26,7 @@ final class Encoder
            $writeStream = fopen('php://temp', 'r+');
        }

        return (new Engine\Encoder($data, $writeStream))->encode();
        return (new Engine\Encoder($data, $writeStream, $this->options))->encode();
    }

    /**
+13 −1
Original line number Diff line number Diff line
@@ -24,13 +24,20 @@ final class Encoder
    private $data;
    /** @var resource */
    private $stream;
    /** @var array */
    private $options;

    public function __construct($data, $stream)
    const DEFAULT_OPTIONS = [
        'useJsonSerializable' => false,
    ];

    public function __construct($data, $stream, array $options)
    {
        Util::detectMbstringOverload();

        $this->data = $data;
        $this->stream = $stream;
        $this->options = array_merge(self::DEFAULT_OPTIONS, $options);

        if (!\is_resource($this->stream) || get_resource_type($this->stream) !== 'stream') {
            throw new InvalidArgumentException('Output is not a valid stream');
@@ -97,6 +104,11 @@ final class Encoder
                $this->encodeValue($value->bencodeSerialize());
                break;

            case $this->options['useJsonSerializable'] && $value instanceof \JsonSerializable:
                // Start again with method result
                $this->encodeValue($value->jsonSerialize());
                break;

            // traversables
            case $value instanceof ListType:
                // ListType forces traversable object to be list
Loading