You are here

public function EasyRdf_Serialiser_JsonLd::serialise in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/easyrdf/easyrdf/lib/EasyRdf/Serialiser/JsonLd_real.php \EasyRdf_Serialiser_JsonLd::serialise()

Parameters

EasyRdf_Graph $graph:

string $format:

array $options:

Return value

string

Throws

EasyRdf_Exception

Overrides EasyRdf_Serialiser::serialise

File

vendor/easyrdf/easyrdf/lib/EasyRdf/Serialiser/JsonLd_real.php, line 63

Class

EasyRdf_Serialiser_JsonLd
Class to serialise an EasyRdf_Graph to JSON-LD

Code

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);
  $should_frame = isset($options['frame']);

  // expanded form
  $data = $ld_graph
    ->toJsonLd($use_native_types);
  if ($should_frame) {
    $data = \ML\JsonLD\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 = \ML\JsonLD\JsonLD::compact($data, $compact_context, $compact_options);
  }
  return \ML\JsonLD\JsonLD::toString($data);
}