class ServicesClientConnectionRestServerParser in Services Client 7.2
Same name and namespace in other branches
- 7 services_client_connection/plugins/ServicesClientConnectionRestServer.inc \ServicesClientConnectionRestServerParser
Hierarchy
Expanded class hierarchy of ServicesClientConnectionRestServerParser
File
- services_client_connection/
plugins/ ServicesClientConnectionRestServer.inc, line 277
View source
class ServicesClientConnectionRestServerParser {
/**
* Definition of supported parsers
*/
private static $parsers = array(
'json' => array(
'title' => 'JSON',
'accept' => 'application/json',
'method' => 'parseJson',
),
'php' => array(
'title' => 'PHP serialized',
'accept' => 'application/vnd.php.serialized',
'method' => 'parseSerialized',
),
'xml' => array(
'title' => 'XML',
'accept' => 'application/xml',
'method' => 'parseXml',
),
);
/**
* Get list of supported parsers.
*
* @return type
*/
public static function getParsers() {
$output = array();
foreach (self::$parsers as $id => $info) {
$output[$id] = $info['title'];
}
return $output;
}
/**
* Parser type
*/
private $parser;
/**
* Create a new reponse parser
*
* @param $parser
*/
public function __construct($parser) {
$this->parser = $parser;
}
/**
* Parse retrieved data
*
* @param string $data
*/
public function parse($data) {
if ($method = $this
->getParserInfo('method')) {
return call_user_func(array(
$this,
$method,
), $data);
}
}
/**
* Prepare request
*
* @param ServicesClientConnectionHttpRequest $request
*/
public function prepareRequest(&$request) {
if ($accept = $this
->getParserInfo('accept')) {
$request->http_headers['Accept'] = $accept;
}
}
/**
* Parse JSON response
*/
protected function parseJson($data) {
$parsed = json_decode($data, TRUE);
// Return array as result of services
if (is_object($parsed)) {
$parsed = (array) $parsed;
}
return $parsed;
}
/**
* Parse serialized PHP
*/
protected function parseSerialized($data) {
return (array) unserialize($data);
}
/**
* Parse XML
*/
protected function parseXml($data) {
return (array) simplexml_load_string($data);
}
/**
* Get info about current formatter
*
* @param string $property
* Optionally property can be specified.
*/
protected function getParserInfo($property = NULL) {
// Get formatter info
$parser = isset(self::$parsers[$this->parser]) ? self::$parsers[$this->parser] : NULL;
// If defined property try to retrieve property from formatter info
if (!empty($property) && !empty($parser)) {
return isset($parser[$property]) ? $parser[$property] : NULL;
}
return $parser;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ServicesClientConnectionRestServerParser:: |
private | property | Parser type | |
ServicesClientConnectionRestServerParser:: |
private static | property | Definition of supported parsers | |
ServicesClientConnectionRestServerParser:: |
protected | function | Get info about current formatter | |
ServicesClientConnectionRestServerParser:: |
public static | function | Get list of supported parsers. | |
ServicesClientConnectionRestServerParser:: |
public | function | Parse retrieved data | |
ServicesClientConnectionRestServerParser:: |
protected | function | Parse JSON response | |
ServicesClientConnectionRestServerParser:: |
protected | function | Parse serialized PHP | |
ServicesClientConnectionRestServerParser:: |
protected | function | Parse XML | |
ServicesClientConnectionRestServerParser:: |
public | function | Prepare request | |
ServicesClientConnectionRestServerParser:: |
public | function | Create a new reponse parser |