### PHP: JSON-LD Custom Cache Document Loader Source: https://github.com/digitalbazaar/php-json-ld/blob/master/README.md Presents an example of a custom document loader that implements a simple in-memory cache for fetched documents. This can improve performance by avoiding redundant network requests for previously loaded contexts. The loader function checks a global `$cache` array. ```php // a custom loader that uses a simplistic in-memory cache (no invalidation) global $cache; $cache = array(); function cache_load($url) { global $jsonld_default_load_document, $cache; if(isset($cache[$url])) { return $cache[$url]; } // use default loader $doc = call_user_func($jsonld_default_load_document, $url); $cache[$url] = $doc; return $doc; } // use the cache loader for just this call, witout modifying the default one $compacted = jsonld_compact($foo, 'http://schema.org', array( 'documentLoader' => 'cache_load')); ``` -------------------------------- ### PHP: JSON-LD Custom Mock Document Loader Source: https://github.com/digitalbazaar/php-json-ld/blob/master/README.md Provides a concrete example of a custom document loader that uses an in-memory mock for specific URLs before falling back to the default loader. This is useful for testing or providing predefined contexts without network access. The loader function checks a global `$mocks` array. ```php // a custom loader that demonstrates using a simple in-memory mock for // certain contexts before falling back to the default loader // note: if you want to set this loader as the new default, you'll need to // store the previous default in another variable first and access that inside // the loader global $mocks; $mocks = array('http://example.com/mycontext' => (object)array( 'hombre' => 'http://schema.org/name')); function mock_load($url) { global $jsonld_default_load_document, $mocks; if(isset($mocks[$url])) { // return a "RemoteDocument", it has these three properties: return (object)array( 'contextUrl' => null, 'document' => $mocks[$url], 'documentUrl' => $url); } // use default loader return call_user_func($jsonld_default_load_document, $url); } // use the mock loader for just this call, witout modifying the default one $compacted = jsonld_compact($foo, 'http://example.com/mycontext', array( 'documentLoader' => 'mock_load')); ``` -------------------------------- ### PHP: Frame JSON-LD Document Source: https://github.com/digitalbazaar/php-json-ld/blob/master/README.md Provides an example of framing a JSON-LD document. Framing transforms a JSON-LD document into a specific tree structure based on a provided frame. This is useful for controlling the output structure and presentation of JSON-LD data. The `jsonld_frame` function is used. ```php // frame a document // see: http://json-ld.org/spec/latest/json-ld-framing/#introduction $framed = jsonld_frame($doc, $frame); // document transformed into a particular tree structure per the given frame ``` -------------------------------- ### PHP: Compact JSON-LD Document with Context Source: https://github.com/digitalbazaar/php-json-ld/blob/master/README.md Demonstrates how to compact a JSON-LD document using a provided context object. This process simplifies the JSON-LD structure by replacing full URIs with shorter aliases defined in the context. It utilizes the `jsonld_compact` function. ```php $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") ); // compact a document according to a particular context // see: http://json-ld.org/spec/latest/json-ld/#compacted-document-form $compacted = jsonld_compact($doc, $context); echo json_encode($compacted, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); /* Output: { "@context": {...}, "image": "http://manu.sporny.org/images/manu.png", "homepage": "http://manu.sporny.org/", "name": "Manu Sporny" } */ // compact using URLs jsonld_compact('http://example.org/doc', 'http://example.org/context'); ``` -------------------------------- ### PHP: Expand JSON-LD Document Source: https://github.com/digitalbazaar/php-json-ld/blob/master/README.md Shows how to expand a compacted JSON-LD document to its full, expanded form. This process explicitly defines all terms using their full URIs, making the structure more verbose but unambiguous. The `jsonld_expand` function is used for this operation. ```php // expand a document, removing its context // see: http://json-ld.org/spec/latest/json-ld/#expanded-document-form $expanded = jsonld_expand($compacted) { echo json_encode($expanded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); /* Output: { "http://schema.org/image": [{"@id": "http://manu.sporny.org/images/manu.png"}], "http://schema.org/name": [{"@value": "Manu Sporny"}], "http://schema.org/url": [{"@id": "http://manu.sporny.org/"}] } */ // expand using URLs jsonld_expand('http://example.org/doc'); ``` -------------------------------- ### PHP: Set Custom JSON-LD Document Loader Source: https://github.com/digitalbazaar/php-json-ld/blob/master/README.md Shows how to set a custom document loader for the JSON-LD library. This allows for advanced control over how external documents and contexts are fetched, such as using mock data or implementing custom caching strategies. The `jsonld_set_document_loader` function is used to register a custom loader function. ```php // set a default custom document loader jsonld_set_document_loader('my_custom_doc_loader'); ``` -------------------------------- ### PHP: Set Secure JSON-LD Document Loader Source: https://github.com/digitalbazaar/php-json-ld/blob/master/README.md Explains how to configure the JSON-LD library to use a secure document loader, which enforces HTTPS for loading contexts. This enhances security by preventing the use of potentially insecure HTTP connections. The `jsonld_set_document_loader` function is used with `jsonld_default_secure_document_loader`. ```php // force HTTPS-only context loading: // use built-in secure document loader jsonld_set_document_loader('jsonld_default_secure_document_loader'); ``` -------------------------------- ### PHP: Normalize JSON-LD Document to N-Quads Source: https://github.com/digitalbazaar/php-json-ld/blob/master/README.md Demonstrates how to normalize a JSON-LD document using the RDF Dataset Normalization Algorithm (URDNA2015). Normalization produces a canonical representation of the JSON-LD data, suitable for hashing and comparison. The `jsonld_normalize` function is used, with options to specify the algorithm and output format. ```php // normalize a document using the RDF Dataset Normalization Algorithm // (URDNA2015), see: http://json-ld.github.io/normalization/spec/ $normalized = jsonld_normalize( $doc, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads')); // normalized is a string that is a canonical representation of the document // that can be used for hashing, comparison, etc. ``` -------------------------------- ### PHP: Flatten JSON-LD Document Source: https://github.com/digitalbazaar/php-json-ld/blob/master/README.md Illustrates the process of flattening a JSON-LD document. Flattening takes a potentially nested JSON-LD structure and brings all defined terms to the top level, creating a more linear representation. The `jsonld_flatten` function performs this transformation. ```php // flatten a document // see: http://json-ld.org/spec/latest/json-ld/#flattened-document-form $flattened = jsonld_flatten($doc); // all deep-level trees flattened to the top-level ``` -------------------------------- ### JSON-LD Processing with JsonLdProcessor in PHP Source: https://context7.com/digitalbazaar/php-json-ld/llms.txt Demonstrates the usage of the JsonLdProcessor class for expanding, compacting, and converting JSON-LD documents. It includes registering custom RDF parsers and handling potential exceptions. This class offers instance-specific configuration for JSON-LD processing. ```php registerRDFParser('application/nquads', function($input) { // Custom parsing logic return JsonLdProcessor::parseNQuads($input); }); $doc = (object)array( "@context" => (object)array("name" => "http://schema.org/name"), "name" => "Example" ); try { // Use processor methods $expanded = $processor->expand($doc, array('base' => 'http://example.org/')); $compacted = $processor->compact($expanded, (object)array( "name" => "http://schema.org/name" ), array()); echo json_encode($compacted, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); // Convert to RDF $rdf = $processor->toRDF($doc, array('format' => 'application/nquads')); echo $rdf; } catch (JsonLdException $e) { echo "Error: " . $e->getMessage(); echo "Type: " . $e->type . "\n"; echo "Code: " . $e->code . "\n"; } ?> ``` -------------------------------- ### Compact JSON-LD Document using php-json-ld Source: https://context7.com/digitalbazaar/php-json-ld/llms.txt The `jsonld_compact` function compacts a JSON-LD document using a provided context, creating a more concise representation with shortened property names and applying type coercion rules. It requires the `jsonld.php` include and can throw `JsonLdException`. ```php "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" ) ); try { $compacted = jsonld_compact($doc, $context); echo json_encode($compacted, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); /* Output: { "@context": { "name": "http://schema.org/name", "homepage": { "@id": "http://schema.org/url", "@type": "@id" }, "image": { "@id": "http://schema.org/image", "@type": "@id" } }, "name": "Manu Sporny", "homepage": "http://manu.sporny.org/", "image": "http://manu.sporny.org/images/manu.png" } */ } catch (JsonLdException $e) { echo "Error: " . $e->getMessage(); } ?> ``` -------------------------------- ### jsonld_compact Source: https://context7.com/digitalbazaar/php-json-ld/llms.txt Compacts a JSON-LD document using a provided context, creating a more concise representation with shortened property names and applying type coercion rules. ```APIDOC ## jsonld_compact ### Description Compacts a JSON-LD document using a provided context, creating a more concise representation with shortened property names and applying type coercion rules. ### Method `PHP Function` ### Endpoint N/A (Local PHP function) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body - **$doc** (object) - Required - The JSON-LD document to compact. - **$context** (object) - Required - The context to use for compaction. ### Request Example ```php "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" ) ); try { $compacted = jsonld_compact($doc, $context); echo json_encode($compacted, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); } catch (JsonLdException $e) { echo "Error: " . $e->getMessage(); } ?> ``` ### Response #### Success Response (Object) - **@context** (object) - The context used for compaction. - **[shortened_property]** (string/object) - The compacted properties. #### Response Example ```json { "@context": { "name": "http://schema.org/name", "homepage": { "@id": "http://schema.org/url", "@type": "@id" }, "image": { "@id": "http://schema.org/image", "@type": "@id" } }, "name": "Manu Sporny", "homepage": "http://manu.sporny.org/", "image": "http://manu.sporny.org/images/manu.png" } ``` ``` -------------------------------- ### jsonld_expand Source: https://context7.com/digitalbazaar/php-json-ld/llms.txt Expands a JSON-LD document by applying context rules and converting shorthand properties to their full IRI form, removing all context information from the output. ```APIDOC ## jsonld_expand ### Description Expands a JSON-LD document by applying context rules and converting shorthand properties to their full IRI form, removing all context information from the output. ### Method `PHP Function` ### Endpoint N/A (Local PHP function) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body - **$doc** (object) - Required - The JSON-LD document to expand. ### Request Example ```php (object)array( "name" => "http://schema.org/name", "homepage" => (object)array( "@id" => "http://schema.org/url", "@type" => "@id" ) ), "name" => "Manu Sporny", "homepage" => "http://manu.sporny.org/" ); try { $expanded = jsonld_expand($doc); echo json_encode($expanded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); } catch (JsonLdException $e) { echo "Error: " . $e->getMessage(); } ?> ``` ### Response #### Success Response (Array of Objects) - **@id** (string) - The IRI of the node. - **[IRI]** (Array of Objects) - Expanded properties. #### Response Example ```json [ { "http://schema.org/name": [ { "@value": "Manu Sporny" } ], "http://schema.org/url": [ { "@id": "http://manu.sporny.org/" } ] } ] ``` ``` -------------------------------- ### Expand JSON-LD Document using php-json-ld Source: https://context7.com/digitalbazaar/php-json-ld/llms.txt The `jsonld_expand` function expands a JSON-LD document by applying context rules and converting shorthand properties to their full IRI form. It removes all context information from the output. This function requires the `jsonld.php` include and handles potential `JsonLdException` errors. ```php (object)array( "name" => "http://schema.org/name", "homepage" => (object)array( "@id" => "http://schema.org/url", "@type" => "@id" ) ), "name" => "Manu Sporny", "homepage" => "http://manu.sporny.org/" ); try { $expanded = jsonld_expand($doc); echo json_encode($expanded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); /* Output: [ { "http://schema.org/name": [ { "@value": "Manu Sporny" } ], "http://schema.org/url": [ { "@id": "http://manu.sporny.org/" } ] } ] */ } catch (JsonLdException $e) { echo "Error: " . $e->getMessage(); } ?> ``` -------------------------------- ### Set Custom Document Loader with php-json-ld Source: https://context7.com/digitalbazaar/php-json-ld/llms.txt Sets a custom document loader function for the JSON-LD library. This allows for advanced control over how remote JSON-LD contexts and documents are fetched, enabling features like caching, custom security policies, or alternative URL resolution strategies. The function takes the name of the custom loader function as an argument. ```php "http://schema.org/", "name" => "Example" ); try { $expanded = jsonld_expand($doc); echo json_encode($expanded, JSON_PRETTY_PRINT); } catch (JsonLdException $e) { echo "Error: " . $e->getMessage(); } ?> ``` -------------------------------- ### jsonld_flatten Source: https://context7.com/digitalbazaar/php-json-ld/llms.txt Flattens a JSON-LD document by collecting all nodes into a flat array at the top level, assigning blank node identifiers where necessary. ```APIDOC ## jsonld_flatten ### Description Flattens a JSON-LD document by collecting all nodes into a flat array at the top level, assigning blank node identifiers where necessary. ### Method `PHP Function` ### Endpoint N/A (Local PHP function) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body - **$doc** (object) - Required - The JSON-LD document to flatten. - **$base** (string/null) - Optional - The base IRI to use for resolving relative IRIs. ### Request Example ```php (object)array( "name" => "http://schema.org/name", "knows" => "http://schema.org/knows" ), "@id" => "http://example.org/person1", "name" => "Alice", "knows" => (object)array( "@id" => "http://example.org/person2", "name" => "Bob" ) ); try { $flattened = jsonld_flatten($doc, null); echo json_encode($flattened, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); } catch (JsonLdException $e) { echo "Error: " . $e->getMessage(); } ?> ``` ### Response #### Success Response (Array of Objects) - **@id** (string) - The IRI of the node. - **[IRI]** (Array of Objects) - Expanded properties. #### Response Example ```json [ { "@id": "http://example.org/person1", "http://schema.org/name": [ { "@value": "Alice" } ], "http://schema.org/knows": [ { "@id": "http://example.org/person2" } ] }, { "@id": "http://example.org/person2", "http://schema.org/name": [ { "@value": "Bob" } ] } ] ``` ``` -------------------------------- ### Convert JSON-LD to RDF (N-Quads) with php-json-ld Source: https://context7.com/digitalbazaar/php-json-ld/llms.txt Converts a JSON-LD document into RDF triples, specifically in the N-Quads format. This is useful for integrating JSON-LD data with RDF triple stores or for querying using SPARQL endpoints. The function requires the JSON-LD document and an options array specifying the desired output format. ```php "http://schema.org/", "@id" => "http://example.org/person", "name" => "Jane Doe", "jobTitle" => "Professor", "telephone" => "(425) 123-4567", "url" => "http://www.janedoe.com" ); try { $nquads = jsonld_to_rdf($doc, array('format' => 'application/nquads')); echo $nquads; /* Output: "Jane Doe" . "Professor" . "(425) 123-4567" . . */ } catch (JsonLdException $e) { echo "Error: " . $e->getMessage(); } ?> ``` -------------------------------- ### Convert RDF (N-Quads) to JSON-LD with php-json-ld Source: https://context7.com/digitalbazaar/php-json-ld/llms.txt Converts RDF triples, provided in N-Quads format, into a JSON-LD document. This function facilitates the consumption of existing RDF data within applications that use JSON-LD. It takes the N-Quads string and an options array (including format and type handling) as input. ```php "Jane Doe" . "Professor" . . '; try { $doc = jsonld_from_rdf( $nquads, array( 'format' => 'application/nquads', 'useNativeTypes' => true ) ); echo json_encode($doc, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); /* Output: JSON-LD array with expanded properties */ } catch (JsonLdException $e) { echo "Error: " . $e->getMessage(); } ?> ``` -------------------------------- ### Frame JSON-LD Document with php-json-ld Source: https://context7.com/digitalbazaar/php-json-ld/llms.txt Frames a JSON-LD document according to a specified frame pattern. This function reshapes the document into a desired tree structure by embedding or linking nodes. It requires the JSON-LD document and a frame object as input and outputs the framed document or throws a JsonLdException on error. ```php "http://example.org/library", "@type" => "http://schema.org/Library", "http://schema.org/name" => array( (object)array("@value" => "City Library") ), "http://schema.org/contains" => array( (object)array("@id" => "http://example.org/book1") ) ), (object)array( "@id" => "http://example.org/book1", "@type" => "http://schema.org/Book", "http://schema.org/name" => array( (object)array("@value" => "Example Book") ) ) ); $frame = (object)array( "@context" => (object)array( "name" => "http://schema.org/name", "contains" => "http://schema.org/contains" ), "@type" => "http://schema.org/Library", "contains" => (object)array( "@type" => "http://schema.org/Book" ) ); try { $framed = jsonld_frame($doc, $frame); echo json_encode($framed, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); /* Output: A framed document with Library embedding Book nodes */ } catch (JsonLdException $e) { echo "Error: " . $e->getMessage(); } ?> ``` -------------------------------- ### Normalize JSON-LD Document with php-json-ld Source: https://context7.com/digitalbazaar/php-json-ld/llms.txt Normalizes a JSON-LD document using the URDNA2015 algorithm. This process produces a canonical representation of the document, which is essential for cryptographic signing or accurate comparison between different versions of JSON-LD data. The function takes the document and an options array (specifying algorithm and output format) as input. ```php (object)array( "name" => "http://schema.org/name", "homepage" => (object)array( "@id" => "http://schema.org/url", "@type" => "@id" ) ), "name" => "Manu Sporny", "homepage" => "http://manu.sporny.org/" ); try { $normalized = jsonld_normalize( $doc, array( 'algorithm' => 'URDNA2015', 'format' => 'application/nquads' ) ); echo $normalized; /* Output: N-Quads formatted canonical representation */ } catch (JsonLdException $e) { echo "Error: " . $e->getMessage(); } ?> ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.