diff --git a/src/JsonParseNode.php b/src/JsonParseNode.php index cfc9a7c..52e5176 100644 --- a/src/JsonParseNode.php +++ b/src/JsonParseNode.php @@ -284,6 +284,11 @@ public function getDateIntervalValue(): ?DateInterval{ } public function getBinaryContent(): ?StreamInterface { - return ($this->jsonNode !== null) ? Utils::streamFor(strval($this->jsonNode)) : null; + if (is_null($this->jsonNode)) { + return null; + } elseif (is_array($this->jsonNode)) { + return Utils::streamFor(json_encode($this->jsonNode)); + } + return Utils::streamFor(strval($this->jsonNode)); } } diff --git a/tests/JsonParseNodeTest.php b/tests/JsonParseNodeTest.php index 8694007..61fcd43 100644 --- a/tests/JsonParseNodeTest.php +++ b/tests/JsonParseNodeTest.php @@ -26,7 +26,7 @@ class JsonParseNodeTest extends TestCase private StreamInterface $stream; protected function setUp(): void { - $this->stream = Utils::streamFor('{"@odata.type":"Missing", "name": "Silas Kenneth", "age": 98, "height": 123.122, "maritalStatus": "complicated,single", "address": {"city": "Nairobi", "street": "Luthuli"}}'); + $this->stream = Utils::streamFor('{"@odata.type":"Missing","name":"Silas Kenneth","age":98,"height":123.122,"maritalStatus":"complicated,single","address":{"city":"Nairobi","street":"Luthuli"}}'); } public function testGetIntegerValue(): void { @@ -175,4 +175,15 @@ public function testCallbacksAreCalled(): void { $person = $this->parseNode->getObjectValue([Person::class, 'createFromDiscriminatorValue']); $this->assertTrue($assigned); } + + public function testGetBinaryContent(): void { + $this->parseNode = new JsonParseNode(100); + $this->assertEquals("100", $this->parseNode->getBinaryContent()->getContents()); + } + + public function testGetBinaryContentFromArray(): void { + $this->parseNode = new JsonParseNode(json_decode($this->stream->getContents(), true)); + $this->stream->rewind(); + $this->assertEquals($this->stream->getContents(), $this->parseNode->getBinaryContent()->getContents()); + } }