php-json-ld

repository·master·Indexed 43 days ago

https://github.com/digitalbazaar/php-json-ld

A PHP implementation of the JSON-LD specification for adding semantics to JSON and working with Linked Data. Provides functions for compacting, expanding, flattening, framing, and normalizing JSON-LD documents, as well as support for custom document loaders.

Tokens
1.1K
Snippets
7
Records
7
Agent score
5%

What's inside php-json-ld

  1. Configure and use custom document loaders

    master

    Document loaders are responsible for fetching remote contexts or documents. You can manage them in two ways:

    1. Set a global default loader

    Use jsonld_set_document_loader($loader_name) to set a global loader for all subsequent calls.

    • To force HTTPS-only loading: jsonld_set_document_loader('jsonld_default_secure_document_loader');
    • To set a custom function: jsonld_set_document_loader('my_custom_doc_loader');

    2. Use a per-call loader

    Pass a documentLoader key in the options array to a specific function call to avoid modifying the global state.

    Note on Custom Loaders: A custom loader should return a RemoteDocument object with properties: contextUrl, document, and documentUrl.

    // Per-call custom loader example
    $compacted = jsonld_compact($foo, 'http://example.com/mycontext', array(
      'documentLoader' => 'mock_load'
    ));
  2. Run the library test suite

    master

    To verify changes using the sample testing utility, you must first clone the json-ld.org and normalization repositories from GitHub. Then, run the PHPUnit test.php application pointing to those directories:

    phpunit --group json-ld.org test.php -d {PATH_TO_JSON_LD_ORG/test-suite}
    phpunit --group normalization test.php -d {PATH_TO_NORMALIZATION/tests}
  3. Normalize a JSON-LD document

    master

    Use jsonld_normalize($doc, $options) to produce a canonical representation of the document. This is useful for hashing or comparing documents.

    Supported options include:

    • algorithm: The normalization algorithm to use (e.g., 'URDNA2015').
    • format: The output format (e.g., 'application/nquads').
    $normalized = jsonld_normalize(
      $doc, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads')
    );
  4. Expand a JSON-LD document

    master

    Use jsonld_expand($doc) to remove the context from a document, expanding all compact terms back into their full IRIs. This results in an expanded document form. You can pass a URL string to expand a remote document.

    $expanded = jsonld_expand($compacted);
    // or using a URL
    jsonld_expand('http://example.org/doc');
  5. Compact a JSON-LD document

    master

    Use jsonld_compact($doc, $context) to transform a document with full IRIs into a more compact form using a specific context. This maps long IRIs to short, human-readable keys. You can also pass URLs as strings for the document and context instead of arrays/objects.

    $doc = (object)array(
      "http://schema.org/name" => "Manu Sporny",
      "http://schema.org/url" => (object)array("@id" => "http://manu.sporny.org/"),
      "http://schema.org/image" => (object)array("@id" => "http://manu.sporny.org/images/manu.png")
    );
    
    $context = (object)array(
      "name" => "http://schema.org/name",
      "homepage" => (object)array("@id" => "http://schema.org/url", "@type" => "@id"),
      "image" => (object)array("@id" => "http://schema.org/image", "@type" => "@id")
    );
    
    $compacted = jsonld_compact($doc, $context);