public function EasyRdf_Serialiser_Turtle::serialise

Serialise an EasyRdf_Graph to Turtle.

Parameters

object EasyRdf_Graph $graph An EasyRdf_Graph object.:

string $format The name of the format to convert to.:

Return value

string The RDF in the new desired format.

Overrides EasyRdf_Serialiser::serialise

File

drupal/core/vendor/easyrdf/easyrdf/lib/EasyRdf/Serialiser/Turtle.php, line 217

Class

EasyRdf_Serialiser_Turtle
Class to serialise an EasyRdf_Graph to Turtle with no external dependancies.

Code

public function serialise($graph, $format) {
  parent::checkSerialiseParams($graph, $format);
  if ($format != 'turtle' and $format != 'n3') {
    throw new EasyRdf_Exception("EasyRdf_Serialiser_Turtle does not support: {$format}");
  }
  $this->prefixes = array();
  $this->outputtedBnodes = array();
  $turtle = '';
  foreach ($graph
    ->resources() as $resource) {

    // If the resource has no properties - don't serialise it
    $properties = $resource
      ->propertyUris();
    if (count($properties) == 0) {
      continue;
    }
    if ($resource
      ->isBnode()) {
      $id = $resource
        ->getNodeId();
      $rpcount = $this
        ->reversePropertyCount($resource);
      if (isset($this->outputtedBnodes[$id])) {

        // Already been serialised
        continue;
      }
      else {
        $this->outputtedBnodes[$id] = true;
        if ($rpcount == 0) {
          $turtle .= '[]';
        }
        else {
          $turtle .= $this
            ->serialiseResource($resource);
        }
      }
    }
    else {
      $turtle .= $this
        ->serialiseResource($resource);
    }
    $turtle .= $this
      ->serialiseProperties($resource);
    $turtle .= "\n";
  }
  if (count($this->prefixes)) {
    return $this
      ->serialisePrefixes() . "\n" . $turtle;
  }
  else {
    return $turtle;
  }
}