/** * @param EasyRdf_Graph $graph * @param string $format * @param array $options * @throws EasyRdf_Exception * @return string */ public function serialise($graph, $format, array $options = array()) { parent::checkSerialiseParams($graph, $format); if ($format != 'jsonld') { throw new EasyRdf_Exception(__CLASS__ . ' does not support: ' . $format); } $ld_graph = new \ML\JsonLD\Graph(); $nodes = array(); // cache for id-to-node association foreach ($graph->toRdfPhp() as $resource => $properties) { if (array_key_exists($resource, $nodes)) { $node = $nodes[$resource]; } else { $node = $ld_graph->createNode($resource); $nodes[$resource] = $node; } foreach ($properties as $property => $values) { foreach ($values as $value) { if ($value['type'] == 'bnode' or $value['type'] == 'uri') { if (array_key_exists($value['value'], $nodes)) { $_value = $nodes[$value['value']]; } else { $_value = $ld_graph->createNode($value['value']); $nodes[$value['value']] = $_value; } } elseif ($value['type'] == 'literal') { if (isset($value['lang'])) { $_value = new \ML\JsonLD\LanguageTaggedString($value['value'], $value['lang']); } elseif (isset($value['datatype'])) { $_value = new \ML\JsonLD\TypedValue($value['value'], $value['datatype']); } else { $_value = $value['value']; } } else { throw new EasyRdf_Exception("Unable to serialise object to JSON-LD: " . $value['type']); } if ($property == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") { $node->addType($_value); } else { $node->addPropertyValue($property, $_value); } } } } // OPTIONS $use_native_types = !(isset($options['expand_native_types']) and $options['expand_native_types'] == true); $should_compact = (isset($options['compact']) and $options['compact'] == true); // expanded form $data = $ld_graph->toJsonLd($use_native_types); if ($should_compact) { // compact form $compact_context = isset($options['context']) ? $options['context'] : null; $compact_options = array('useNativeTypes' => $use_native_types, 'base' => $graph->getUri()); $data = \ML\JsonLD\JsonLD::compact($data, $compact_context, $compact_options); } return \ML\JsonLD\JsonLD::toString($data); }
/** * Method to serialise an EasyRdf_Graph to RDF/PHP * * http://n2.talis.com/wiki/RDF_PHP_Specification * docs/appendix-a-rdf-formats-php.md * * @param EasyRdf_Graph $graph An EasyRdf_Graph object. * @param string $format The name of the format to convert to. * @param array $options * @throws EasyRdf_Exception * @return string The RDF in the new desired format. */ public function serialise($graph, $format, array $options = array()) { parent::checkSerialiseParams($graph, $format); if ($format != 'php') { throw new EasyRdf_Exception("EasyRdf_Serialiser_RdfPhp does not support: {$format}"); } // Graph is already stored as RDF/PHP resource-centric array internally within the EasyRdf_Graph object return $graph->toRdfPhp(); }
/** * Method to serialise an EasyRdf_Graph to RDF/PHP * * http://n2.talis.com/wiki/RDF_PHP_Specification * * @param string $graph An EasyRdf_Graph object. * @param string $format The name of the format to convert to. * @return string The RDF in the new desired format. */ public function serialise($graph, $format) { parent::checkSerialiseParams($graph, $format); if ($format != 'php') { throw new EasyRdf_Exception("EasyRdf_Serialiser_RdfPhp does not support: {$format}"); } $rdfphp = array(); foreach ($graph->resources() as $resource) { $properties = $resource->propertyUris(); if (count($properties) == 0) { continue; } $subj = $resource->getUri(); if (!isset($rdfphp[$subj])) { $rdfphp[$subj] = array(); } foreach ($properties as $property) { if (!isset($rdfphp[$subj][$property])) { $rdfphp[$subj][$property] = array(); } $objects = $resource->all($property); foreach ($objects as $obj) { if (is_object($obj) and $obj instanceof EasyRdf_Resource) { if ($obj->isBNode()) { $object = array('type' => 'bnode', 'value' => $obj->getUri()); } else { $object = array('type' => 'uri', 'value' => $obj->getUri()); } } else { if (is_object($obj) and $obj instanceof EasyRdf_Literal) { $object = array('type' => 'literal', 'value' => $obj->getValue()); if ($obj->getLang()) { $object['lang'] = $obj->getLang(); } if ($obj->getDatatype()) { $object['datatype'] = $obj->getDatatypeUri(); } } else { throw new EasyRdf_Exception("Unsupported to serialise: " . gettype($obj)); } } array_push($rdfphp[$subj][$property], $object); } } } return $rdfphp; }
/** * Serialise an EasyRdf_Graph into a GraphViz dot document. * * Supported output format names: dot, gif, png, svg * * @param EasyRdf_Graph $graph An EasyRdf_Graph object. * @param string $format The name of the format to convert to. * @param array $options * @throws EasyRdf_Exception * @return string The RDF in the new desired format. */ public function serialise($graph, $format, array $options = array()) { parent::checkSerialiseParams($graph, $format); switch ($format) { case 'dot': return $this->serialiseDot($graph); case 'png': case 'gif': case 'svg': return $this->renderImage($graph, $format); default: throw new EasyRdf_Exception("EasyRdf_Serialiser_GraphViz does not support: {$format}"); } }
/** * Serialise an EasyRdf_Graph into N-Triples * * @param object EasyRdf_Graph $graph An EasyRdf_Graph object. * @param string $format The name of the format to convert to. * @return string The RDF in the new desired format. */ public function serialise($graph, $format) { parent::checkSerialiseParams($graph, $format); if ($format != 'ntriples') { throw new EasyRdf_Exception("EasyRdf_Serialiser_Ntriples does not support: {$format}"); } $nt = ''; foreach ($graph->resources() as $resource) { foreach ($resource->propertyUris() as $property) { $objects = $resource->all($property); foreach ($objects as $object) { $nt .= $this->ntriplesResource($resource) . " "; $nt .= "<" . $this->escape($property) . "> "; $nt .= $this->ntriplesObject($object) . " .\n"; } } } return $nt; }
/** * Serialise an EasyRdf_Graph to Turtle. * * @param object EasyRdf_Graph $graph An EasyRdf_Graph object. * @param string $format The name of the format to convert to. * @return string The RDF in the new desired format. */ 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('rdf' => true); $turtle = ''; foreach ($graph->resources() as $subject) { $properties = $subject->propertyUris(); if (count($properties) == 0) { continue; } $turtle .= $this->serialiseResource($subject); if (count($properties) > 1) { $turtle .= "\n "; } $pCount = 0; foreach ($properties as $property) { $short = EasyRdf_Namespace::shorten($property, true); if ($short) { $this->addPrefix($short); $pStr = $short == 'rdf:type' ? 'a' : $short; } else { $pStr = '<' . str_replace('>', '\\>', $property) . '>'; } if ($pCount) { $turtle .= " ;\n "; } $turtle .= " " . $pStr; $objects = $subject->all($property); $oCount = 0; foreach ($objects as $object) { if ($oCount) { $turtle .= ","; } $turtle .= " " . $this->serialiseObject($object); $oCount++; } $pCount++; } $turtle .= " .\n\n"; } return $this->serialisePrefixes() . "\n" . $turtle; }
/** * Method to serialise an EasyRdf_Graph to RDF/JSON * * http://n2.talis.com/wiki/RDF_JSON_Specification * docs/appendix-a-rdf-formats-json.md * * @param EasyRdf_Graph $graph An EasyRdf_Graph object. * @param string $format The name of the format to convert to. * @param array $options * @throws EasyRdf_Exception * @return string The RDF in the new desired format. */ public function serialise($graph, $format, array $options = array()) { return json_encode(parent::serialise($graph, 'php')); }
/** * Serialise an EasyRdf_Graph into N-Triples * * @param object EasyRdf_Graph $graph An EasyRdf_Graph object. * @param string $format The name of the format to convert to. * @return string The RDF in the new desired format. */ public function serialise($graph, $format) { parent::checkSerialiseParams($graph, $format); if ($format == 'ntriples') { $nt = ''; foreach ($graph->toArray() as $resource => $properties) { foreach ($properties as $property => $values) { foreach ($values as $value) { $nt .= $this->ntriplesResource($resource) . " "; $nt .= "<" . $this->escape($property) . "> "; $nt .= $this->ntriplesValue($value) . " .\n"; } } } return $nt; } else { throw new EasyRdf_Exception("EasyRdf_Serialiser_Ntriples does not support: {$format}"); } }
/** * Serialise an EasyRdf_Graph to Turtle. * * @param EasyRdf_Graph $graph An EasyRdf_Graph object. * @param string $format The name of the format to convert to. * @param array $options * @throws EasyRdf_Exception * @return string The RDF in the new desired format. */ public function serialise($graph, $format, array $options = array()) { 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 = ''; $turtle .= $this->serialiseSubjects($graph, 'uri'); $turtle .= $this->serialiseSubjects($graph, 'bnode'); if (count($this->prefixes)) { return $this->serialisePrefixes() . "\n" . $turtle; } else { return $turtle; } }
public function serialise($graph, $format) { parent::checkSerialiseParams($graph, $format); // Serialising goes here return true; }
/** * Method to serialise an EasyRdf_Graph to RDF/XML * * @param object EasyRdf_Graph $graph An EasyRdf_Graph object. * @param string $format The name of the format to convert to. * @return string The RDF in the new desired format. */ public function serialise($graph, $format) { parent::checkSerialiseParams($graph, $format); if ($format != 'rdfxml') { throw new EasyRdf_Exception("EasyRdf_Serialiser_RdfXml does not support: {$format}"); } // store of namespaces to be appended to the rdf:RDF tag $this->_prefixes = array('rdf' => true); // store of the resource URIs we have serialised $this->_outputtedResources = array(); $xml = ''; foreach ($graph->resources() as $resource) { $xml .= $this->rdfxmlResource($resource, true, 1); } // iterate through namepsaces array prefix and output a string. $namespaceStr = ''; foreach ($this->_prefixes as $prefix => $count) { $url = EasyRdf_Namespace::get($prefix); if (strlen($namespaceStr)) { $namespaceStr .= "\n "; } $namespaceStr .= ' xmlns:' . $prefix . '="' . htmlspecialchars($url) . '"'; } return "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" . "<rdf:RDF" . $namespaceStr . ">\n" . $xml . "\n</rdf:RDF>\n"; }
public function testSerialiseUndefined() { $this->setExpectedException('EasyRdf_Exception', 'This method should be overridden by sub-classes.'); $serialiser = new EasyRdf_Serialiser(); $serialiser->serialise($this->graph, 'format'); }
/** * Method to serialise an EasyRdf_Graph to RDF/XML * * @param string $graph An EasyRdf_Graph object. * @param string $format The name of the format to convert to. * @return string The RDF in the new desired format. */ public function serialise($graph, $format) { parent::checkSerialiseParams($graph, $format); if ($format != 'rdfxml') { throw new EasyRdf_Exception("EasyRdf_Serialiser_RdfXml does not support: {$format}"); } // store of namespaces to be appended to the rdf:RDF tag $this->_prefixes = array('rdf' => true); $xml = ''; foreach ($graph->resources() as $resource) { $properties = $resource->propertyUris(); if (count($properties) == 0) { continue; } $xml .= "\n " . $this->rdfxmlResource($resource) . "\n"; foreach ($properties as $property) { $short = EasyRdf_Namespace::shorten($property, true); if ($short) { $this->addPrefix($short); $objects = $resource->all($property); foreach ($objects as $object) { $xml .= $this->rdfxmlObject($short, $object); } } else { throw new EasyRdf_Exception("It is not possible to serialse the property " . "'{$property}' to RDF/XML."); } } $xml .= " </rdf:Description>\n"; } // iterate through namepsaces array prefix and output a string. $namespaceStr = ''; foreach ($this->_prefixes as $prefix => $count) { $url = EasyRdf_Namespace::get($prefix); if (strlen($namespaceStr)) { $namespaceStr .= "\n "; } $namespaceStr .= ' xmlns:' . $prefix . '="' . htmlspecialchars($url) . '"'; } return "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" . "<rdf:RDF" . $namespaceStr . ">\n" . $xml . "\n</rdf:RDF>\n"; }
/** * Serialise an EasyRdf_Graph to Turtle. * * @param object EasyRdf_Graph $graph An EasyRdf_Graph object. * @param string $format The name of the format to convert to. * @return string The RDF in the new desired format. */ 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; } }