You are here

public function RestWSFormatRDF::viewResource in RESTful Web Services 7

Same name and namespace in other branches
  1. 7.2 restws.formats.inc \RestWSFormatRDF::viewResource()

Gets the representation of a resource.

Overrides RestWSBaseFormat::viewResource

File

./restws.formats.inc, line 320
RESTful web services module formats.

Class

RestWSFormatRDF
A simple formatter for RDF. Requires the RDF module for the mapping.

Code

public function viewResource($resourceController, $id) {
  $xml = new DOMDocument('1.0', 'utf-8');
  $rdf_element = $xml
    ->createElementNS($this->namespaces['rdf'], 'rdf:RDF');
  $xml
    ->appendChild($rdf_element);
  $element = $xml
    ->createElementNS($this->namespaces['rdf'], 'rdf:Description');
  $element
    ->setAttributeNS($this->namespaces['rdf'], 'rdf:about', restws_resource_uri($resourceController
    ->resource(), $id));

  // Add the RDF type of the resource if there is a mapping.
  $entity = $resourceController
    ->read($id);
  if (!empty($entity->rdf_mapping['rdftype'])) {
    foreach ($entity->rdf_mapping['rdftype'] as $rdf_type) {
      $type_element = $xml
        ->createElementNS($this->namespaces['rdf'], 'rdf:type');
      list($ns, $name) = explode(':', $rdf_type);
      $type_element
        ->setAttributeNS($this->namespaces['rdf'], 'rdf:resource', $this->namespaces[$ns] . $name);
      $element
        ->appendChild($type_element);
    }
  }
  $this
    ->addToXML($xml, $element, $resourceController
    ->wrapper($id));
  $rdf_element
    ->appendChild($element);
  return $xml
    ->saveXML();
}