class RestWSFormatXML in RESTful Web Services 7
Same name and namespace in other branches
- 7.2 restws.formats.inc \RestWSFormatXML
A formatter for XML.
Hierarchy
- class \RestWSBaseFormat implements RestWSFormatInterface
- class \RestWSFormatXML
Expanded class hierarchy of RestWSFormatXML
2 string references to 'RestWSFormatXML'
- hook_restws_format_info in ./
restws.api.php - Define restws compatible formats.
- restws_restws_format_info in ./
restws.module - Implements hook_restws_format_info().
File
- ./
restws.formats.inc, line 210 - RESTful web services module formats.
View source
class RestWSFormatXML extends RestWSBaseFormat {
/**
* Gets the representation of a resource.
*/
public function viewResource($resourceController, $id) {
$xml = new DOMDocument('1.0', 'utf-8');
$element = $xml
->createElement($resourceController
->resource());
self::addToXML($xml, $element, $resourceController
->wrapper($id));
$xml
->appendChild($element);
return $xml
->saveXML();
}
/**
* Creates a new resource.
*/
public function createResource($resourceController, $data) {
$values = $this
->unserialize($data);
$id = $resourceController
->create($values);
$xml = new DOMDocument('1.0', 'utf-8');
$element = $xml
->createElement('uri');
self::setXMLReference($element, $resourceController
->resource(), $id);
$xml
->appendChild($element);
return $xml
->saveXML();
}
public function serialize($data) {
// Return an empty XML document.
$xml = new DOMDocument('1.0', 'utf-8');
return $xml
->saveXML();
}
public function unserialize($data) {
// Disable XML external entity expansion for security reasons.
libxml_disable_entity_loader(TRUE);
$xml = simplexml_load_string($data);
return self::xmlToArray($xml);
}
/**
* Turns the xml structure into an array of values.
*/
public static function xmlToArray(SimpleXMLElement $xml) {
$children = $xml
->children();
foreach ($xml
->children() as $name => $element) {
$result[$name] = self::xmlToArray($element);
}
if (!isset($result)) {
$result = ($string = (string) $xml) ? $string : NULL;
}
return $result;
}
/**
* Adds the data of the given wrapper to the given XML element.
*/
public static function addToXML(DOMDocument $doc, DOMNode $parent, $wrapper) {
$filtered = restws_property_access_filter($wrapper);
foreach ($filtered as $name => $property) {
try {
if ($property instanceof EntityDrupalWrapper) {
// For referenced entities only return the URI.
if ($id = $property
->getIdentifier()) {
$element = $doc
->createElement(is_numeric($name) ? 'item' : $name);
$parent
->appendChild($element);
self::setXMLReference($element, $property
->type(), $id);
}
}
elseif ($property instanceof EntityValueWrapper) {
$escaped = $doc
->createTextNode($property
->value());
$element = $doc
->createElement(is_numeric($name) ? 'item' : $name);
$element
->appendChild($escaped);
$parent
->appendChild($element);
}
elseif ($property instanceof EntityListWrapper || $property instanceof EntityStructureWrapper) {
$element = $doc
->createElement(is_numeric($name) ? 'item' : $name);
$parent
->appendChild($element);
self::addToXML($doc, $element, $property);
}
} catch (EntityMetadataWrapperException $e) {
// A property causes problems - ignore that.
}
}
}
public static function setXMLReference(DOMElement $node, $resource, $id) {
$node->nodeValue = restws_resource_uri($resource, $id);
$node
->setAttribute('resource', $resource);
$node
->setAttribute('id', $id);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RestWSBaseFormat:: |
protected | property | ||
RestWSBaseFormat:: |
protected | property | ||
RestWSBaseFormat:: |
public | function |
Deletes a resource. Overrides RestWSFormatInterface:: |
|
RestWSBaseFormat:: |
public static | function | Gets a simple PHP array using URI references for some wrapped data. | |
RestWSBaseFormat:: |
public | function |
Returns the short name of this format. Overrides RestWSFormatInterface:: |
|
RestWSBaseFormat:: |
public static | function | ||
RestWSBaseFormat:: |
public | function |
Returns the mime type of this format, e.g. 'application/json' or
'application/xml'. Overrides RestWSFormatInterface:: |
|
RestWSBaseFormat:: |
public | function |
Updates a resource. Overrides RestWSFormatInterface:: |
1 |
RestWSBaseFormat:: |
public | function | 1 | |
RestWSFormatXML:: |
public static | function | Adds the data of the given wrapper to the given XML element. | |
RestWSFormatXML:: |
public | function |
Creates a new resource. Overrides RestWSBaseFormat:: |
|
RestWSFormatXML:: |
public | function | ||
RestWSFormatXML:: |
public static | function | ||
RestWSFormatXML:: |
public | function | ||
RestWSFormatXML:: |
public | function |
Gets the representation of a resource. Overrides RestWSBaseFormat:: |
|
RestWSFormatXML:: |
public static | function | Turns the xml structure into an array of values. |