From 55653cf34dc3aa1fbd1a4befddae379b242868c2 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Fri, 27 Jan 2023 13:00:27 +0300 Subject: [PATCH] stream JSON encoder to resource --- src/StreamJsonEncoder.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/StreamJsonEncoder.php b/src/StreamJsonEncoder.php index 7de44e8..c8b35cf 100644 --- a/src/StreamJsonEncoder.php +++ b/src/StreamJsonEncoder.php @@ -11,7 +11,7 @@ */ class StreamJsonEncoder extends AbstractJsonEncoder { - /** @var callable|null The stream callable */ + /** @var callable|resource|null Write stream */ private $stream; /** @var int Number of bytes written in the current step */ @@ -28,9 +28,9 @@ class StreamJsonEncoder extends AbstractJsonEncoder * will simply output the json using an echo statement. * * @param mixed $value The value to encode as JSON - * @param callable|null $stream An optional stream to pass the output or null to echo it + * @param callable|resource|null $stream An optional stream to pass the output or null to echo it */ - public function __construct($value, callable $stream = null) + public function __construct($value, $stream = null) { parent::__construct($value); @@ -86,6 +86,8 @@ protected function write($string, $token) { if ($this->stream === null) { echo $string; + } elseif (is_resource($this->stream)) { + fwrite($this->stream, $string); } else { $callback = $this->stream; $callback($string, $token);