Example #1
0
 /**
  * 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 Graph  $graph   An EasyRdf\Graph object.
  * @param string $format  The name of the format to convert to.
  * @param array  $options
  *
  * @throws 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 Exception(__CLASS__ . " does not support: {$format}");
     }
     // Graph is already stored as RDF/PHP resource-centric array internally within the EasyRdf\Graph object
     return $graph->toRdfPhp();
 }
Example #2
0
 /**
  * Method to serialise an EasyRdf\Graph to RDF/XML
  *
  * @param Graph  $graph  An EasyRdf\Graph object.
  * @param string $format The name of the format to convert to.
  * @param array  $options
  *
  * @return string The RDF in the new desired format.
  * @throws Exception
  */
 public function serialise(Graph $graph, $format, array $options = array())
 {
     parent::checkSerialiseParams($format);
     if ($format != 'rdfxml') {
         throw new 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 = '';
     // Serialise URIs first
     foreach ($graph->resources() as $resource) {
         if (!$resource->isBnode()) {
             $xml .= $this->rdfxmlResource($resource, true);
         }
     }
     // Serialise bnodes afterwards
     foreach ($graph->resources() as $resource) {
         if ($resource->isBnode()) {
             $xml .= $this->rdfxmlResource($resource, true);
         }
     }
     // iterate through namepsaces array prefix and output a string.
     $namespaceStr = '';
     foreach ($this->prefixes as $prefix => $count) {
         $url = RdfNamespace::get($prefix);
         if (strlen($namespaceStr)) {
             $namespaceStr .= "\n        ";
         }
         if (strlen($prefix) === 0) {
             $namespaceStr .= ' xmlns="' . htmlspecialchars($url) . '"';
         } else {
             $namespaceStr .= ' xmlns:' . $prefix . '="' . htmlspecialchars($url) . '"';
         }
     }
     return "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" . "<rdf:RDF" . $namespaceStr . ">\n" . $xml . "\n</rdf:RDF>\n";
 }
Example #3
0
 /**
  * Serialise an EasyRdf\Graph into N-Triples
  *
  * @param Graph  $graph   An EasyRdf\Graph object.
  * @param string $format  The name of the format to convert to.
  * @param array  $options
  *
  * @throws Exception
  *
  * @return string The RDF in the new desired format.
  */
 public function serialise($graph, $format, array $options = array())
 {
     parent::checkSerialiseParams($graph, $format);
     if ($format == 'ntriples') {
         $nt = '';
         foreach ($graph->toRdfPhp() as $resource => $properties) {
             foreach ($properties as $property => $values) {
                 foreach ($values as $value) {
                     $nt .= $this->serialiseResource($resource) . " ";
                     $nt .= "<" . $this->escapeString($property) . "> ";
                     $nt .= $this->serialiseValue($value) . " .\n";
                 }
             }
         }
         return $nt;
     } else {
         throw new Exception(__CLASS__ . " does not support: {$format}");
     }
 }
Example #4
0
 /**
  * @param \EasyRdf\Graph  $graph
  * @param string          $format
  * @param array           $options
  *
  * @throws Exception
  * @return string
  */
 public function serialise($graph, $format, array $options = array())
 {
     parent::checkSerialiseParams($graph, $format);
     if ($format != 'jsonld') {
         throw new Exception(__CLASS__ . ' does not support: ' . $format);
     }
     $ld_graph = new LD\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 LD\LanguageTaggedString($value['value'], $value['lang']);
                     } elseif (isset($value['datatype'])) {
                         $_value = new LD\TypedValue($value['value'], $value['datatype']);
                     } else {
                         $_value = $value['value'];
                     }
                 } else {
                     throw new 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);
     $should_frame = isset($options['frame']);
     // expanded form
     $data = $ld_graph->toJsonLd($use_native_types);
     if ($should_frame) {
         $data = LD\JsonLD::frame($data, $options['frame'], $options);
     }
     if ($should_compact) {
         // compact form
         $compact_context = isset($options['context']) ? $options['context'] : null;
         $compact_options = array('useNativeTypes' => $use_native_types, 'base' => $graph->getUri());
         $data = LD\JsonLD::compact($data, $compact_context, $compact_options);
     }
     return LD\JsonLD::toString($data);
 }
Example #5
0
 /**
  * Serialise an EasyRdf\Graph to Turtle.
  *
  * @param Graph  $graph  An EasyRdf\Graph object.
  * @param string $format The name of the format to convert to.
  * @param array  $options
  *
  * @return string The RDF in the new desired format.
  * @throws Exception
  */
 public function serialise(Graph $graph, $format, array $options = array())
 {
     parent::checkSerialiseParams($format);
     if ($format != 'turtle' and $format != 'n3') {
         throw new 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;
     }
 }
Example #6
0
 /**
  * Serialise an EasyRdf\Graph into a GraphViz dot document.
  *
  * Supported output format names: dot, gif, png, svg
  *
  * @param Graph  $graph  An EasyRdf\Graph object.
  * @param string $format The name of the format to convert to.
  * @param array  $options
  *
  * @return string The RDF in the new desired format.
  * @throws Exception
  */
 public function serialise(Graph $graph, $format, array $options = array())
 {
     parent::checkSerialiseParams($format);
     switch ($format) {
         case 'dot':
             return $this->serialiseDot($graph);
         case 'png':
         case 'gif':
         case 'svg':
             return $this->renderImage($graph, $format);
         default:
             throw new Exception("EasyRdf\\Serialiser\\GraphViz does not support: {$format}");
     }
 }