You are here

class EasyRdf_Parser_JsonLd in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/JsonLdImplementation.php \EasyRdf_Parser_JsonLd

Class to parse JSON-LD to an EasyRdf_Graph

@package EasyRdf @copyright Copyright (c) 2014 Markus Lanthaler @author Markus Lanthaler <mail@markus-lanthaler.com> @license http://www.opensource.org/licenses/bsd-license.php

Hierarchy

Expanded class hierarchy of EasyRdf_Parser_JsonLd

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

File

vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/JsonLdImplementation.php, line 46

View source
class EasyRdf_Parser_JsonLd extends EasyRdf_Parser {

  /**
   * Parse a JSON-LD document into an EasyRdf_Graph
   *
   * Attention: Since JSON-LD supports datasets, a document may contain
   * multiple graphs and not just one. This parser returns only the
   * default graph. An alternative would be to merge all graphs.
   *
   * @param object EasyRdf_Graph $graph   the graph to load the data into
   * @param string               $data    the RDF document data
   * @param string               $format  the format of the input data
   * @param string               $baseUri the base URI of the data being parsed
   * @return integer             The number of triples added to the graph
   */
  public function parse($graph, $data, $format, $baseUri) {
    parent::checkParseParams($graph, $data, $format, $baseUri);
    if ($format != 'jsonld') {
      throw new EasyRdf_Exception("EasyRdf_Parser_JsonLd does not support {$format}");
    }
    try {
      $quads = \ML\JsonLD\JsonLD::toRdf($data, array(
        'base' => $baseUri,
      ));
    } catch (\ML\JsonLD\Exception\JsonLdException $e) {
      throw new EasyRdf_Parser_Exception($e
        ->getMessage());
    }
    foreach ($quads as $quad) {

      // Ignore named graphs
      if (null !== $quad
        ->getGraph()) {
        continue;
      }
      $subject = (string) $quad
        ->getSubject();
      if ('_:' === substr($subject, 0, 2)) {
        $subject = $this
          ->remapBnode($subject);
      }
      $predicate = (string) $quad
        ->getProperty();
      if ($quad
        ->getObject() instanceof \ML\IRI\IRI) {
        $object = array(
          'type' => 'uri',
          'value' => (string) $quad
            ->getObject(),
        );
        if ('_:' === substr($object['value'], 0, 2)) {
          $object = array(
            'type' => 'bnode',
            'value' => $this
              ->remapBnode($object['value']),
          );
        }
      }
      else {
        $object = array(
          'type' => 'literal',
          'value' => $quad
            ->getObject()
            ->getValue(),
        );
        if ($quad
          ->getObject() instanceof \ML\JsonLD\LanguageTaggedString) {
          $object['lang'] = $quad
            ->getObject()
            ->getLanguage();
        }
        else {
          $object['datatype'] = $quad
            ->getObject()
            ->getType();
        }
      }
      $this
        ->addTriple($subject, $predicate, $object);
    }
    return $this->tripleCount;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EasyRdf_Parser::$baseUri protected property The base URI for the document currently being parsed
EasyRdf_Parser::$bnodeMap private property Mapping from source to graph bnode identifiers
EasyRdf_Parser::$format protected property The format of the document currently being parsed
EasyRdf_Parser::$graph protected property The current graph to insert triples into
EasyRdf_Parser::$tripleCount protected property
EasyRdf_Parser::addTriple protected function Add a triple to the current graph, and keep count of the number of triples @ignore 1
EasyRdf_Parser::checkParseParams protected function Check, cleanup parameters and prepare for parsing @ignore
EasyRdf_Parser::remapBnode protected function Create a new, unique bnode identifier from a source identifier. If the source identifier has previously been seen, the same new bnode identifier is returned. @ignore
EasyRdf_Parser::resetBnodeMap protected function Delete the bnode mapping - to be called at the start of a new parse @ignore
EasyRdf_Parser_JsonLd::parse public function Parse a JSON-LD document into an EasyRdf_Graph Overrides EasyRdf_Parser::parse