Documentation

JsonLD
in package

JsonLD

JsonLD implements the algorithms defined by the JSON-LD 1.0 API and Processing Algorithms specification. Its interface is, apart from the usage of Promises, exactly the same as the one defined by the specification.

Furthermore, it implements an enhanced version of the JSON-LD Framing 1.0 draft and an object-oriented interface to access and manipulate JSON-LD documents.

Tags
author

Markus Lanthaler mail@markus-lanthaler.com

Table of Contents

Constants

DEFAULT_GRAPH  = '@default'
Identifier for the default graph
MERGED_GRAPH  = '@merged'
Identifier for the merged graph

Properties

$documentLoader  : mixed

Methods

compact()  : stdClass
Compact a JSON-LD document according a supplied context
expand()  : array<string|int, mixed>
Expand a JSON-LD document
flatten()  : stdClass
Flatten a JSON-LD document
frame()  : stdClass
Frame a JSON-LD document according a supplied frame
fromRdf()  : array<string|int, mixed>
Convert an array of RDF quads to a JSON-LD document
getDocument()  : Document
Load and parse a JSON-LD document
setDefaultDocumentLoader()  : mixed
Set the default document loader.
toRdf()  : array<string|int, Quad>
Convert a JSON-LD document to RDF quads
toString()  : string
Convert the PHP structure returned by the various processing methods to a string
doCompact()  : stdClass
Compact a JSON-LD document according a supplied context
mergeOptions()  : stdClass
Merge the passed options with the options' default values.

Constants

DEFAULT_GRAPH

Identifier for the default graph

public mixed DEFAULT_GRAPH = '@default'

MERGED_GRAPH

Identifier for the merged graph

public mixed MERGED_GRAPH = '@merged'

Properties

$documentLoader

private static mixed $documentLoader = null

Methods

compact()

Compact a JSON-LD document according a supplied context

public static compact(string|stdClass|array<string|int, mixed> $input[, null|string|stdClass|array<string|int, mixed> $context = null ][, null|array<string|int, mixed>|stdClass $options = null ]) : stdClass
APIYes

Both the document and the context can be supplied directly as string, by passing a file path, or by passing a URL.

Usage:

$compacted = JsonLD::compact('document.jsonld', 'context.jsonld');
print_r($compacted);

It is possible to configure the compaction process by setting the options parameter accordingly. Available options are:

base
The base IRI of the input document.
expandContext
An optional context to use additionally to the context embedded in input when expanding the input.
optimize
If set to true, the processor is free to optimize the result to produce an even compacter representation than the algorithm described by the official JSON-LD specification.
compactArrays
If set to true, arrays holding just one element are compacted to scalars, otherwise the arrays are kept as arrays.
documentLoader
The document loader.

The options parameter might be passed as associative array or as object.

Parameters
$input : string|stdClass|array<string|int, mixed>

The JSON-LD document to compact.

$context : null|string|stdClass|array<string|int, mixed> = null

The context.

$options : null|array<string|int, mixed>|stdClass = null

Options to configure the compaction process.

Tags
throws
JsonLdException
Return values
stdClass

The compacted JSON-LD document.

expand()

Expand a JSON-LD document

public static expand(string|stdClass|array<string|int, mixed> $input[, null|array<string|int, mixed>|stdClass $options = null ]) : array<string|int, mixed>
APIYes

The document can be supplied directly as string, by passing a file path, or by passing a URL.

Usage:

$expanded = JsonLD::expand('document.jsonld');
print_r($expanded);

It is possible to configure the expansion process by setting the options parameter accordingly. Available options are:

base
The base IRI of the input document.
expandContext
An optional context to use additionally to the context embedded in input when expanding the input.
documentLoader
The document loader.

The options parameter might be passed as associative array or as object.

Parameters
$input : string|stdClass|array<string|int, mixed>

The JSON-LD document to expand.

$options : null|array<string|int, mixed>|stdClass = null

Options to configure the expansion process.

Tags
throws
JsonLdException
Return values
array<string|int, mixed>

The expanded JSON-LD document.

flatten()

Flatten a JSON-LD document

public static flatten(string|stdClass|array<string|int, mixed> $input[, null|string|stdClass|array<string|int, mixed> $context = null ][, null|array<string|int, mixed>|stdClass $options = null ]) : stdClass
APIYes

Both the document and the context can be supplied directly as string, by passing a file path, or by passing a URL.

Usage:

$flattened = JsonLD::flatten('document.jsonld');
print_r($flattened);

It is possible to configure the flattening process by setting the options parameter accordingly. Available options are:

base
The base IRI of the input document.
expandContext
An optional context to use additionally to the context embedded in input when expanding the input.
graph
The graph whose flattened representation should be returned. The default graph is identified by [](DEFAULT_GRAPH) and the merged dataset graph by [](MERGED_GRAPH). If null is passed, all graphs will be returned.
documentLoader
The document loader.

The options parameter might be passed as associative array or as object.

Parameters
$input : string|stdClass|array<string|int, mixed>

The JSON-LD document to flatten.

$context : null|string|stdClass|array<string|int, mixed> = null

The context to compact the flattened document. If null is passed, the result will not be compacted.

$options : null|array<string|int, mixed>|stdClass = null

Options to configure the flattening process.

Tags
throws
JsonLdException
Return values
stdClass

The flattened JSON-LD document.

frame()

Frame a JSON-LD document according a supplied frame

public static frame(string|stdClass|array<string|int, mixed> $input, string|stdClass $frame[, null|array<string|int, mixed>|stdClass $options = null ]) : stdClass
APIYes

