Skip to content

Commit

Permalink
Merge pull request #9 from nathanmac/fix/xml-empty-values
Browse files Browse the repository at this point in the history
Fix for xml empty values, should return null.
  • Loading branch information
Nathan Macnamara committed Jul 3, 2015
2 parents b8f225c + 569cf2e commit e3dbac2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Formats/XML.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
14 changes: 14 additions & 0 deletions tests/XMLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<xml><comments><title></title><message>hello world</message></comments><comments><title>world</title><message></message></comments></xml>');

$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()
{
Expand Down

0 comments on commit e3dbac2

Please sign in to comment.