From 348cbf86bcd874e72cc0bff2f05cdb37e0572e75 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Wed, 22 Mar 2023 23:30:41 +0300 Subject: [PATCH 1/2] Support deserializing object to stream --- src/JsonParseNode.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/JsonParseNode.php b/src/JsonParseNode.php index cfc9a7c..1840a36 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; + } else if (is_array($this->jsonNode)) { + return Utils::streamFor(json_encode($this->jsonNode)); + } + return Utils::streamFor(strval($this->jsonNode)); } } From fdac1779dde05ab4e445952c6e63c380dbef3830 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Wed, 22 Mar 2023 23:54:49 +0300 Subject: [PATCH 2/2] Add tests + fix code smell --- src/JsonParseNode.php | 2 +- tests/JsonParseNodeTest.php | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/JsonParseNode.php b/src/JsonParseNode.php index 1840a36..52e5176 100644 --- a/src/JsonParseNode.php +++ b/src/JsonParseNode.php @@ -286,7 +286,7 @@ public function getDateIntervalValue(): ?DateInterval{ public function getBinaryContent(): ?StreamInterface { if (is_null($this->jsonNode)) { return null; - } else if (is_array($this->jsonNode)) { + } 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()); + } }