You are here

class EasyRdf_Serialiser_JsonLd 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

Class to serialise an EasyRdf_Graph to JSON-LD

@package EasyRdf @copyright Copyright (c) 2013 Alexey Zakhlestin @license http://www.opensource.org/licenses/bsd-license.php

Hierarchy

Expanded class hierarchy of EasyRdf_Serialiser_JsonLd

1 string reference to 'EasyRdf_Serialiser_JsonLd'
Format.php in vendor/easyrdf/easyrdf/lib/EasyRdf/Format.php

File

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

View source
class EasyRdf_Serialiser_JsonLd extends EasyRdf_Serialiser {
  public function __construct() {
    if (!class_exists('\\ML\\JsonLD\\JsonLD')) {
      throw new LogicException('Please install "ml/json-ld" dependency to use JSON-LD serialisation');
    }
    parent::__construct();
  }

  /**
   * @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);
    $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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EasyRdf_Serialiser::$prefixes protected property
EasyRdf_Serialiser::addPrefix protected function Keep track of the prefixes used while serialising @ignore
EasyRdf_Serialiser::checkSerialiseParams protected function Check and cleanup parameters passed to serialise() method @ignore
EasyRdf_Serialiser::reversePropertyCount protected function Protected method to get the number of reverse properties for a resource If a resource only has a single property, the number of values for that property is returned instead. @ignore
EasyRdf_Serialiser_JsonLd::serialise public function Overrides EasyRdf_Serialiser::serialise
EasyRdf_Serialiser_JsonLd::__construct public function Overrides EasyRdf_Serialiser::__construct