Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/JsonStream/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
class Encoder
{
private $_stream;
private $_echo;

/**
* @param resource $stream A stream resource.
* @throws \InvalidArgumentException If $stream is not a stream resource.
*/
public function __construct($stream)
public function __construct($stream,$echo=false)
{
if (!is_resource($stream) || get_resource_type($stream) != 'stream') {
$this->_echo=$echo;
if (!$this->_echo && (!is_resource($stream) || get_resource_type($stream) != 'stream')) {
throw new \InvalidArgumentException("Resource is not a stream");
}

$this->_stream = $stream;
if(!$this->_echo)
$this->_stream = $stream;
}

/**
Expand Down Expand Up @@ -63,7 +66,11 @@ public function encode($value)
*/
private function _writeValue($value)
{
fwrite($this->_stream, $value);
if(!$this->_echo){
fwrite($this->_stream, $value);
} else {
echo $value;
}
}

/**
Expand Down