From 569cf2e4333a885474ddf1ad555d2f1c76b91ead Mon Sep 17 00:00:00 2001 From: Nathan Macnamara Date: Fri, 3 Jul 2015 12:13:56 +0100 Subject: [PATCH] Fix for xml empty values, should return null. --- src/Formats/XML.php | 7 ++++++- tests/XMLTest.php | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Formats/XML.php b/src/Formats/XML.php index 56c80b9..61b5cb1 100644 --- a/src/Formats/XML.php +++ b/src/Formats/XML.php @@ -28,7 +28,12 @@ public function parse($payload) { try { $xml = simplexml_load_string($payload, 'SimpleXMLElement', LIBXML_NOCDATA); - return json_decode(json_encode((array) $xml), 1); // Work around to accept xml input + + // Fix for empty values in XML + $json = json_encode((array) $xml); + $json = str_replace(':{}',':null', $json); + $json = str_replace(':[]',':null', $json); + return json_decode($json, 1); // Work around to accept xml input } catch (\Exception $ex) { throw new ParserException('Failed To Parse XML'); } diff --git a/tests/XMLTest.php b/tests/XMLTest.php index 45a9e0c..acb49c5 100644 --- a/tests/XMLTest.php +++ b/tests/XMLTest.php @@ -15,6 +15,20 @@ protected function tearDown() m::close(); } + /** @test */ + public function null_values_for_empty_values() + { + $parser = m::mock('Nathanmac\Utilities\Parser\Parser') + ->shouldDeferMissing() + ->shouldAllowMockingProtectedMethods(); + + $parser->shouldReceive('getPayload') + ->once() + ->andReturn('hello worldworld'); + + $this->assertEquals(array("comments" => array(array("title" => null, "message" => "hello world"), array("title" => "world", "message" => null))), $parser->payload('application/xml')); + } + /** @test */ public function array_structured_getPayload_xml() {