Both the document and the frame can be supplied directly as string, by passing a file path, or by passing a URL.

Usage:

$result = JsonLD::frame('document.jsonld', 'frame.jsonldf');
print_r($compacted);

It is possible to configure the framing process by setting the options parameter accordingly. Available options are:

base
The base IRI of the input document.
expandContext
An optional context to use additionally to the context embedded in input when expanding the input.
optimize
If set to true, the processor is free to optimize the result to produce an even compacter representation than the algorithm described by the official JSON-LD specification.
compactArrays
If set to true, arrays holding just one element are compacted to scalars, otherwise the arrays are kept as arrays.
documentLoader
The document loader.

The options parameter might be passed as associative array or as object.

Parameters
$input : string|stdClass|array<string|int, mixed>

The JSON-LD document to compact.

$frame : string|stdClass

The frame.

$options : null|array<string|int, mixed>|stdClass = null

Options to configure the framing process.

Tags
throws
JsonLdException
Return values
stdClass

The framed JSON-LD document.

fromRdf()

Convert an array of RDF quads to a JSON-LD document

public static fromRdf(array<string|int, Quad$quads[, null|array<string|int, mixed>|stdClass $options = null ]) : array<string|int, mixed>
APIYes

Usage:

$document = JsonLD::fromRdf($quads);
print(JsonLD::toString($document, true));

It is possible to configure the conversion process by setting the options parameter accordingly. Available options are:

base
The base IRI of the input document.
useNativeTypes
If set to true, native types are used for xsd:integer, xsd:double, and xsd:boolean; otherwise, typed strings will be used instead.
useRdfType
If set to true, rdf:type will be used instead of @type
documentLoader
The document loader.

The options parameter might be passed as associative array or as object.

Parameters
$quads : array<string|int, Quad>

Array of quads.

$options : null|array<string|int, mixed>|stdClass = null

Options to configure the expansion process.

Tags
throws
InvalidQuadException

If an invalid quad was detected.

throws
JsonLdException

If converting the quads to a JSON-LD document failed.

Return values
array<string|int, mixed>

The JSON-LD document in expanded form.

getDocument()

Load and parse a JSON-LD document

public static getDocument(string|stdClass|array<string|int, mixed> $input[, null|array<string|int, mixed>|stdClass $options = null ]) : Document
APIYes

The document can be supplied directly as string, by passing a file path, or by passing a URL.

Usage:

$document = JsonLD::getDocument('document.jsonld');
print_r($document->getGraphNames());

It is possible to configure the processing by setting the options parameter accordingly. Available options are:

base
The base IRI of the input document.
expandContext
An optional context to use additionally to the context embedded in input when expanding the input.
documentFactory
The document factory.
documentLoader
The document loader.

The options parameter might be passed as associative array or as object.

Parameters
$input : string|stdClass|array<string|int, mixed>

The JSON-LD document to process.

$options : null|array<string|int, mixed>|stdClass = null

Options to configure the processing.

Tags
throws
JsonLdException
Return values
Document

The parsed JSON-LD document.

setDefaultDocumentLoader()

Set the default document loader.

public static setDefaultDocumentLoader(DocumentLoaderInterface $documentLoader) : mixed

It can be overridden in individual operations by setting the documentLoader option.

Parameters
$documentLoader : DocumentLoaderInterface

toRdf()

Convert a JSON-LD document to RDF quads

public static toRdf(string|stdClass|array<string|int, mixed> $input[, null|array<string|int, mixed>|stdClass $options = null ]) : array<string|int, Quad>
APIYes

The document can be supplied directly as string, by passing a file path, or by passing a URL.

Usage:

$quads = JsonLD::toRdf('document.jsonld');
print_r($quads);

It is possible to configure the extraction process by setting the options parameter accordingly. Available options are:

base
The base IRI of the input document.
expandContext
An optional context to use additionally to the context embedded in input when expanding the input.
documentLoader
The document loader.

The options parameter might be passed as associative array or as object.

Parameters
$input : string|stdClass|array<string|int, mixed>

The JSON-LD document to expand.

$options : null|array<string|int, mixed>|stdClass = null

Options to configure the expansion process.

Tags
throws
JsonLdException
Return values
array<string|int, Quad>

The extracted quads.

toString()

Convert the PHP structure returned by the various processing methods to a string

public static toString(mixed $value[, bool $pretty = false ]) : string

Usage:

$compacted = JsonLD::compact('document.jsonld', 'context.jsonld'); $prettyString = JsonLD::toString($compacted, true); print($prettyString);

Parameters
$value : mixed

The value to convert.

$pretty : bool = false

Use whitespace in returned string to format it (this just works in PHP >=5.4)?

Return values
string

doCompact()

Compact a JSON-LD document according a supplied context

private static doCompact(array<string|int, mixed> $input, null|string|stdClass|array<string|int, mixed> $context, stdClass $options[, bool $alwaysGraph = false ]) : stdClass

In contrast to , this method assumes that the input has already been expanded.

Parameters
$input : array<string|int, mixed>

The JSON-LD document to compact.

$context : null|string|stdClass|array<string|int, mixed>

The context.

$options : stdClass

Options to configure the compaction process.

$alwaysGraph : bool = false

If set to true, the resulting document will always explicitly contain the default graph at the top-level.

Tags
throws
JsonLdException
Return values
stdClass

The compacted JSON-LD document.

mergeOptions()

Merge the passed options with the options' default values.

private static mergeOptions(null|array<string|int, mixed>|stdClass $options) : stdClass
Parameters
$options : null|array<string|int, mixed>|stdClass

The options.

Return values
stdClass

The merged options.


        
On this page

Search results