Serialise an EasyRdf_Graph to Turtle.
object EasyRdf_Graph $graph An EasyRdf_Graph object.:
string $format The name of the format to convert to.:
string The RDF in the new desired format.
Overrides EasyRdf_Serialiser::serialise
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;
}
}