ServicesParser.inc in Services 7.3
File
servers/rest_server/includes/ServicesParser.inc
View source
<?php
interface ServicesParserInterface {
public function parse(ServicesContextInterface $context);
}
class ServicesParserURLEncoded implements ServicesParserInterface {
public function parse(ServicesContextInterface $context) {
parse_str($context
->getRequestBody(), $data);
return $data;
}
}
class ServicesParserPHP implements ServicesParserInterface {
public function parse(ServicesContextInterface $context) {
return unserialize($context
->getRequestBody());
}
}
class ServicesParserXML implements ServicesParserInterface {
public function parse(ServicesContextInterface $context) {
$old_error_state = libxml_use_internal_errors(1);
libxml_clear_errors();
$xml_data = simplexml_load_string($context
->getRequestBody());
if (!$xml_data) {
$message = '';
$errors = libxml_get_errors();
foreach ($errors as $error) {
$message .= t('Line @line, Col @column: @message', array(
'@line' => $error->line,
'@column' => $error->column,
'@message' => $error->message,
)) . "\n\n";
}
libxml_clear_errors();
libxml_use_internal_errors($old_error_state);
services_error($message, 406);
}
libxml_use_internal_errors($old_error_state);
$php_array = $this
->unmarshalXML($xml_data, NULL);
return (array) $php_array;
}
protected function unmarshalXML($node, $array) {
foreach ($node
->children() as $child) {
if (count($child
->children()) > 0) {
$att = 'is_array';
if ($child
->attributes()->{$att}) {
$new_array = array();
foreach ($child
->children() as $item) {
$item_keys = array_keys((array) $item);
if (count($item_keys) == 1 && current($item_keys) === 0) {
$new_array[] = (string) $item[0];
}
elseif (is_object($item) && $item
->count() == 0) {
$new_array[] = (string) $item;
}
else {
$new_array[] = self::unmarshalXML($item, $array[$item
->getName()]);
}
}
}
else {
$array[$child
->getName()] = array();
$new_array = $this
->unmarshalXML($child, $array[$child
->getName()]);
}
$array[$child
->getName()] = $new_array;
}
else {
if ($child
->attributes()->is_raw) {
return (string) $child;
}
$array[$child
->getName()] = (string) $child;
}
}
return $array;
}
}
class ServicesParserJSON implements ServicesParserInterface {
public function parse(ServicesContextInterface $context) {
$requestBody = $context
->getRequestBody();
if ($requestBody) {
$data = json_decode($requestBody, TRUE);
if ($data === NULL) {
return services_error(t('Invalid JSON.'), 400);
}
return $data;
}
return $requestBody;
}
}
class ServicesParserFile implements ServicesParserInterface {
public function parse(ServicesContextInterface $context) {
return $context
->getRequestBody();
}
}
class ServicesParserYAML implements ServicesParserInterface {
public function parse(ServicesContextInterface $context) {
if (($library = libraries_load('spyc')) && !empty($library['loaded'])) {
return Spyc::YAMLLoadString($context
->getPostData());
}
else {
watchdog('REST Server', 'Spyc library not found!', array(), WATCHDOG_ERROR);
return array();
}
}
}
class ServicesParserMultipart implements ServicesParserInterface {
public function parse(ServicesContextInterface $context) {
return $context
->getPostData();
}
}