Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added XML Support #24

Merged
merged 1 commit into from Jul 11, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 54 additions & 0 deletions RestServer.php
Expand Up @@ -33,12 +33,14 @@ class RestFormat
const HTML = 'text/html'; const HTML = 'text/html';
const AMF = 'applicaton/x-amf'; const AMF = 'applicaton/x-amf';
const JSON = 'application/json'; const JSON = 'application/json';
const XML = 'application/xml';
static public $formats = array( static public $formats = array(
'plain' => RestFormat::PLAIN, 'plain' => RestFormat::PLAIN,
'txt' => RestFormat::PLAIN, 'txt' => RestFormat::PLAIN,
'html' => RestFormat::HTML, 'html' => RestFormat::HTML,
'amf' => RestFormat::AMF, 'amf' => RestFormat::AMF,
'json' => RestFormat::JSON, 'json' => RestFormat::JSON,
'xml' => RestFormat::XML,
); );
} }


Expand Down Expand Up @@ -405,6 +407,20 @@ public function sendData($data)
$serializer = new Zend_Amf_Parse_Amf3_Serializer($stream); $serializer = new Zend_Amf_Parse_Amf3_Serializer($stream);
$serializer->writeTypeMarker($data); $serializer->writeTypeMarker($data);
$data = $stream->getStream(); $data = $stream->getStream();

elseif ($this->format == RestFormat::XML) {

if (is_object($data) && method_exists($data, '__keepOut')) {
$data = clone $data;
foreach ($data->__keepOut() as $prop) {
unset($data->$prop);
}
}
$data = $this->xml_encode($data);
if ($data && $this->mode == 'debug') {
$data = $this->json_format($data);
}

} else { } else {
if (is_object($data) && method_exists($data, '__keepOut')) { if (is_object($data) && method_exists($data, '__keepOut')) {
$data = clone $data; $data = clone $data;
Expand All @@ -427,6 +443,44 @@ public function setStatus($code)
header("{$_SERVER['SERVER_PROTOCOL']} $code"); header("{$_SERVER['SERVER_PROTOCOL']} $code");
} }


private function xml_encode($mixed, $domElement=null, $DOMDocument=null) {
if (is_null($DOMDocument)) {
$DOMDocument =new DOMDocument;
$DOMDocument->formatOutput = true;
$this->xml_encode($mixed, $DOMDocument, $DOMDocument);
echo $DOMDocument->saveXML();
}
else {
if (is_array($mixed)) {
foreach ($mixed as $index => $mixedElement) {
if (is_int($index)) {
if ($index === 0) {
$node = $domElement;
}
else {
$node = $DOMDocument->createElement($domElement->tagName);
$domElement->parentNode->appendChild($node);
}
}
else {
$plural = $DOMDocument->createElement($index);
$domElement->appendChild($plural);
$node = $plural;
if (!(rtrim($index, 's') === $index)) {
$singular = $DOMDocument->createElement(rtrim($index, 's'));
$plural->appendChild($singular);
$node = $singular;
}
}

$this->xml_encode($mixedElement, $node, $DOMDocument);
}
}
else {
$domElement->appendChild($DOMDocument->createTextNode($mixed));
}
}
}
// Pretty print some JSON // Pretty print some JSON
private function json_format($json) private function json_format($json)
{ {
Expand Down