JsonLD for PHP

repository·master·Indexed 43 days ago

https://github.com/lanthaler/jsonld

A fully conforming JSON-LD processor for PHP supporting the standard JSON-LD API, including expansion, compaction, framing, flattening, and conversion to RDF quads. It features N-Quads serialization and parsing, as well as an experimental object-oriented interface for graph manipulation.

Tokens
950
Snippets
4
Records
4
Agent score
6%

What's inside JsonLD

  1. Install JsonLD via Composer

    master

    The recommended way to install JsonLD is using Composer. This will automatically handle dependencies and allow you to use the library in your PHP projects.

    Requirements:

    • PHP 5.3 or later
    composer require ml/json-ld
    require('vendor/autoload.php');
  2. Use the official JSON-LD API

    master

    JsonLD provides a static interface for standard JSON-LD operations as defined by the JSON-LD API specification. You can expand, compact, frame, flatten, and convert documents to RDF quads.

    // Official JSON-LD API
    $expanded = JsonLD::expand('document.jsonld');
    $compacted = JsonLD::compact('document.jsonld', 'context.jsonld');
    $framed = JsonLD::frame('document.jsonld', 'frame.jsonld');
    $flattened = JsonLD::flatten('document.jsonld');
    $quads = JsonLD::toRdf('document.jsonld');
    
    // Output the expanded document (pretty print)
    print JsonLD::toString($expanded, true);
  3. Serialize and parse N-Quads

    master

    You can use the NQuads class to serialize RDF quads into N-Quads format and parse them back into quads.

    // Serialize the quads as N-Quads
    $nquads = new NQuads();
    $serialized = $nquads->serialize($quads);
    print $serialized;
    
    // And parse them again to a JSON-LD document
    $quads = $nquads->parse($serialized);
    $document = JsonLD::fromRdf($quads);
    
    print JsonLD::toString($document, true);
  4. Use the Node-centric (Object-Oriented) API

    master

    JsonLD includes an experimental object-oriented interface for interacting with JSON-LD documents as a graph of nodes. This allows you to traverse graphs, retrieve specific nodes by ID, access properties, and manipulate the graph structure directly.

    // Node-centric API
    $doc = JsonLD::getDocument('document.jsonld');
    
    // get the default graph
    $graph = $doc->getGraph();
    
    // get all nodes in the graph
    $nodes = $graph->getNodes();
    
    // retrieve a node by ID
    $node = $graph->getNode('http://example.com/node1');
    
    // get a property
    $node->getProperty('http://example.com/vocab/name');
    
    // add a new blank node to the graph
    $newNode = $graph->createNode();
    
    // link the new blank node to the existing node
    $node->addPropertyValue('http://example.com/vocab/link', $newNode);
    
    // even reverse properties are supported; this returns $newNode
    $node->getReverseProperty('http://example.com/vocab/link');
    
    // serialize the graph and convert it to a string
    $serialized = JsonLD::toString($graph->toJsonLd());