Skip to content

Commit

Permalink
Merge pull request #4 from clue-labs/compat
Browse files Browse the repository at this point in the history
Ignore optional JSON parameters for legacy PHP versions
  • Loading branch information
clue authored Nov 24, 2016
2 parents 9652f5c + 0c1cb43 commit 54077cf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class Decoder extends EventEmitter implements ReadableStreamInterface

public function __construct(ReadableStreamInterface $input, $assoc = false, $depth = 512, $options = 0)
{
// @codeCoverageIgnoreStart
if ($options !== 0 && PHP_VERSION < 5.4) {
throw new \BadMethodCallException('Options parameter is only supported on PHP 5.4+');
}
// @codeCoverageIgnoreEnd

$this->input = $input;

if (!$input->isReadable()) {
Expand Down Expand Up @@ -87,7 +93,11 @@ public function handleData($data)
$this->buffer = (string)substr($this->buffer, $newline + 1);

// decode data with options given in ctor
$data = json_decode($data, $this->assoc, $this->depth, $this->options);
if ($this->options === 0) {
$data = json_decode($data, $this->assoc, $this->depth);
} else {
$data = json_decode($data, $this->assoc, $this->depth, $this->options);
}

// abort stream if decoding failed
if ($data === null && json_last_error() !== JSON_ERROR_NONE) {
Expand Down
31 changes: 30 additions & 1 deletion src/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ class Encoder extends EventEmitter implements WritableStreamInterface

public function __construct(WritableStreamInterface $output, $options = 0, $depth = 512)
{
// @codeCoverageIgnoreStart
if (defined('JSON_PRETTY_PRINT') && $options & JSON_PRETTY_PRINT) {
throw new \InvalidArgumentException('Pretty printing not available for NDJSON');
}
if ($depth !== 512 && PHP_VERSION < 5.5) {
throw new \BadMethodCallException('Depth parameter is only supported on PHP 5.5+');
}
// @codeCoverageIgnoreEnd

$this->output = $output;

Expand All @@ -42,8 +47,32 @@ public function write($data)
return false;
}

// we have to handle PHP warning for legacy PHP < 5.5 (see below)
if (PHP_VERSION_ID < 50500) {
$found = null;
set_error_handler(function ($error) use (&$found) {
$found = $error;
});
}

// encode data with options given in ctor
$data = json_encode($data, $this->options, $this->depth);
if ($this->depth === 512) {
$data = json_encode($data, $this->options);
} else {
$data = json_encode($data, $this->options, $this->depth);
}

// legacy error handler for PHP < 5.5
// certain values (such as INF etc.) emit a warning, but still encode successfully
if (PHP_VERSION_ID < 50500) {
restore_error_handler();

// emit an error event if a warning has been raised
if ($found !== null) {
$this->emit('error', array(new \RuntimeException('Unable to encode JSON: ' . $found)));
return $this->close();
}
}

// abort stream if encoding fails
if ($data === false && json_last_error() !== JSON_ERROR_NONE) {
Expand Down

0 comments on commit 54077cf

Please sign in to comment.