Documentation

Processor
in package

Processor processes JSON-LD documents as specified by the JSON-LD specification.

Tags
author

Markus Lanthaler mail@markus-lanthaler.com

Table of Contents

Constants

CONTEXT_MAX_IRI_RECURSIONS  = 10
Maximum number of recursion that are allowed to resolve an IRI
REMOTE_TIMEOUT  = 10
Timeout for retrieving remote documents in seconds

Properties

$baseIri  : IRI
$blankNodeCounter  : int
$blankNodeMap  : array<string|int, mixed>
$compactArrays  : bool
Compact arrays with just one element to a scalar
$documentFactory  : DocumentFactoryInterface
$documentLoader  : DocumentLoaderInterface
$framingKeywords  : array<string|int, mixed>
$generalizedRdf  : bool
Produce generalized RDF
$keywords  : array<string|int, mixed>
$optimize  : bool
Optimize compacted output
$useNativeTypes  : bool
Use native types when converting from RDF
$useRdfType  : bool
Use rdf:type instead of \@type when converting from RDF

Methods

__construct()  : mixed
Constructor
compact()  : mixed
Compacts a JSON-LD document
createInverseContext()  : array<string|int, mixed>
Creates an inverse context to simplify IRI compaction
expand()  : mixed
Expands a JSON-LD document
flatten()  : array<string|int, mixed>
Flattens a JSON-LD document
frame()  : array<string|int, mixed>
Frames a JSON-LD document according a supplied frame
fromRdf()  : array<string|int, mixed>
Converts an array of RDF quads to a JSON-LD document
getDocument()  : Document
Parses a JSON-LD document and returns it as a Document
parse()  : mixed
Parses a JSON-LD document to a PHP value
processContext()  : mixed
Processes a local context to update the active context
toRdf()  : array<string|int, Quad>
Converts an expanded JSON-LD document to RDF quads
addMissingNodeProperties()  : mixed
Adds all properties from node to result if they haven't been added yet
compactIri()  : string
Compacts an absolute IRI (or aliases a keyword)
compactValue()  : mixed
Compacts a value
createListObjects()  : mixed
Reconstruct @list arrays from linked list structures
elementToRdf()  : IRI|TypedValue|LanguageTagged|null
Converts a JSON-LD element to a RDF Quad object
expandIri()  : string
Expands a JSON-LD IRI value (term, compact IRI, IRI) to an absolute IRI and relabels blank nodes
expandKeywordValue()  : mixed
Expands the value of a keyword
expandValue()  : stdClass
Expands a scalar value
generateNodeMap()  : mixed
Creates a node map of an expanded JSON-LD document
getBlankNodeId()  : string
Generate a new blank node identifier
getPropertyDefinition()  : array<string|int, mixed>|string|null
Returns a property's definition
getValueProfile()  : array<string|int, mixed>
Calculates a value profile
hasNodeProperties()  : bool
Checks whether a node has properties and not just an @id
listToRdf()  : IRI
Converts a JSON-LD list to a linked RDF list (quads)
loadDocument()  : mixed
Load a JSON-LD document
mergeIntoProperty()  : mixed
Merges a value into a property of an object
nodeMatchesFrame()  : bool
Checks whether a node matches a frame or not.
objectToJsonLd()  : mixed
Converts an object to a JSON-LD representation
queryInverseContext()  : null|string
Queries the inverse context to find the term for a given query path (= value profile)
setProperty()  : mixed
Adds a property to an object if it doesn't exist yet
sortTerms()  : int
Compares two values by their length and then lexicographically
subtreeEquals()  : bool
Verifies whether two JSON-LD subtrees are equal not

Constants

CONTEXT_MAX_IRI_RECURSIONS

Maximum number of recursion that are allowed to resolve an IRI

public mixed CONTEXT_MAX_IRI_RECURSIONS = 10

REMOTE_TIMEOUT

Timeout for retrieving remote documents in seconds

public mixed REMOTE_TIMEOUT = 10

Properties

$baseIri

private IRI $baseIri = null

The base IRI

$blankNodeCounter

private int $blankNodeCounter = 0

Blank node counter

$blankNodeMap

private array<string|int, mixed> $blankNodeMap = array()

Blank node map

$compactArrays

Compact arrays with just one element to a scalar

private bool $compactArrays

