Skip to content

Commit

Permalink
Add shortcut accessors for json and xml
Browse files Browse the repository at this point in the history
This makes reading/dealing with xml & json easier. It also leaves room
to add other commonly used data types in the future if necessary.
  • Loading branch information
markstory committed Jan 28, 2013
1 parent ba296d6 commit feb5ebb
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
52 changes: 51 additions & 1 deletion lib/Cake/Network/Http/Response.php
Expand Up @@ -49,6 +49,20 @@
*
* `$content = $response->body;`
*
* If your response body is in XML or JSON you can use
* special content type specific accessors to read the decoded data.
* JSON data will be returned as arrays, while XML data will be returned
* as SimpleXML nodes:
*
* {{{
* // Get as xml
* $content = $response->xml
* // Get as json
* $content = $response->json
* }}}
*
* If the response cannot be decoded, null will be returned.
*
* ### Check the status code
*
* You can access the response status code using:
Expand Down Expand Up @@ -84,7 +98,9 @@ class Response extends Message {
'cookies' => '_cookies',
'headers' => '_headers',
'body' => '_body',
'code' => '_code'
'code' => '_code',
'json' => '_getJson',
'xml' => '_getXml'
];

/**
Expand Down Expand Up @@ -290,6 +306,33 @@ public function body($parser = null) {
return $this->_body;
}

/**
* Get the response body as JSON decoded data.
*
* @return null|array
*/
protected function _getJson() {
$data = json_decode($this->_body, true);
if ($data) {
return $data;
}
return null;
}

/**
* Get the response body as XML decoded data.
*
* @return null|SimpleXML
*/
protected function _getXml() {
try {
$data = new \SimpleXMLElement($this->_body);
} catch (Exception $e) {
return null;
}
return $data;
}

/**
* Read values as properties.
*
Expand All @@ -301,6 +344,9 @@ public function __get($name) {
return false;
}
$key = $this->_exposedProperties[$name];
if (substr($key, 0, 4) == '_get') {
return $this->{$key}();
}
return $this->{$key};
}

Expand All @@ -315,6 +361,10 @@ public function __isset($name) {
return false;
}
$key = $this->_exposedProperties[$name];
if (substr($key, 0, 4) == '_get') {
$val = $this->{$key}();
return $val !== null;
}
return isset($this->$key);
}

Expand Down
32 changes: 32 additions & 0 deletions lib/Cake/Test/TestCase/Network/Http/ResponseTest.php
Expand Up @@ -72,6 +72,38 @@ public function testBody() {
$this->assertTrue(isset($response->body));
}

/**
* Test accessor for json
*
* @return void
*/
public function testBodyJson() {
$data = [
'property' => 'value'
];
$encoded = json_encode($data);
$response = new Response([], $encoded);
$this->assertTrue(isset($response->json));
$this->assertEquals($data['property'], $response->json['property']);
}

/**
* Test accessor for xml
*
* @return void
*/
public function testBodyXml() {
$data = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<root>
<test>Test</test>
</root>
XML;
$response = new Response([], $data);
$this->assertTrue(isset($response->xml));
$this->assertEquals('Test', (string)$response->xml->test);
}

/**
* Test isOk()
*
Expand Down

0 comments on commit feb5ebb

Please sign in to comment.