If set to true, arrays holding just one element are compacted to scalars, otherwise the arrays are kept as arrays.

$framingKeywords

private static array<string|int, mixed> $framingKeywords = array( '@explicit', '@default', '@embed', //'@omitDefault', // TODO Is this really needed? '@embedChildren', )

Framing options keywords

$generalizedRdf

Produce generalized RDF

private bool $generalizedRdf

Unless set to true, triples/quads with a blank node predicate are dropped when converting to RDF.

$keywords

private static array<string|int, mixed> $keywords = array('@context', '@id', '@value', '@language', '@type', '@container', '@list', '@set', '@graph', '@reverse', '@base', '@vocab', '@index', '@null')

A list of all defined keywords

$optimize

Optimize compacted output

private bool $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.

$useNativeTypes

Use native types when converting from RDF

private bool $useNativeTypes

If set to true, the processor will try to convert datatyped literals to native types instead of using the expanded object form when converting from RDF. xsd:boolean values will be converted to booleans whereas xsd:integer and xsd:double values will be converted to numbers.

$useRdfType

Use rdf:type instead of \@type when converting from RDF

private bool $useRdfType

If set to true, the JSON-LD processor will use the expanded rdf:type IRI as the property instead of @type when converting from RDF.

Methods

__construct()

Constructor

public __construct(stdClass $options) : mixed

The options parameter must be passed and all off the following properties have to be set:

base
The base IRI.
compactArrays
If set to true, arrays holding just one element are compacted to scalars, otherwise the arrays are kept as arrays.
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.
useNativeTypes
If set to true, the processor will try to convert datatyped literals to native types instead of using the expanded object form when converting from RDF. xsd:boolean values will be converted to booleans whereas xsd:integer and xsd:double values will be converted to numbers.
useRdfType
If set to true, the JSON-LD processor will use the expanded rdf:type IRI as the property instead of @type when converting from RDF.
Parameters
$options : stdClass

Options to configure the various algorithms.

compact()

Compacts a JSON-LD document

public compact(mixed &$element[, array<string|int, mixed> $activectx = array() ][, array<string|int, mixed> $inversectx = array() ][, null|string $activeprty = null ]) : mixed

Attention: This method must be called with an expanded element, otherwise it might not work.

Parameters
$element : mixed

A JSON-LD element to be compacted.

$activectx : array<string|int, mixed> = array()

The active context.

$inversectx : array<string|int, mixed> = array()

The inverse context.

$activeprty : null|string = null

The active property.

Return values
mixed

The compacted JSON-LD document.

createInverseContext()

Creates an inverse context to simplify IRI compaction

public createInverseContext(array<string|int, mixed> $activectx) : array<string|int, mixed>

The inverse context is a multidimensional array that has the following shape:

[container|@null|term] [@type|@language][typeIRI|languageCode] [@null][@null] [term|propGen] [ array of terms ]
Parameters
$activectx : array<string|int, mixed>

The active context.

Return values
array<string|int, mixed>

The inverse context.

expand()

Expands a JSON-LD document

public expand(mixed &$element[, array<string|int, mixed> $activectx = array() ][, null|string $activeprty = null ][, bool $frame = false ]) : mixed
Parameters
$element : mixed

A JSON-LD element to be expanded.

$activectx : array<string|int, mixed> = array()

The active context.

$activeprty : null|string = null

The active property.

$frame : bool = false

True if a frame is being expanded, otherwise false.

Tags
throws
JsonLdException
Return values
mixed

The expanded document.

flatten()

Flattens a JSON-LD document

public flatten(mixed $element) : array<string|int, mixed>
Parameters
$element : mixed

A JSON-LD element to be flattened.

Return values
array<string|int, mixed>

An array representing the flattened element.

frame()

Frames a JSON-LD document according a supplied frame

public frame(array<string|int, mixed>|stdClass $element, mixed $frame) : array<string|int, mixed>
Parameters
$element : array<string|int, mixed>|stdClass

A JSON-LD element to be framed.

$frame : mixed

The frame.

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

$result The framed element in expanded form.

fromRdf()

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

public fromRdf(array<string|int, Quad$quads) : array<string|int, mixed>

The resulting JSON-LD document will be in expanded form.

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

The quads to convert

Tags
throws
InvalidQuadException

If the quad is invalid.

Return values
array<string|int, mixed>

The JSON-LD document.

getDocument()

Parses a JSON-LD document and returns it as a Document

public getDocument(array<string|int, mixed>|stdClass $input) : Document
Parameters
$input : array<string|int, mixed>|stdClass

The JSON-LD document to process.

Tags
throws
JsonLdException

If the JSON-LD input document is invalid.

Return values
Document

The parsed JSON-LD document.

parse()

Parses a JSON-LD document to a PHP value

public static parse(string $document) : mixed
Parameters
$document : string

A JSON-LD document.

Tags
throws
JsonLdException

If the JSON-LD document is not valid.

Return values
mixed

A PHP value.

processContext()

Processes a local context to update the active context

public processContext(mixed $loclctx, array<string|int, mixed> &$activectx[, array<string|int, mixed> $remotectxs = array() ]) : mixed
Parameters
$loclctx : mixed

The local context.

$activectx : array<string|int, mixed>

The active context.

$remotectxs : array<string|int, mixed> = array()

The already included remote contexts.

Tags
throws
JsonLdException

toRdf()

Converts an expanded JSON-LD document to RDF quads

public toRdf(array<string|int, mixed> $document) : array<string|int, Quad>

The result is an array of Quads.

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

The expanded JSON-LD document to be transformed into quads.

Return values
array<string|int, Quad>

The extracted quads.

addMissingNodeProperties()

Adds all properties from node to result if they haven't been added yet

private addMissingNodeProperties(stdClass $node, stdClass $options, stdClass $nodeMap, string $graph, stdClass &$result, array<string|int, mixed> $path) : mixed
Parameters
$node : stdClass

The node whose properties should processed.

$options : stdClass

The current framing options.

$nodeMap : stdClass

The node map.

$graph : string

The currently used graph.

$result : stdClass

The object to which the properties should be added.

$path : array<string|int, mixed>

The path of already processed nodes.

compactIri()

Compacts an absolute IRI (or aliases a keyword)

private compactIri(mixed $iri, array<string|int, mixed> $activectx, array<string|int, mixed> $inversectx[, mixed $value = null ][, bool $vocabRelative = false ][, bool $reverse = false ]) : string

If the IRI couldn't be compacted, the IRI is returned as is.

Parameters
$iri : mixed

The IRI to be compacted.

$activectx : array<string|int, mixed>

The active context.

$inversectx : array<string|int, mixed>

The inverse context.

$value : mixed = null

The value of the property to compact.

$vocabRelative : bool = false

If true is passed, this method tries to convert the IRI to an IRI relative to @vocab; otherwise, that fall back mechanism is disabled.

$reverse : bool = false

Is the IRI used within a @reverse container?

Return values
string

Returns the compacted IRI on success; otherwise the IRI is returned as is.

compactValue()

Compacts a value

private compactValue(mixed $value, array<string|int, mixed> $definition, array<string|int, mixed> $activectx, array<string|int, mixed> $inversectx) : mixed

The passed property definition must be an associative array containing the following data:

Parameters
$value : mixed

The value to compact (arrays are not allowed!).

$definition : array<string|int, mixed>

The active property's definition.

$activectx : array<string|int, mixed>

The active context.

$inversectx : array<string|int, mixed>

The inverse context.

Tags
type

=> type IRI or null @language => language code or null @index => index string or null @container => the container: @set, @list, @language, or @index

Return values
mixed

The compacted value.

createListObjects()

Reconstruct @list arrays from linked list structures

private createListObjects(stdClass $graphs, stdClass $usages) : mixed
Parameters
$graphs : stdClass

The graph map

$usages : stdClass

The global node usage map

elementToRdf()

Converts a JSON-LD element to a RDF Quad object

private elementToRdf(stdClass $element) : IRI|TypedValue|LanguageTagged|null
Parameters
$element : stdClass

The element to be converted.

Return values
IRI|TypedValue|LanguageTagged|null

The converted element to be used as Quad object.

expandIri()

Expands a JSON-LD IRI value (term, compact IRI, IRI) to an absolute IRI and relabels blank nodes

private expandIri(mixed $value, array<string|int, mixed> $activectx[, bool $relativeIri = false ][, bool $vocabRelative = false ][, null|stdClass $localctx = null ][, array<string|int, mixed> $path = array() ]) : string
Parameters
$value : mixed

The value to be expanded to an absolute IRI.

$activectx : array<string|int, mixed>

The active context.

$relativeIri : bool = false

Specifies whether $value should be treated as relative IRI against the base IRI or not.

$vocabRelative : bool = false

Specifies whether $value is relative to @vocab if set or not.

$localctx : null|stdClass = null

If the IRI is being expanded as part of context processing, the current local context has to be passed as well.

$path : array<string|int, mixed> = array()

A path of already processed terms to detect circular dependencies

Return values
string

The expanded IRI.

expandKeywordValue()

Expands the value of a keyword

private expandKeywordValue(stdClass &$element, string $activeprty, string $keyword, mixed $value, array<string|int, mixed> $activectx, bool $frame) : mixed
Parameters
$element : stdClass

The object this property-value pair is part of.

$activeprty : string

The active property.

$keyword : string

The keyword whose value is being expanded.

$value : mixed

The value to expand.

$activectx : array<string|int, mixed>

The active context.

$frame : bool

True if a frame is being expanded, otherwise false.

Tags
throws
JsonLdException

expandValue()

Expands a scalar value

private expandValue(mixed $value, array<string|int, mixed> $activectx, string $activeprty) : stdClass
Parameters
$value : mixed

The value to expand.

$activectx : array<string|int, mixed>

The active context.

$activeprty : string

The active property.

Return values
stdClass

The expanded value.

generateNodeMap()

Creates a node map of an expanded JSON-LD document

private generateNodeMap(stdClass &$nodeMap, stdClass|array<string|int, stdClass$element[, string $activegraph = JsonLD::DEFAULT_GRAPH ][, null|string $activeid = null ][, null|string $activeprty = null ][, null|stdClass &$list = null ]) : mixed

All keys in the node map are prefixed with "-" to support empty strings.

Parameters
$nodeMap : stdClass

The object holding the node map.

$element : stdClass|array<string|int, stdClass>

An expanded JSON-LD element to be put into the node map

$activegraph : string = JsonLD::DEFAULT_GRAPH

The graph currently being processed.

$activeid : null|string = null

The node currently being processed.

$activeprty : null|string = null

The property currently being processed.

$list : null|stdClass = null

The list object if a list is being processed.

getBlankNodeId()

Generate a new blank node identifier

private getBlankNodeId([null|string $id = null ]) : string

If an identifier is passed, a new blank node identifier is generated for it and stored for subsequent use. Calling the method with the same identifier (except null) will thus always return the same blank node identifier.

Parameters
$id : null|string = null

If available, existing blank node identifier.

Return values
string

Returns a blank node identifier.

getPropertyDefinition()

Returns a property's definition

private getPropertyDefinition(array<string|int, mixed> $activectx, string $property[, null|string $only = null ]) : array<string|int, mixed>|string|null

The result will be in the form

array('@type' => type or null, '@language' => language or null, '@container' => container or null, 'isKeyword' => true or false)

If $only is set, only the value of that key of the array above will be returned.

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

The active context.

$property : string

The property.

$only : null|string = null

If set, only this element of the definition will be returned.

Return values
array<string|int, mixed>|string|null

Returns either the property's definition or null if not found.

getValueProfile()

Calculates a value profile

private getValueProfile(stdClass $value, array<string|int, mixed> $inversectx) : array<string|int, mixed>

A value profile represent the schema of the value ignoring the concrete value. It is an associative array containing the following keys-value pairs:

  • @container: the container, defaults to @set
  • typeLang: is set to @type for typed values or @language for (language-tagged) strings; for all other values it is set to null
  • typeLangValue: set to the type of a typed value or the language of a language-tagged string (@null for all other strings); for all other values it is set to null
Parameters
$value : stdClass

The value.

$inversectx : array<string|int, mixed>

The inverse context.

Return values
array<string|int, mixed>

The value profile.

hasNodeProperties()

Checks whether a node has properties and not just an @id

private hasNodeProperties(stdClass $node) : bool

This is used to filter nodes consisting just of an @id-member when flattening and converting from RDF.

Parameters
$node : stdClass

The node

Return values
bool

True if the node has properties (other than @id), false otherwise.

listToRdf()

Converts a JSON-LD list to a linked RDF list (quads)

private listToRdf(array<string|int, mixed> $entries, array<string|int, mixed> &$quads[, null|IRI $graph = null ]) : IRI
Parameters
$entries : array<string|int, mixed>

The list entries

$quads : array<string|int, mixed>

The array to be used to hold the linked list

$graph : null|IRI = null

The graph to be used in the constructed Quads

Return values
IRI

Returns the IRI of the head of the list

loadDocument()

Load a JSON-LD document

private loadDocument(null|string|array<string|int, mixed>|stdClass $input) : mixed

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

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

The JSON-LD document or a path or URL pointing to one.

Tags
throws
JsonLdException
Return values
mixed

The loaded JSON-LD document

mergeIntoProperty()

Merges a value into a property of an object

private static mergeIntoProperty(stdClass &$object, string $property, mixed $value[, bool $alwaysArray = false ][, bool $unique = false ]) : mixed
Parameters
$object : stdClass

The object.

$property : string

The name of the property to which the value should be merged into.

$value : mixed

The value to merge into the property.

$alwaysArray : bool = false

If set to true, the resulting property will always be an array.

$unique : bool = false

If set to true, the value is only added if it doesn't exist yet.

nodeMatchesFrame()

Checks whether a node matches a frame or not.

private nodeMatchesFrame(stdClass $node, null|stdClass $frame, stdClass $options, stdClass $nodeMap, string $graph, array<string|int, mixed> &$parent[, array<string|int, mixed> $path = array() ]) : bool
Parameters
$node : stdClass

The node.

$frame : null|stdClass

The frame.

$options : stdClass

The current framing options.

$nodeMap : stdClass

The node map.

$graph : string

The currently used graph.

$parent : array<string|int, mixed>

The parent to which matching results should be added.

$path : array<string|int, mixed> = array()

The path of already processed nodes.

Return values
bool

Returns true if the node matches the frame, otherwise false.

objectToJsonLd()

Converts an object to a JSON-LD representation

private static objectToJsonLd(stdClass $object[, bool $useNativeTypes = true ]) : mixed

Only IRIs, language-tagged strings, and typed values are converted by this method. All other objects are returned as-is.

Parameters
$object : stdClass

The object to convert.

$useNativeTypes : bool = true

If set to true, native types are used for xsd:integer, xsd:double, and xsd:boolean, otherwise typed strings will be used instead.

Return values
mixed

The JSON-LD representation of the object.

queryInverseContext()

Queries the inverse context to find the term for a given query path (= value profile)

private queryInverseContext(array<string|int, mixed> $inversectx, array<string|int, string> $containers, array<string|int, string> $typeOrLangs, array<string|int, string> $typeOrLangValues) : null|string
Parameters
$inversectx : array<string|int, mixed>

The inverse context (or a subtree thereof)

$containers : array<string|int, string>
$typeOrLangs : array<string|int, string>
$typeOrLangValues : array<string|int, string>
Return values
null|string

The best matching term or null if none was found.

setProperty()

Adds a property to an object if it doesn't exist yet

private static setProperty(stdClass &$object, string $property, mixed $value[, mixed $errorCode = null ]) : mixed

If the property already exists, an exception is thrown as otherwise the existing value would be lost.

Parameters
$object : stdClass

The object.

$property : string

The name of the property.

$value : mixed

The value of the property.

$errorCode : mixed = null
Tags
throws
JsonLdException

If the property exists already JSON-LD.

sortTerms()

Compares two values by their length and then lexicographically

private static sortTerms(mixed $a, mixed $b) : int

If two strings have different lengths, the shorter one will be considered less than the other. If they have the same length, they are compared lexicographically.

Parameters
$a : mixed

Value A.

$b : mixed

Value B.

Return values
int

If value A is shorter than value B, -1 will be returned; if it's longer 1 will be returned. If both values have the same length and value A is considered lexicographically less, -1 will be returned, if they are equal 0 will be returned, otherwise 1 will be returned.

subtreeEquals()

Verifies whether two JSON-LD subtrees are equal not

private static subtreeEquals(mixed $a, mixed $b) : bool

Please note that two unlabeled blank nodes will never be equal by definition.

Parameters
$a : mixed

The first subtree.

$b : mixed

The second subree.

Return values
bool

Returns true if the two subtrees are equal; otherwise false.


        
On this page

Search results