Compare commits

..

No commits in common. "master" and "0.3.7" have entirely different histories.

5 changed files with 6013 additions and 6530 deletions

14
.travis.yml Normal file
View file

@ -0,0 +1,14 @@
language: php
php:
- 5.5
- 5.4
- 5.3
# download test suite and run tests... submodule? meta testing project with
# all of the reference implementations?
script:
- git clone https://github.com/json-ld/json-ld.org.git spec
- phpunit test.php -d spec/test-suite
notifications:
email:
on_success: change
on_failure: change

114
README.md
View file

@ -1,29 +1,27 @@
php-json-ld
===========
Introduction Introduction
------------ ------------
This library is an implementation of the [JSON-LD][] specification in [PHP][]. JSON, as specified in RFC4627, is a simple language for representing
JSON, as specified in [RFC7159][], is a simple language for representing
objects on the Web. Linked Data is a way of describing content across objects on the Web. Linked Data is a way of describing content across
different documents or Web sites. Web resources are described using different documents or Web sites. Web resources are described using
IRIs, and typically are dereferencable entities that may be used to find IRIs, and typically are dereferencable entities that may be used to find
more information, creating a "Web of Knowledge". [JSON-LD][] is intended more information, creating a "Web of Knowledge". JSON-LD is intended to
to be a simple publishing method for expressing not only Linked Data in be a simple publishing method for expressing not only Linked Data in
JSON, but for adding semantics to existing JSON. JSON, but for adding semantics to existing JSON.
This library is an implementation of the [JSON-LD] specification
in [PHP].
JSON-LD is designed as a light-weight syntax that can be used to express JSON-LD is designed as a light-weight syntax that can be used to express
Linked Data. It is primarily intended to be a way to express Linked Data Linked Data. It is primarily intended to be a way to express Linked Data
in JavaScript and other Web-based programming environments. It is also in Javascript and other Web-based programming environments. It is also
useful when building interoperable Web Services and when storing Linked useful when building interoperable Web Services and when storing Linked
Data in JSON-based document storage engines. It is practical and Data in JSON-based document storage engines. It is practical and
designed to be as simple as possible, utilizing the large number of JSON designed to be as simple as possible, utilizing the large number of JSON
parsers and existing code that is in use today. It is designed to be parsers and existing code that is in use today. It is designed to be
able to express key-value pairs, RDF data, [RDFa][] data, able to express key-value pairs, RDF data, RDFa [RDFA-CORE] data,
[Microformats][] data, and [Microdata][]. That is, it supports every Microformats [MICROFORMATS] data, and Microdata [MICRODATA]. That is, it
major Web-based structured data model in use today. supports every major Web-based structured data model in use today.
The syntax does not require many applications to change their JSON, but The syntax does not require many applications to change their JSON, but
easily add meaning by adding context in a way that is either in-band or easily add meaning by adding context in a way that is either in-band or
@ -90,92 +88,40 @@ $flattened = jsonld_flatten($doc);
$framed = jsonld_frame($doc, $frame); $framed = jsonld_frame($doc, $frame);
// document transformed into a particular tree structure per the given frame // document transformed into a particular tree structure per the given frame
// normalize a document using the RDF Dataset Normalization Algorithm // normalize a document
// (URDNA2015), see: http://json-ld.github.io/normalization/spec/ $normalized = jsonld_normalize($doc, array('format' => 'application/nquads'));
$normalized = jsonld_normalize(
$doc, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
// normalized is a string that is a canonical representation of the document // normalized is a string that is a canonical representation of the document
// that can be used for hashing, comparison, etc. // that can be used for hashing
// force HTTPS-only context loading:
// use built-in secure document loader
jsonld_set_document_loader('jsonld_default_secure_document_loader');
// set a default custom document loader
jsonld_set_document_loader('my_custom_doc_loader');
// 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'));
// 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'));
``` ```
Commercial Support
------------------
Commercial support for this library is available upon request from
Digital Bazaar: support@digitalbazaar.com
Source Source
------ ------
The source code for the PHP implementation of the JSON-LD API is available at: The source code for the PHP implementation of the JSON-LD API
is available at:
https://git.friendi.ca/friendica/php-json-ld http://github.com/digitalbazaar/php-json-ld
Tests
-----
This library includes a sample testing utility which may be used to verify This library includes a sample testing utility which may be used to verify
that changes to the processor maintain the correct output. that changes to the processor maintain the correct output.
To run the sample tests you will need to get the test suite files by cloning To run the sample tests you will need to get the test suite files by cloning
the `json-ld.org` and `normalization` repositories hosted on GitHub: the [json-ld.org repository][json-ld.org] hosted on GitHub.
- https://github.com/json-ld/json-ld.org https://github.com/json-ld/json-ld.org
- https://github.com/json-ld/normalization
Then run the PHPUnit test.php application and point it at the directories Then run the PHPUnit test.php application and point it at the directory
containing the tests: containing the tests.
phpunit --group json-ld.org test.php -d {PATH_TO_JSON_LD_ORG/test-suite} phpunit test.php -d {PATH_TO_JSON_LD_ORG/test-suite}
phpunit --group normalization test.php -d {PATH_TO_NORMALIZATION/tests}
[JSON-LD]: http://json-ld.org/
[Microdata]: http://www.w3.org/TR/microdata/
[Microformats]: http://microformats.org/
[PHP]: http://php.net [PHP]: http://php.net
[RDFa]: http://www.w3.org/TR/rdfa-core/ [JSON-LD]: http://json-ld.org/
[RFC7159]: http://tools.ietf.org/html/rfc7159 [json-ld.org]: https://github.com/json-ld/json-ld.org

View file

@ -1,33 +0,0 @@
{
"name": "friendica/json-ld",
"type": "library",
"description": "A JSON-LD Processor and API implementation in PHP.",
"keywords": [
"JSON",
"Linked Data",
"JSON-LD",
"RDF",
"Semantic Web",
"jsonld"
],
"homepage": "https://git.friendi.ca/friendica/php-json-ld",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Digital Bazaar, Inc.",
"email": "support@digitalbazaar.com",
"homepage": "http://digitalbazaar.com/"
},
{
"name": "Friendica Team",
"homepage": "https://friendi.ca/"
}
],
"require": {
"php": ">=5.4.0",
"ext-json": "*"
},
"autoload": {
"files": ["jsonld.php"]
}
}

11079
jsonld.php
View file

@ -1,11 +1,9 @@
<?php <?php
/** /**
* PHP implementation of the JSON-LD API. * PHP implementation of the JSON-LD API.
* Version: 1.1.0 * Version: 0.3.7
* *
* @author Dave Longley * @author Dave Longley
* @author Michael Vogel
* *
* BSD 3-Clause License * BSD 3-Clause License
* Copyright (c) 2011-2014 Digital Bazaar, Inc. * Copyright (c) 2011-2014 Digital Bazaar, Inc.
@ -50,10 +48,9 @@
* *
* @return mixed the compacted JSON-LD output. * @return mixed the compacted JSON-LD output.
*/ */
function jsonld_compact($input, $ctx, $options = []) function jsonld_compact($input, $ctx, $options=array()) {
{ $p = new JsonLdProcessor();
$p = new JsonLdProcessor(); return $p->compact($input, $ctx, $options);
return $p->compact($input, $ctx, $options);
} }
/** /**
@ -66,10 +63,9 @@ function jsonld_compact($input, $ctx, $options = [])
* *
* @return array the expanded JSON-LD output. * @return array the expanded JSON-LD output.
*/ */
function jsonld_expand($input, $options = []) function jsonld_expand($input, $options=array()) {
{ $p = new JsonLdProcessor();
$p = new JsonLdProcessor(); return $p->expand($input, $options);
return $p->expand($input, $options);
} }
/** /**
@ -84,10 +80,9 @@ function jsonld_expand($input, $options = [])
* *
* @return mixed the flattened JSON-LD output. * @return mixed the flattened JSON-LD output.
*/ */
function jsonld_flatten($input, $ctx, $options = []) function jsonld_flatten($input, $ctx, $options=array()) {
{ $p = new JsonLdProcessor();
$p = new JsonLdProcessor(); return $p->flatten($input, $ctx, $options);
return $p->flatten($input, $ctx, $options);
} }
/** /**
@ -99,64 +94,32 @@ function jsonld_flatten($input, $ctx, $options = [])
* [base] the base IRI to use. * [base] the base IRI to use.
* [embed] default @embed flag (default: true). * [embed] default @embed flag (default: true).
* [explicit] default @explicit flag (default: false). * [explicit] default @explicit flag (default: false).
* [requireAll] default @requireAll flag (default: true).
* [omitDefault] default @omitDefault flag (default: false). * [omitDefault] default @omitDefault flag (default: false).
* [documentLoader(url)] the document loader. * [documentLoader(url)] the document loader.
* *
* @return stdClass the framed JSON-LD output. * @return stdClass the framed JSON-LD output.
*/ */
function jsonld_frame($input, $frame, $options = []) function jsonld_frame($input, $frame, $options=array()) {
{ $p = new JsonLdProcessor();
$p = new JsonLdProcessor(); return $p->frame($input, $frame, $options);
return $p->frame($input, $frame, $options);
} }
/** /**
* **Experimental** * Performs RDF dataset normalization on the given JSON-LD input. The output
* * is an RDF dataset unless the 'format' option is used.
* Links a JSON-LD document's nodes in memory.
*
* @param mixed $input the JSON-LD document to link.
* @param mixed $ctx the JSON-LD context to apply or null.
* @param assoc [$options] the options to use:
* [base] the base IRI to use.
* [expandContext] a context to expand with.
* [documentLoader(url)] the document loader.
*
* @return the linked JSON-LD output.
*/
function jsonld_link($input, $ctx, $options)
{
// API matches running frame with a wildcard frame and embed: '@link'
// get arguments
$frame = new stdClass();
if ($ctx) {
$frame->{'@context'} = $ctx;
}
$frame->{'@embed'} = '@link';
return jsonld_frame($input, $frame, $options);
}
/**
* Performs RDF dataset normalization on the given input. The input is
* JSON-LD unless the 'inputFormat' option is used. The output is an RDF
* dataset unless the 'format' option is used.
* *
* @param mixed $input the JSON-LD object to normalize. * @param mixed $input the JSON-LD object to normalize.
* @param assoc [$options] the options to use: * @param assoc [$options] the options to use:
* [base] the base IRI to use. * [base] the base IRI to use.
* [intputFormat] the format if input is not JSON-LD:
* 'application/nquads' for N-Quads.
* [format] the format if output is a string: * [format] the format if output is a string:
* 'application/nquads' for N-Quads. * 'application/nquads' for N-Quads.
* [documentLoader(url)] the document loader. * [documentLoader(url)] the document loader.
* *
* @return mixed the normalized output. * @return mixed the normalized output.
*/ */
function jsonld_normalize($input, $options = []) function jsonld_normalize($input, $options=array()) {
{ $p = new JsonLdProcessor();
$p = new JsonLdProcessor(); return $p->normalize($input, $options);
return $p->normalize($input, $options);
} }
/** /**
@ -174,10 +137,9 @@ function jsonld_normalize($input, $options = [])
* *
* @return array the JSON-LD output. * @return array the JSON-LD output.
*/ */
function jsonld_from_rdf($input, $options = []) function jsonld_from_rdf($input, $options=array()) {
{ $p = new JsonLdProcessor();
$p = new JsonLdProcessor(); return $p->fromRDF($input, $options);
return $p->fromRDF($input, $options);
} }
/** /**
@ -194,10 +156,9 @@ function jsonld_from_rdf($input, $options = [])
* *
* @return mixed the resulting RDF dataset (or a serialization of it). * @return mixed the resulting RDF dataset (or a serialization of it).
*/ */
function jsonld_to_rdf($input, $options = []) function jsonld_to_rdf($input, $options=array()) {
{ $p = new JsonLdProcessor();
$p = new JsonLdProcessor(); return $p->toRDF($input, $options);
return $p->toRDF($input, $options);
} }
/** /**
@ -211,14 +172,13 @@ function jsonld_to_rdf($input, $options = [])
* *
* @return the encoded JSON data. * @return the encoded JSON data.
*/ */
function jsonld_encode($input, $options = 0, $depth = 512) function jsonld_encode($input, $options=0, $depth=512) {
{ // newer PHP has a flag to avoid escaped '/'
// newer PHP has a flag to avoid escaped '/' if(defined('JSON_UNESCAPED_SLASHES')) {
if (defined('JSON_UNESCAPED_SLASHES')) { return json_encode($input, JSON_UNESCAPED_SLASHES | $options, $depth);
return json_encode($input, JSON_UNESCAPED_SLASHES | $options, $depth); }
} // use a simple string replacement of '\/' to '/'.
// use a simple string replacement of '\/' to '/'. return str_replace('\\/', '/', json_encode($input, $options, $depth));
return str_replace('\\/', '/', json_encode($input, $options, $depth));
} }
/** /**
@ -228,9 +188,8 @@ function jsonld_encode($input, $options = 0, $depth = 512)
* *
* @return mixed the resolved JSON-LD object, null on error. * @return mixed the resolved JSON-LD object, null on error.
*/ */
function jsonld_decode($input) function jsonld_decode($input) {
{ return json_decode($input);
return json_decode($input);
} }
/** /**
@ -253,37 +212,35 @@ function jsonld_decode($input)
* *
* @return assoc the parsed result. * @return assoc the parsed result.
*/ */
function jsonld_parse_link_header($header) function jsonld_parse_link_header($header) {
{ $rval = array();
$rval = []; // split on unbracketed/unquoted commas
// split on unbracketed/unquoted commas if(!preg_match_all(
if (!preg_match_all('/(?:<[^>]*?>|"[^"]*?"|[^,])+/', $header, $entries, PREG_SET_ORDER)) { '/(?:<[^>]*?>|"[^"]*?"|[^,])+/', $header, $entries, PREG_SET_ORDER)) {
return $rval; return $rval;
} }
$r_link_header = '/\s*<([^>]*?)>\s*(?:;\s*(.*))?/';
$r_link_header = '/\s*<([^>]*?)>\s*(?:;\s*(.*))?/'; foreach($entries as $entry) {
foreach ($entries as $entry) { if(!preg_match($r_link_header, $entry[0], $match)) {
if (!preg_match($r_link_header, $entry[0], $match)) { continue;
continue; }
} $result = (object)array('target' => $match[1]);
$result = (object) ['target' => $match[1]]; $params = $match[2];
$params = $match[2]; $r_params = '/(.*?)=(?:(?:"([^"]*?)")|([^"]*?))\s*(?:(?:;\s*)|$)/';
$r_params = '/(.*?)=(?:(?:"([^"]*?)")|([^"]*?))\s*(?:(?:;\s*)|$)/'; preg_match_all($r_params, $params, $matches, PREG_SET_ORDER);
preg_match_all($r_params, $params, $matches, PREG_SET_ORDER); foreach($matches as $match) {
foreach ($matches as $match) { $result->{$match[1]} = $match[2] ?: $match[3];
$result->{$match[1]} = $match[2] ?: $match[3]; }
} $rel = property_exists($result, 'rel') ? $result->rel : '';
if(!isset($rval[$rel])) {
$rel = property_exists($result, 'rel') ? $result->rel : ''; $rval[$rel] = $result;
if (!isset($rval[$rel])) { } else if(is_array($rval[$rel])) {
$rval[$rel] = $result; $rval[$rel][] = $result;
} else if (is_array($rval[$rel])) { } else {
$rval[$rel][] = $result; $rval[$rel] = array($rval[$rel], $result);
} else { }
$rval[$rel] = [$rval[$rel], $result]; }
} return $rval;
}
return $rval;
} }
/** /**
@ -291,10 +248,9 @@ function jsonld_parse_link_header($header)
* *
* @param mixed input the JSON-LD input. * @param mixed input the JSON-LD input.
*/ */
function jsonld_relabel_blank_nodes($input) function jsonld_relabel_blank_nodes($input) {
{ $p = new JsonLdProcessor();
$p = new JsonLdProcessor(); return $p->_labelBlankNodes(new UniqueNamer('_:b'), $input);
return $p->_labelBlankNodes(new UniqueNamer('_:b'), $input);
} }
/** JSON-LD shared in-memory cache. */ /** JSON-LD shared in-memory cache. */
@ -313,10 +269,9 @@ $jsonld_default_load_document = 'jsonld_default_document_loader';
* *
* @param callable load_document(url) the document loader. * @param callable load_document(url) the document loader.
*/ */
function jsonld_set_document_loader($load_document) function jsonld_set_document_loader($load_document) {
{ global $jsonld_default_load_document;
global $jsonld_default_load_document; $jsonld_default_load_document = $load_document;
$jsonld_default_load_document = $load_document;
} }
/** /**
@ -326,21 +281,19 @@ function jsonld_set_document_loader($load_document)
* *
* @return the JSON-LD. * @return the JSON-LD.
*/ */
function jsonld_get_url($url) function jsonld_get_url($url) {
{ global $jsonld_default_load_document;
global $jsonld_default_load_document; if($jsonld_default_load_document !== null) {
if ($jsonld_default_load_document !== null) { $document_loader = $jsonld_default_load_document;
$document_loader = $jsonld_default_load_document; } else {
} else { $document_loader = 'jsonld_default_document_loader';
$document_loader = 'jsonld_default_document_loader'; }
}
$remote_doc = call_user_func($document_loader, $url); $remote_doc = call_user_func($document_loader, $url);
if ($remote_doc) { if($remote_doc) {
return $remote_doc->document; return $remote_doc->document;
} }
return null;
return null;
} }
/** /**
@ -350,80 +303,76 @@ function jsonld_get_url($url)
* *
* @return stdClass the RemoteDocument object. * @return stdClass the RemoteDocument object.
*/ */
function jsonld_default_document_loader($url) function jsonld_default_document_loader($url) {
{ $doc = (object)array(
$doc = (object) [ 'contextUrl' => null, 'document' => null, 'documentUrl' => $url);
'contextUrl' => null, 'document' => null, 'documentUrl' => $url]; $redirects = array();
$redirects = [];
$opts = [ $opts = array(
'http' => [ 'http' => array(
'method' => 'GET', 'method' => 'GET',
'header' => 'header' =>
"Accept: application/ld+json\r\n"], "Accept: application/ld+json\r\n"),
/* Note: Use jsonld_default_secure_document_loader for security. */ /* Note: Use jsonld_default_secure_document_loader for security. */
'ssl' => [ 'ssl' => array(
'verify_peer' => false, 'verify_peer' => false,
'allow_self_signed' => true] 'allow_self_signed' => true)
]; );
$context = stream_context_create($opts); $context = stream_context_create($opts);
$content_type = null; $content_type = null;
stream_context_set_params($context, ['notification' => stream_context_set_params($context, array('notification' =>
function($notification_code, $severity, $message) use ( function($notification_code, $severity, $message) use (
&$redirects, &$content_type) { &$redirects, &$content_type) {
switch ($notification_code) { switch($notification_code) {
case STREAM_NOTIFY_REDIRECTED: case STREAM_NOTIFY_REDIRECTED:
$redirects[] = $message; $redirects[] = $message;
break; break;
case STREAM_NOTIFY_MIME_TYPE_IS: case STREAM_NOTIFY_MIME_TYPE_IS:
$content_type = $message; $content_type = $message;
break; break;
}; };
}]); }));
$result = @file_get_contents($url, false, $context); $result = @file_get_contents($url, false, $context);
if ($result === false) { if($result === false) {
throw new JsonLdException( throw new JsonLdException(
'Could not retrieve a JSON-LD document from the URL: ' . $url, 'jsonld.LoadDocumentError', 'loading document failed'); 'Could not retrieve a JSON-LD document from the URL: ' . $url,
} 'jsonld.LoadDocumentError', 'loading document failed');
}
$link_header = array();
foreach($http_response_header as $header) {
if(strpos($header, 'link') === 0) {
$value = explode(': ', $header);
if(count($value) > 1) {
$link_header[] = $value[1];
}
}
}
$link_header = jsonld_parse_link_header(join(',', $link_header));
if(isset($link_header['http://www.w3.org/ns/json-ld#context'])) {
$link_header = $link_header['http://www.w3.org/ns/json-ld#context'];
} else {
$link_header = null;
}
if($link_header && $content_type !== 'application/ld+json') {
// only 1 related link header permitted
if(is_array($link_header)) {
throw new JsonLdException(
'URL could not be dereferenced, it has more than one ' .
'associated HTTP Link Header.', 'jsonld.LoadDocumentError',
'multiple context link headers', array('url' => $url));
}
$doc->{'contextUrl'} = $link_header->target;
}
$link_header = []; // update document url based on redirects
foreach ($http_response_header as $header) { $redirs = count($redirects);
if (strpos($header, 'link') === 0) { if($redirs > 0) {
$value = explode(': ', $header); $url = $redirects[$redirs - 1];
if (count($value) > 1) { }
$link_header[] = $value[1]; $doc->document = $result;
} $doc->documentUrl = $url;
} return $doc;
}
$link_header = jsonld_parse_link_header(join(',', $link_header));
if (isset($link_header['http://www.w3.org/ns/json-ld#context'])) {
$link_header = $link_header['http://www.w3.org/ns/json-ld#context'];
} else {
$link_header = null;
}
if ($link_header && $content_type !== 'application/ld+json') {
// only 1 related link header permitted
if (is_array($link_header)) {
throw new JsonLdException(
'URL could not be dereferenced, it has more than one ' .
'associated HTTP Link Header.', 'jsonld.LoadDocumentError', 'multiple context link headers', ['url' => $url]);
}
$doc->{'contextUrl'} = $link_header->target;
}
// update document url based on redirects
$redirs = count($redirects);
if ($redirs > 0) {
$url = $redirects[$redirs - 1];
}
$doc->document = $result;
$doc->documentUrl = $url;
return $doc;
} }
/** /**
@ -433,92 +382,85 @@ function jsonld_default_document_loader($url)
* *
* @return stdClass the RemoteDocument object. * @return stdClass the RemoteDocument object.
*/ */
function jsonld_default_secure_document_loader($url) function jsonld_default_secure_document_loader($url) {
{ if(strpos($url, 'https') !== 0) {
if (strpos($url, 'https') !== 0) { throw new JsonLdException(
throw new JsonLdException( "Could not GET url: '$url'; 'https' is required.",
"Could not GET url: '$url'; 'https' is required.", 'jsonld.LoadDocumentError', 'loading document failed');
'jsonld.LoadDocumentError', }
'loading document failed'
);
}
$doc = (object) [ $doc = (object)array(
'contextUrl' => null, 'document' => null, 'documentUrl' => $url]; 'contextUrl' => null, 'document' => null, 'documentUrl' => $url);
$redirects = []; $redirects = array();
// default JSON-LD https GET implementation // default JSON-LD https GET implementation
$opts = [ $opts = array(
'http' => [ 'http' => array(
'method' => 'GET', 'method' => 'GET',
'header' => 'header' =>
"Accept: application/ld+json\r\n"], "Accept: application/ld+json\r\n"),
'ssl' => [ 'ssl' => array(
'verify_peer' => true, 'verify_peer' => true,
'allow_self_signed' => false, 'allow_self_signed' => false,
'cafile' => '/etc/ssl/certs/ca-certificates.crt']]; 'cafile' => '/etc/ssl/certs/ca-certificates.crt'));
$context = stream_context_create($opts); $context = stream_context_create($opts);
$content_type = null; $content_type = null;
stream_context_set_params($context, ['notification' => stream_context_set_params($context, array('notification' =>
function($notification_code, $severity, $message) use ( function($notification_code, $severity, $message) use (
&$redirects, &$content_type) { &$redirects, &$content_type) {
switch ($notification_code) { switch($notification_code) {
case STREAM_NOTIFY_REDIRECTED: case STREAM_NOTIFY_REDIRECTED:
$redirects[] = $message; $redirects[] = $message;
break; break;
case STREAM_NOTIFY_MIME_TYPE_IS: case STREAM_NOTIFY_MIME_TYPE_IS:
$content_type = $message; $content_type = $message;
break; break;
}; };
}]); }));
$result = @file_get_contents($url, false, $context); $result = @file_get_contents($url, false, $context);
if ($result === false) { if($result === false) {
throw new JsonLdException( throw new JsonLdException(
'Could not retrieve a JSON-LD document from the URL: ' + $url, 'jsonld.LoadDocumentError', 'loading document failed'); 'Could not retrieve a JSON-LD document from the URL: ' + $url,
} 'jsonld.LoadDocumentError', 'loading document failed');
}
$link_header = array();
foreach($http_response_header as $header) {
if(strpos($header, 'link') === 0) {
$value = explode(': ', $header);
if(count($value) > 1) {
$link_header[] = $value[1];
}
}
}
$link_header = jsonld_parse_link_header(join(',', $link_header));
if(isset($link_header['http://www.w3.org/ns/json-ld#context'])) {
$link_header = $link_header['http://www.w3.org/ns/json-ld#context'];
} else {
$link_header = null;
}
if($link_header && $content_type !== 'application/ld+json') {
// only 1 related link header permitted
if(is_array($link_header)) {
throw new JsonLdException(
'URL could not be dereferenced, it has more than one ' .
'associated HTTP Link Header.', 'jsonld.LoadDocumentError',
'multiple context link headers', array('url' => $url));
}
$doc->{'contextUrl'} = $link_header->target;
}
$link_header = []; // update document url based on redirects
foreach ($http_response_header as $header) { foreach($redirects as $redirect) {
if (strpos($header, 'link') === 0) { if(strpos($redirect, 'https') !== 0) {
$value = explode(': ', $header); throw new JsonLdException(
if (count($value) > 1) { "Could not GET redirected url: '$redirect'; 'https' is required.",
$link_header[] = $value[1]; 'jsonld.LoadDocumentError', 'loading document failed');
} }
} $url = $redirect;
} }
$doc->document = $result;
$link_header = jsonld_parse_link_header(join(',', $link_header)); $doc->documentUrl = $url;
if (isset($link_header['http://www.w3.org/ns/json-ld#context'])) { return $doc;
$link_header = $link_header['http://www.w3.org/ns/json-ld#context'];
} else {
$link_header = null;
}
if ($link_header && $content_type !== 'application/ld+json') {
// only 1 related link header permitted
if (is_array($link_header)) {
throw new JsonLdException(
'URL could not be dereferenced, it has more than one ' .
'associated HTTP Link Header.', 'jsonld.LoadDocumentError', 'multiple context link headers', ['url' => $url]);
}
$doc->{'contextUrl'} = $link_header->target;
}
// update document url based on redirects
foreach ($redirects as $redirect) {
if (strpos($redirect, 'https') !== 0) {
throw new JsonLdException(
"Could not GET redirected url: '$redirect'; 'https' is required.",
'jsonld.LoadDocumentError',
'loading document failed'
);
}
$url = $redirect;
}
$doc->document = $result;
$doc->documentUrl = $url;
return $doc;
} }
/** Registered global RDF dataset parsers hashed by content-type. */ /** Registered global RDF dataset parsers hashed by content-type. */
@ -534,10 +476,9 @@ $jsonld_rdf_parsers = new stdClass();
* @param callable $parser(input) the parser function (takes a string as * @param callable $parser(input) the parser function (takes a string as
* a parameter and returns an RDF dataset). * a parameter and returns an RDF dataset).
*/ */
function jsonld_register_rdf_parser($content_type, $parser) function jsonld_register_rdf_parser($content_type, $parser) {
{ global $jsonld_rdf_parsers;
global $jsonld_rdf_parsers; $jsonld_rdf_parsers->{$content_type} = $parser;
$jsonld_rdf_parsers->{$content_type} = $parser;
} }
/** /**
@ -545,12 +486,11 @@ function jsonld_register_rdf_parser($content_type, $parser)
* *
* @param string $content_type the content-type for the parser. * @param string $content_type the content-type for the parser.
*/ */
function jsonld_unregister_rdf_parser($content_type) function jsonld_unregister_rdf_parser($content_type) {
{ global $jsonld_rdf_parsers;
global $jsonld_rdf_parsers; if(property_exists($jsonld_rdf_parsers, $content_type)) {
if (property_exists($jsonld_rdf_parsers, $content_type)) { unset($jsonld_rdf_parsers->{$content_type});
unset($jsonld_rdf_parsers->{$content_type}); }
}
} }
/** /**
@ -560,53 +500,66 @@ function jsonld_unregister_rdf_parser($content_type)
* *
* @return assoc the parsed URL. * @return assoc the parsed URL.
*/ */
function jsonld_parse_url($url) function jsonld_parse_url($url) {
{ if($url === null) {
if ($url === null) { $url = '';
$url = ''; }
}
$keys = [ $rval = parse_url($url);
'href', 'protocol', 'scheme', '?authority', 'authority',
'?auth', 'auth', 'user', 'pass', 'host', '?port', 'port', 'path',
'?query', 'query', '?fragment', 'fragment'];
$regex = "/^(([^:\/?#]+):)?(\/\/(((([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(:(\d*))?))?([^?#]*)(\?([^#]*))?(#(.*))?/";
preg_match($regex, $url, $match);
$rval = []; // malformed url
$flags = []; if($rval === false) {
$len = count($keys); $rval = array();
for ($i = 0; $i < $len; ++$i) { }
$key = $keys[$i];
if (strpos($key, '?') === 0) {
$flags[substr($key, 1)] = !empty($match[$i]);
} else if (!isset($match[$i])) {
$rval[$key] = null;
} else {
$rval[$key] = $match[$i];
}
}
if (!$flags['authority']) { $rval['href'] = $url;
$rval['authority'] = null; if(!isset($rval['scheme'])) {
} $rval['scheme'] = '';
if (!$flags['auth']) { $rval['protocol'] = '';
$rval['auth'] = $rval['user'] = $rval['pass'] = null; } else {
} $rval['protocol'] = $rval['scheme'] . ':';
if (!$flags['port']) { }
$rval['port'] = null; if(!isset($rval['host'])) {
} $rval['host'] = '';
if (!$flags['query']) { }
$rval['query'] = null; if(!isset($rval['path'])) {
} $rval['path'] = '';
if (!$flags['fragment']) { }
$rval['fragment'] = null; if(isset($rval['user']) || isset($rval['pass'])) {
} $rval['auth'] = '';
if(isset($rval['user'])) {
$rval['auth'] = $rval['user'];
}
if(isset($rval['pass'])) {
$rval['auth'] .= ":{$rval['pass']}";
}
}
// parse authority for unparsed relative network-path reference
if(strpos($rval['href'], ':') === false &&
strpos($rval['href'], '//') === 0 && $rval['host'] === '') {
// must parse authority from pathname
$rval['path'] = substr($rval['path'], 2);
$idx = strpos($rval['path'], '/');
if($idx === false) {
$rval['authority'] = $rval['path'];
$rval['path'] = '';
} else {
$rval['authority'] = substr($rval['path'], 0, $idx);
$rval['path'] = substr($rval['path'], $idx);
}
} else {
$rval['authority'] = $rval['host'];
if(isset($rval['port'])) {
$rval['authority'] .= ":{$rval['port']}";
}
if(isset($rval['auth'])) {
$rval['authority'] = "{$rval['auth']}@{$rval['authority']}";
}
}
$rval['normalizedPath'] = jsonld_remove_dot_segments(
$rval['path'], $rval['authority'] !== '');
$rval['normalizedPath'] = jsonld_remove_dot_segments( return $rval;
$rval['path'], !!$rval['authority']);
return $rval;
} }
/** /**
@ -615,37 +568,36 @@ function jsonld_parse_url($url)
* @param string $path the path to remove dot segments from. * @param string $path the path to remove dot segments from.
* @param bool $has_authority true if the URL has an authority, false if not. * @param bool $has_authority true if the URL has an authority, false if not.
*/ */
function jsonld_remove_dot_segments($path, $has_authority) function jsonld_remove_dot_segments($path, $has_authority) {
{ $rval = '';
$rval = '';
if (strpos($path, '/') === 0) { if(strpos($path, '/') === 0) {
$rval = '/'; $rval = '/';
} }
// RFC 3986 5.2.4 (reworked) // RFC 3986 5.2.4 (reworked)
$input = explode('/', $path); $input = explode('/', $path);
$output = []; $output = array();
while (count($input) > 0) { while(count($input) > 0) {
if ($input[0] === '.' || ($input[0] === '' && count($input) > 1)) { if($input[0] === '.' || ($input[0] === '' && count($input) > 1)) {
array_shift($input); array_shift($input);
continue; continue;
} }
if ($input[0] === '..') { if($input[0] === '..') {
array_shift($input); array_shift($input);
if ($has_authority || if($has_authority ||
(count($output) > 0 && $output[count($output) - 1] !== '..')) { (count($output) > 0 && $output[count($output) - 1] !== '..')) {
array_pop($output); array_pop($output);
} else { } else {
// leading relative URL '..' // leading relative URL '..'
$output[] = '..'; $output[] = '..';
} }
continue; continue;
} }
$output[] = array_shift($input); $output[] = array_shift($input);
} }
return $rval . implode('/', $output); return $rval . implode('/', $output);
} }
/** /**
@ -656,91 +608,71 @@ function jsonld_remove_dot_segments($path, $has_authority)
* *
* @return string the absolute IRI. * @return string the absolute IRI.
*/ */
function jsonld_prepend_base($base, $iri) function jsonld_prepend_base($base, $iri) {
{ // skip IRI processing
// skip IRI processing if($base === null) {
if ($base === null) { return $iri;
return $iri; }
}
// already an absolute IRI // already an absolute IRI
if (strpos($iri, ':') !== false) { if(strpos($iri, ':') !== false) {
return $iri; return $iri;
} }
// parse base if it is a string // parse base if it is a string
if (is_string($base)) { if(is_string($base)) {
$base = jsonld_parse_url($base); $base = jsonld_parse_url($base);
} }
// parse given IRI // parse given IRI
$rel = jsonld_parse_url($iri); $rel = jsonld_parse_url($iri);
// per RFC3986 5.2.2 // start hierarchical part
$transform = ['protocol' => $base['protocol']]; $hierPart = $base['protocol'];
if($rel['authority']) {
$hierPart .= "//{$rel['authority']}";
} else if($base['href'] !== '') {
$hierPart .= "//{$base['authority']}";
}
if ($rel['authority'] !== null) { // per RFC3986 normalize
$transform['authority'] = $rel['authority'];
$transform['path'] = $rel['path'];
$transform['query'] = $rel['query'];
} else {
$transform['authority'] = $base['authority'];
if ($rel['path'] === '') { // IRI represents an absolute path
$transform['path'] = $base['path']; if(strpos($rel['path'], '/') === 0) {
if ($rel['query'] !== null) { $path = $rel['path'];
$transform['query'] = $rel['query']; } else {
} else { $path = $base['path'];
$transform['query'] = $base['query'];
}
} else {
if (strpos($rel['path'], '/') === 0) {
// IRI represents an absolute path
$transform['path'] = $rel['path'];
} else {
// merge paths
$path = $base['path'];
// append relative path to the end of the last directory from base // append relative path to the end of the last directory from base
if ($rel['path'] !== '') { if($rel['path'] !== '') {
$idx = strrpos($path, '/'); $idx = strrpos($path, '/');
$idx = ($idx === false) ? 0 : $idx + 1; $idx = ($idx === false) ? 0 : $idx + 1;
$path = substr($path, 0, $idx); $path = substr($path, 0, $idx);
if (strlen($path) > 0 && substr($path, -1) !== '/') { if(strlen($path) > 0 && substr($path, -1) !== '/') {
$path .= '/'; $path .= '/';
} }
$path .= $rel['path']; $path .= $rel['path'];
} }
}
$transform['path'] = $path; // remove slashes and dots in path
} $path = jsonld_remove_dot_segments($path, $hierPart !== '');
$transform['query'] = $rel['query'];
}
}
// remove slashes and dots in path // add query and hash
$transform['path'] = jsonld_remove_dot_segments( if(isset($rel['query'])) {
$transform['path'], !!$transform['authority']); $path .= "?{$rel['query']}";
}
if(isset($rel['fragment'])) {
$path .= "#{$rel['fragment']}";
}
// construct URL $rval = $hierPart . $path;
$rval = $transform['protocol'];
if ($transform['authority'] !== null) {
$rval .= '//' . $transform['authority'];
}
$rval .= $transform['path'];
if ($transform['query'] !== null) {
$rval .= '?' . $transform['query'];
}
if ($rel['fragment'] !== null) {
$rval .= '#' . $rel['fragment'];
}
// handle empty base if($rval === '') {
if ($rval === '') { $rval = './';
$rval = './'; }
}
return $rval; return $rval;
} }
/** /**
@ -752,5286 +684,5113 @@ function jsonld_prepend_base($base, $iri)
* @return string the relative IRI if relative to base, otherwise the absolute * @return string the relative IRI if relative to base, otherwise the absolute
* IRI. * IRI.
*/ */
function jsonld_remove_base($base, $iri) function jsonld_remove_base($base, $iri) {
{ // skip IRI processing
// skip IRI processing if($base === null) {
if ($base === null) { return $iri;
return $iri; }
}
if (is_string($base)) { if(is_string($base)) {
$base = jsonld_parse_url($base); $base = jsonld_parse_url($base);
} }
// establish base root // establish base root
$root = ''; $root = '';
if ($base['href'] !== '') { if($base['href'] !== '') {
$root .= "{$base['protocol']}//{$base['authority']}"; $root .= "{$base['protocol']}//{$base['authority']}";
} else if (strpos($iri, '//') === false) { } else if(strpos($iri, '//') === false) {
// support network-path reference with empty base // support network-path reference with empty base
$root .= '//'; $root .= '//';
} }
// IRI not relative to base // IRI not relative to base
if ($root === '' || strpos($iri, $root) !== 0) { if($root === '' || strpos($iri, $root) !== 0) {
return $iri; return $iri;
} }
// remove root from IRI // remove root from IRI
$rel = jsonld_parse_url(substr($iri, strlen($root))); $rel = jsonld_parse_url(substr($iri, strlen($root)));
// remove path segments that match (do not remove last segment unless there // remove path segments that match (do not remove last segment unless there
// is a hash or query) // is a hash or query)
$base_segments = explode('/', $base['normalizedPath']); $base_segments = explode('/', $base['normalizedPath']);
$iri_segments = explode('/', $rel['normalizedPath']); $iri_segments = explode('/', $rel['normalizedPath']);
$last = ($rel['query'] || $rel['fragment']) ? 0 : 1; $last = (isset($rel['query']) || isset($rel['fragment'])) ? 0 : 1;
while (count($base_segments) > 0 && count($iri_segments) > $last) { while(count($base_segments) > 0 && count($iri_segments) > $last) {
if ($base_segments[0] !== $iri_segments[0]) { if($base_segments[0] !== $iri_segments[0]) {
break; break;
} }
array_shift($base_segments); array_shift($base_segments);
array_shift($iri_segments); array_shift($iri_segments);
} }
// use '../' for each non-matching base segment // use '../' for each non-matching base segment
$rval = ''; $rval = '';
if (count($base_segments) > 0) { if(count($base_segments) > 0) {
// don't count the last segment (if it ends with '/' last path doesn't // don't count the last segment (if it ends with '/' last path doesn't
// count and if it doesn't end with '/' it isn't a path) // count and if it doesn't end with '/' it isn't a path)
array_pop($base_segments); array_pop($base_segments);
foreach ($base_segments as $segment) { foreach($base_segments as $segment) {
$rval .= '../'; $rval .= '../';
} }
} }
// prepend remaining segments // prepend remaining segments
$rval .= implode('/', $iri_segments); $rval .= implode('/', $iri_segments);
// add query and hash // add query and hash
if ($rel['query'] !== null) { if(isset($rel['query'])) {
$rval .= "?{$rel['query']}"; $rval .= "?{$rel['query']}";
} }
if ($rel['fragment'] !== null) { if(isset($rel['fragment'])) {
$rval .= "#{$rel['fragment']}"; $rval .= "#{$rel['fragment']}";
} }
if ($rval === '') { if($rval === '') {
$rval = './'; $rval = './';
} }
return $rval; return $rval;
} }
/** /**
* A JSON-LD processor. * A JSON-LD processor.
*/ */
class JsonLdProcessor class JsonLdProcessor {
{ /** XSD constants */
const XSD_BOOLEAN = 'http://www.w3.org/2001/XMLSchema#boolean';
/** XSD constants */ const XSD_DOUBLE = 'http://www.w3.org/2001/XMLSchema#double';
const XSD_BOOLEAN = 'http://www.w3.org/2001/XMLSchema#boolean'; const XSD_INTEGER = 'http://www.w3.org/2001/XMLSchema#integer';
const XSD_DOUBLE = 'http://www.w3.org/2001/XMLSchema#double'; const XSD_STRING = 'http://www.w3.org/2001/XMLSchema#string';
const XSD_INTEGER = 'http://www.w3.org/2001/XMLSchema#integer';
const XSD_STRING = 'http://www.w3.org/2001/XMLSchema#string'; /** RDF constants */
const RDF_LIST = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#List';
/** RDF constants */ const RDF_FIRST = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first';
const RDF_LIST = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#List'; const RDF_REST = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest';
const RDF_FIRST = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first'; const RDF_NIL = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil';
const RDF_REST = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest'; const RDF_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';
const RDF_NIL = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'; const RDF_LANGSTRING =
const RDF_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'; 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString';
const RDF_LANGSTRING = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString';
/** Restraints */
/** Restraints */ const MAX_CONTEXT_URLS = 10;
const MAX_CONTEXT_URLS = 10;
/** Processor-specific RDF dataset parsers. */
/** Processor-specific RDF dataset parsers. */ protected $rdfParsers = null;
protected $rdfParsers = null;
/**
/** * Constructs a JSON-LD processor.
* Constructs a JSON-LD processor. */
*/ public function __construct() {}
public function __construct()
{ /**
* Performs JSON-LD compaction.
} *
* @param mixed $input the JSON-LD object to compact.
/** * @param mixed $ctx the context to compact with.
* Performs JSON-LD compaction. * @param assoc $options the compaction options.
* * [base] the base IRI to use.
* @param mixed $input the JSON-LD object to compact. * [compactArrays] true to compact arrays to single values when
* @param mixed $ctx the context to compact with. * appropriate, false not to (default: true).
* @param assoc $options the compaction options. * [graph] true to always output a top-level graph (default: false).
* [base] the base IRI to use. * [skipExpansion] true to assume the input is expanded and skip
* [compactArrays] true to compact arrays to single values when * expansion, false not to, defaults to false.
* appropriate, false not to (default: true). * [activeCtx] true to also return the active context used.
* [graph] true to always output a top-level graph (default: false). * [documentLoader(url)] the document loader.
* [skipExpansion] true to assume the input is expanded and skip *
* expansion, false not to, defaults to false. * @return mixed the compacted JSON-LD output.
* [activeCtx] true to also return the active context used. */
* [documentLoader(url)] the document loader. public function compact($input, $ctx, $options) {
* global $jsonld_default_load_document;
* @return mixed the compacted JSON-LD output.
*/ if($ctx === null) {
public function compact($input, $ctx, $options) throw new JsonLdException(
{ 'The compaction context must not be null.',
global $jsonld_default_load_document; 'jsonld.CompactError', 'invalid local context');
}
if ($ctx === null) {
throw new JsonLdException( // nothing to compact
'The compaction context must not be null.', 'jsonld.CompactError', 'invalid local context'); if($input === null) {
} return null;
}
// nothing to compact
if ($input === null) { self::setdefaults($options, array(
return null; 'base' => is_string($input) ? $input : '',
} 'compactArrays' => true,
'graph' => false,
self::setdefaults($options, [ 'skipExpansion' => false,
'base' => is_string($input) ? $input : '', 'activeCtx' => false,
'compactArrays' => true, 'documentLoader' => $jsonld_default_load_document));
'graph' => false,
'skipExpansion' => false, if($options['skipExpansion'] === true) {
'activeCtx' => false, $expanded = $input;
'documentLoader' => $jsonld_default_load_document, } else {
'link' => false]); // expand input
if ($options['link']) { try {
// force skip expansion when linking, "link" is not part of the $expanded = $this->expand($input, $options);
// public API, it should only be called from framing } catch(JsonLdException $e) {
$options['skipExpansion'] = true; throw new JsonLdException(
} 'Could not expand input before compaction.',
'jsonld.CompactError', null, null, $e);
if ($options['skipExpansion'] === true) { }
$expanded = $input; }
} else {
// expand input // process context
try { $active_ctx = $this->_getInitialContext($options);
$expanded = $this->expand($input, $options); try {
} catch (JsonLdException $e) { $active_ctx = $this->processContext($active_ctx, $ctx, $options);
throw new JsonLdException( } catch(JsonLdException $e) {
'Could not expand input before compaction.', 'jsonld.CompactError', null, null, $e); throw new JsonLdException(
} 'Could not process context before compaction.',
} 'jsonld.CompactError', null, null, $e);
}
// process context
$active_ctx = $this->_getInitialContext($options); // do compaction
try { $compacted = $this->_compact($active_ctx, null, $expanded, $options);
$active_ctx = $this->processContext($active_ctx, $ctx, $options);
} catch (JsonLdException $e) { if($options['compactArrays'] &&
throw new JsonLdException( !$options['graph'] && is_array($compacted)) {
'Could not process context before compaction.', 'jsonld.CompactError', null, null, $e); if(count($compacted) === 1) {
} // simplify to a single item
$compacted = $compacted[0];
// do compaction } else if(count($compacted) === 0) {
$compacted = $this->_compact($active_ctx, null, $expanded, $options); // simplify to an empty object
$compacted = new stdClass();
if ($options['compactArrays'] && }
!$options['graph'] && is_array($compacted)) { } else if($options['graph']) {
if (count($compacted) === 1) { // always use array if graph option is on
// simplify to a single item $compacted = self::arrayify($compacted);
$compacted = $compacted[0]; }
} else if (count($compacted) === 0) {
// simplify to an empty object // follow @context key
$compacted = new stdClass(); if(is_object($ctx) && property_exists($ctx, '@context')) {
} $ctx = $ctx->{'@context'};
} else if ($options['graph']) { }
// always use array if graph option is on
$compacted = self::arrayify($compacted); // build output context
} $ctx = self::copy($ctx);
$ctx = self::arrayify($ctx);
// follow @context key
if (is_object($ctx) && property_exists($ctx, '@context')) { // remove empty contexts
$ctx = $ctx->{'@context'}; $tmp = $ctx;
} $ctx = array();
foreach($tmp as $v) {
// build output context if(!is_object($v) || count(array_keys((array)$v)) > 0) {
$ctx = self::copy($ctx); $ctx[] = $v;
$ctx = self::arrayify($ctx); }
}
// remove empty contexts
$tmp = $ctx; // remove array if only one context
$ctx = []; $ctx_length = count($ctx);
foreach ($tmp as $v) { $has_context = ($ctx_length > 0);
if (!is_object($v) || count(array_keys((array) $v)) > 0) { if($ctx_length === 1) {
$ctx[] = $v; $ctx = $ctx[0];
} }
}
// add context and/or @graph
// remove array if only one context if(is_array($compacted)) {
$ctx_length = count($ctx); // use '@graph' keyword
$has_context = ($ctx_length > 0); $kwgraph = $this->_compactIri($active_ctx, '@graph');
if ($ctx_length === 1) { $graph = $compacted;
$ctx = $ctx[0]; $compacted = new stdClass();
} if($has_context) {
$compacted->{'@context'} = $ctx;
// add context and/or @graph }
if (is_array($compacted)) { $compacted->{$kwgraph} = $graph;
// use '@graph' keyword } else if(is_object($compacted) && $has_context) {
$kwgraph = $this->_compactIri($active_ctx, '@graph'); // reorder keys so @context is first
$graph = $compacted; $graph = $compacted;
$compacted = new stdClass(); $compacted = new stdClass();
if ($has_context) { $compacted->{'@context'} = $ctx;
$compacted->{'@context'} = $ctx; foreach($graph as $k => $v) {
} $compacted->{$k} = $v;
$compacted->{$kwgraph} = $graph; }
} else if (is_object($compacted) && $has_context) { }
// reorder keys so @context is first
$graph = $compacted; if($options['activeCtx']) {
$compacted = new stdClass(); return array('compacted' => $compacted, 'activeCtx' => $active_ctx);
$compacted->{'@context'} = $ctx; }
foreach ($graph as $k => $v) {
$compacted->{$k} = $v; return $compacted;
} }
}
/**
if ($options['activeCtx']) { * Performs JSON-LD expansion.
return ['compacted' => $compacted, 'activeCtx' => $active_ctx]; *
} * @param mixed $input the JSON-LD object to expand.
* @param assoc $options the options to use:
return $compacted; * [base] the base IRI to use.
} * [expandContext] a context to expand with.
* [keepFreeFloatingNodes] true to keep free-floating nodes,
/** * false not to, defaults to false.
* Performs JSON-LD expansion. * [documentLoader(url)] the document loader.
* *
* @param mixed $input the JSON-LD object to expand. * @return array the expanded JSON-LD output.
* @param assoc $options the options to use: */
* [base] the base IRI to use. public function expand($input, $options) {
* [expandContext] a context to expand with. global $jsonld_default_load_document;
* [keepFreeFloatingNodes] true to keep free-floating nodes, self::setdefaults($options, array(
* false not to, defaults to false. 'keepFreeFloatingNodes' => false,
* [documentLoader(url)] the document loader. 'documentLoader' => $jsonld_default_load_document));
*
* @return array the expanded JSON-LD output. // if input is a string, attempt to dereference remote document
*/ if(is_string($input)) {
public function expand($input, $options) $remote_doc = call_user_func($options['documentLoader'], $input);
{ } else {
global $jsonld_default_load_document; $remote_doc = (object)array(
self::setdefaults($options, [ 'contextUrl' => null,
'keepFreeFloatingNodes' => false, 'documentUrl' => null,
'documentLoader' => $jsonld_default_load_document]); 'document' => $input);
}
// if input is a string, attempt to dereference remote document
if (is_string($input)) { try {
$remote_doc = call_user_func($options['documentLoader'], $input); if($remote_doc->document === null) {
} else { throw new JsonLdException(
$remote_doc = (object) [ 'No remote document found at the given URL.',
'contextUrl' => null, 'jsonld.NullRemoteDocument');
'documentUrl' => null, }
'document' => $input]; if(is_string($remote_doc->document)) {
} $remote_doc->document = self::_parse_json($remote_doc->document);
}
try { } catch(Exception $e) {
if ($remote_doc->document === null) { throw new JsonLdException(
throw new JsonLdException( 'Could not retrieve a JSON-LD document from the URL.',
'No remote document found at the given URL.', 'jsonld.NullRemoteDocument'); 'jsonld.LoadDocumentError', 'loading document failed',
} array('remoteDoc' => $remote_doc), $e);
if (is_string($remote_doc->document)) { }
$remote_doc->document = self::_parse_json($remote_doc->document);
} // set default base
} catch (Exception $e) { self::setdefault($options, 'base', $remote_doc->documentUrl ?: '');
throw new JsonLdException(
'Could not retrieve a JSON-LD document from the URL.', 'jsonld.LoadDocumentError', 'loading document failed', ['remoteDoc' => $remote_doc], $e); // build meta-object and retrieve all @context urls
} $input = (object)array(
'document' => self::copy($remote_doc->document),
// set default base 'remoteContext' => (object)array(
self::setdefault($options, 'base', $remote_doc->documentUrl ?: ''); '@context' => $remote_doc->contextUrl));
if(isset($options['expandContext'])) {
// build meta-object and retrieve all @context urls $expand_context = self::copy($options['expandContext']);
$input = (object) [ if(is_object($expand_context) &&
'document' => self::copy($remote_doc->document), property_exists($expand_context, '@context')) {
'remoteContext' => (object) [ $input->expandContext = $expand_context;
'@context' => $remote_doc->contextUrl]]; } else {
if (isset($options['expandContext'])) { $input->expandContext = (object)array('@context' => $expand_context);
$expand_context = self::copy($options['expandContext']); }
if (is_object($expand_context) && }
property_exists($expand_context, '@context')) {
$input->expandContext = $expand_context; // retrieve all @context URLs in the input
} else { try {
$input->expandContext = (object) ['@context' => $expand_context]; $this->_retrieveContextUrls(
} $input, new stdClass(), $options['documentLoader'], $options['base']);
} } catch(Exception $e) {
throw new JsonLdException(
// retrieve all @context URLs in the input 'Could not perform JSON-LD expansion.',
try { 'jsonld.ExpandError', null, null, $e);
$this->_retrieveContextUrls( }
$input, new stdClass(), $options['documentLoader'], $options['base']);
} catch (Exception $e) { $active_ctx = $this->_getInitialContext($options);
throw new JsonLdException( $document = $input->document;
'Could not perform JSON-LD expansion.', 'jsonld.ExpandError', null, null, $e); $remote_context = $input->remoteContext->{'@context'};
}
// process optional expandContext
$active_ctx = $this->_getInitialContext($options); if(property_exists($input, 'expandContext')) {
$document = $input->document; $active_ctx = self::_processContext(
$remote_context = $input->remoteContext->{'@context'}; $active_ctx, $input->expandContext, $options);
}
// process optional expandContext
if (property_exists($input, 'expandContext')) { // process remote context from HTTP Link Header
$active_ctx = self::_processContext( if($remote_context) {
$active_ctx, $input->expandContext, $options); $active_ctx = self::_processContext(
} $active_ctx, $remote_context, $options);
}
// process remote context from HTTP Link Header
if ($remote_context) { // do expansion
$active_ctx = self::_processContext( $expanded = $this->_expand($active_ctx, null, $document, $options, false);
$active_ctx, $remote_context, $options);
} // optimize away @graph with no other properties
if(is_object($expanded) && property_exists($expanded, '@graph') &&
// do expansion count(array_keys((array)$expanded)) === 1) {
$expanded = $this->_expand($active_ctx, null, $document, $options, false); $expanded = $expanded->{'@graph'};
} else if($expanded === null) {
// optimize away @graph with no other properties $expanded = array();
if (is_object($expanded) && property_exists($expanded, '@graph') && }
count(array_keys((array) $expanded)) === 1) { // normalize to an array
$expanded = $expanded->{'@graph'}; return self::arrayify($expanded);
} else if ($expanded === null) { }
$expanded = [];
} /**
// normalize to an array * Performs JSON-LD flattening.
return self::arrayify($expanded); *
} * @param mixed $input the JSON-LD to flatten.
* @param ctx the context to use to compact the flattened output, or null.
/** * @param assoc $options the options to use:
* Performs JSON-LD flattening. * [base] the base IRI to use.
* * [expandContext] a context to expand with.
* @param mixed $input the JSON-LD to flatten. * [documentLoader(url)] the document loader.
* @param ctx the context to use to compact the flattened output, or null. *
* @param assoc $options the options to use: * @return array the flattened output.
* [base] the base IRI to use. */
* [expandContext] a context to expand with. public function flatten($input, $ctx, $options) {
* [documentLoader(url)] the document loader. global $jsonld_default_load_document;
* self::setdefaults($options, array(
* @return array the flattened output. 'base' => is_string($input) ? $input : '',
*/ 'documentLoader' => $jsonld_default_load_document));
public function flatten($input, $ctx, $options)
{ try {
global $jsonld_default_load_document; // expand input
self::setdefaults($options, [ $expanded = $this->expand($input, $options);
'base' => is_string($input) ? $input : '', } catch(Exception $e) {
'documentLoader' => $jsonld_default_load_document]); throw new JsonLdException(
'Could not expand input before flattening.',
try { 'jsonld.FlattenError', null, null, $e);
// expand input }
$expanded = $this->expand($input, $options);
} catch (Exception $e) { // do flattening
throw new JsonLdException( $flattened = $this->_flatten($expanded);
'Could not expand input before flattening.', 'jsonld.FlattenError', null, null, $e);
} if($ctx === null) {
return $flattened;
// do flattening }
$flattened = $this->_flatten($expanded);
// compact result (force @graph option to true, skip expansion)
if ($ctx === null) { $options['graph'] = true;
return $flattened; $options['skipExpansion'] = true;
} try {
$compacted = $this->compact($flattened, $ctx, $options);
// compact result (force @graph option to true, skip expansion) } catch(Exception $e) {
$options['graph'] = true; throw new JsonLdException(
$options['skipExpansion'] = true; 'Could not compact flattened output.',
try { 'jsonld.FlattenError', null, null, $e);
$compacted = $this->compact($flattened, $ctx, $options); }
} catch (Exception $e) {
throw new JsonLdException( return $compacted;
'Could not compact flattened output.', 'jsonld.FlattenError', null, null, $e); }
}
/**
return $compacted; * Performs JSON-LD framing.
} *
* @param mixed $input the JSON-LD object to frame.
/** * @param stdClass $frame the JSON-LD frame to use.
* Performs JSON-LD framing. * @param $options the framing options.
* * [base] the base IRI to use.
* @param mixed $input the JSON-LD object to frame. * [expandContext] a context to expand with.
* @param stdClass $frame the JSON-LD frame to use. * [embed] default @embed flag (default: true).
* @param $options the framing options. * [explicit] default @explicit flag (default: false).
* [base] the base IRI to use. * [omitDefault] default @omitDefault flag (default: false).
* [expandContext] a context to expand with. * [documentLoader(url)] the document loader.
* [embed] default @embed flag: '@last', '@always', '@never', '@link' *
* (default: '@last'). * @return stdClass the framed JSON-LD output.
* [explicit] default @explicit flag (default: false). */
* [requireAll] default @requireAll flag (default: true). public function frame($input, $frame, $options) {
* [omitDefault] default @omitDefault flag (default: false). global $jsonld_default_load_document;
* [documentLoader(url)] the document loader. self::setdefaults($options, array(
* 'base' => is_string($input) ? $input : '',
* @return stdClass the framed JSON-LD output. 'compactArrays' => true,
*/ 'embed' => true,
public function frame($input, $frame, $options) 'explicit' => false,
{ 'omitDefault' => false,
global $jsonld_default_load_document; 'documentLoader' => $jsonld_default_load_document));
self::setdefaults($options, [
'base' => is_string($input) ? $input : '', // if frame is a string, attempt to dereference remote document
'compactArrays' => true, if(is_string($frame)) {
'embed' => '@last', $remote_frame = call_user_func($options['documentLoader'], $frame);
'explicit' => false, } else {
'requireAll' => true, $remote_frame = (object)array(
'omitDefault' => false, 'contextUrl' => null,
'documentLoader' => $jsonld_default_load_document]); 'documentUrl' => null,
'document' => $frame);
// if frame is a string, attempt to dereference remote document }
if (is_string($frame)) {
$remote_frame = call_user_func($options['documentLoader'], $frame); try {
} else { if($remote_frame->document === null) {
$remote_frame = (object) [ throw new JsonLdException(
'contextUrl' => null, 'No remote document found at the given URL.',
'documentUrl' => null, 'jsonld.NullRemoteDocument');
'document' => $frame]; }
} if(is_string($remote_frame->document)) {
$remote_frame->document = self::_parse_json($remote_frame->document);
try { }
if ($remote_frame->document === null) { } catch(Exception $e) {
throw new JsonLdException( throw new JsonLdException(
'No remote document found at the given URL.', 'jsonld.NullRemoteDocument'); 'Could not retrieve a JSON-LD document from the URL.',
} 'jsonld.LoadDocumentError', 'loading document failed',
if (is_string($remote_frame->document)) { array('remoteDoc' => $remote_frame), $e);
$remote_frame->document = self::_parse_json($remote_frame->document); }
}
} catch (Exception $e) { // preserve frame context
throw new JsonLdException( $frame = $remote_frame->document;
'Could not retrieve a JSON-LD document from the URL.', 'jsonld.LoadDocumentError', 'loading document failed', ['remoteDoc' => $remote_frame], $e); if($frame !== null) {
} $ctx = (property_exists($frame, '@context') ?
$frame->{'@context'} : new stdClass());
// preserve frame context if($remote_frame->contextUrl !== null) {
$frame = $remote_frame->document; if($ctx !== null) {
if ($frame !== null) { $ctx = $remote_frame->contextUrl;
$ctx = (property_exists($frame, '@context') ? } else {
$frame->{'@context'} : new stdClass()); $ctx = self::arrayify($ctx);
if ($remote_frame->contextUrl !== null) { $ctx[] = $remote_frame->contextUrl;
if ($ctx !== null) { }
$ctx = $remote_frame->contextUrl; $frame->{'@context'} = $ctx;
} else { }
$ctx = self::arrayify($ctx); }
$ctx[] = $remote_frame->contextUrl;
} try {
$frame->{'@context'} = $ctx; // expand input
} $expanded = $this->expand($input, $options);
} } catch(Exception $e) {
throw new JsonLdException(
try { 'Could not expand input before framing.',
// expand input 'jsonld.FrameError', null, null, $e);
$expanded = $this->expand($input, $options); }
} catch (Exception $e) {
throw new JsonLdException( try {
'Could not expand input before framing.', 'jsonld.FrameError', null, null, $e); // expand frame
} $opts = $options;
$opts['keepFreeFloatingNodes'] = true;
try { $expanded_frame = $this->expand($frame, $opts);
// expand frame } catch(Exception $e) {
$opts = $options; throw new JsonLdException(
$opts['keepFreeFloatingNodes'] = true; 'Could not expand frame before framing.',
$expanded_frame = $this->expand($frame, $opts); 'jsonld.FrameError', null, null, $e);
} catch (Exception $e) { }
throw new JsonLdException(
'Could not expand frame before framing.', 'jsonld.FrameError', null, null, $e); // do framing
} $framed = $this->_frame($expanded, $expanded_frame, $options);
// do framing try {
$framed = $this->_frame($expanded, $expanded_frame, $options); // compact result (force @graph option to true)
$options['graph'] = true;
try { $options['skipExpansion'] = true;
// compact result (force @graph option to true, skip expansion, check $options['activeCtx'] = true;
// for linked embeds) $result = $this->compact($framed, $ctx, $options);
$options['graph'] = true; } catch(Exception $e) {
$options['skipExpansion'] = true; throw new JsonLdException(
$options['link'] = new ArrayObject(); 'Could not compact framed output.',
$options['activeCtx'] = true; 'jsonld.FrameError', null, null, $e);
$result = $this->compact($framed, $ctx, $options); }
} catch (Exception $e) {
throw new JsonLdException( $compacted = $result['compacted'];
'Could not compact framed output.', 'jsonld.FrameError', null, null, $e); $active_ctx = $result['activeCtx'];
}
// get graph alias
$compacted = $result['compacted']; $graph = $this->_compactIri($active_ctx, '@graph');
$active_ctx = $result['activeCtx']; // remove @preserve from results
$compacted->{$graph} = $this->_removePreserve(
// get graph alias $active_ctx, $compacted->{$graph}, $options);
$graph = $this->_compactIri($active_ctx, '@graph'); return $compacted;
// remove @preserve from results }
$options['link'] = new ArrayObject();
$compacted->{$graph} = $this->_removePreserve( /**
$active_ctx, $compacted->{$graph}, $options); * Performs JSON-LD normalization.
return $compacted; *
} * @param mixed $input the JSON-LD object to normalize.
* @param assoc $options the options to use:
/** * [base] the base IRI to use.
* Performs JSON-LD normalization. * [expandContext] a context to expand with.
* * [format] the format if output is a string:
* @param mixed $input the JSON-LD object to normalize. * 'application/nquads' for N-Quads.
* @param assoc $options the options to use: * [documentLoader(url)] the document loader.
* [base] the base IRI to use. *
* [expandContext] a context to expand with. * @return mixed the normalized output.
* [inputFormat] the format if input is not JSON-LD: */
* 'application/nquads' for N-Quads. public function normalize($input, $options) {
* [format] the format if output is a string: global $jsonld_default_load_document;
* 'application/nquads' for N-Quads. self::setdefaults($options, array(
* [documentLoader(url)] the document loader. 'base' => is_string($input) ? $input : '',
* 'documentLoader' => $jsonld_default_load_document));
* @return mixed the normalized output.
*/ try {
public function normalize($input, $options) // convert to RDF dataset then do normalization
{ $opts = $options;
global $jsonld_default_load_document; if(isset($opts['format'])) {
self::setdefaults($options, [ unset($opts['format']);
'base' => is_string($input) ? $input : '', }
'documentLoader' => $jsonld_default_load_document]); $opts['produceGeneralizedRdf'] = false;
$dataset = $this->toRDF($input, $opts);
if (isset($options['inputFormat'])) { } catch(Exception $e) {
if ($options['inputFormat'] != 'application/nquads') { throw new JsonLdException(
throw new JsonLdException( 'Could not convert input to RDF dataset before normalization.',
'Unknown normalization input format.', 'jsonld.NormalizeError'); 'jsonld.NormalizeError', null, null, $e);
} }
$dataset = $this->parseNQuads($input);
} else { // do normalization
try { return $this->_normalize($dataset, $options);
// convert to RDF dataset then do normalization }
$opts = $options;
if (isset($opts['format'])) { /**
unset($opts['format']); * Converts an RDF dataset to JSON-LD.
} *
$opts['produceGeneralizedRdf'] = false; * @param mixed $dataset a serialized string of RDF in a format specified
$dataset = $this->toRDF($input, $opts); * by the format option or an RDF dataset to convert.
} catch (Exception $e) { * @param assoc $options the options to use:
throw new JsonLdException( * [format] the format if input is a string:
'Could not convert input to RDF dataset before normalization.', 'jsonld.NormalizeError', null, null, $e); * 'application/nquads' for N-Quads (default).
} * [useRdfType] true to use rdf:type, false to use @type
} * (default: false).
* [useNativeTypes] true to convert XSD types into native types
// do normalization * (boolean, integer, double), false not to (default: false).
return $this->_normalize($dataset, $options); *
} * @return array the JSON-LD output.
*/
/** public function fromRDF($dataset, $options) {
* Converts an RDF dataset to JSON-LD. global $jsonld_rdf_parsers;
*
* @param mixed $dataset a serialized string of RDF in a format specified self::setdefaults($options, array(
* by the format option or an RDF dataset to convert. 'useRdfType' => false,
* @param assoc $options the options to use: 'useNativeTypes' => false));
* [format] the format if input is a string:
* 'application/nquads' for N-Quads (default). if(!isset($options['format']) && is_string($dataset)) {
* [useRdfType] true to use rdf:type, false to use @type // set default format to nquads
* (default: false). $options['format'] = 'application/nquads';
* [useNativeTypes] true to convert XSD types into native types }
* (boolean, integer, double), false not to (default: false).
* // handle special format
* @return array the JSON-LD output. if(isset($options['format']) && $options['format']) {
*/ // supported formats (processor-specific and global)
public function fromRDF($dataset, $options) if(($this->rdfParsers !== null &&
{ !property_exists($this->rdfParsers, $options['format'])) ||
global $jsonld_rdf_parsers; $this->rdfParsers === null &&
!property_exists($jsonld_rdf_parsers, $options['format'])) {
self::setdefaults($options, [ throw new JsonLdException(
'useRdfType' => false, 'Unknown input format.',
'useNativeTypes' => false]); 'jsonld.UnknownFormat', null, array('format' => $options['format']));
}
if (!isset($options['format']) && is_string($dataset)) { if($this->rdfParsers !== null) {
// set default format to nquads $callable = $this->rdfParsers->{$options['format']};
$options['format'] = 'application/nquads'; } else {
} $callable = $jsonld_rdf_parsers->{$options['format']};
}
// handle special format $dataset = call_user_func($callable, $dataset);
if (isset($options['format']) && $options['format']) { }
// supported formats (processor-specific and global)
if (($this->rdfParsers !== null && // convert from RDF
!property_exists($this->rdfParsers, $options['format'])) || return $this->_fromRDF($dataset, $options);
$this->rdfParsers === null && }
!property_exists($jsonld_rdf_parsers, $options['format'])) {
throw new JsonLdException( /**
'Unknown input format.', 'jsonld.UnknownFormat', null, ['format' => $options['format']]); * Outputs the RDF dataset found in the given JSON-LD object.
} *
if ($this->rdfParsers !== null) { * @param mixed $input the JSON-LD object.
$callable = $this->rdfParsers->{$options['format']}; * @param assoc $options the options to use:
} else { * [base] the base IRI to use.
$callable = $jsonld_rdf_parsers->{$options['format']}; * [expandContext] a context to expand with.
} * [format] the format to use to output a string:
$dataset = call_user_func($callable, $dataset); * 'application/nquads' for N-Quads.
} * [produceGeneralizedRdf] true to output generalized RDF, false
* to produce only standard RDF (default: false).
// convert from RDF * [documentLoader(url)] the document loader.
return $this->_fromRDF($dataset, $options); *
} * @return mixed the resulting RDF dataset (or a serialization of it).
*/
/** public function toRDF($input, $options) {
* Outputs the RDF dataset found in the given JSON-LD object. global $jsonld_default_load_document;
* self::setdefaults($options, array(
* @param mixed $input the JSON-LD object. 'base' => is_string($input) ? $input : '',
* @param assoc $options the options to use: 'produceGeneralizedRdf' => false,
* [base] the base IRI to use. 'documentLoader' => $jsonld_default_load_document));
* [expandContext] a context to expand with.
* [format] the format to use to output a string: try {
* 'application/nquads' for N-Quads. // expand input
* [produceGeneralizedRdf] true to output generalized RDF, false $expanded = $this->expand($input, $options);
* to produce only standard RDF (default: false). } catch(JsonLdException $e) {
* [documentLoader(url)] the document loader. throw new JsonLdException(
* 'Could not expand input before serialization to RDF.',
* @return mixed the resulting RDF dataset (or a serialization of it). 'jsonld.RdfError', null, null, $e);
*/ }
public function toRDF($input, $options)
{ // create node map for default graph (and any named graphs)
global $jsonld_default_load_document; $namer = new UniqueNamer('_:b');
self::setdefaults($options, [ $node_map = (object)array('@default' => new stdClass());
'base' => is_string($input) ? $input : '', $this->_createNodeMap($expanded, $node_map, '@default', $namer);
'produceGeneralizedRdf' => false,
'documentLoader' => $jsonld_default_load_document]); // output RDF dataset
$dataset = new stdClass();
try { $graph_names = array_keys((array)$node_map);
// expand input sort($graph_names);
$expanded = $this->expand($input, $options); foreach($graph_names as $graph_name) {
} catch (JsonLdException $e) { $graph = $node_map->{$graph_name};
throw new JsonLdException( // skip relative IRIs
'Could not expand input before serialization to RDF.', 'jsonld.RdfError', null, null, $e); if($graph_name === '@default' || self::_isAbsoluteIri($graph_name)) {
} $dataset->{$graph_name} = $this->_graphToRDF($graph, $namer, $options);
}
// create node map for default graph (and any named graphs) }
$namer = new UniqueNamer('_:b');
$node_map = (object) ['@default' => new stdClass()]; $rval = $dataset;
$this->_createNodeMap($expanded, $node_map, '@default', $namer);
// convert to output format
// output RDF dataset if(isset($options['format']) && $options['format']) {
$dataset = new stdClass(); // supported formats
$graph_names = array_keys((array) $node_map); if($options['format'] === 'application/nquads') {
sort($graph_names); $rval = self::toNQuads($dataset);
foreach ($graph_names as $graph_name) { } else {
$graph = $node_map->{$graph_name}; throw new JsonLdException(
// skip relative IRIs 'Unknown output format.', 'jsonld.UnknownFormat',
if ($graph_name === '@default' || self::_isAbsoluteIri($graph_name)) { null, array('format' => $options['format']));
$dataset->{$graph_name} = $this->_graphToRDF($graph, $namer, $options); }
} }
}
return $rval;
$rval = $dataset; }
// convert to output format /**
if (isset($options['format']) && $options['format']) { * Processes a local context, resolving any URLs as necessary, and returns a
// supported formats * new active context in its callback.
if ($options['format'] === 'application/nquads') { *
$rval = self::toNQuads($dataset); * @param stdClass $active_ctx the current active context.
} else { * @param mixed $local_ctx the local context to process.
throw new JsonLdException( * @param assoc $options the options to use:
'Unknown output format.', 'jsonld.UnknownFormat', null, ['format' => $options['format']]); * [documentLoader(url)] the document loader.
} *
} * @return stdClass the new active context.
*/
return $rval; public function processContext($active_ctx, $local_ctx, $options) {
} global $jsonld_default_load_document;
self::setdefaults($options, array(
/** 'base' => '',
* Processes a local context, resolving any URLs as necessary, and returns a 'documentLoader' => $jsonld_default_load_document));
* new active context in its callback.
* // return initial context early for null context
* @param stdClass $active_ctx the current active context. if($local_ctx === null) {
* @param mixed $local_ctx the local context to process. return $this->_getInitialContext($options);
* @param assoc $options the options to use: }
* [documentLoader(url)] the document loader.
* // retrieve URLs in local_ctx
* @return stdClass the new active context. $local_ctx = self::copy($local_ctx);
*/ if(is_string($local_ctx) or (
public function processContext($active_ctx, $local_ctx, $options) is_object($local_ctx) && !property_exists($local_ctx, '@context'))) {
{ $local_ctx = (object)array('@context' => $local_ctx);
global $jsonld_default_load_document; }
self::setdefaults($options, [ try {
'base' => '', $this->_retrieveContextUrls(
'documentLoader' => $jsonld_default_load_document]); $local_ctx, new stdClass(),
$options['documentLoader'], $options['base']);
// return initial context early for null context } catch(Exception $e) {
if ($local_ctx === null) { throw new JsonLdException(
return $this->_getInitialContext($options); 'Could not process JSON-LD context.',
} 'jsonld.ContextError', null, null, $e);
}
// retrieve URLs in local_ctx
$local_ctx = self::copy($local_ctx); // process context
if (is_string($local_ctx) or ( return $this->_processContext($active_ctx, $local_ctx, $options);
is_object($local_ctx) && !property_exists($local_ctx, '@context'))) { }
$local_ctx = (object) ['@context' => $local_ctx];
} /**
try { * Returns true if the given subject has the given property.
$this->_retrieveContextUrls( *
$local_ctx, new stdClass(), $options['documentLoader'], $options['base']); * @param stdClass $subject the subject to check.
} catch (Exception $e) { * @param string $property the property to look for.
throw new JsonLdException( *
'Could not process JSON-LD context.', 'jsonld.ContextError', null, null, $e); * @return bool true if the subject has the given property, false if not.
} */
public static function hasProperty($subject, $property) {
// process context $rval = false;
return $this->_processContext($active_ctx, $local_ctx, $options); if(property_exists($subject, $property)) {
} $value = $subject->{$property};
$rval = (!is_array($value) || count($value) > 0);
/** }
* Returns true if the given subject has the given property. return $rval;
* }
* @param stdClass $subject the subject to check.
* @param string $property the property to look for. /**
* * Determines if the given value is a property of the given subject.
* @return bool true if the subject has the given property, false if not. *
*/ * @param stdClass $subject the subject to check.
public static function hasProperty($subject, $property) * @param string $property the property to check.
{ * @param mixed $value the value to check.
$rval = false; *
if (property_exists($subject, $property)) { * @return bool true if the value exists, false if not.
$value = $subject->{$property}; */
$rval = (!is_array($value) || count($value) > 0); public static function hasValue($subject, $property, $value) {
} $rval = false;
return $rval; if(self::hasProperty($subject, $property)) {
} $val = $subject->{$property};
$is_list = self::_isList($val);
/** if(is_array($val) || $is_list) {
* Determines if the given value is a property of the given subject. if($is_list) {
* $val = $val->{'@list'};
* @param stdClass $subject the subject to check. }
* @param string $property the property to check. foreach($val as $v) {
* @param mixed $value the value to check. if(self::compareValues($value, $v)) {
* $rval = true;
* @return bool true if the value exists, false if not. break;
*/ }
public static function hasValue($subject, $property, $value) }
{ } else if(!is_array($value)) {
$rval = false; // avoid matching the set of values with an array value parameter
if (self::hasProperty($subject, $property)) { $rval = self::compareValues($value, $val);
$val = $subject->{$property}; }
$is_list = self::_isList($val); }
if (is_array($val) || $is_list) { return $rval;
if ($is_list) { }
$val = $val->{'@list'};
} /**
foreach ($val as $v) { * Adds a value to a subject. If the value is an array, all values in the
if (self::compareValues($value, $v)) { * array will be added.
$rval = true; *
break; * Note: If the value is a subject that already exists as a property of the
} * given subject, this method makes no attempt to deeply merge properties.
} * Instead, the value will not be added.
} else if (!is_array($value)) { *
// avoid matching the set of values with an array value parameter * @param stdClass $subject the subject to add the value to.
$rval = self::compareValues($value, $val); * @param string $property the property that relates the value to the subject.
} * @param mixed $value the value to add.
} * @param assoc [$options] the options to use:
return $rval; * [propertyIsArray] true if the property is always an array, false
} * if not (default: false).
* [allowDuplicate] true to allow duplicates, false not to (uses a
/** * simple shallow comparison of subject ID or value)
* Adds a value to a subject. If the value is an array, all values in the * (default: true).
* array will be added. */
* public static function addValue(
* Note: If the value is a subject that already exists as a property of the $subject, $property, $value, $options=array()) {
* given subject, this method makes no attempt to deeply merge properties. self::setdefaults($options, array(
* Instead, the value will not be added. 'allowDuplicate' => true,
* 'propertyIsArray' => false));
* @param stdClass $subject the subject to add the value to.
* @param string $property the property that relates the value to the subject. if(is_array($value)) {
* @param mixed $value the value to add. if(count($value) === 0 && $options['propertyIsArray'] &&
* @param assoc [$options] the options to use: !property_exists($subject, $property)) {
* [propertyIsArray] true if the property is always an array, false $subject->{$property} = array();
* if not (default: false). }
* [allowDuplicate] true to allow duplicates, false not to (uses a foreach($value as $v) {
* simple shallow comparison of subject ID or value) self::addValue($subject, $property, $v, $options);
* (default: true). }
*/ } else if(property_exists($subject, $property)) {
public static function addValue( // check if subject already has value if duplicates not allowed
$subject, $property, $value, $options = []) $has_value = (!$options['allowDuplicate'] &&
{ self::hasValue($subject, $property, $value));
self::setdefaults($options, [
'allowDuplicate' => true, // make property an array if value not present or always an array
'propertyIsArray' => false]); if(!is_array($subject->{$property}) &&
(!$has_value || $options['propertyIsArray'])) {
if (is_array($value)) { $subject->{$property} = array($subject->{$property});
if (count($value) === 0 && $options['propertyIsArray'] && }
!property_exists($subject, $property)) {
$subject->{$property} = []; // add new value
} if(!$has_value) {
foreach ($value as $v) { $subject->{$property}[] = $value;
self::addValue($subject, $property, $v, $options); }
} } else {
} else if (property_exists($subject, $property)) { // add new value as set or single value
// check if subject already has value if duplicates not allowed $subject->{$property} = ($options['propertyIsArray'] ?
$has_value = (!$options['allowDuplicate'] && array($value) : $value);
self::hasValue($subject, $property, $value)); }
}
// make property an array if value not present or always an array
if (!is_array($subject->{$property}) && /**
(!$has_value || $options['propertyIsArray'])) { * Gets all of the values for a subject's property as an array.
$subject->{$property} = [$subject->{$property}]; *
} * @param stdClass $subject the subject.
* @param string $property the property.
// add new value *
if (!$has_value) { * @return array all of the values for a subject's property as an array.
$subject->{$property}[] = $value; */
} public static function getValues($subject, $property) {
} else { $rval = (property_exists($subject, $property) ?
// add new value as set or single value $subject->{$property} : array());
$subject->{$property} = ($options['propertyIsArray'] ? return self::arrayify($rval);
[$value] : $value); }
}
} /**
* Removes a property from a subject.
/** *
* Gets all of the values for a subject's property as an array. * @param stdClass $subject the subject.
* * @param string $property the property.
* @param stdClass $subject the subject. */
* @param string $property the property. public static function removeProperty($subject, $property) {
* unset($subject->{$property});
* @return array all of the values for a subject's property as an array. }
*/
public static function getValues($subject, $property) /**
{ * Removes a value from a subject.
$rval = (property_exists($subject, $property) ? *
$subject->{$property} : []); * @param stdClass $subject the subject.
return self::arrayify($rval); * @param string $property the property that relates the value to the subject.
} * @param mixed $value the value to remove.
* @param assoc [$options] the options to use:
/** * [propertyIsArray] true if the property is always an array,
* Removes a property from a subject. * false if not (default: false).
* */
* @param stdClass $subject the subject. public static function removeValue(
* @param string $property the property. $subject, $property, $value, $options=array()) {
*/ self::setdefaults($options, array(
public static function removeProperty($subject, $property) 'propertyIsArray' => false));
{
unset($subject->{$property}); // filter out value
} $filter = function($e) use ($value) {
return !self::compareValues($e, $value);
/** };
* Removes a value from a subject. $values = self::getValues($subject, $property);
* $values = array_values(array_filter($values, $filter));
* @param stdClass $subject the subject.
* @param string $property the property that relates the value to the subject. if(count($values) === 0) {
* @param mixed $value the value to remove. self::removeProperty($subject, $property);
* @param assoc [$options] the options to use: } else if(count($values) === 1 && !$options['propertyIsArray']) {
* [propertyIsArray] true if the property is always an array, $subject->{$property} = $values[0];
* false if not (default: false). } else {
*/ $subject->{$property} = $values;
public static function removeValue( }
$subject, $property, $value, $options = []) }
{
self::setdefaults($options, [ /**
'propertyIsArray' => false]); * Compares two JSON-LD values for equality. Two JSON-LD values will be
* considered equal if:
// filter out value *
$filter = function($e) use ($value) { * 1. They are both primitives of the same type and value.
return !self::compareValues($e, $value); * 2. They are both @values with the same @value, @type, @language,
}; * and @index, OR
$values = self::getValues($subject, $property); * 3. They both have @ids that are the same.
$values = array_values(array_filter($values, $filter)); *
* @param mixed $v1 the first value.
if (count($values) === 0) { * @param mixed $v2 the second value.
self::removeProperty($subject, $property); *
} else if (count($values) === 1 && !$options['propertyIsArray']) { * @return bool true if v1 and v2 are considered equal, false if not.
$subject->{$property} = $values[0]; */
} else { public static function compareValues($v1, $v2) {
$subject->{$property} = $values; // 1. equal primitives
} if($v1 === $v2) {
} return true;
}
/**
* Compares two JSON-LD values for equality. Two JSON-LD values will be // 2. equal @values
* considered equal if: if(self::_isValue($v1) && self::_isValue($v2)) {
* return (
* 1. They are both primitives of the same type and value. self::_compareKeyValues($v1, $v2, '@value') &&
* 2. They are both @values with the same @value, @type, @language, self::_compareKeyValues($v1, $v2, '@type') &&
* and @index, OR self::_compareKeyValues($v1, $v2, '@language') &&
* 3. They both have @ids that are the same. self::_compareKeyValues($v1, $v2, '@index'));
* }
* @param mixed $v1 the first value.
* @param mixed $v2 the second value. // 3. equal @ids
* if(is_object($v1) && property_exists($v1, '@id') &&
* @return bool true if v1 and v2 are considered equal, false if not. is_object($v2) && property_exists($v2, '@id')) {
*/ return $v1->{'@id'} === $v2->{'@id'};
public static function compareValues($v1, $v2) }
{
// 1. equal primitives return false;
if ($v1 === $v2) { }
return true;
} /**
* Gets the value for the given active context key and type, null if none is
// 2. equal @values * set.
if (self::_isValue($v1) && self::_isValue($v2)) { *
return ( * @param stdClass $ctx the active context.
self::_compareKeyValues($v1, $v2, '@value') && * @param string $key the context key.
self::_compareKeyValues($v1, $v2, '@type') && * @param string [$type] the type of value to get (eg: '@id', '@type'), if not
self::_compareKeyValues($v1, $v2, '@language') && * specified gets the entire entry for a key, null if not found.
self::_compareKeyValues($v1, $v2, '@index')); *
} * @return mixed the value.
*/
// 3. equal @ids public static function getContextValue($ctx, $key, $type) {
if (is_object($v1) && property_exists($v1, '@id') && $rval = null;
is_object($v2) && property_exists($v2, '@id')) {
return $v1->{'@id'} === $v2->{'@id'}; // return null for invalid key
} if($key === null) {
return $rval;
return false; }
}
// get default language
/** if($type === '@language' && property_exists($ctx, $type)) {
* Gets the value for the given active context key and type, null if none is $rval = $ctx->{$type};
* set. }
*
* @param stdClass $ctx the active context. // get specific entry information
* @param string $key the context key. if(property_exists($ctx->mappings, $key)) {
* @param string [$type] the type of value to get (eg: '@id', '@type'), if not $entry = $ctx->mappings->{$key};
* specified gets the entire entry for a key, null if not found. if($entry === null) {
* return null;
* @return mixed the value. }
*/
public static function getContextValue($ctx, $key, $type) if($type === null) {
{ // return whole entry
$rval = null; $rval = $entry;
} else if(property_exists($entry, $type)) {
// return null for invalid key // return entry value for type
if ($key === null) { $rval = $entry->{$type};
return $rval; }
} }
// get default language return $rval;
if ($type === '@language' && property_exists($ctx, $type)) { }
$rval = $ctx->{$type};
} /**
* Parses RDF in the form of N-Quads.
// get specific entry information *
if (property_exists($ctx->mappings, $key)) { * @param string $input the N-Quads input to parse.
$entry = $ctx->mappings->{$key}; *
if ($entry === null) { * @return stdClass an RDF dataset.
return null; */
} public static function parseNQuads($input) {
// define partial regexes
if ($type === null) { $iri = '(?:<([^:]+:[^>]*)>)';
// return whole entry $bnode = '(_:(?:[A-Za-z][A-Za-z0-9]*))';
$rval = $entry; $plain = '"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"';
} else if (property_exists($entry, $type)) { $datatype = "(?:\\^\\^$iri)";
// return entry value for type $language = '(?:@([a-z]+(?:-[a-z0-9]+)*))';
$rval = $entry->{$type}; $literal = "(?:$plain(?:$datatype|$language)?)";
} $ws = '[ \\t]';
} $eoln = '/(?:\r\n)|(?:\n)|(?:\r)/';
$empty = "/^$ws*$/";
return $rval;
} // define quad part regexes
$subject = "(?:$iri|$bnode)$ws+";
/** $property = "$iri$ws+";
* Parses RDF in the form of N-Quads. $object = "(?:$iri|$bnode|$literal)$ws*";
* $graph_name = "(?:\\.|(?:(?:$iri|$bnode)$ws*\\.))";
* @param string $input the N-Quads input to parse.
* // full quad regex
* @return stdClass an RDF dataset. $quad = "/^$ws*$subject$property$object$graph_name$ws*$/";
*/
public static function parseNQuads($input) // build RDF dataset
{ $dataset = new stdClass();
// define partial regexes
$iri = '(?:<([^:]+:[^>]*)>)'; // split N-Quad input into lines
$bnode = '(_:(?:[A-Za-z][A-Za-z0-9]*))'; $lines = preg_split($eoln, $input);
$plain = '"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"'; $line_number = 0;
$datatype = "(?:\\^\\^$iri)"; foreach($lines as $line) {
$language = '(?:@([a-z]+(?:-[a-z0-9]+)*))'; $line_number += 1;
$literal = "(?:$plain(?:$datatype|$language)?)";
$ws = '[ \\t]'; // skip empty lines
$eoln = '/(?:\r\n)|(?:\n)|(?:\r)/'; if(preg_match($empty, $line)) {
$empty = "/^$ws*$/"; continue;
}
// define quad part regexes
$subject = "(?:$iri|$bnode)$ws+"; // parse quad
$property = "$iri$ws+"; if(!preg_match($quad, $line, $match)) {
$object = "(?:$iri|$bnode|$literal)$ws*"; throw new JsonLdException(
$graph_name = "(?:\\.|(?:(?:$iri|$bnode)$ws*\\.))"; 'Error while parsing N-Quads; invalid quad.',
'jsonld.ParseError', null, array('line' => $line_number));
// full quad regex }
$quad = "/^$ws*$subject$property$object$graph_name$ws*$/";
// create RDF triple
// build RDF dataset $triple = (object)array(
$dataset = new stdClass(); 'subject' => new stdClass(),
'predicate' => new stdClass(),
// split N-Quad input into lines 'object' => new stdClass());
$lines = preg_split($eoln, $input);
$line_number = 0; // get subject
foreach ($lines as $line) { if($match[1] !== '') {
$line_number += 1; $triple->subject->type = 'IRI';
$triple->subject->value = $match[1];
// skip empty lines } else {
if (preg_match($empty, $line)) { $triple->subject->type = 'blank node';
continue; $triple->subject->value = $match[2];
} }
// parse quad // get predicate
if (!preg_match($quad, $line, $match)) { $triple->predicate->type = 'IRI';
throw new JsonLdException( $triple->predicate->value = $match[3];
'Error while parsing N-Quads; invalid quad.', 'jsonld.ParseError', null, ['line' => $line_number]);
} // get object
if($match[4] !== '') {
// create RDF triple $triple->object->type = 'IRI';
$triple = (object) [ $triple->object->value = $match[4];
'subject' => new stdClass(), } else if($match[5] !== '') {
'predicate' => new stdClass(), $triple->object->type = 'blank node';
'object' => new stdClass()]; $triple->object->value = $match[5];
} else {
// get subject $triple->object->type = 'literal';
if ($match[1] !== '') { $unescaped = str_replace(
$triple->subject->type = 'IRI'; array('\"', '\t', '\n', '\r', '\\\\'),
$triple->subject->value = $match[1]; array('"', "\t", "\n", "\r", '\\'),
} else { $match[6]);
$triple->subject->type = 'blank node'; if(isset($match[7]) && $match[7] !== '') {
$triple->subject->value = $match[2]; $triple->object->datatype = $match[7];
} } else if(isset($match[8]) && $match[8] !== '') {
$triple->object->datatype = self::RDF_LANGSTRING;
// get predicate $triple->object->language = $match[8];
$triple->predicate->type = 'IRI'; } else {
$triple->predicate->value = $match[3]; $triple->object->datatype = self::XSD_STRING;
}
// get object $triple->object->value = $unescaped;
if ($match[4] !== '') { }
$triple->object->type = 'IRI';
$triple->object->value = $match[4]; // get graph name ('@default' is used for the default graph)
} else if ($match[5] !== '') { $name = '@default';
$triple->object->type = 'blank node'; if(isset($match[9]) && $match[9] !== '') {
$triple->object->value = $match[5]; $name = $match[9];
} else { } else if(isset($match[10]) && $match[10] !== '') {
$triple->object->type = 'literal'; $name = $match[10];
$unescaped = str_replace( }
['\"', '\t', '\n', '\r', '\\\\'], ['"', "\t", "\n", "\r", '\\'], $match[6]);
if (isset($match[7]) && $match[7] !== '') { // initialize graph in dataset
$triple->object->datatype = $match[7]; if(!property_exists($dataset, $name)) {
} else if (isset($match[8]) && $match[8] !== '') { $dataset->{$name} = array($triple);
$triple->object->datatype = self::RDF_LANGSTRING; } else {
$triple->object->language = $match[8]; // add triple if unique to its graph
} else { $unique = true;
$triple->object->datatype = self::XSD_STRING; $triples = &$dataset->{$name};
} foreach($triples as $t) {
$triple->object->value = $unescaped; if(self::_compareRDFTriples($t, $triple)) {
} $unique = false;
break;
// get graph name ('@default' is used for the default graph) }
$name = '@default'; }
if (isset($match[9]) && $match[9] !== '') { if($unique) {
$name = $match[9]; $triples[] = $triple;
} else if (isset($match[10]) && $match[10] !== '') { }
$name = $match[10]; }
} }
// initialize graph in dataset return $dataset;
if (!property_exists($dataset, $name)) { }
$dataset->{$name} = [$triple];
} else { /**
// add triple if unique to its graph * Converts an RDF dataset to N-Quads.
$unique = true; *
$triples = &$dataset->{$name}; * @param stdClass $dataset the RDF dataset to convert.
foreach ($triples as $t) { *
if (self::_compareRDFTriples($t, $triple)) { * @return string the N-Quads string.
$unique = false; */
break; public static function toNQuads($dataset) {
} $quads = array();
} foreach($dataset as $graph_name => $triples) {
if ($unique) { foreach($triples as $triple) {
$triples[] = $triple; if($graph_name === '@default') {
} $graph_name = null;
} }
} $quads[] = self::toNQuad($triple, $graph_name);
}
return $dataset; }
} sort($quads);
return implode($quads);
/** }
* Converts an RDF dataset to N-Quads.
* /**
* @param stdClass $dataset the RDF dataset to convert. * Converts an RDF triple and graph name to an N-Quad string (a single quad).
* *
* @return string the N-Quads string. * @param stdClass $triple the RDF triple to convert.
*/ * @param mixed $graph_name the name of the graph containing the triple, null
public static function toNQuads($dataset) * for the default graph.
{ * @param string $bnode the bnode the quad is mapped to (optional, for
$quads = []; * use during normalization only).
foreach ($dataset as $graph_name => $triples) { *
foreach ($triples as $triple) { * @return string the N-Quad string.
if ($graph_name === '@default') { */
$graph_name = null; public static function toNQuad($triple, $graph_name, $bnode=null) {
} $s = $triple->subject;
$quads[] = self::toNQuad($triple, $graph_name); $p = $triple->predicate;
} $o = $triple->object;
} $g = $graph_name;
sort($quads);
return implode($quads); $quad = '';
}
// subject is an IRI
/** if($s->type === 'IRI') {
* Converts an RDF triple and graph name to an N-Quad string (a single quad). $quad .= "<{$s->value}>";
* } else if($bnode !== null) {
* @param stdClass $triple the RDF triple to convert. // bnode normalization mode
* @param mixed $graph_name the name of the graph containing the triple, null $quad .= ($s->value === $bnode) ? '_:a' : '_:z';
* for the default graph. } else {
* @param string $bnode the bnode the quad is mapped to (optional, for // bnode normal mode
* use during normalization only). $quad .= $s->value;
* }
* @return string the N-Quad string. $quad .= ' ';
*/
public static function toNQuad($triple, $graph_name, $bnode = null) // predicate is an IRI
{ if($p->type === 'IRI') {
$s = $triple->subject; $quad .= "<{$p->value}>";
$p = $triple->predicate; } else if($bnode !== null) {
$o = $triple->object; // FIXME: TBD what to do with bnode predicates during normalization
$g = $graph_name; // bnode normalization mode
$quad .= '_:p';
$quad = ''; } else {
// bnode normal mode
// subject is an IRI $quad .= $p->value;
if ($s->type === 'IRI') { }
$quad .= "<{$s->value}>"; $quad .= ' ';
} else if ($bnode !== null) {
// bnode normalization mode // object is IRI, bnode, or literal
$quad .= ($s->value === $bnode) ? '_:a' : '_:z'; if($o->type === 'IRI') {
} else { $quad .= "<{$o->value}>";
// bnode normal mode } else if($o->type === 'blank node') {
$quad .= $s->value; if($bnode !== null) {
} // normalization mode
$quad .= ' '; $quad .= ($o->value === $bnode) ? '_:a' : '_:z';
} else {
// predicate is an IRI // normal mode
if ($p->type === 'IRI') { $quad .= $o->value;
$quad .= "<{$p->value}>"; }
} else if ($bnode !== null) { } else {
// FIXME: TBD what to do with bnode predicates during normalization $escaped = str_replace(
// bnode normalization mode array('\\', "\t", "\n", "\r", '"'),
$quad .= '_:p'; array('\\\\', '\t', '\n', '\r', '\"'),
} else { $o->value);
// bnode normal mode $quad .= '"' . $escaped . '"';
$quad .= $p->value; if($o->datatype === self::RDF_LANGSTRING) {
} if($o->language) {
$quad .= ' '; $quad .= "@{$o->language}";
}
// object is IRI, bnode, or literal } else if($o->datatype !== self::XSD_STRING) {
if ($o->type === 'IRI') { $quad .= "^^<{$o->datatype}>";
$quad .= "<{$o->value}>"; }
} else if ($o->type === 'blank node') { }
if ($bnode !== null) {
// normalization mode // graph
$quad .= ($o->value === $bnode) ? '_:a' : '_:z'; if($g !== null) {
} else { if(strpos($g, '_:') !== 0) {
// normal mode $quad .= " <$g>";
$quad .= $o->value; } else if($bnode) {
} $quad .= ' _:g';
} else { } else {
$escaped = str_replace( $quad .= " $g";
['\\', "\t", "\n", "\r", '"'], ['\\\\', '\t', '\n', '\r', '\"'], $o->value); }
$quad .= '"' . $escaped . '"'; }
if ($o->datatype === self::RDF_LANGSTRING) {
if ($o->language) { $quad .= " .\n";
$quad .= "@{$o->language}"; return $quad;
} }
} else if ($o->datatype !== self::XSD_STRING) {
$quad .= "^^<{$o->datatype}>"; /**
} * Registers a processor-specific RDF dataset parser by content-type.
} * Global parsers will no longer be used by this processor.
*
// graph * @param string $content_type the content-type for the parser.
if ($g !== null) { * @param callable $parser(input) the parser function (takes a string as
if (strpos($g, '_:') !== 0) { * a parameter and returns an RDF dataset).
$quad .= " <$g>"; */
} else if ($bnode) { public function registerRDFParser($content_type, $parser) {
$quad .= ' _:g'; if($this->rdfParsers === null) {
} else { $this->rdfParsers = new stdClass();
$quad .= " $g"; }
} $this->rdfParsers->{$content_type} = $parser;
} }
$quad .= " .\n"; /**
return $quad; * Unregisters a process-specific RDF dataset parser by content-type. If
} * there are no remaining processor-specific parsers, then the global
* parsers will be re-enabled.
/** *
* Registers a processor-specific RDF dataset parser by content-type. * @param string $content_type the content-type for the parser.
* Global parsers will no longer be used by this processor. */
* public function unregisterRDFParser($content_type) {
* @param string $content_type the content-type for the parser. if($this->rdfParsers !== null &&
* @param callable $parser(input) the parser function (takes a string as property_exists($this->rdfParsers, $content_type)) {
* a parameter and returns an RDF dataset). unset($this->rdfParsers->{$content_type});
*/ if(count(get_object_vars($content_type)) === 0) {
public function registerRDFParser($content_type, $parser) $this->rdfParsers = null;
{ }
if ($this->rdfParsers === null) { }
$this->rdfParsers = new stdClass(); }
}
$this->rdfParsers->{$content_type} = $parser; /**
} * If $value is an array, returns $value, otherwise returns an array
* containing $value as the only element.
/** *
* Unregisters a process-specific RDF dataset parser by content-type. If * @param mixed $value the value.
* there are no remaining processor-specific parsers, then the global *
* parsers will be re-enabled. * @return array an array.
* */
* @param string $content_type the content-type for the parser. public static function arrayify($value) {
*/ return is_array($value) ? $value : array($value);
public function unregisterRDFParser($content_type) }
{
if ($this->rdfParsers !== null && /**
property_exists($this->rdfParsers, $content_type)) { * Clones an object, array, or string/number.
unset($this->rdfParsers->{$content_type}); *
if (count(get_object_vars($content_type)) === 0) { * @param mixed $value the value to clone.
$this->rdfParsers = null; *
} * @return mixed the cloned value.
} */
} public static function copy($value) {
if(is_object($value) || is_array($value)) {
/** return unserialize(serialize($value));
* If $value is an array, returns $value, otherwise returns an array }
* containing $value as the only element. return $value;
* }
* @param mixed $value the value.
* /**
* @return array an array. * Sets the value of a key for the given array if that property
*/ * has not already been set.
public static function arrayify($value) *
{ * @param &assoc $arr the object to update.
return is_array($value) ? $value : [$value]; * @param string $key the key to update.
} * @param mixed $value the value to set.
*/
/** public static function setdefault(&$arr, $key, $value) {
* Clones an object, array, or string/number. isset($arr[$key]) or $arr[$key] = $value;
* }
* @param mixed $value the value to clone.
* /**
* @return mixed the cloned value. * Sets default values for keys in the given array.
*/ *
public static function copy($value) * @param &assoc $arr the object to update.
{ * @param assoc $defaults the default keys and values.
if (is_object($value) || is_array($value)) { */
return unserialize(serialize($value)); public static function setdefaults(&$arr, $defaults) {
} foreach($defaults as $key => $value) {
return $value; self::setdefault($arr, $key, $value);
} }
}
/**
* Sets the value of a key for the given array if that property /**
* has not already been set. * Recursively compacts an element using the given active context. All values
* * must be in expanded form before this method is called.
* @param &assoc $arr the object to update. *
* @param string $key the key to update. * @param stdClass $active_ctx the active context to use.
* @param mixed $value the value to set. * @param mixed $active_property the compacted property with the element
*/ * to compact, null for none.
public static function setdefault(&$arr, $key, $value) * @param mixed $element the element to compact.
{ * @param assoc $options the compaction options.
isset($arr[$key]) or $arr[$key] = $value; *
} * @return mixed the compacted value.
*/
/** protected function _compact(
* Sets default values for keys in the given array. $active_ctx, $active_property, $element, $options) {
* // recursively compact array
* @param &assoc $arr the object to update. if(is_array($element)) {
* @param assoc $defaults the default keys and values. $rval = array();
*/ foreach($element as $e) {
public static function setdefaults(&$arr, $defaults) // compact, dropping any null values
{ $compacted = $this->_compact(
foreach ($defaults as $key => $value) { $active_ctx, $active_property, $e, $options);
self::setdefault($arr, $key, $value); if($compacted !== null) {
} $rval[] = $compacted;
} }
}
/** if($options['compactArrays'] && count($rval) === 1) {
* Recursively compacts an element using the given active context. All values // use single element if no container is specified
* must be in expanded form before this method is called. $container = self::getContextValue(
* $active_ctx, $active_property, '@container');
* @param stdClass $active_ctx the active context to use. if($container === null) {
* @param mixed $active_property the compacted property with the element $rval = $rval[0];
* to compact, null for none. }
* @param mixed $element the element to compact. }
* @param assoc $options the compaction options. return $rval;
* }
* @return mixed the compacted value.
*/ // recursively compact object
protected function _compact( if(is_object($element)) {
$active_ctx, $active_property, $element, $options) // do value compaction on @values and subject references
{ if(self::_isValue($element) || self::_isSubjectReference($element)) {
// recursively compact array return $this->_compactValue($active_ctx, $active_property, $element);
if (is_array($element)) { }
$rval = [];
foreach ($element as $e) { // FIXME: avoid misuse of active property as an expanded property?
// compact, dropping any null values $inside_reverse = ($active_property === '@reverse');
$compacted = $this->_compact(
$active_ctx, $active_property, $e, $options); // process element keys in order
if ($compacted !== null) { $keys = array_keys((array)$element);
$rval[] = $compacted; sort($keys);
} $rval = new stdClass();
} foreach($keys as $expanded_property) {
if ($options['compactArrays'] && count($rval) === 1) { $expanded_value = $element->{$expanded_property};
// use single element if no container is specified
$container = self::getContextValue( // compact @id and @type(s)
$active_ctx, $active_property, '@container'); if($expanded_property === '@id' || $expanded_property === '@type') {
if ($container === null) { if(is_string($expanded_value)) {
$rval = $rval[0]; // compact single @id
} $compacted_value = $this->_compactIri(
} $active_ctx, $expanded_value, null,
return $rval; array('vocab' => ($expanded_property === '@type')));
} } else {
// expanded value must be a @type array
// recursively compact object $compacted_value = array();
if (is_object($element)) { foreach($expanded_value as $ev) {
if ($options['link'] && property_exists($element, '@id') && $compacted_value[] = $this->_compactIri(
isset($options['link'][$element->{'@id'}])) { $active_ctx, $ev, null, array('vocab' => true));
// check for a linked element to reuse }
$linked = $options['link'][$element->{'@id'}]; }
foreach ($linked as $link) {
if ($link['expanded'] === $element) { // use keyword alias and add value
return $link['compacted']; $alias = $this->_compactIri($active_ctx, $expanded_property);
} $is_array = (is_array($compacted_value) &&
} count($expanded_value) === 0);
} self::addValue(
$rval, $alias, $compacted_value,
// do value compaction on @values and subject references array('propertyIsArray' => $is_array));
if (self::_isValue($element) || self::_isSubjectReference($element)) { continue;
$rval = $this->_compactValue($active_ctx, $active_property, $element); }
if ($options['link'] && self::_isSubjectReference($element)) {
// store linked element // handle @reverse
if (!isset($options['link'][$element->{'@id'}])) { if($expanded_property === '@reverse') {
$options['link'][$element->{'@id'}] = []; // recursively compact expanded value
} $compacted_value = $this->_compact(
$options['link'][$element->{'@id'}][] = [ $active_ctx, '@reverse', $expanded_value, $options);
'expanded' => $element, 'compacted' => $rval];
} // handle double-reversed properties
return $rval; foreach($compacted_value as $compacted_property => $value) {
} if(property_exists($active_ctx->mappings, $compacted_property) &&
$active_ctx->mappings->{$compacted_property} &&
// FIXME: avoid misuse of active property as an expanded property? $active_ctx->mappings->{$compacted_property}->reverse) {
$inside_reverse = ($active_property === '@reverse'); $container = self::getContextValue(
$active_ctx, $compacted_property, '@container');
$rval = new stdClass(); $use_array = ($container === '@set' ||
!$options['compactArrays']);
if ($options['link'] && property_exists($element, '@id')) { self::addValue(
// store linked element $rval, $compacted_property, $value,
if (!isset($options['link'][$element->{'@id'}])) { array('propertyIsArray' => $use_array));
$options['link'][$element->{'@id'}] = []; unset($compacted_value->{$compacted_property});
} }
$options['link'][$element->{'@id'}][] = [ }
'expanded' => $element, 'compacted' => $rval];
} if(count(array_keys((array)$compacted_value)) > 0) {
// use keyword alias and add value
// process element keys in order $alias = $this->_compactIri($active_ctx, $expanded_property);
$keys = array_keys((array) $element); self::addValue($rval, $alias, $compacted_value);
sort($keys); }
foreach ($keys as $expanded_property) {
$expanded_value = $element->{$expanded_property}; continue;
}
// compact @id and @type(s)
if ($expanded_property === '@id' || $expanded_property === '@type') { // handle @index property
if (is_string($expanded_value)) { if($expanded_property === '@index') {
// compact single @id // drop @index if inside an @index container
$compacted_value = $this->_compactIri( $container = self::getContextValue(
$active_ctx, $expanded_value, null, ['vocab' => ($expanded_property === '@type')]); $active_ctx, $active_property, '@container');
} else { if($container === '@index') {
// expanded value must be a @type array continue;
$compacted_value = []; }
foreach ($expanded_value as $ev) {
$compacted_value[] = $this->_compactIri( // use keyword alias and add value
$active_ctx, $ev, null, ['vocab' => true]); $alias = $this->_compactIri($active_ctx, $expanded_property);
} self::addValue($rval, $alias, $expanded_value);
} continue;
}
// use keyword alias and add value
$alias = $this->_compactIri($active_ctx, $expanded_property); // Note: expanded value must be an array due to expansion algorithm.
$is_array = (is_array($compacted_value) &&
count($expanded_value) === 0); // preserve empty arrays
self::addValue( if(count($expanded_value) === 0) {
$rval, $alias, $compacted_value, ['propertyIsArray' => $is_array]); $item_active_property = $this->_compactIri(
continue; $active_ctx, $expanded_property, $expanded_value,
} array('vocab' => true), $inside_reverse);
self::addValue(
// handle @reverse $rval, $item_active_property, array(),
if ($expanded_property === '@reverse') { array('propertyIsArray' => true));
// recursively compact expanded value }
$compacted_value = $this->_compact(
$active_ctx, '@reverse', $expanded_value, $options); // recusively process array values
foreach($expanded_value as $expanded_item) {
// handle double-reversed properties // compact property and get container type
foreach ($compacted_value as $compacted_property => $value) { $item_active_property = $this->_compactIri(
if (property_exists($active_ctx->mappings, $compacted_property) && $active_ctx, $expanded_property, $expanded_item,
$active_ctx->mappings->{$compacted_property} && array('vocab' => true), $inside_reverse);
$active_ctx->mappings->{$compacted_property}->reverse) { $container = self::getContextValue(
$container = self::getContextValue( $active_ctx, $item_active_property, '@container');
$active_ctx, $compacted_property, '@container');
$use_array = ($container === '@set' || // get @list value if appropriate
!$options['compactArrays']); $is_list = self::_isList($expanded_item);
self::addValue( $list = null;
$rval, $compacted_property, $value, ['propertyIsArray' => $use_array]); if($is_list) {
unset($compacted_value->{$compacted_property}); $list = $expanded_item->{'@list'};
} }
}
// recursively compact expanded item
if (count(array_keys((array) $compacted_value)) > 0) { $compacted_item = $this->_compact(
// use keyword alias and add value $active_ctx, $item_active_property,
$alias = $this->_compactIri($active_ctx, $expanded_property); $is_list ? $list : $expanded_item, $options);
self::addValue($rval, $alias, $compacted_value);
} // handle @list
if($is_list) {
continue; // ensure @list value is an array
} $compacted_item = self::arrayify($compacted_item);
// handle @index property if($container !== '@list') {
if ($expanded_property === '@index') { // wrap using @list alias
// drop @index if inside an @index container $compacted_item = (object)array(
$container = self::getContextValue( $this->_compactIri($active_ctx, '@list') => $compacted_item);
$active_ctx, $active_property, '@container');
if ($container === '@index') { // include @index from expanded @list, if any
continue; if(property_exists($expanded_item, '@index')) {
} $compacted_item->{$this->_compactIri($active_ctx, '@index')} =
$expanded_item->{'@index'};
// use keyword alias and add value }
$alias = $this->_compactIri($active_ctx, $expanded_property); } else if(property_exists($rval, $item_active_property)) {
self::addValue($rval, $alias, $expanded_value); // can't use @list container for more than 1 list
continue; throw new JsonLdException(
} 'JSON-LD compact error; property has a "@list" @container ' .
'rule but there is more than a single @list that matches ' .
// skip array processing for keywords that aren't @graph or @list 'the compacted term in the document. Compaction might mix ' .
if ($expanded_property !== '@graph' && $expanded_property !== '@list' && 'unwanted items into the list.', 'jsonld.SyntaxError',
self::_isKeyword($expanded_property)) { 'compaction to list of lists');
// use keyword alias and add value as is }
$alias = $this->_compactIri($active_ctx, $expanded_property); }
self::addValue($rval, $alias, $expanded_value);
continue; // handle language and index maps
} if($container === '@language' || $container === '@index') {
// get or create the map object
// Note: expanded value must be an array due to expansion algorithm. if(property_exists($rval, $item_active_property)) {
// preserve empty arrays $map_object = $rval->{$item_active_property};
if (count($expanded_value) === 0) { } else {
$item_active_property = $this->_compactIri( $rval->{$item_active_property} = $map_object = new stdClass();
$active_ctx, $expanded_property, $expanded_value, ['vocab' => true], $inside_reverse); }
self::addValue(
$rval, $item_active_property, [], ['propertyIsArray' => true]); // if container is a language map, simplify compacted value to
} // a simple string
if($container === '@language' && self::_isValue($compacted_item)) {
// recusively process array values $compacted_item = $compacted_item->{'@value'};
foreach ($expanded_value as $expanded_item) { }
// compact property and get container type
$item_active_property = $this->_compactIri( // add compact value to map object using key from expanded value
$active_ctx, $expanded_property, $expanded_item, ['vocab' => true], $inside_reverse); // based on the container type
$container = self::getContextValue( self::addValue(
$active_ctx, $item_active_property, '@container'); $map_object, $expanded_item->{$container}, $compacted_item);
} else {
// get @list value if appropriate // use an array if: compactArrays flag is false,
$is_list = self::_isList($expanded_item); // @container is @set or @list, value is an empty
$list = null; // array, or key is @graph
if ($is_list) { $is_array = (!$options['compactArrays'] ||
$list = $expanded_item->{'@list'}; $container === '@set' || $container === '@list' ||
} (is_array($compacted_item) && count($compacted_item) === 0) ||
$expanded_property === '@list' ||
// recursively compact expanded item $expanded_property === '@graph');
$compacted_item = $this->_compact(
$active_ctx, $item_active_property, $is_list ? $list : $expanded_item, $options); // add compact value
self::addValue(
// handle @list $rval, $item_active_property, $compacted_item,
if ($is_list) { array('propertyIsArray' => $is_array));
// ensure @list value is an array }
$compacted_item = self::arrayify($compacted_item); }
}
if ($container !== '@list') {
// wrap using @list alias return $rval;
$compacted_item = (object) [ }
$this->_compactIri($active_ctx, '@list') => $compacted_item];
// only primitives remain which are already compact
// include @index from expanded @list, if any return $element;
if (property_exists($expanded_item, '@index')) { }
$compacted_item->{$this->_compactIri($active_ctx, '@index')} = $expanded_item->{'@index'};
} /**
} else if (property_exists($rval, $item_active_property)) { * Recursively expands an element using the given context. Any context in
// can't use @list container for more than 1 list * the element will be removed. All context URLs must have been retrieved
throw new JsonLdException( * before calling this method.
'JSON-LD compact error; property has a "@list" @container ' . *
'rule but there is more than a single @list that matches ' . * @param stdClass $active_ctx the active context to use.
'the compacted term in the document. Compaction might mix ' . * @param mixed $active_property the property for the element, null for none.
'unwanted items into the list.', 'jsonld.SyntaxError', 'compaction to list of lists'); * @param mixed $element the element to expand.
} * @param assoc $options the expansion options.
} * @param bool $inside_list true if the property is a list, false if not.
*
// handle language and index maps * @return mixed the expanded value.
if ($container === '@language' || $container === '@index') { */
// get or create the map object protected function _expand(
if (property_exists($rval, $item_active_property)) { $active_ctx, $active_property, $element, $options, $inside_list) {
$map_object = $rval->{$item_active_property}; // nothing to expand
} else { if($element === null) {
$rval->{$item_active_property} = $map_object = new stdClass(); return $element;
} }
// if container is a language map, simplify compacted value to // recursively expand array
// a simple string if(is_array($element)) {
if ($container === '@language' && self::_isValue($compacted_item)) { $rval = array();
$compacted_item = $compacted_item->{'@value'}; $container = self::getContextValue(
} $active_ctx, $active_property, '@container');
$inside_list = $inside_list || $container === '@list';
// add compact value to map object using key from expanded value foreach($element as $e) {
// based on the container type // expand element
self::addValue( $e = $this->_expand(
$map_object, $expanded_item->{$container}, $compacted_item); $active_ctx, $active_property, $e, $options, $inside_list);
} else { if($inside_list && (is_array($e) || self::_isList($e))) {
// use an array if: compactArrays flag is false, // lists of lists are illegal
// @container is @set or @list, value is an empty throw new JsonLdException(
// array, or key is @graph 'Invalid JSON-LD syntax; lists of lists are not permitted.',
$is_array = (!$options['compactArrays'] || 'jsonld.SyntaxError', 'list of lists');
$container === '@set' || $container === '@list' || }
(is_array($compacted_item) && count($compacted_item) === 0) || // drop null values
$expanded_property === '@list' || if($e !== null) {
$expanded_property === '@graph'); if(is_array($e)) {
$rval = array_merge($rval, $e);
// add compact value } else {
self::addValue( $rval[] = $e;
$rval, $item_active_property, $compacted_item, ['propertyIsArray' => $is_array]); }
} }
} }
} return $rval;
}
return $rval;
} if(!is_object($element)) {
// drop free-floating scalars that are not in lists
// only primitives remain which are already compact if(!$inside_list &&
return $element; ($active_property === null ||
} $this->_expandIri($active_ctx, $active_property,
array('vocab' => true)) === '@graph')) {
/** return null;
* Recursively expands an element using the given context. Any context in }
* the element will be removed. All context URLs must have been retrieved
* before calling this method. // expand element according to value expansion rules
* return $this->_expandValue($active_ctx, $active_property, $element);
* @param stdClass $active_ctx the active context to use. }
* @param mixed $active_property the property for the element, null for none.
* @param mixed $element the element to expand. // recursively expand object:
* @param assoc $options the expansion options.
* @param bool $inside_list true if the property is a list, false if not. // if element has a context, process it
* if(property_exists($element, '@context')) {
* @return mixed the expanded value. $active_ctx = $this->_processContext(
*/ $active_ctx, $element->{'@context'}, $options);
protected function _expand( }
$active_ctx, $active_property, $element, $options, $inside_list)
{ // expand the active property
// nothing to expand $expanded_active_property = $this->_expandIri(
if ($element === null) { $active_ctx, $active_property, array('vocab' => true));
return $element;
} $rval = new stdClass();
$keys = array_keys((array)$element);
// recursively expand array sort($keys);
if (is_array($element)) { foreach($keys as $key) {
$rval = []; $value = $element->{$key};
$container = self::getContextValue(
$active_ctx, $active_property, '@container'); if($key === '@context') {
$inside_list = $inside_list || $container === '@list'; continue;
foreach ($element as $e) { }
// expand element
$e = $this->_expand( // expand key to IRI
$active_ctx, $active_property, $e, $options, $inside_list); $expanded_property = $this->_expandIri(
if ($inside_list && (is_array($e) || self::_isList($e))) { $active_ctx, $key, array('vocab' => true));
// lists of lists are illegal
throw new JsonLdException( // drop non-absolute IRI keys that aren't keywords
'Invalid JSON-LD syntax; lists of lists are not permitted.', 'jsonld.SyntaxError', 'list of lists'); if($expanded_property === null ||
} !(self::_isAbsoluteIri($expanded_property) ||
// drop null values self::_isKeyword($expanded_property))) {
if ($e !== null) { continue;
if (is_array($e)) { }
$rval = array_merge($rval, $e);
} else { if(self::_isKeyword($expanded_property)) {
$rval[] = $e; if($expanded_active_property === '@reverse') {
} throw new JsonLdException(
} 'Invalid JSON-LD syntax; a keyword cannot be used as a @reverse ' .
} 'property.', 'jsonld.SyntaxError', 'invalid reverse property map',
return $rval; array('value' => $value));
} }
if(property_exists($rval, $expanded_property)) {
if (!is_object($element)) { throw new JsonLdException(
// drop free-floating scalars that are not in lists 'Invalid JSON-LD syntax; colliding keywords detected.',
if (!$inside_list && 'jsonld.SyntaxError', 'colliding keywords',
($active_property === null || array('keyword' => $expanded_property));
$this->_expandIri($active_ctx, $active_property, ['vocab' => true]) === '@graph')) { }
return null; }
}
// syntax error if @id is not a string
// expand element according to value expansion rules if($expanded_property === '@id' && !is_string($value)) {
return $this->_expandValue($active_ctx, $active_property, $element); if(!isset($options['isFrame']) || !$options['isFrame']) {
} throw new JsonLdException(
'Invalid JSON-LD syntax; "@id" value must a string.',
// recursively expand object: 'jsonld.SyntaxError', 'invalid @id value',
// if element has a context, process it array('value' => $value));
if (property_exists($element, '@context')) { }
$active_ctx = $this->_processContext( if(!is_object($value)) {
$active_ctx, $element->{'@context'}, $options); throw new JsonLdException(
} 'Invalid JSON-LD syntax; "@id" value must a string or an object.',
'jsonld.SyntaxError', 'invalid @id value',
// expand the active property array('value' => $value));
$expanded_active_property = $this->_expandIri( }
$active_ctx, $active_property, ['vocab' => true]); }
$rval = new stdClass(); // validate @type value
$keys = array_keys((array) $element); if($expanded_property === '@type') {
sort($keys); $this->_validateTypeValue($value);
foreach ($keys as $key) { }
$value = $element->{$key};
// @graph must be an array or an object
if ($key === '@context') { if($expanded_property === '@graph' &&
continue; !(is_object($value) || is_array($value))) {
} throw new JsonLdException(
'Invalid JSON-LD syntax; "@graph" value must not be an ' .
// expand key to IRI 'object or an array.', 'jsonld.SyntaxError',
$expanded_property = $this->_expandIri( 'invalid @graph value', array('value' => $value));
$active_ctx, $key, ['vocab' => true]); }
// drop non-absolute IRI keys that aren't keywords // @value must not be an object or an array
if ($expanded_property === null || if($expanded_property === '@value' &&
!(self::_isAbsoluteIri($expanded_property) || (is_object($value) || is_array($value))) {
self::_isKeyword($expanded_property))) { throw new JsonLdException(
continue; 'Invalid JSON-LD syntax; "@value" value must not be an ' .
} 'object or an array.', 'jsonld.SyntaxError',
'invalid value object value', array('value' => $value));
if (self::_isKeyword($expanded_property)) { }
if ($expanded_active_property === '@reverse') {
throw new JsonLdException( // @language must be a string
'Invalid JSON-LD syntax; a keyword cannot be used as a @reverse ' . if($expanded_property === '@language') {
'property.', 'jsonld.SyntaxError', 'invalid reverse property map', ['value' => $value]); if(!is_string($value)) {
} throw new JsonLdException(
if (property_exists($rval, $expanded_property)) { 'Invalid JSON-LD syntax; "@language" value must not be a string.',
throw new JsonLdException( 'jsonld.SyntaxError', 'invalid language-tagged string',
'Invalid JSON-LD syntax; colliding keywords detected.', 'jsonld.SyntaxError', 'colliding keywords', ['keyword' => $expanded_property]); array('value' => $value));
} }
} // ensure language value is lowercase
$value = strtolower($value);
// syntax error if @id is not a string }
if ($expanded_property === '@id' && !is_string($value)) {
if (!isset($options['isFrame']) || !$options['isFrame']) { // @index must be a string
throw new JsonLdException( if($expanded_property === '@index') {
'Invalid JSON-LD syntax; "@id" value must a string.', 'jsonld.SyntaxError', 'invalid @id value', ['value' => $value]); if(!is_string($value)) {
} throw new JsonLdException(
if (!is_object($value)) { 'Invalid JSON-LD syntax; "@index" value must be a string.',
throw new JsonLdException( 'jsonld.SyntaxError', 'invalid @index value',
'Invalid JSON-LD syntax; "@id" value must a string or an object.', 'jsonld.SyntaxError', 'invalid @id value', ['value' => $value]); array('value' => $value));
} }
} }
// validate @type value // @reverse must be an object
if ($expanded_property === '@type') { if($expanded_property === '@reverse') {
$this->_validateTypeValue($value); if(!is_object($value)) {
} throw new JsonLdException(
'Invalid JSON-LD syntax; "@reverse" value must be an object.',
// @graph must be an array or an object 'jsonld.SyntaxError', 'invalid @reverse value',
if ($expanded_property === '@graph' && array('value' => $value));
!(is_object($value) || is_array($value))) { }
throw new JsonLdException(
'Invalid JSON-LD syntax; "@graph" value must not be an ' . $expanded_value = $this->_expand(
'object or an array.', 'jsonld.SyntaxError', 'invalid @graph value', ['value' => $value]); $active_ctx, '@reverse', $value, $options, $inside_list);
}
// properties double-reversed
// @value must not be an object or an array if(property_exists($expanded_value, '@reverse')) {
if ($expanded_property === '@value' && foreach($expanded_value->{'@reverse'} as $rproperty => $rvalue) {
(is_object($value) || is_array($value))) { self::addValue(
throw new JsonLdException( $rval, $rproperty, $rvalue, array('propertyIsArray' => true));
'Invalid JSON-LD syntax; "@value" value must not be an ' . }
'object or an array.', 'jsonld.SyntaxError', 'invalid value object value', ['value' => $value]); }
}
// FIXME: can this be merged with code below to simplify?
// @language must be a string // merge in all reversed properties
if ($expanded_property === '@language') { if(property_exists($rval, '@reverse')) {
if ($value === null) { $reverse_map = $rval->{'@reverse'};
// drop null @language values, they expand as if they didn't exist } else {
continue; $reverse_map = null;
} }
if (!is_string($value)) { foreach($expanded_value as $property => $items) {
throw new JsonLdException( if($property === '@reverse') {
'Invalid JSON-LD syntax; "@language" value must not be a string.', 'jsonld.SyntaxError', 'invalid language-tagged string', ['value' => $value]); continue;
} }
// ensure language value is lowercase if($reverse_map === null) {
$value = strtolower($value); $reverse_map = $rval->{'@reverse'} = new stdClass();
} }
self::addValue(
// @index must be a string $reverse_map, $property, array(),
if ($expanded_property === '@index') { array('propertyIsArray' => true));
if (!is_string($value)) { foreach($items as $item) {
throw new JsonLdException( if(self::_isValue($item) || self::_isList($item)) {
'Invalid JSON-LD syntax; "@index" value must be a string.', 'jsonld.SyntaxError', 'invalid @index value', ['value' => $value]); throw new JsonLdException(
} 'Invalid JSON-LD syntax; "@reverse" value must not be a ' +
} '@value or an @list.', 'jsonld.SyntaxError',
'invalid reverse property value',
// @reverse must be an object array('value' => $expanded_value));
if ($expanded_property === '@reverse') { }
if (!is_object($value)) { self::addValue(
throw new JsonLdException( $reverse_map, $property, $item,
'Invalid JSON-LD syntax; "@reverse" value must be an object.', 'jsonld.SyntaxError', 'invalid @reverse value', ['value' => $value]); array('propertyIsArray' => true));
} }
}
$expanded_value = $this->_expand(
$active_ctx, '@reverse', $value, $options, $inside_list); continue;
}
// properties double-reversed
if (property_exists($expanded_value, '@reverse')) { $container = self::getContextValue($active_ctx, $key, '@container');
foreach ($expanded_value->{'@reverse'} as $rproperty => $rvalue) {
self::addValue( if($container === '@language' && is_object($value)) {
$rval, $rproperty, $rvalue, ['propertyIsArray' => true]); // handle language map container (skip if value is not an object)
} $expanded_value = $this->_expandLanguageMap($value);
} } else if($container === '@index' && is_object($value)) {
// handle index container (skip if value is not an object)
// FIXME: can this be merged with code below to simplify? $expanded_value = array();
// merge in all reversed properties $value_keys = array_keys((array)$value);
if (property_exists($rval, '@reverse')) { sort($value_keys);
$reverse_map = $rval->{'@reverse'}; foreach($value_keys as $value_key) {
} else { $val = $value->{$value_key};
$reverse_map = null; $val = self::arrayify($val);
} $val = $this->_expand($active_ctx, $key, $val, $options, false);
foreach ($expanded_value as $property => $items) { foreach($val as $item) {
if ($property === '@reverse') { if(!property_exists($item, '@index')) {
continue; $item->{'@index'} = $value_key;
} }
if ($reverse_map === null) { $expanded_value[] = $item;
$reverse_map = $rval->{'@reverse'} = new stdClass(); }
} }
self::addValue( } else {
$reverse_map, $property, [], ['propertyIsArray' => true]); // recurse into @list or @set
foreach ($items as $item) { $is_list = ($expanded_property === '@list');
if (self::_isValue($item) || self::_isList($item)) { if($is_list || $expanded_property === '@set') {
throw new JsonLdException( $next_active_property = $active_property;
'Invalid JSON-LD syntax; "@reverse" value must not be a ' + if($is_list && $expanded_active_property === '@graph') {
'@value or an @list.', 'jsonld.SyntaxError', 'invalid reverse property value', ['value' => $expanded_value]); $next_active_property = null;
} }
self::addValue( $expanded_value = $this->_expand(
$reverse_map, $property, $item, ['propertyIsArray' => true]); $active_ctx, $next_active_property, $value, $options, $is_list);
} if($is_list && self::_isList($expanded_value)) {
} throw new JsonLdException(
'Invalid JSON-LD syntax; lists of lists are not permitted.',
continue; 'jsonld.SyntaxError', 'list of lists');
} }
} else {
$container = self::getContextValue($active_ctx, $key, '@container'); // recursively expand value with key as new active property
$expanded_value = $this->_expand(
if ($container === '@language' && is_object($value)) { $active_ctx, $key, $value, $options, false);
// handle language map container (skip if value is not an object) }
$expanded_value = $this->_expandLanguageMap($value); }
} else if ($container === '@index' && is_object($value)) {
// handle index container (skip if value is not an object) // drop null values if property is not @value
$expanded_value = []; if($expanded_value === null && $expanded_property !== '@value') {
$value_keys = array_keys((array) $value); continue;
sort($value_keys); }
foreach ($value_keys as $value_key) {
$val = $value->{$value_key}; // convert expanded value to @list if container specifies it
$val = self::arrayify($val); if($expanded_property !== '@list' && !self::_isList($expanded_value) &&
$val = $this->_expand($active_ctx, $key, $val, $options, false); $container === '@list') {
foreach ($val as $item) { // ensure expanded value is an array
if (!property_exists($item, '@index')) { $expanded_value = (object)array(
$item->{'@index'} = $value_key; '@list' => self::arrayify($expanded_value));
} }
$expanded_value[] = $item;
} // FIXME: can this be merged with code above to simplify?
} // merge in reverse properties
} else { if(property_exists($active_ctx->mappings, $key) &&
// recurse into @list or @set $active_ctx->mappings->{$key} &&
$is_list = ($expanded_property === '@list'); $active_ctx->mappings->{$key}->reverse) {
if ($is_list || $expanded_property === '@set') { if(property_exists($rval, '@reverse')) {
$next_active_property = $active_property; $reverse_map = $rval->{'@reverse'};
if ($is_list && $expanded_active_property === '@graph') { } else {
$next_active_property = null; $reverse_map = $rval->{'@reverse'} = new stdClass();
} }
$expanded_value = $this->_expand( $expanded_value = self::arrayify($expanded_value);
$active_ctx, $next_active_property, $value, $options, $is_list); foreach($expanded_value as $item) {
if ($is_list && self::_isList($expanded_value)) { if(self::_isValue($item) || self::_isList($item)) {
throw new JsonLdException( throw new JsonLdException(
'Invalid JSON-LD syntax; lists of lists are not permitted.', 'jsonld.SyntaxError', 'list of lists'); 'Invalid JSON-LD syntax; "@reverse" value must not be a ' +
} '@value or an @list.', 'jsonld.SyntaxError',
} else { 'invalid reverse property value',
// recursively expand value with key as new active property array('value' => $expanded_value));
$expanded_value = $this->_expand( }
$active_ctx, $key, $value, $options, false); self::addValue(
} $reverse_map, $expanded_property, $item,
} array('propertyIsArray' => true));
}
// drop null values if property is not @value continue;
if ($expanded_value === null && $expanded_property !== '@value') { }
continue;
} // add value for property
// use an array except for certain keywords
// convert expanded value to @list if container specifies it $use_array = (!in_array(
if ($expanded_property !== '@list' && !self::_isList($expanded_value) && $expanded_property, array(
$container === '@list') { '@index', '@id', '@type', '@value', '@language')));
// ensure expanded value is an array self::addValue(
$expanded_value = (object) [ $rval, $expanded_property, $expanded_value,
'@list' => self::arrayify($expanded_value)]; array('propertyIsArray' => $use_array));
} }
// FIXME: can this be merged with code above to simplify? // get property count on expanded output
// merge in reverse properties $keys = array_keys((array)$rval);
if (property_exists($active_ctx->mappings, $key) && $count = count($keys);
$active_ctx->mappings->{$key} &&
$active_ctx->mappings->{$key}->reverse) { // @value must only have @language or @type
if (property_exists($rval, '@reverse')) { if(property_exists($rval, '@value')) {
$reverse_map = $rval->{'@reverse'}; // @value must only have @language or @type
} else { if(property_exists($rval, '@type') &&
$reverse_map = $rval->{'@reverse'} = new stdClass(); property_exists($rval, '@language')) {
} throw new JsonLdException(
$expanded_value = self::arrayify($expanded_value); 'Invalid JSON-LD syntax; an element containing "@value" may not ' .
foreach ($expanded_value as $item) { 'contain both "@type" and "@language".',
if (self::_isValue($item) || self::_isList($item)) { 'jsonld.SyntaxError', 'invalid value object',
throw new JsonLdException( array('element' => $rval));
'Invalid JSON-LD syntax; "@reverse" value must not be a ' + }
'@value or an @list.', 'jsonld.SyntaxError', 'invalid reverse property value', ['value' => $expanded_value]); $valid_count = $count - 1;
} if(property_exists($rval, '@type')) {
self::addValue( $valid_count -= 1;
$reverse_map, $expanded_property, $item, ['propertyIsArray' => true]); }
} if(property_exists($rval, '@index')) {
continue; $valid_count -= 1;
} }
if(property_exists($rval, '@language')) {
// add value for property $valid_count -= 1;
// use an array except for certain keywords }
$use_array = (!in_array( if($valid_count !== 0) {
$expanded_property, [ throw new JsonLdException(
'@index', '@id', '@type', '@value', '@language'])); 'Invalid JSON-LD syntax; an element containing "@value" may only ' .
self::addValue( 'have an "@index" property and at most one other property ' .
$rval, $expanded_property, $expanded_value, ['propertyIsArray' => $use_array]); 'which can be "@type" or "@language".',
} 'jsonld.SyntaxError', 'invalid value object',
array('element' => $rval));
// get property count on expanded output }
$keys = array_keys((array) $rval); // drop null @values
$count = count($keys); if($rval->{'@value'} === null) {
$rval = null;
// @value must only have @language or @type } else if(property_exists($rval, '@language') &&
if (property_exists($rval, '@value')) { !is_string($rval->{'@value'})) {
// @value must only have @language or @type // if @language is present, @value must be a string
if (property_exists($rval, '@type') && throw new JsonLdException(
property_exists($rval, '@language')) { 'Invalid JSON-LD syntax; only strings may be language-tagged.',
throw new JsonLdException( 'jsonld.SyntaxError', 'invalid language-tagged value',
'Invalid JSON-LD syntax; an element containing "@value" may not ' . array('element' => $rval));
'contain both "@type" and "@language".', 'jsonld.SyntaxError', 'invalid value object', ['element' => $rval]); } else if(property_exists($rval, '@type') &&
} (!self::_isAbsoluteIri($rval->{'@type'}) ||
$valid_count = $count - 1; strpos($rval->{'@type'}, '_:') === 0)) {
if (property_exists($rval, '@type')) { throw new JsonLdException(
$valid_count -= 1; 'Invalid JSON-LD syntax; an element containing "@value" ' .
} 'and "@type" must have an absolute IRI for the value ' .
if (property_exists($rval, '@index')) { 'of "@type".', 'jsonld.SyntaxError', 'invalid typed value',
$valid_count -= 1; array('element' => $rval));
} }
if (property_exists($rval, '@language')) { } else if(property_exists($rval, '@type') && !is_array($rval->{'@type'})) {
$valid_count -= 1; // convert @type to an array
} $rval->{'@type'} = array($rval->{'@type'});
if ($valid_count !== 0) { } else if(property_exists($rval, '@set') ||
throw new JsonLdException( property_exists($rval, '@list')) {
'Invalid JSON-LD syntax; an element containing "@value" may only ' . // handle @set and @list
'have an "@index" property and at most one other property ' . if($count > 1 && !($count === 2 && property_exists($rval, '@index'))) {
'which can be "@type" or "@language".', 'jsonld.SyntaxError', 'invalid value object', ['element' => $rval]); throw new JsonLdException(
} 'Invalid JSON-LD syntax; if an element has the property "@set" ' .
// drop null @values 'or "@list", then it can have at most one other property that is ' .
if ($rval->{'@value'} === null) { '"@index".', 'jsonld.SyntaxError', 'invalid set or list object',
$rval = null; array('element' => $rval));
} else if (property_exists($rval, '@language') && }
!is_string($rval->{'@value'})) { // optimize away @set
// if @language is present, @value must be a string if(property_exists($rval, '@set')) {
throw new JsonLdException( $rval = $rval->{'@set'};
'Invalid JSON-LD syntax; only strings may be language-tagged.', 'jsonld.SyntaxError', 'invalid language-tagged value', ['element' => $rval]); $keys = array_keys((array)$rval);
} else if (property_exists($rval, '@type') && $count = count($keys);
(!self::_isAbsoluteIri($rval->{'@type'}) || }
strpos($rval->{'@type'}, '_:') === 0)) { } else if($count === 1 && property_exists($rval, '@language')) {
throw new JsonLdException( // drop objects with only @language
'Invalid JSON-LD syntax; an element containing "@value" ' . $rval = null;
'and "@type" must have an absolute IRI for the value ' . }
'of "@type".', 'jsonld.SyntaxError', 'invalid typed value', ['element' => $rval]);
} // drop certain top-level objects that do not occur in lists
} else if (property_exists($rval, '@type') && !is_array($rval->{'@type'})) { if(is_object($rval) &&
// convert @type to an array !$options['keepFreeFloatingNodes'] && !$inside_list &&
$rval->{'@type'} = [$rval->{'@type'}]; ($active_property === null || $expanded_active_property === '@graph')) {
} else if (property_exists($rval, '@set') || // drop empty object or top-level @value/@list, or object with only @id
property_exists($rval, '@list')) { if($count === 0 || property_exists($rval, '@value') ||
// handle @set and @list property_exists($rval, '@list') ||
if ($count > 1 && !($count === 2 && property_exists($rval, '@index'))) { ($count === 1 && property_exists($rval, '@id'))) {
throw new JsonLdException( $rval = null;
'Invalid JSON-LD syntax; if an element has the property "@set" ' . }
'or "@list", then it can have at most one other property that is ' . }
'"@index".', 'jsonld.SyntaxError', 'invalid set or list object', ['element' => $rval]);
} return $rval;
// optimize away @set }
if (property_exists($rval, '@set')) {
$rval = $rval->{'@set'}; /**
$keys = array_keys((array) $rval); * Performs JSON-LD flattening.
$count = count($keys); *
} * @param array $input the expanded JSON-LD to flatten.
} else if ($count === 1 && property_exists($rval, '@language')) { *
// drop objects with only @language * @return array the flattened output.
$rval = null; */
} protected function _flatten($input) {
// produce a map of all subjects and name each bnode
// drop certain top-level objects that do not occur in lists $namer = new UniqueNamer('_:b');
if (is_object($rval) && $graphs = (object)array('@default' => new stdClass());
!$options['keepFreeFloatingNodes'] && !$inside_list && $this->_createNodeMap($input, $graphs, '@default', $namer);
($active_property === null || $expanded_active_property === '@graph')) {
// drop empty object or top-level @value/@list, or object with only @id // add all non-default graphs to default graph
if ($count === 0 || property_exists($rval, '@value') || $default_graph = $graphs->{'@default'};
property_exists($rval, '@list') || $graph_names = array_keys((array)$graphs);
($count === 1 && property_exists($rval, '@id'))) { foreach($graph_names as $graph_name) {
$rval = null; if($graph_name === '@default') {
} continue;
} }
$node_map = $graphs->{$graph_name};
return $rval; if(!property_exists($default_graph, $graph_name)) {
} $default_graph->{$graph_name} = (object)array(
'@id' => $graph_name, '@graph' => array());
/** }
* Performs JSON-LD flattening. $subject = $default_graph->{$graph_name};
* if(!property_exists($subject, '@graph')) {
* @param array $input the expanded JSON-LD to flatten. $subject->{'@graph'} = array();
* }
* @return array the flattened output. $ids = array_keys((array)$node_map);
*/ sort($ids);
protected function _flatten($input) foreach($ids as $id) {
{ $node = $node_map->{$id};
// produce a map of all subjects and name each bnode // only add full subjects
$namer = new UniqueNamer('_:b'); if(!self::_isSubjectReference($node)) {
$graphs = (object) ['@default' => new stdClass()]; $subject->{'@graph'}[] = $node;
$this->_createNodeMap($input, $graphs, '@default', $namer); }
}
// add all non-default graphs to default graph }
$default_graph = $graphs->{'@default'};
$graph_names = array_keys((array) $graphs); // produce flattened output
foreach ($graph_names as $graph_name) { $flattened = array();
if ($graph_name === '@default') { $keys = array_keys((array)$default_graph);
continue; sort($keys);
} foreach($keys as $key) {
$node_map = $graphs->{$graph_name}; $node = $default_graph->{$key};
if (!property_exists($default_graph, $graph_name)) { // only add full subjects to top-level
$default_graph->{$graph_name} = (object) [ if(!self::_isSubjectReference($node)) {
'@id' => $graph_name, '@graph' => []]; $flattened[] = $node;
} }
$subject = $default_graph->{$graph_name}; }
if (!property_exists($subject, '@graph')) { return $flattened;
$subject->{'@graph'} = []; }
}
$ids = array_keys((array) $node_map); /**
sort($ids); * Performs JSON-LD framing.
foreach ($ids as $id) { *
$node = $node_map->{$id}; * @param array $input the expanded JSON-LD to frame.
// only add full subjects * @param array $frame the expanded JSON-LD frame to use.
if (!self::_isSubjectReference($node)) { * @param assoc $options the framing options.
$subject->{'@graph'}[] = $node; *
} * @return array the framed output.
} */
} protected function _frame($input, $frame, $options) {
// create framing state
// produce flattened output $state = (object)array(
$flattened = []; 'options' => $options,
$keys = array_keys((array) $default_graph); 'graphs' => (object)array(
sort($keys); '@default' => new stdClass(),
foreach ($keys as $key) { '@merged' => new stdClass()));
$node = $default_graph->{$key};
// only add full subjects to top-level // produce a map of all graphs and name each bnode
if (!self::_isSubjectReference($node)) { // FIXME: currently uses subjects from @merged graph only
$flattened[] = $node; $namer = new UniqueNamer('_:b');
} $this->_createNodeMap($input, $state->graphs, '@merged', $namer);
} $state->subjects = $state->graphs->{'@merged'};
return $flattened;
} // frame the subjects
$framed = new ArrayObject();
/** $keys = array_keys((array)$state->subjects);
* Performs JSON-LD framing. sort($keys);
* $this->_matchFrame($state, $keys, $frame, $framed, null);
* @param array $input the expanded JSON-LD to frame. return (array)$framed;
* @param array $frame the expanded JSON-LD frame to use. }
* @param assoc $options the framing options.
* /**
* @return array the framed output. * Performs normalization on the given RDF dataset.
*/ *
protected function _frame($input, $frame, $options) * @param stdClass $dataset the RDF dataset to normalize.
{ * @param assoc $options the normalization options.
// create framing state *
$state = (object) [ * @return mixed the normalized output.
'options' => $options, */
'graphs' => (object) [ protected function _normalize($dataset, $options) {
'@default' => new stdClass(), // create quads and map bnodes to their associated quads
'@merged' => new stdClass()], $quads = array();
'subjectStack' => [], $bnodes = new stdClass();
'link' => new stdClass()]; foreach($dataset as $graph_name => $triples) {
if($graph_name === '@default') {
// produce a map of all graphs and name each bnode $graph_name = null;
// FIXME: currently uses subjects from @merged graph only }
$namer = new UniqueNamer('_:b'); foreach($triples as $triple) {
$this->_createNodeMap($input, $state->graphs, '@merged', $namer); $quad = $triple;
$state->subjects = $state->graphs->{'@merged'}; if($graph_name !== null) {
if(strpos($graph_name, '_:') === 0) {
// frame the subjects $quad->name = (object)array(
$framed = new ArrayObject(); 'type' => 'blank node', 'value' => $graph_name);
$keys = array_keys((array) $state->subjects); } else {
sort($keys); $quad->name = (object)array(
$this->_matchFrame($state, $keys, $frame, $framed, null); 'type' => 'IRI', 'value' => $graph_name);
return (array) $framed; }
} }
$quads[] = $quad;
/**
* Performs normalization on the given RDF dataset. foreach(array('subject', 'object', 'name') as $attr) {
* if(property_exists($quad, $attr) &&
* @param stdClass $dataset the RDF dataset to normalize. $quad->{$attr}->type === 'blank node') {
* @param assoc $options the normalization options. $id = $quad->{$attr}->value;
* if(property_exists($bnodes, $id)) {
* @return mixed the normalized output. $bnodes->{$id}->quads[] = $quad;
*/ } else {
protected function _normalize($dataset, $options) $bnodes->{$id} = (object)array('quads' => array($quad));
{ }
// create quads and map bnodes to their associated quads }
$quads = []; }
$bnodes = new stdClass(); }
foreach ($dataset as $graph_name => $triples) { }
if ($graph_name === '@default') {
$graph_name = null; // mapping complete, start canonical naming
} $namer = new UniqueNamer('_:c14n');
foreach ($triples as $triple) {
$quad = $triple; // continue to hash bnode quads while bnodes are assigned names
if ($graph_name !== null) { $unnamed = null;
if (strpos($graph_name, '_:') === 0) { $nextUnnamed = array_keys((array)$bnodes);
$quad->name = (object) [ $duplicates = null;
'type' => 'blank node', 'value' => $graph_name]; do {
} else { $unnamed = $nextUnnamed;
$quad->name = (object) [ $nextUnnamed = array();
'type' => 'IRI', 'value' => $graph_name]; $duplicates = new stdClass();
} $unique = new stdClass();
} foreach($unnamed as $bnode) {
$quads[] = $quad; // hash quads for each unnamed bnode
$hash = $this->_hashQuads($bnode, $bnodes, $namer);
foreach (['subject', 'object', 'name'] as $attr) {
if (property_exists($quad, $attr) && // store hash as unique or a duplicate
$quad->{$attr}->type === 'blank node') { if(property_exists($duplicates, $hash)) {
$id = $quad->{$attr}->value; $duplicates->{$hash}[] = $bnode;
if (property_exists($bnodes, $id)) { $nextUnnamed[] = $bnode;
$bnodes->{$id}->quads[] = $quad; } else if(property_exists($unique, $hash)) {
} else { $duplicates->{$hash} = array($unique->{$hash}, $bnode);
$bnodes->{$id} = (object) ['quads' => [$quad]]; $nextUnnamed[] = $unique->{$hash};
} $nextUnnamed[] = $bnode;
} unset($unique->{$hash});
} } else {
} $unique->{$hash} = $bnode;
} }
}
// mapping complete, start canonical naming
$namer = new UniqueNamer('_:c14n'); // name unique bnodes in sorted hash order
$hashes = array_keys((array)$unique);
// continue to hash bnode quads while bnodes are assigned names sort($hashes);
$unnamed = null; foreach($hashes as $hash) {
$nextUnnamed = array_keys((array) $bnodes); $namer->getName($unique->{$hash});
$duplicates = null; }
do { }
$unnamed = $nextUnnamed; while(count($unnamed) > count($nextUnnamed));
$nextUnnamed = [];
$duplicates = new stdClass(); // enumerate duplicate hash groups in sorted order
$unique = new stdClass(); $hashes = array_keys((array)$duplicates);
foreach ($unnamed as $bnode) { sort($hashes);
// hash quads for each unnamed bnode foreach($hashes as $hash) {
$hash = $this->_hashQuads($bnode, $bnodes, $namer); // process group
$group = $duplicates->{$hash};
// store hash as unique or a duplicate $results = array();
if (property_exists($duplicates, $hash)) { foreach($group as $bnode) {
$duplicates->{$hash}[] = $bnode; // skip already-named bnodes
$nextUnnamed[] = $bnode; if($namer->isNamed($bnode)) {
} else if (property_exists($unique, $hash)) { continue;
$duplicates->{$hash} = [$unique->{$hash}, $bnode]; }
$nextUnnamed[] = $unique->{$hash};
$nextUnnamed[] = $bnode; // hash bnode paths
unset($unique->{$hash}); $path_namer = new UniqueNamer('_:b');
} else { $path_namer->getName($bnode);
$unique->{$hash} = $bnode; $results[] = $this->_hashPaths($bnode, $bnodes, $namer, $path_namer);
} }
}
// name bnodes in hash order
// name unique bnodes in sorted hash order usort($results, function($a, $b) {
$hashes = array_keys((array) $unique); $a = $a->hash;
sort($hashes); $b = $b->hash;
foreach ($hashes as $hash) { return ($a < $b) ? -1 : (($a > $b) ? 1 : 0);
$namer->getName($unique->{$hash}); });
} foreach($results as $result) {
} while (count($unnamed) > count($nextUnnamed)); // name all bnodes in path namer in key-entry order
foreach($result->pathNamer->order as $bnode) {
// enumerate duplicate hash groups in sorted order $namer->getName($bnode);
$hashes = array_keys((array) $duplicates); }
sort($hashes); }
foreach ($hashes as $hash) { }
// process group
$group = $duplicates->{$hash}; // create normalized array
$results = []; $normalized = array();
foreach ($group as $bnode) {
// skip already-named bnodes /* Note: At this point all bnodes in the set of RDF quads have been
if ($namer->isNamed($bnode)) { assigned canonical names, which have been stored in the 'namer' object.
continue; Here each quad is updated by assigning each of its bnodes its new name
} via the 'namer' object. */
// hash bnode paths // update bnode names in each quad and serialize
$path_namer = new UniqueNamer('_:b'); foreach($quads as $quad) {
$path_namer->getName($bnode); foreach(array('subject', 'object', 'name') as $attr) {
$results[] = $this->_hashPaths($bnode, $bnodes, $namer, $path_namer); if(property_exists($quad, $attr) &&
} $quad->{$attr}->type === 'blank node' &&
strpos($quad->{$attr}->value, '_:c14n') !== 0) {
// name bnodes in hash order $quad->{$attr}->value = $namer->getName($quad->{$attr}->value);
usort($results, function($a, $b) { }
$a = $a->hash; }
$b = $b->hash; $normalized[] = $this->toNQuad($quad, property_exists($quad, 'name') ?
return ($a < $b) ? -1 : (($a > $b) ? 1 : 0); $quad->name->value : null);
}); }
foreach ($results as $result) {
// name all bnodes in path namer in key-entry order // sort normalized output
foreach ($result->pathNamer->order as $bnode) { sort($normalized);
$namer->getName($bnode);
} // handle output format
} if(isset($options['format']) && $options['format']) {
} if($options['format'] === 'application/nquads') {
return implode($normalized);
// create normalized array }
$normalized = []; throw new JsonLdException(
'Unknown output format.',
/* Note: At this point all bnodes in the set of RDF quads have been 'jsonld.UnknownFormat', null, array('format' => $options['format']));
assigned canonical names, which have been stored in the 'namer' object. }
Here each quad is updated by assigning each of its bnodes its new name
via the 'namer' object. */ // return RDF dataset
return $this->parseNQuads(implode($normalized));
// update bnode names in each quad and serialize }
foreach ($quads as $quad) {
foreach (['subject', 'object', 'name'] as $attr) { /**
if (property_exists($quad, $attr) && * Converts an RDF dataset to JSON-LD.
$quad->{$attr}->type === 'blank node' && *
strpos($quad->{$attr}->value, '_:c14n') !== 0) { * @param stdClass $dataset the RDF dataset.
$quad->{$attr}->value = $namer->getName($quad->{$attr}->value); * @param assoc $options the RDF serialization options.
} *
} * @return array the JSON-LD output.
$normalized[] = $this->toNQuad($quad, property_exists($quad, 'name') ? */
$quad->name->value : null); protected function _fromRDF($dataset, $options) {
} $default_graph = new stdClass();
$graph_map = (object)array('@default' => $default_graph);
// sort normalized output // TODO: seems like usages could be replaced by this single map
sort($normalized); $node_references = (object)array();
// handle output format foreach($dataset as $name => $graph) {
if (isset($options['format']) && $options['format']) { if(!property_exists($graph_map, $name)) {
if ($options['format'] === 'application/nquads') { $graph_map->{$name} = new stdClass();
return implode($normalized); }
} if($name !== '@default' && !property_exists($default_graph, $name)) {
throw new JsonLdException( $default_graph->{$name} = (object)array('@id' => $name);
'Unknown output format.', 'jsonld.UnknownFormat', null, ['format' => $options['format']]); }
} $node_map = $graph_map->{$name};
foreach($graph as $triple) {
// return RDF dataset // get subject, predicate, object
return $this->parseNQuads(implode($normalized)); $s = $triple->subject->value;
} $p = $triple->predicate->value;
$o = $triple->object;
/**
* Converts an RDF dataset to JSON-LD. if(!property_exists($node_map, $s)) {
* $node_map->{$s} = (object)array('@id' => $s);
* @param stdClass $dataset the RDF dataset. }
* @param assoc $options the RDF serialization options. $node = $node_map->{$s};
*
* @return array the JSON-LD output. $object_is_id = ($o->type === 'IRI' || $o->type === 'blank node');
*/ if($object_is_id && !property_exists($node_map, $o->value)) {
protected function _fromRDF($dataset, $options) $node_map->{$o->value} = (object)array('@id' => $o->value);
{ }
$default_graph = new stdClass();
$graph_map = (object) ['@default' => $default_graph]; if($p === self::RDF_TYPE && !$options['useRdfType'] && $object_is_id) {
$referenced_once = (object) []; self::addValue(
$node, '@type', $o->value, array('propertyIsArray' => true));
foreach ($dataset as $name => $graph) { continue;
if (!property_exists($graph_map, $name)) { }
$graph_map->{$name} = new stdClass();
} $value = self::_RDFToObject($o, $options['useNativeTypes']);
if ($name !== '@default' && !property_exists($default_graph, $name)) { self::addValue($node, $p, $value, array('propertyIsArray' => true));
$default_graph->{$name} = (object) ['@id' => $name];
} // object may be an RDF list/partial list node but we can't know
$node_map = $graph_map->{$name}; // easily until all triples are read
foreach ($graph as $triple) { if($object_is_id) {
// get subject, predicate, object self::addValue(
$s = $triple->subject->value; $node_references, $o->value, $node->{'@id'}, array(
$p = $triple->predicate->value; 'propertyIsArray' => true));
$o = $triple->object; $object = $node_map->{$o->value};
if(!property_exists($object, 'usages')) {
if (!property_exists($node_map, $s)) { $object->usages = array();
$node_map->{$s} = (object) ['@id' => $s]; }
} $object->usages[] = (object)array(
$node = $node_map->{$s}; 'node' => $node,
'property' => $p,
$object_is_id = ($o->type === 'IRI' || $o->type === 'blank node'); 'value' => $value);
if ($object_is_id && !property_exists($node_map, $o->value)) { }
$node_map->{$o->value} = (object) ['@id' => $o->value]; }
} }
if ($p === self::RDF_TYPE && !$options['useRdfType'] && $object_is_id) { // convert linked lists to @list arrays
self::addValue( foreach($graph_map as $name => $graph_object) {
$node, '@type', $o->value, ['propertyIsArray' => true]); // no @lists to be converted, continue
continue; if(!property_exists($graph_object, self::RDF_NIL)) {
} continue;
}
$value = self::_RDFToObject($o, $options['useNativeTypes']);
self::addValue($node, $p, $value, ['propertyIsArray' => true]); // iterate backwards through each RDF list
$nil = $graph_object->{self::RDF_NIL};
// object may be an RDF list/partial list node but we can't know foreach($nil->usages as $usage) {
// easily until all triples are read $node = $usage->node;
if ($object_is_id) { $property = $usage->property;
if ($o->value === self::RDF_NIL) { $head = $usage->value;
$object = $node_map->{$o->value}; $list = array();
if (!property_exists($object, 'usages')) { $list_nodes = array();
$object->usages = [];
} // ensure node is a well-formed list node; it must:
$object->usages[] = (object) [ // 1. Be referenced only once and used only once in a list.
'node' => $node, // 2. Have an array for rdf:first that has 1 item.
'property' => $p, // 3. Have an array for rdf:rest that has 1 item.
'value' => $value]; // 4. Have no keys other than: @id, usages, rdf:first, rdf:rest, and,
} else if (property_exists($referenced_once, $o->value)) { // optionally, @type where the value is rdf:List.
// object referenced more than once $node_key_count = count(array_keys((array)$node));
$referenced_once->{$o->value} = false; while($property === self::RDF_REST &&
} else { property_exists($node_references, $node->{'@id'}) &&
// track single reference count($node_references->{$node->{'@id'}}) === 1 &&
$referenced_once->{$o->value} = (object) [ count($node->usages) === 1 &&
'node' => $node, property_exists($node, self::RDF_FIRST) &&
'property' => $p, property_exists($node, self::RDF_REST) &&
'value' => $value]; is_array($node->{self::RDF_FIRST}) &&
} is_array($node->{self::RDF_REST}) &&
} count($node->{self::RDF_FIRST}) === 1 &&
} count($node->{self::RDF_REST}) === 1 &&
} ($node_key_count === 4 || ($node_key_count === 5 &&
property_exists($node, '@type') && is_array($node->{'@type'}) &&
// convert linked lists to @list arrays count($node->{'@type'}) === 1 &&
foreach ($graph_map as $name => $graph_object) { $node->{'@type'}[0] === self::RDF_LIST))) {
// no @lists to be converted, continue $list[] = $node->{self::RDF_FIRST}[0];
if (!property_exists($graph_object, self::RDF_NIL)) { $list_nodes[] = $node->{'@id'};
continue;
} // get next node, moving backwards through list
$usage = $node->usages[0];
// iterate backwards through each RDF list $node = $usage->node;
$nil = $graph_object->{self::RDF_NIL}; $property = $usage->property;
foreach ($nil->usages as $usage) { $head = $usage->value;
$node = $usage->node; $node_key_count = count(array_keys((array)$node));
$property = $usage->property;
$head = $usage->value; // if node is not a blank node, then list head found
$list = []; if(strpos($node->{'@id'}, '_:') !== 0) {
$list_nodes = []; break;
}
// ensure node is a well-formed list node; it must: }
// 1. Be referenced only once.
// 2. Have an array for rdf:first that has 1 item. // list is nested in another list
// 3. Have an array for rdf:rest that has 1 item. if($property === self::RDF_FIRST) {
// 4. Have no keys other than: @id, rdf:first, rdf:rest, and, // empty list
// optionally, @type where the value is rdf:List. if($node->{'@id'} === self::RDF_NIL) {
$node_key_count = count(array_keys((array) $node)); // can't convert rdf:nil to a @list object because it would
while ($property === self::RDF_REST && // result in a list of lists which isn't supported
property_exists($referenced_once, $node->{'@id'}) && continue;
is_object($referenced_once->{$node->{'@id'}}) && }
property_exists($node, self::RDF_FIRST) &&
property_exists($node, self::RDF_REST) && // preserve list head
is_array($node->{self::RDF_FIRST}) && $head = $graph_object->{$head->{'@id'}}->{self::RDF_REST}[0];
is_array($node->{self::RDF_REST}) && array_pop($list);
count($node->{self::RDF_FIRST}) === 1 && array_pop($list_nodes);
count($node->{self::RDF_REST}) === 1 && }
($node_key_count === 3 || ($node_key_count === 4 &&
property_exists($node, '@type') && is_array($node->{'@type'}) && // transform list into @list object
count($node->{'@type'}) === 1 && unset($head->{'@id'});
$node->{'@type'}[0] === self::RDF_LIST))) { $head->{'@list'} = array_reverse($list);
$list[] = $node->{self::RDF_FIRST}[0]; foreach($list_nodes as $list_node) {
$list_nodes[] = $node->{'@id'}; unset($graph_object->{$list_node});
}
// get next node, moving backwards through list }
$usage = $referenced_once->{$node->{'@id'}}; }
$node = $usage->node;
$property = $usage->property; $result = array();
$head = $usage->value; $subjects = array_keys((array)$default_graph);
$node_key_count = count(array_keys((array) $node)); sort($subjects);
foreach($subjects as $subject) {
// if node is not a blank node, then list head found $node = $default_graph->{$subject};
if (strpos($node->{'@id'}, '_:') !== 0) { if(property_exists($graph_map, $subject)) {
break; $node->{'@graph'} = array();
} $graph_object = $graph_map->{$subject};
} $subjects_ = array_keys((array)$graph_object);
sort($subjects_);
// list is nested in another list foreach($subjects_ as $subject_) {
if ($property === self::RDF_FIRST) { $node_ = $graph_object->{$subject_};
// empty list unset($node_->usages);
if ($node->{'@id'} === self::RDF_NIL) { // only add full subjects to top-level
// can't convert rdf:nil to a @list object because it would if(!self::_isSubjectReference($node_)) {
// result in a list of lists which isn't supported $node->{'@graph'}[] = $node_;
continue; }
} }
}
// preserve list head unset($node->usages);
$head = $graph_object->{$head->{'@id'}}->{self::RDF_REST}[0]; // only add full subjects to top-level
array_pop($list); if(!self::_isSubjectReference($node)) {
array_pop($list_nodes); $result[] = $node;
} }
}
// transform list into @list object
unset($head->{'@id'}); return $result;
$head->{'@list'} = array_reverse($list); }
foreach ($list_nodes as $list_node) {
unset($graph_object->{$list_node}); /**
} * Processes a local context and returns a new active context.
} *
* @param stdClass $active_ctx the current active context.
unset($nil->usages); * @param mixed $local_ctx the local context to process.
} * @param assoc $options the context processing options.
*
$result = []; * @return stdClass the new active context.
$subjects = array_keys((array) $default_graph); */
sort($subjects); protected function _processContext($active_ctx, $local_ctx, $options) {
foreach ($subjects as $subject) { global $jsonld_cache;
$node = $default_graph->{$subject};
if (property_exists($graph_map, $subject)) { // normalize local context to an array
$node->{'@graph'} = []; if(is_object($local_ctx) && property_exists($local_ctx, '@context') &&
$graph_object = $graph_map->{$subject}; is_array($local_ctx->{'@context'})) {
$subjects_ = array_keys((array) $graph_object); $local_ctx = $local_ctx->{'@context'};
sort($subjects_); }
foreach ($subjects_ as $subject_) { $ctxs = self::arrayify($local_ctx);
$node_ = $graph_object->{$subject_};
// only add full subjects to top-level // no contexts in array, clone existing context
if (!self::_isSubjectReference($node_)) { if(count($ctxs) === 0) {
$node->{'@graph'}[] = $node_; return self::_cloneActiveContext($active_ctx);
} }
}
} // process each context in order, update active context
// only add full subjects to top-level // on each iteration to ensure proper caching
if (!self::_isSubjectReference($node)) { $rval = $active_ctx;
$result[] = $node; foreach($ctxs as $ctx) {
} // reset to initial context
} if($ctx === null) {
$rval = $active_ctx = $this->_getInitialContext($options);
return $result; continue;
} }
/** // dereference @context key if present
* Processes a local context and returns a new active context. if(is_object($ctx) && property_exists($ctx, '@context')) {
* $ctx = $ctx->{'@context'};
* @param stdClass $active_ctx the current active context. }
* @param mixed $local_ctx the local context to process.
* @param assoc $options the context processing options. // context must be an object by now, all URLs retrieved before this call
* if(!is_object($ctx)) {
* @return stdClass the new active context. throw new JsonLdException(
*/ 'Invalid JSON-LD syntax; @context must be an object.',
protected function _processContext($active_ctx, $local_ctx, $options) 'jsonld.SyntaxError', 'invalid local context',
{ array('context' => $ctx));
global $jsonld_cache; }
// normalize local context to an array // get context from cache if available
if (is_object($local_ctx) && property_exists($local_ctx, '@context') && if(property_exists($jsonld_cache, 'activeCtx')) {
is_array($local_ctx->{'@context'})) { $cached = $jsonld_cache->activeCtx->get($active_ctx, $ctx);
$local_ctx = $local_ctx->{'@context'}; if($cached) {
} $rval = $active_ctx = $cached;
$ctxs = self::arrayify($local_ctx); $must_clone = true;
continue;
// no contexts in array, clone existing context }
if (count($ctxs) === 0) { }
return self::_cloneActiveContext($active_ctx);
} // update active context and clone new one before updating
$active_ctx = $rval;
// process each context in order, update active context $rval = self::_cloneActiveContext($rval);
// on each iteration to ensure proper caching
$rval = $active_ctx; // define context mappings for keys in local context
foreach ($ctxs as $ctx) { $defined = new stdClass();
// reset to initial context
if ($ctx === null) { // handle @base
$rval = $active_ctx = $this->_getInitialContext($options); if(property_exists($ctx, '@base')) {
continue; $base = $ctx->{'@base'};
} if($base === null) {
$base = null;
// dereference @context key if present } else if(!is_string($base)) {
if (is_object($ctx) && property_exists($ctx, '@context')) { throw new JsonLdException(
$ctx = $ctx->{'@context'}; 'Invalid JSON-LD syntax; the value of "@base" in a ' .
} '@context must be a string or null.',
'jsonld.SyntaxError', 'invalid base IRI', array('context' => $ctx));
// context must be an object by now, all URLs retrieved before this call } else if($base !== '' && !self::_isAbsoluteIri($base)) {
if (!is_object($ctx)) { throw new JsonLdException(
throw new JsonLdException( 'Invalid JSON-LD syntax; the value of "@base" in a ' .
'Invalid JSON-LD syntax; @context must be an object.', 'jsonld.SyntaxError', 'invalid local context', ['context' => $ctx]); '@context must be an absolute IRI or the empty string.',
} 'jsonld.SyntaxError', 'invalid base IRI', array('context' => $ctx));
}
// get context from cache if available if($base !== null) {
if (property_exists($jsonld_cache, 'activeCtx')) { $base = jsonld_parse_url($base);
$cached = $jsonld_cache->activeCtx->get($active_ctx, $ctx); }
if ($cached) { $rval->{'@base'} = $base;
$rval = $active_ctx = $cached; $defined->{'@base'} = true;
$must_clone = true; }
continue;
} // handle @vocab
} if(property_exists($ctx, '@vocab')) {
$value = $ctx->{'@vocab'};
// update active context and clone new one before updating if($value === null) {
$active_ctx = $rval; unset($rval->{'@vocab'});
$rval = self::_cloneActiveContext($rval); } else if(!is_string($value)) {
throw new JsonLdException(
// define context mappings for keys in local context 'Invalid JSON-LD syntax; the value of "@vocab" in a ' .
$defined = new stdClass(); '@context must be a string or null.',
'jsonld.SyntaxError', 'invalid vocab mapping',
// handle @base array('context' => $ctx));
if (property_exists($ctx, '@base')) { } else if(!self::_isAbsoluteIri($value)) {
$base = $ctx->{'@base'}; throw new JsonLdException(
if ($base === null) { 'Invalid JSON-LD syntax; the value of "@vocab" in a ' .
$base = null; '@context must be an absolute IRI.',
} else if (!is_string($base)) { 'jsonld.SyntaxError', 'invalid vocab mapping',
throw new JsonLdException( array('context' => $ctx));
'Invalid JSON-LD syntax; the value of "@base" in a ' . } else {
'@context must be a string or null.', 'jsonld.SyntaxError', 'invalid base IRI', ['context' => $ctx]); $rval->{'@vocab'} = $value;
} else if ($base !== '' && !self::_isAbsoluteIri($base)) { }
throw new JsonLdException( $defined->{'@vocab'} = true;
'Invalid JSON-LD syntax; the value of "@base" in a ' . }
'@context must be an absolute IRI or the empty string.', 'jsonld.SyntaxError', 'invalid base IRI', ['context' => $ctx]);
} // handle @language
if ($base !== null) { if(property_exists($ctx, '@language')) {
$base = jsonld_parse_url($base); $value = $ctx->{'@language'};
} if($value === null) {
$rval->{'@base'} = $base; unset($rval->{'@language'});
$defined->{'@base'} = true; } else if(!is_string($value)) {
} throw new JsonLdException(
'Invalid JSON-LD syntax; the value of "@language" in a ' .
// handle @vocab '@context must be a string or null.',
if (property_exists($ctx, '@vocab')) { 'jsonld.SyntaxError', 'invalid default language',
$value = $ctx->{'@vocab'}; array('context' => $ctx));
if ($value === null) { } else {
unset($rval->{'@vocab'}); $rval->{'@language'} = strtolower($value);
} else if (!is_string($value)) { }
throw new JsonLdException( $defined->{'@language'} = true;
'Invalid JSON-LD syntax; the value of "@vocab" in a ' . }
'@context must be a string or null.', 'jsonld.SyntaxError', 'invalid vocab mapping', ['context' => $ctx]);
} else if (!self::_isAbsoluteIri($value)) { // process all other keys
throw new JsonLdException( foreach($ctx as $k => $v) {
'Invalid JSON-LD syntax; the value of "@vocab" in a ' . $this->_createTermDefinition($rval, $ctx, $k, $defined);
'@context must be an absolute IRI.', 'jsonld.SyntaxError', 'invalid vocab mapping', ['context' => $ctx]); }
} else {
$rval->{'@vocab'} = $value; // cache result
} if(property_exists($jsonld_cache, 'activeCtx')) {
$defined->{'@vocab'} = true; $jsonld_cache->activeCtx->set($active_ctx, $ctx, $rval);
} }
}
// handle @language
if (property_exists($ctx, '@language')) { return $rval;
$value = $ctx->{'@language'}; }
if ($value === null) {
unset($rval->{'@language'}); /**
} else if (!is_string($value)) { * Expands a language map.
throw new JsonLdException( *
'Invalid JSON-LD syntax; the value of "@language" in a ' . * @param stdClass $language_map the language map to expand.
'@context must be a string or null.', 'jsonld.SyntaxError', 'invalid default language', ['context' => $ctx]); *
} else { * @return array the expanded language map.
$rval->{'@language'} = strtolower($value); */
} protected function _expandLanguageMap($language_map) {
$defined->{'@language'} = true; $rval = array();
} $keys = array_keys((array)$language_map);
sort($keys);
// process all other keys foreach($keys as $key) {
foreach ($ctx as $k => $v) { $values = $language_map->{$key};
$this->_createTermDefinition($rval, $ctx, $k, $defined); $values = self::arrayify($values);
} foreach($values as $item) {
if(!is_string($item)) {
// cache result throw new JsonLdException(
if (property_exists($jsonld_cache, 'activeCtx')) { 'Invalid JSON-LD syntax; language map values must be strings.',
$jsonld_cache->activeCtx->set($active_ctx, $ctx, $rval); 'jsonld.SyntaxError', 'invalid language map value',
} array('languageMap', $language_map));
} }
$rval[] = (object)array(
return $rval; '@value' => $item,
} '@language' => strtolower($key));
}
/** }
* Expands a language map. return $rval;
* }
* @param stdClass $language_map the language map to expand.
* /**
* @return array the expanded language map. * Labels the blank nodes in the given value using the given UniqueNamer.
*/ *
protected function _expandLanguageMap($language_map) * @param UniqueNamer $namer the UniqueNamer to use.
{ * @param mixed $element the element with blank nodes to rename.
$rval = []; *
$keys = array_keys((array) $language_map); * @return mixed the element.
sort($keys); */
foreach ($keys as $key) { public function _labelBlankNodes($namer, $element) {
$values = $language_map->{$key}; if(is_array($element)) {
$values = self::arrayify($values); $length = count($element);
foreach ($values as $item) { for($i = 0; $i < $length; ++$i) {
if ($item === null) { $element[$i] = $this->_labelBlankNodes($namer, $element[$i]);
continue; }
} } else if(self::_isList($element)) {
if (!is_string($item)) { $element->{'@list'} = $this->_labelBlankNodes(
throw new JsonLdException( $namer, $element->{'@list'});
'Invalid JSON-LD syntax; language map values must be strings.', 'jsonld.SyntaxError', 'invalid language map value', ['languageMap', $language_map]); } else if(is_object($element)) {
} // rename blank node
$rval[] = (object) [ if(self::_isBlankNode($element)) {
'@value' => $item, $name = null;
'@language' => strtolower($key)]; if(property_exists($element, '@id')) {
} $name = $element->{'@id'};
} }
return $rval; $element->{'@id'} = $namer->getName($name);
} }
/** // recursively apply to all keys
* Labels the blank nodes in the given value using the given UniqueNamer. $keys = array_keys((array)$element);
* sort($keys);
* @param UniqueNamer $namer the UniqueNamer to use. foreach($keys as $key) {
* @param mixed $element the element with blank nodes to rename. if($key !== '@id') {
* $element->{$key} = $this->_labelBlankNodes($namer, $element->{$key});
* @return mixed the element. }
*/ }
public function _labelBlankNodes($namer, $element) }
{
if (is_array($element)) { return $element;
$length = count($element); }
for ($i = 0; $i < $length; ++$i) {
$element[$i] = $this->_labelBlankNodes($namer, $element[$i]); /**
} * Expands the given value by using the coercion and keyword rules in the
} else if (self::_isList($element)) { * given context.
$element->{'@list'} = $this->_labelBlankNodes( *
$namer, $element->{'@list'}); * @param stdClass $active_ctx the active context to use.
} else if (is_object($element)) { * @param string $active_property the property the value is associated with.
// rename blank node * @param mixed $value the value to expand.
if (self::_isBlankNode($element)) { *
$name = null; * @return mixed the expanded value.
if (property_exists($element, '@id')) { */
$name = $element->{'@id'}; protected function _expandValue($active_ctx, $active_property, $value) {
} // nothing to expand
$element->{'@id'} = $namer->getName($name); if($value === null) {
} return null;
}
// recursively apply to all keys
$keys = array_keys((array) $element); // special-case expand @id and @type (skips '@id' expansion)
sort($keys); $expanded_property = $this->_expandIri(
foreach ($keys as $key) { $active_ctx, $active_property, array('vocab' => true));
if ($key !== '@id') { if($expanded_property === '@id') {
$element->{$key} = $this->_labelBlankNodes($namer, $element->{$key}); return $this->_expandIri($active_ctx, $value, array('base' => true));
} } else if($expanded_property === '@type') {
} return $this->_expandIri(
} $active_ctx, $value, array('vocab' => true, 'base' => true));
}
return $element;
} // get type definition from context
$type = self::getContextValue($active_ctx, $active_property, '@type');
/**
* Expands the given value by using the coercion and keyword rules in the // do @id expansion (automatic for @graph)
* given context. if($type === '@id' || ($expanded_property === '@graph' &&
* is_string($value))) {
* @param stdClass $active_ctx the active context to use. return (object)array('@id' => $this->_expandIri(
* @param string $active_property the property the value is associated with. $active_ctx, $value, array('base' => true)));
* @param mixed $value the value to expand. }
* // do @id expansion w/vocab
* @return mixed the expanded value. if($type === '@vocab') {
*/ return (object)array('@id' => $this->_expandIri(
protected function _expandValue($active_ctx, $active_property, $value) $active_ctx, $value, array('vocab' => true, 'base' => true)));
{ }
// nothing to expand
if ($value === null) { // do not expand keyword values
return null; if(self::_isKeyword($expanded_property)) {
} return $value;
}
// special-case expand @id and @type (skips '@id' expansion)
$expanded_property = $this->_expandIri( $rval = new stdClass();
$active_ctx, $active_property, ['vocab' => true]);
if ($expanded_property === '@id') { // other type
return $this->_expandIri($active_ctx, $value, ['base' => true]); if($type !== null) {
} else if ($expanded_property === '@type') { $rval->{'@type'} = $type;
return $this->_expandIri( } else if(is_string($value)) {
$active_ctx, $value, ['vocab' => true, 'base' => true]); // check for language tagging for strings
} $language = self::getContextValue(
$active_ctx, $active_property, '@language');
// get type definition from context if($language !== null) {
$type = self::getContextValue($active_ctx, $active_property, '@type'); $rval->{'@language'} = $language;
}
// do @id expansion (automatic for @graph) }
if ($type === '@id' || ($expanded_property === '@graph' && $rval->{'@value'} = $value;
is_string($value))) {
return (object) ['@id' => $this->_expandIri( return $rval;
$active_ctx, $value, ['base' => true])]; }
}
// do @id expansion w/vocab /**
if ($type === '@vocab') { * Creates an array of RDF triples for the given graph.
return (object) ['@id' => $this->_expandIri( *
$active_ctx, $value, ['vocab' => true, 'base' => true])]; * @param stdClass $graph the graph to create RDF triples for.
} * @param UniqueNamer $namer for assigning bnode names.
* @param assoc $options the RDF serialization options.
// do not expand keyword values *
if (self::_isKeyword($expanded_property)) { * @return array the array of RDF triples for the given graph.
return $value; */
} protected function _graphToRDF($graph, $namer, $options) {
$rval = array();
$rval = new stdClass();
$ids = array_keys((array)$graph);
// other type sort($ids);
if ($type !== null) { foreach($ids as $id) {
$rval->{'@type'} = $type; $node = $graph->{$id};
} else if (is_string($value)) { if($id === '"') {
// check for language tagging for strings $id = '';
$language = self::getContextValue( }
$active_ctx, $active_property, '@language'); $properties = array_keys((array)$node);
if ($language !== null) { sort($properties);
$rval->{'@language'} = $language; foreach($properties as $property) {
} $items = $node->{$property};
} if($property === '@type') {
$rval->{'@value'} = $value; $property = self::RDF_TYPE;
} else if(self::_isKeyword($property)) {
return $rval; continue;
} }
/** foreach($items as $item) {
* Creates an array of RDF triples for the given graph. // skip relative IRI subjects and predicates
* if(!(self::_isAbsoluteIri($id) && self::_isAbsoluteIri($property))) {
* @param stdClass $graph the graph to create RDF triples for. continue;
* @param UniqueNamer $namer for assigning bnode names. }
* @param assoc $options the RDF serialization options.
* // RDF subject
* @return array the array of RDF triples for the given graph. $subject = new stdClass();
*/ $subject->type = (strpos($id, '_:') === 0) ? 'blank node' : 'IRI';
protected function _graphToRDF($graph, $namer, $options) $subject->value = $id;
{
$rval = []; // RDF predicate
$predicate = new stdClass();
$ids = array_keys((array) $graph); $predicate->type = (strpos($property, '_:') === 0 ?
sort($ids); 'blank node' : 'IRI');
foreach ($ids as $id) { $predicate->value = $property;
$node = $graph->{$id};
if ($id === '"') { // skip bnode predicates unless producing generalized RDF
$id = ''; if($predicate->type === 'blank node' &&
} !$options['produceGeneralizedRdf']) {
$properties = array_keys((array) $node); continue;
sort($properties); }
foreach ($properties as $property) {
$items = $node->{$property}; if(self::_isList($item)) {
if ($property === '@type') { // convert @list to triples
$property = self::RDF_TYPE; $this->_listToRDF(
} else if (self::_isKeyword($property)) { $item->{'@list'}, $namer, $subject, $predicate, $rval);
continue; } else {
} // convert value or node object to triple
$object = $this->_objectToRDF($item);
foreach ($items as $item) { // skip null objects (they are relative IRIs)
// skip relative IRI subjects and predicates if($object) {
if (!(self::_isAbsoluteIri($id) && self::_isAbsoluteIri($property))) { $rval[] = (object)array(
continue; 'subject' => $subject,
} 'predicate' => $predicate,
'object' => $object);
// RDF subject }
$subject = new stdClass(); }
$subject->type = (strpos($id, '_:') === 0) ? 'blank node' : 'IRI'; }
$subject->value = $id; }
}
// RDF predicate
$predicate = new stdClass(); return $rval;
$predicate->type = (strpos($property, '_:') === 0 ? }
'blank node' : 'IRI');
$predicate->value = $property; /**
* Converts a @list value into linked list of blank node RDF triples
// skip bnode predicates unless producing generalized RDF * (an RDF collection).
if ($predicate->type === 'blank node' && *
!$options['produceGeneralizedRdf']) { * @param array $list the @list value.
continue; * @param UniqueNamer $namer for assigning blank node names.
} * @param stdClass $subject the subject for the head of the list.
* @param stdClass $predicate the predicate for the head of the list.
if (self::_isList($item)) { * @param &array $triples the array of triples to append to.
// convert @list to triples */
$this->_listToRDF( protected function _listToRDF(
$item->{'@list'}, $namer, $subject, $predicate, $rval); $list, $namer, $subject, $predicate, &$triples) {
} else { $first = (object)array('type' => 'IRI', 'value' => self::RDF_FIRST);
// convert value or node object to triple $rest = (object)array('type' => 'IRI', 'value' => self::RDF_REST);
$object = $this->_objectToRDF($item); $nil = (object)array('type' => 'IRI', 'value' => self::RDF_NIL);
// skip null objects (they are relative IRIs)
if ($object) { foreach($list as $item) {
$rval[] = (object) [ $blank_node = (object)array(
'subject' => $subject, 'type' => 'blank node', 'value' => $namer->getName());
'predicate' => $predicate, $triples[] = (object)array(
'object' => $object]; 'subject' => $subject,
} 'predicate' => $predicate,
} 'object' => $blank_node);
}
} $subject = $blank_node;
} $predicate = $first;
$object = $this->_objectToRDF($item);
return $rval; // skip null objects (they are relative IRIs)
} if($object) {
$triples[] = (object)array(
/** 'subject' => $subject,
* Converts a @list value into linked list of blank node RDF triples 'predicate' => $predicate,
* (an RDF collection). 'object' => $object);
* }
* @param array $list the @list value.
* @param UniqueNamer $namer for assigning blank node names. $predicate = $rest;
* @param stdClass $subject the subject for the head of the list. }
* @param stdClass $predicate the predicate for the head of the list.
* @param &array $triples the array of triples to append to. $triples[] = (object)array(
*/ 'subject' => $subject, 'predicate' => $predicate, 'object' => $nil);
protected function _listToRDF( }
$list, $namer, $subject, $predicate, &$triples)
{ /**
$first = (object) ['type' => 'IRI', 'value' => self::RDF_FIRST]; * Converts a JSON-LD value object to an RDF literal or a JSON-LD string or
$rest = (object) ['type' => 'IRI', 'value' => self::RDF_REST]; * node object to an RDF resource.
$nil = (object) ['type' => 'IRI', 'value' => self::RDF_NIL]; *
* @param mixed $item the JSON-LD value or node object.
foreach ($list as $item) { *
$blank_node = (object) [ * @return stdClass the RDF literal or RDF resource.
'type' => 'blank node', 'value' => $namer->getName()]; */
$triples[] = (object) [ protected function _objectToRDF($item) {
'subject' => $subject, $object = new stdClass();
'predicate' => $predicate,
'object' => $blank_node]; if(self::_isValue($item)) {
$object->type = 'literal';
$subject = $blank_node; $value = $item->{'@value'};
$predicate = $first; $datatype = property_exists($item, '@type') ? $item->{'@type'} : null;
$object = $this->_objectToRDF($item);
// skip null objects (they are relative IRIs) // convert to XSD datatypes as appropriate
if ($object) { if(is_bool($value)) {
$triples[] = (object) [ $object->value = ($value ? 'true' : 'false');
'subject' => $subject, $object->datatype = $datatype ? $datatype : self::XSD_BOOLEAN;
'predicate' => $predicate, } else if(is_double($value) || $datatype == self::XSD_DOUBLE) {
'object' => $object]; // canonical double representation
} $object->value = preg_replace(
'/(\d)0*E\+?/', '$1E', sprintf('%1.15E', $value));
$predicate = $rest; $object->datatype = $datatype ? $datatype : self::XSD_DOUBLE;
} } else if(is_integer($value)) {
$object->value = strval($value);
$triples[] = (object) [ $object->datatype = $datatype ? $datatype : self::XSD_INTEGER;
'subject' => $subject, 'predicate' => $predicate, 'object' => $nil]; } else if(property_exists($item, '@language')) {
} $object->value = $value;
$object->datatype = $datatype ? $datatype : self::RDF_LANGSTRING;
/** $object->language = $item->{'@language'};
* Converts a JSON-LD value object to an RDF literal or a JSON-LD string or } else {
* node object to an RDF resource. $object->value = $value;
* $object->datatype = $datatype ? $datatype : self::XSD_STRING;
* @param mixed $item the JSON-LD value or node object. }
* } else {
* @return stdClass the RDF literal or RDF resource. // convert string/node object to RDF
*/ $id = is_object($item) ? $item->{'@id'} : $item;
protected function _objectToRDF($item) $object->type = (strpos($id, '_:') === 0) ? 'blank node' : 'IRI';
{ $object->value = $id;
$object = new stdClass(); }
if (self::_isValue($item)) { // skip relative IRIs
$object->type = 'literal'; if($object->type === 'IRI' && !self::_isAbsoluteIri($object->value)) {
$value = $item->{'@value'}; return null;
$datatype = property_exists($item, '@type') ? $item->{'@type'} : null; }
// convert to XSD datatypes as appropriate return $object;
if (is_bool($value)) { }
$object->value = ($value ? 'true' : 'false');
$object->datatype = $datatype ? $datatype : self::XSD_BOOLEAN; /**
} else if (is_double($value) || $datatype == self::XSD_DOUBLE) { * Converts an RDF triple object to a JSON-LD object.
// canonical double representation *
$object->value = preg_replace( * @param stdClass $o the RDF triple object to convert.
'/(\d)0*E\+?/', '$1E', sprintf('%1.15E', $value)); * @param bool $use_native_types true to output native types, false not to.
$object->datatype = $datatype ? $datatype : self::XSD_DOUBLE; *
} else if (is_integer($value)) { * @return stdClass the JSON-LD object.
$object->value = strval($value); */
$object->datatype = $datatype ? $datatype : self::XSD_INTEGER; protected function _RDFToObject($o, $use_native_types) {
} else if (property_exists($item, '@language')) { // convert IRI/blank node object to JSON-LD
$object->value = $value; if($o->type === 'IRI' || $o->type === 'blank node') {
$object->datatype = $datatype ? $datatype : self::RDF_LANGSTRING; return (object)array('@id' => $o->value);
$object->language = $item->{'@language'}; }
} else {
$object->value = $value; // convert literal object to JSON-LD
$object->datatype = $datatype ? $datatype : self::XSD_STRING; $rval = (object)array('@value' => $o->value);
}
} else { if(property_exists($o, 'language')) {
// convert string/node object to RDF // add language
$id = is_object($item) ? $item->{'@id'} : $item; $rval->{'@language'} = $o->language;
$object->type = (strpos($id, '_:') === 0) ? 'blank node' : 'IRI'; } else {
$object->value = $id; // add datatype
} $type = $o->datatype;
// use native types for certain xsd types
// skip relative IRIs if($use_native_types) {
if ($object->type === 'IRI' && !self::_isAbsoluteIri($object->value)) { if($type === self::XSD_BOOLEAN) {
return null; if($rval->{'@value'} === 'true') {
} $rval->{'@value'} = true;
} else if($rval->{'@value'} === 'false') {
return $object; $rval->{'@value'} = false;
} }
} else if(is_numeric($rval->{'@value'})) {
/** if($type === self::XSD_INTEGER) {
* Converts an RDF triple object to a JSON-LD object. $i = intval($rval->{'@value'});
* if(strval($i) === $rval->{'@value'}) {
* @param stdClass $o the RDF triple object to convert. $rval->{'@value'} = $i;
* @param bool $use_native_types true to output native types, false not to. }
* } else if($type === self::XSD_DOUBLE) {
* @return stdClass the JSON-LD object. $rval->{'@value'} = doubleval($rval->{'@value'});
*/ }
protected function _RDFToObject($o, $use_native_types) }
{ // do not add native type
// convert IRI/blank node object to JSON-LD if(!in_array($type, array(
if ($o->type === 'IRI' || $o->type === 'blank node') { self::XSD_BOOLEAN, self::XSD_INTEGER, self::XSD_DOUBLE,
return (object) ['@id' => $o->value]; self::XSD_STRING))) {
} $rval->{'@type'} = $type;
}
// convert literal object to JSON-LD } else if($type !== self::XSD_STRING) {
$rval = (object) ['@value' => $o->value]; $rval->{'@type'} = $type;
}
if (property_exists($o, 'language')) { }
// add language
$rval->{'@language'} = $o->language; return $rval;
} else { }
// add datatype
$type = $o->datatype; /**
// use native types for certain xsd types * Recursively flattens the subjects in the given JSON-LD expanded input
if ($use_native_types) { * into a node map.
if ($type === self::XSD_BOOLEAN) { *
if ($rval->{'@value'} === 'true') { * @param mixed $input the JSON-LD expanded input.
$rval->{'@value'} = true; * @param stdClass $graphs a map of graph name to subject map.
} else if ($rval->{'@value'} === 'false') { * @param string $graph the name of the current graph.
$rval->{'@value'} = false; * @param UniqueNamer $namer the blank node namer.
} * @param mixed $name the name assigned to the current input if it is a bnode.
} else if (is_numeric($rval->{'@value'})) { * @param mixed $list the list to append to, null for none.
if ($type === self::XSD_INTEGER) { */
$i = intval($rval->{'@value'}); protected function _createNodeMap(
if (strval($i) === $rval->{'@value'}) { $input, $graphs, $graph, $namer, $name=null, $list=null) {
$rval->{'@value'} = $i; // recurse through array
} if(is_array($input)) {
} else if ($type === self::XSD_DOUBLE) { foreach($input as $e) {
$rval->{'@value'} = doubleval($rval->{'@value'}); $this->_createNodeMap($e, $graphs, $graph, $namer, null, $list);
} }
} return;
// do not add native type }
if (!in_array($type, [
self::XSD_BOOLEAN, self::XSD_INTEGER, self::XSD_DOUBLE, // add non-object to list
self::XSD_STRING])) { if(!is_object($input)) {
$rval->{'@type'} = $type; if($list !== null) {
} $list[] = $input;
} else if ($type !== self::XSD_STRING) { }
$rval->{'@type'} = $type; return;
} }
}
// add values to list
return $rval; if(self::_isValue($input)) {
} if(property_exists($input, '@type')) {
$type = $input->{'@type'};
/** // rename @type blank node
* Recursively flattens the subjects in the given JSON-LD expanded input if(strpos($type, '_:') === 0) {
* into a node map. $type = $input->{'@type'} = $namer->getName($type);
* }
* @param mixed $input the JSON-LD expanded input. }
* @param stdClass $graphs a map of graph name to subject map. if($list !== null) {
* @param string $graph the name of the current graph. $list[] = $input;
* @param UniqueNamer $namer the blank node namer. }
* @param mixed $name the name assigned to the current input if it is a bnode. return;
* @param mixed $list the list to append to, null for none. }
*/
protected function _createNodeMap( // Note: At this point, input must be a subject.
$input, $graphs, $graph, $namer, $name = null, $list = null)
{ // spec requires @type to be named first, so assign names early
// recurse through array if(property_exists($input, '@type')) {
if (is_array($input)) { foreach($input->{'@type'} as $type) {
foreach ($input as $e) { if(strpos($type, '_:') === 0) {
$this->_createNodeMap($e, $graphs, $graph, $namer, null, $list); $namer->getName($type);
} }
return; }
} }
// add non-object to list // get name for subject
if (!is_object($input)) { if($name === null) {
if ($list !== null) { if(property_exists($input, '@id')) {
$list[] = $input; $name = $input->{'@id'};
} }
return; if(self::_isBlankNode($input)) {
} $name = $namer->getName($name);
}
// add values to list }
if (self::_isValue($input)) {
if (property_exists($input, '@type')) { // add subject reference to list
$type = $input->{'@type'}; if($list !== null) {
// rename @type blank node $list[] = (object)array('@id' => $name);
if (strpos($type, '_:') === 0) { }
$type = $input->{'@type'} = $namer->getName($type);
} // create new subject or merge into existing one
} if(!property_exists($graphs, $graph)) {
if ($list !== null) { $graphs->{$graph} = new stdClass();
$list[] = $input; }
} $subjects = $graphs->{$graph};
return; if(!property_exists($subjects, $name)) {
} if($name === '') {
$subjects->{'"'} = new stdClass();
// Note: At this point, input must be a subject. } else {
// spec requires @type to be named first, so assign names early $subjects->{$name} = new stdClass();
if (property_exists($input, '@type')) { }
foreach ($input->{'@type'} as $type) { }
if (strpos($type, '_:') === 0) { if($name === '') {
$namer->getName($type); $subject = $subjects->{'"'};
} } else {
} $subject = $subjects->{$name};
} }
$subject->{'@id'} = $name;
// get name for subject $properties = array_keys((array)$input);
if ($name === null) { sort($properties);
if (property_exists($input, '@id')) { foreach($properties as $property) {
$name = $input->{'@id'}; // skip @id
} if($property === '@id') {
if (self::_isBlankNode($input)) { continue;
$name = $namer->getName($name); }
}
} // handle reverse properties
if($property === '@reverse') {
// add subject reference to list $referenced_node = (object)array('@id' => $name);
if ($list !== null) { $reverse_map = $input->{'@reverse'};
$list[] = (object) ['@id' => $name]; foreach($reverse_map as $reverse_property => $items) {
} foreach($items as $item) {
$item_name = null;
// create new subject or merge into existing one if(property_exists($item, '@id')) {
if (!property_exists($graphs, $graph)) { $item_name = $item->{'@id'};
$graphs->{$graph} = new stdClass(); }
} if(self::_isBlankNode($item)) {
$subjects = $graphs->{$graph}; $item_name = $namer->getName($item_name);
if (!property_exists($subjects, $name)) { }
if ($name === '') { $this->_createNodeMap($item, $graphs, $graph, $namer, $item_name);
$subjects->{'"'} = new stdClass(); if($item_name === '') {
} else { $item_name = '"';
$subjects->{$name} = new stdClass(); }
} self::addValue(
} $subjects->{$item_name}, $reverse_property, $referenced_node,
if ($name === '') { array('propertyIsArray' => true, 'allowDuplicate' => false));
$subject = $subjects->{'"'}; }
} else { }
$subject = $subjects->{$name}; continue;
} }
$subject->{'@id'} = $name;
$properties = array_keys((array) $input); // recurse into graph
sort($properties); if($property === '@graph') {
foreach ($properties as $property) { // add graph subjects map entry
// skip @id if(!property_exists($graphs, $name)) {
if ($property === '@id') { // FIXME: temporary hack to avoid empty property bug
continue; if(!$name) {
} $name = '"';
}
// handle reverse properties $graphs->{$name} = new stdClass();
if ($property === '@reverse') { }
$referenced_node = (object) ['@id' => $name]; $g = ($graph === '@merged') ? $graph : $name;
$reverse_map = $input->{'@reverse'}; $this->_createNodeMap(
foreach ($reverse_map as $reverse_property => $items) { $input->{$property}, $graphs, $g, $namer, null, null);
foreach ($items as $item) { continue;
$item_name = null; }
if (property_exists($item, '@id')) {
$item_name = $item->{'@id'}; // copy non-@type keywords
} if($property !== '@type' && self::_isKeyword($property)) {
if (self::_isBlankNode($item)) { if($property === '@index' && property_exists($subject, '@index')) {
$item_name = $namer->getName($item_name); throw new JsonLdException(
} 'Invalid JSON-LD syntax; conflicting @index property detected.',
$this->_createNodeMap($item, $graphs, $graph, $namer, $item_name); 'jsonld.SyntaxError', 'conflicting indexes',
if ($item_name === '') { array('subject' => $subject));
$item_name = '"'; }
} $subject->{$property} = $input->{$property};
self::addValue( continue;
$subjects->{$item_name}, $reverse_property, $referenced_node, ['propertyIsArray' => true, 'allowDuplicate' => false]); }
}
} // iterate over objects
continue; $objects = $input->{$property};
}
// if property is a bnode, assign it a new id
// recurse into graph if(strpos($property, '_:') === 0) {
if ($property === '@graph') { $property = $namer->getName($property);
// add graph subjects map entry }
if (!property_exists($graphs, $name)) {
// FIXME: temporary hack to avoid empty property bug // ensure property is added for empty arrays
if (!$name) { if(count($objects) === 0) {
$name = '"'; self::addValue(
} $subject, $property, array(), array('propertyIsArray' => true));
$graphs->{$name} = new stdClass(); continue;
} }
$g = ($graph === '@merged') ? $graph : $name; foreach($objects as $o) {
$this->_createNodeMap( if($property === '@type') {
$input->{$property}, $graphs, $g, $namer, null, null); // rename @type blank nodes
continue; $o = (strpos($o, '_:') === 0) ? $namer->getName($o) : $o;
} }
// copy non-@type keywords // handle embedded subject or subject reference
if ($property !== '@type' && self::_isKeyword($property)) { if(self::_isSubject($o) || self::_isSubjectReference($o)) {
if ($property === '@index' && property_exists($subject, '@index') && // rename blank node @id
($input->{'@index'} !== $subject->{'@index'} || $id = property_exists($o, '@id') ? $o->{'@id'} : null;
$input->{'@index'}->{'@id'} !== $subject->{'@index'}->{'@id'})) { if(self::_isBlankNode($o)) {
throw new JsonLdException( $id = $namer->getName($id);
'Invalid JSON-LD syntax; conflicting @index property detected.', 'jsonld.SyntaxError', 'conflicting indexes', ['subject' => $subject]); }
}
$subject->{$property} = $input->{$property}; // add reference and recurse
continue; self::addValue(
} $subject, $property, (object)array('@id' => $id),
array('propertyIsArray' => true, 'allowDuplicate' => false));
// iterate over objects $this->_createNodeMap($o, $graphs, $graph, $namer, $id, null);
$objects = $input->{$property}; } else if(self::_isList($o)) {
// handle @list
// if property is a bnode, assign it a new id $_list = new ArrayObject();
if (strpos($property, '_:') === 0) { $this->_createNodeMap(
$property = $namer->getName($property); $o->{'@list'}, $graphs, $graph, $namer, $name, $_list);
} $o = (object)array('@list' => (array)$_list);
self::addValue(
// ensure property is added for empty arrays $subject, $property, $o,
if (count($objects) === 0) { array('propertyIsArray' => true, 'allowDuplicate' => false));
self::addValue( } else {
$subject, $property, [], ['propertyIsArray' => true]); // handle @value
continue; $this->_createNodeMap($o, $graphs, $graph, $namer, $name, null);
} self::addValue(
foreach ($objects as $o) { $subject, $property, $o,
if ($property === '@type') { array('propertyIsArray' => true, 'allowDuplicate' => false));
// rename @type blank nodes }
$o = (strpos($o, '_:') === 0) ? $namer->getName($o) : $o; }
} }
}
// handle embedded subject or subject reference
if (self::_isSubject($o) || self::_isSubjectReference($o)) { /**
// rename blank node @id * Frames subjects according to the given frame.
$id = property_exists($o, '@id') ? $o->{'@id'} : null; *
if (self::_isBlankNode($o)) { * @param stdClass $state the current framing state.
$id = $namer->getName($id); * @param array $subjects the subjects to filter.
} * @param array $frame the frame.
* @param mixed $parent the parent subject or top-level array.
// add reference and recurse * @param mixed $property the parent property, initialized to null.
self::addValue( */
$subject, $property, (object) ['@id' => $id], ['propertyIsArray' => true, 'allowDuplicate' => false]); protected function _matchFrame(
$this->_createNodeMap($o, $graphs, $graph, $namer, $id, null); $state, $subjects, $frame, $parent, $property) {
} else if (self::_isList($o)) { // validate the frame
// handle @list $this->_validateFrame($state, $frame);
$_list = new ArrayObject(); $frame = $frame[0];
$this->_createNodeMap(
$o->{'@list'}, $graphs, $graph, $namer, $name, $_list); // filter out subjects that match the frame
$o = (object) ['@list' => (array) $_list]; $matches = $this->_filterSubjects($state, $subjects, $frame);
self::addValue(
$subject, $property, $o, ['propertyIsArray' => true, 'allowDuplicate' => false]); // get flags for current frame
} else { $options = $state->options;
// handle @value $embed_on = $this->_getFrameFlag($frame, $options, 'embed');
$this->_createNodeMap($o, $graphs, $graph, $namer, $name, null); $explicit_on = $this->_getFrameFlag($frame, $options, 'explicit');
self::addValue(
$subject, $property, $o, ['propertyIsArray' => true, 'allowDuplicate' => false]); // add matches to output
} foreach($matches as $id => $subject) {
} /* Note: In order to treat each top-level match as a compartmentalized
} result, create an independent copy of the embedded subjects map when the
} property is null, which only occurs at the top-level. */
if($property === null) {
/** $state->embeds = new stdClass();
* Frames subjects according to the given frame. }
*
* @param stdClass $state the current framing state. // start output
* @param array $subjects the subjects to filter. $output = new stdClass();
* @param array $frame the frame. $output->{'@id'} = $id;
* @param mixed $parent the parent subject or top-level array.
* @param mixed $property the parent property, initialized to null. // prepare embed meta info
*/ $embed = (object)array('parent' => $parent, 'property' => $property);
protected function _matchFrame(
$state, $subjects, $frame, $parent, $property) // if embed is on and there is an existing embed
{ if($embed_on && property_exists($state->embeds, $id)) {
// validate the frame // only overwrite an existing embed if it has already been added to its
$this->_validateFrame($frame); // parent -- otherwise its parent is somewhere up the tree from this
$frame = $frame[0]; // embed and the embed would occur twice once the tree is added
$embed_on = false;
// get flags for current frame
$options = $state->options; // existing embed's parent is an array
$flags = [ $existing = $state->embeds->{$id};
'embed' => $this->_getFrameFlag($frame, $options, 'embed'), if(is_array($existing->parent)) {
'explicit' => $this->_getFrameFlag($frame, $options, 'explicit'), foreach($existing->parent as $p) {
'requireAll' => $this->_getFrameFlag($frame, $options, 'requireAll')]; if(self::compareValues($output, $p)) {
$embed_on = true;
// filter out subjects that match the frame break;
$matches = $this->_filterSubjects($state, $subjects, $frame, $flags); }
}
// add matches to output } else if(self::hasValue(
foreach ($matches as $id => $subject) { $existing->parent, $existing->property, $output)) {
if ($flags['embed'] === '@link' && property_exists($state->link, $id)) { // existing embed's parent is an object
// TODO: may want to also match an existing linked subject against $embed_on = true;
// the current frame ... so different frames could produce different }
// subjects that are only shared in-memory when the frames are the same
// add existing linked subject // existing embed has already been added, so allow an overwrite
$this->_addFrameOutput($parent, $property, $state->link->{$id}); if($embed_on) {
continue; $this->_removeEmbed($state, $id);
} }
}
/* Note: In order to treat each top-level match as a compartmentalized
result, clear the unique embedded subjects map when the property is null, // not embedding, add output without any other properties
which only occurs at the top-level. */ if(!$embed_on) {
if ($property === null) { $this->_addFrameOutput($state, $parent, $property, $output);
$state->uniqueEmbeds = new stdClass(); } else {
} // add embed meta info
$state->embeds->{$id} = $embed;
// start output for subject
$output = new stdClass(); // iterate over subject properties
$output->{'@id'} = $id; $props = array_keys((array)$subject);
$state->link->{$id} = $output; sort($props);
foreach($props as $prop) {
// if embed is @never or if a circular reference would be created by an // copy keywords to output
// embed, the subject cannot be embedded, just add the reference; if(self::_isKeyword($prop)) {
// note that a circular reference won't occur when the embed flag is $output->{$prop} = self::copy($subject->{$prop});
// `@link` as the above check will short-circuit before reaching this point continue;
if ($flags['embed'] === '@never' || }
$this->_createsCircularReference($subject, $state->subjectStack)) {
$this->_addFrameOutput($parent, $property, $output); // if property isn't in the frame
continue; if(!property_exists($frame, $prop)) {
} // if explicit is off, embed values
if(!$explicit_on) {
// if only the last match should be embedded $this->_embedValues($state, $subject, $prop, $output);
if ($flags['embed'] === '@last') { }
// remove any existing embed continue;
if (property_exists($state->uniqueEmbeds, $id)) { }
$this->_removeEmbed($state, $id);
} // add objects
$state->uniqueEmbeds->{$id} = [ $objects = $subject->{$prop};
'parent' => $parent, 'property' => $property]; foreach($objects as $o) {
} // recurse into list
if(self::_isList($o)) {
// push matching subject onto stack to enable circular embed checks // add empty list
$state->subjectStack[] = $subject; $list = (object)array('@list' => array());
$this->_addFrameOutput($state, $output, $prop, $list);
// iterate over subject properties
$props = array_keys((array) $subject); // add list objects
sort($props); $src = $o->{'@list'};
foreach ($props as $prop) { foreach($src as $o) {
// copy keywords to output if(self::_isSubjectReference($o)) {
if (self::_isKeyword($prop)) { // recurse into subject reference
$output->{$prop} = self::copy($subject->{$prop}); $this->_matchFrame(
continue; $state, array($o->{'@id'}), $frame->{$prop}[0]->{'@list'},
} $list, '@list');
} else {
// explicit is on and property isn't in the frame, skip processing // include other values automatically
if ($flags['explicit'] && !property_exists($frame, $prop)) { $this->_addFrameOutput(
continue; $state, $list, '@list', self::copy($o));
} }
}
// add objects continue;
$objects = $subject->{$prop}; }
foreach ($objects as $o) {
// recurse into list if(self::_isSubjectReference($o)) {
if (self::_isList($o)) { // recurse into subject reference
// add empty list $this->_matchFrame(
$list = (object) ['@list' => []]; $state, array($o->{'@id'}), $frame->{$prop}, $output, $prop);
$this->_addFrameOutput($output, $prop, $list); } else {
// include other values automatically
// add list objects $this->_addFrameOutput($state, $output, $prop, self::copy($o));
$src = $o->{'@list'}; }
foreach ($src as $o) { }
if (self::_isSubjectReference($o)) { }
// recurse into subject reference
$subframe = (property_exists($frame, $prop) ? // handle defaults
$frame->{$prop}[0]->{'@list'} : $props = array_keys((array)$frame);
$this->_createImplicitFrame($flags)); sort($props);
$this->_matchFrame( foreach($props as $prop) {
$state, [$o->{'@id'}], $subframe, $list, '@list'); // skip keywords
} else { if(self::_isKeyword($prop)) {
// include other values automatically continue;
$this->_addFrameOutput($list, '@list', self::copy($o)); }
}
} // if omit default is off, then include default values for properties
continue; // that appear in the next frame but are not in the matching subject
} $next = $frame->{$prop}[0];
$omit_default_on = $this->_getFrameFlag(
if (self::_isSubjectReference($o)) { $next, $options, 'omitDefault');
// recurse into subject reference if(!$omit_default_on && !property_exists($output, $prop)) {
$subframe = (property_exists($frame, $prop) ? $preserve = '@null';
$frame->{$prop} : $this->_createImplicitFrame($flags)); if(property_exists($next, '@default')) {
$this->_matchFrame( $preserve = self::copy($next->{'@default'});
$state, [$o->{'@id'}], $subframe, $output, $prop); }
} else { $preserve = self::arrayify($preserve);
// include other values automatically $output->{$prop} = array((object)array('@preserve' => $preserve));
$this->_addFrameOutput($output, $prop, self::copy($o)); }
} }
}
} // add output to parent
$this->_addFrameOutput($state, $parent, $property, $output);
// handle defaults }
$props = array_keys((array) $frame); }
sort($props); }
foreach ($props as $prop) {
// skip keywords /**
if (self::_isKeyword($prop)) { * Gets the frame flag value for the given flag name.
continue; *
} * @param stdClass $frame the frame.
* @param stdClass $options the framing options.
// if omit default is off, then include default values for properties * @param string $name the flag name.
// that appear in the next frame but are not in the matching subject *
$next = $frame->{$prop}[0]; * @return mixed $the flag value.
$omit_default_on = $this->_getFrameFlag( */
$next, $options, 'omitDefault'); protected function _getFrameFlag($frame, $options, $name) {
if (!$omit_default_on && !property_exists($output, $prop)) { $flag = "@$name";
$preserve = '@null'; return (property_exists($frame, $flag) ?
if (property_exists($next, '@default')) { $frame->{$flag}[0] : $options[$name]);
$preserve = self::copy($next->{'@default'}); }
}
$preserve = self::arrayify($preserve); /**
$output->{$prop} = [(object) ['@preserve' => $preserve]]; * Validates a JSON-LD frame, throwing an exception if the frame is invalid.
} *
} * @param stdClass $state the current frame state.
* @param array $frame the frame to validate.
// add output to parent */
$this->_addFrameOutput($parent, $property, $output); protected function _validateFrame($state, $frame) {
if(!is_array($frame) || count($frame) !== 1 || !is_object($frame[0])) {
// pop matching subject from circular ref-checking stack throw new JsonLdException(
array_pop($state->subjectStack); 'Invalid JSON-LD syntax; a JSON-LD frame must be a single object.',
} 'jsonld.SyntaxError', null, array('frame' => $frame));
} }
}
/**
* Creates an implicit frame when recursing through subject matches. If /**
* a frame doesn't have an explicit frame for a particular property, then * Returns a map of all of the subjects that match a parsed frame.
* a wildcard child frame will be created that uses the same flags that the *
* parent frame used. * @param stdClass $state the current framing state.
* * @param array $subjects the set of subjects to filter.
* @param assoc flags the current framing flags. * @param stdClass $frame the parsed frame.
* *
* @return array the implicit frame. * @return stdClass all of the matched subjects.
*/ */
function _createImplicitFrame($flags) protected function _filterSubjects($state, $subjects, $frame) {
{ $rval = new stdClass();
$frame = new stdClass(); sort($subjects);
foreach ($flags as $key => $value) { foreach($subjects as $id) {
$frame->{'@' . $key} = [$flags[$key]]; $subject = $state->subjects->{$id};
} if($this->_filterSubject($subject, $frame)) {
return [$frame]; $rval->{$id} = $subject;
} }
}
/** return $rval;
* Checks the current subject stack to see if embedding the given subject }
* would cause a circular reference.
* /**
* @param stdClass subject_to_embed the subject to embed. * Returns true if the given subject matches the given frame.
* @param assoc subject_stack the current stack of subjects. *
* * @param stdClass $subject the subject to check.
* @return bool true if a circular reference would be created, false if not. * @param stdClass $frame the frame to check.
*/ *
function _createsCircularReference($subject_to_embed, $subject_stack) * @return bool true if the subject matches, false if not.
{ */
for ($i = count($subject_stack) - 1; $i >= 0; --$i) { protected function _filterSubject($subject, $frame) {
if ($subject_stack[$i]->{'@id'} === $subject_to_embed->{'@id'}) { // check @type (object value means 'any' type, fall through to ducktyping)
return true; if(property_exists($frame, '@type') &&
} !(count($frame->{'@type'}) === 1 && is_object($frame->{'@type'}[0]))) {
} $types = $frame->{'@type'};
return false; foreach($types as $type) {
} // any matching @type is a match
if(self::hasValue($subject, '@type', $type)) {
/** return true;
* Gets the frame flag value for the given flag name. }
* }
* @param stdClass $frame the frame. return false;
* @param stdClass $options the framing options. }
* @param string $name the flag name.
* // check ducktype
* @return mixed $the flag value. foreach($frame as $k => $v) {
*/ // only not a duck if @id or non-keyword isn't in subject
protected function _getFrameFlag($frame, $options, $name) if(($k === '@id' || !self::_isKeyword($k)) &&
{ !property_exists($subject, $k)) {
$flag = "@$name"; return false;
$rval = (property_exists($frame, $flag) ? }
$frame->{$flag}[0] : $options[$name]); }
if ($name === 'embed') { return true;
// default is "@last" }
// backwards-compatibility support for "embed" maps:
// true => "@last" /**
// false => "@never" * Embeds values for the given subject and property into the given output
if ($rval === true) { * during the framing algorithm.
$rval = '@last'; *
} else if ($rval === false) { * @param stdClass $state the current framing state.
$rval = '@never'; * @param stdClass $subject the subject.
} else if ($rval !== '@always' && $rval !== '@never' && * @param string $property the property.
$rval !== '@link') { * @param mixed $output the output.
$rval = '@last'; */
} protected function _embedValues($state, $subject, $property, $output) {
} // embed subject properties in output
return $rval; $objects = $subject->{$property};
} foreach($objects as $o) {
// recurse into @list
/** if(self::_isList($o)) {
* Validates a JSON-LD frame, throwing an exception if the frame is invalid. $list = (object)array('@list' => new ArrayObject());
* $this->_addFrameOutput($state, $output, $property, $list);
* @param array $frame the frame to validate. $this->_embedValues($state, $o, '@list', $list->{'@list'});
*/ $list->{'@list'} = (array)$list->{'@list'};
protected function _validateFrame($frame) return;
{ }
if (!is_array($frame) || count($frame) !== 1 || !is_object($frame[0])) {
throw new JsonLdException( // handle subject reference
'Invalid JSON-LD syntax; a JSON-LD frame must be a single object.', 'jsonld.SyntaxError', null, ['frame' => $frame]); if(self::_isSubjectReference($o)) {
} $id = $o->{'@id'};
}
// embed full subject if isn't already embedded
/** if(!property_exists($state->embeds, $id)) {
* Returns a map of all of the subjects that match a parsed frame. // add embed
* $embed = (object)array('parent' => $output, 'property' => $property);
* @param stdClass $state the current framing state. $state->embeds->{$id} = $embed;
* @param array $subjects the set of subjects to filter.
* @param stdClass $frame the parsed frame. // recurse into subject
* @param assoc $flags the frame flags. $o = new stdClass();
* $s = $state->subjects->{$id};
* @return stdClass all of the matched subjects. foreach($s as $prop => $v) {
*/ // copy keywords
protected function _filterSubjects($state, $subjects, $frame, $flags) if(self::_isKeyword($prop)) {
{ $o->{$prop} = self::copy($v);
$rval = new stdClass(); continue;
sort($subjects); }
foreach ($subjects as $id) { $this->_embedValues($state, $s, $prop, $o);
$subject = $state->subjects->{$id}; }
if ($this->_filterSubject($subject, $frame, $flags)) { }
$rval->{$id} = $subject; $this->_addFrameOutput($state, $output, $property, $o);
} } else {
} // copy non-subject value
return $rval; $this->_addFrameOutput($state, $output, $property, self::copy($o));
} }
}
/** }
* Returns true if the given subject matches the given frame.
* /**
* @param stdClass $subject the subject to check. * Removes an existing embed.
* @param stdClass $frame the frame to check. *
* @param assoc $flags the frame flags. * @param stdClass $state the current framing state.
* * @param string $id the @id of the embed to remove.
* @return bool true if the subject matches, false if not. */
*/ protected function _removeEmbed($state, $id) {
protected function _filterSubject($subject, $frame, $flags) // get existing embed
{ $embeds = $state->embeds;
// check @type (object value means 'any' type, fall through to ducktyping) $embed = $embeds->{$id};
if (property_exists($frame, '@type') && $property = $embed->property;
!(count($frame->{'@type'}) === 1 && is_object($frame->{'@type'}[0]))) {
$types = $frame->{'@type'}; // create reference to replace embed
foreach ($types as $type) { $subject = (object)array('@id' => id);
// any matching @type is a match
if (self::hasValue($subject, '@type', $type)) { // remove existing embed
return true; if(is_array($embed->parent)) {
} // replace subject with reference
} foreach($embed->parent as $i => $parent) {
return false; if(self::compareValues($parent, $subject)) {
} $embed->parent[$i] = $subject;
break;
// check ducktype }
$wildcard = true; }
$matches_some = false; } else {
foreach ($frame as $k => $v) { // replace subject with reference
if (self::_isKeyword($k)) { $use_array = is_array($embed->parent->{$property});
// skip non-@id and non-@type self::removeValue($embed->parent, $property, $subject,
if ($k !== '@id' && $k !== '@type') { array('propertyIsArray' => $use_array));
continue; self::addValue($embed->parent, $property, $subject,
} array('propertyIsArray' => $use_array));
$wildcard = false; }
// check @id for a specific @id value // recursively remove dependent dangling embeds
if ($k === '@id' && is_string($v)) { $removeDependents = function($id) {
if (!property_exists($subject, $k) || $subject->{$k} !== $v) { // get embed keys as a separate array to enable deleting keys in map
return false; $ids = array_keys((array)$embeds);
} foreach($ids as $next) {
$matches_some = true; if(property_exists($embeds, $next) &&
continue; is_object($embeds->{$next}->parent) &&
} $embeds->{$next}->parent->{'@id'} === $id) {
} unset($embeds->{$next});
$removeDependents($next);
$wildcard = false; }
}
if (property_exists($subject, $k)) { };
// $v === [] means do not match if property is present $removeDependents($id);
if (is_array($v) && count($v) === 0) { }
return false;
} /**
$matches_some = true; * Adds framing output to the given parent.
continue; *
} * @param stdClass $state the current framing state.
* @param mixed $parent the parent to add to.
// all properties must match to be a duck unless a @default is specified * @param string $property the parent property.
$has_default = (is_array($v) && count($v) === 1 && is_object($v[0]) && * @param mixed $output the output to add.
property_exists($v[0], '@default')); */
if ($flags['requireAll'] && !$has_default) { protected function _addFrameOutput($state, $parent, $property, $output) {
return false; if(is_object($parent) && !($parent instanceof ArrayObject)) {
} self::addValue(
} $parent, $property, $output, array('propertyIsArray' => true));
} else {
// return true if wildcard or subject matches some properties $parent[] = $output;
return $wildcard || $matches_some; }
} }
/** /**
* Removes an existing embed. * Removes the @preserve keywords as the last step of the framing algorithm.
* *
* @param stdClass $state the current framing state. * @param stdClass $ctx the active context used to compact the input.
* @param string $id the @id of the embed to remove. * @param mixed $input the framed, compacted output.
*/ * @param assoc $options the compaction options used.
protected function _removeEmbed($state, $id) *
{ * @return mixed the resulting output.
// get existing embed */
$embeds = $state->uniqueEmbeds; protected function _removePreserve($ctx, $input, $options) {
$embed = $embeds->{$id}; // recurse through arrays
$property = $embed['property']; if(is_array($input)) {
$output = array();
// create reference to replace embed foreach($input as $e) {
$subject = (object) ['@id' => $id]; $result = $this->_removePreserve($ctx, $e, $options);
// drop nulls from arrays
// remove existing embed if($result !== null) {
if (is_array($embed->parent)) { $output[] = $result;
// replace subject with reference }
foreach ($embed->parent as $i => $parent) { }
if (self::compareValues($parent, $subject)) { $input = $output;
$embed->parent[$i] = $subject; } else if(is_object($input)) {
break; // remove @preserve
} if(property_exists($input, '@preserve')) {
} if($input->{'@preserve'} === '@null') {
} else { return null;
// replace subject with reference }
$use_array = is_array($embed->parent->{$property}); return $input->{'@preserve'};
self::removeValue($embed->parent, $property, $subject, ['propertyIsArray' => $use_array]); }
self::addValue($embed->parent, $property, $subject, ['propertyIsArray' => $use_array]);
} // skip @values
if(self::_isValue($input)) {
// recursively remove dependent dangling embeds return $input;
$removeDependents = function($id) { }
// get embed keys as a separate array to enable deleting keys in map
$ids = array_keys((array) $embeds); // recurse through @lists
foreach ($ids as $next) { if(self::_isList($input)) {
if (property_exists($embeds, $next) && $input->{'@list'} = $this->_removePreserve(
is_object($embeds->{$next}->parent) && $ctx, $input->{'@list'}, $options);
$embeds->{$next}->parent->{'@id'} === $id) { return $input;
unset($embeds->{$next}); }
$removeDependents($next);
} // recurse through properties
} foreach($input as $prop => $v) {
}; $result = $this->_removePreserve($ctx, $v, $options);
$removeDependents($id); $container = self::getContextValue($ctx, $prop, '@container');
} if($options['compactArrays'] &&
is_array($result) && count($result) === 1 &&
/** $container !== '@set' && $container !== '@list') {
* Adds framing output to the given parent. $result = $result[0];
* }
* @param mixed $parent the parent to add to. $input->{$prop} = $result;
* @param string $property the parent property. }
* @param mixed $output the output to add. }
*/ return $input;
protected function _addFrameOutput($parent, $property, $output) }
{
if (is_object($parent) && !($parent instanceof ArrayObject)) { /**
self::addValue( * Compares two RDF triples for equality.
$parent, $property, $output, ['propertyIsArray' => true]); *
} else { * @param stdClass $t1 the first triple.
$parent[] = $output; * @param stdClass $t2 the second triple.
} *
} * @return true if the triples are the same, false if not.
*/
/** protected static function _compareRDFTriples($t1, $t2) {
* Removes the @preserve keywords as the last step of the framing algorithm. foreach(array('subject', 'predicate', 'object') as $attr) {
* if($t1->{$attr}->type !== $t2->{$attr}->type ||
* @param stdClass $ctx the active context used to compact the input. $t1->{$attr}->value !== $t2->{$attr}->value) {
* @param mixed $input the framed, compacted output. return false;
* @param assoc $options the compaction options used. }
* }
* @return mixed the resulting output. if(property_exists($t1->object, 'language') !==
*/ property_exists($t1->object, 'language')) {
protected function _removePreserve($ctx, $input, $options) return false;
{ }
// recurse through arrays if(property_exists($t1->object, 'language') &&
if (is_array($input)) { $t1->object->language !== $t2->object->language) {
$output = []; return false;
foreach ($input as $e) { }
$result = $this->_removePreserve($ctx, $e, $options); if(property_exists($t1->object, 'datatype') &&
// drop nulls from arrays $t1->object->datatype !== $t2->object->datatype) {
if ($result !== null) { return false;
$output[] = $result; }
} return true;
} }
$input = $output;
} else if (is_object($input)) { /**
// remove @preserve * Hashes all of the quads about a blank node.
if (property_exists($input, '@preserve')) { *
if ($input->{'@preserve'} === '@null') { * @param string $id the ID of the bnode to hash quads for.
return null; * @param stdClass $bnodes the mapping of bnodes to quads.
} * @param UniqueNamer $namer the canonical bnode namer.
return $input->{'@preserve'}; *
} * @return string the new hash.
*/
// skip @values protected function _hashQuads($id, $bnodes, $namer) {
if (self::_isValue($input)) { // return cached hash
return $input; if(property_exists($bnodes->{$id}, 'hash')) {
} return $bnodes->{$id}->hash;
}
// recurse through @lists
if (self::_isList($input)) { // serialize all of bnode's quads
$input->{'@list'} = $this->_removePreserve( $quads = $bnodes->{$id}->quads;
$ctx, $input->{'@list'}, $options); $nquads = array();
return $input; foreach($quads as $quad) {
} $nquads[] = $this->toNQuad($quad, property_exists($quad, 'name') ?
$quad->name->value : null, $id);
// handle in-memory linked nodes }
$id_alias = $this->_compactIri($ctx, '@id');
if (property_exists($input, $id_alias)) { // sort serialized quads
$id = $input->{$id_alias}; sort($nquads);
if (isset($options['link'][$id])) {
$idx = array_search($input, $options['link'][$id]); // cache and return hashed quads
if ($idx === false) { $hash = $bnodes->{$id}->hash = sha1(implode($nquads));
// prevent circular visitation return $hash;
$options['link'][$id][] = $input; }
} else {
// already visited /**
return $options['link'][$id][$idx]; * Produces a hash for the paths of adjacent bnodes for a bnode,
} * incorporating all information about its subgraph of bnodes. This
} else { * method will recursively pick adjacent bnode permutations that produce the
// prevent circular visitation * lexicographically-least 'path' serializations.
$options['link'][$id] = [$input]; *
} * @param string $id the ID of the bnode to hash paths for.
} * @param stdClass $bnodes the map of bnode quads.
* @param UniqueNamer $namer the canonical bnode namer.
// recurse through properties * @param UniqueNamer $path_namer the namer used to assign names to adjacent
foreach ($input as $prop => $v) { * bnodes.
$result = $this->_removePreserve($ctx, $v, $options); *
$container = self::getContextValue($ctx, $prop, '@container'); * @return stdClass the hash and path namer used.
if ($options['compactArrays'] && */
is_array($result) && count($result) === 1 && protected function _hashPaths($id, $bnodes, $namer, $path_namer) {
$container !== '@set' && $container !== '@list') { // create SHA-1 digest
$result = $result[0]; $md = hash_init('sha1');
}
$input->{$prop} = $result; // group adjacent bnodes by hash, keep properties and references separate
} $groups = new stdClass();
} $quads = $bnodes->{$id}->quads;
return $input; foreach($quads as $quad) {
} // get adjacent bnode
$bnode = $this->_getAdjacentBlankNodeName($quad->subject, $id);
/** if($bnode !== null) {
* Compares two RDF triples for equality. // normal property
* $direction = 'p';
* @param stdClass $t1 the first triple. } else {
* @param stdClass $t2 the second triple. $bnode = $this->_getAdjacentBlankNodeName($quad->object, $id);
* if($bnode !== null) {
* @return true if the triples are the same, false if not. // reverse property
*/ $direction = 'r';
protected static function _compareRDFTriples($t1, $t2) }
{ }
foreach (['subject', 'predicate', 'object'] as $attr) { if($bnode !== null) {
if ($t1->{$attr}->type !== $t2->{$attr}->type || // get bnode name (try canonical, path, then hash)
$t1->{$attr}->value !== $t2->{$attr}->value) { if($namer->isNamed($bnode)) {
return false; $name = $namer->getName($bnode);
} } else if($path_namer->isNamed($bnode)) {
} $name = $path_namer->getName($bnode);
if (property_exists($t1->object, 'language') !== property_exists($t1->object, 'language')) { } else {
return false; $name = $this->_hashQuads($bnode, $bnodes, $namer);
} }
if (property_exists($t1->object, 'language') &&
$t1->object->language !== $t2->object->language) { // hash direction, property, and bnode name/hash
return false; $group_md = hash_init('sha1');
} hash_update($group_md, $direction);
if (property_exists($t1->object, 'datatype') && hash_update($group_md, $quad->predicate->value);
$t1->object->datatype !== $t2->object->datatype) { hash_update($group_md, $name);
return false; $group_hash = hash_final($group_md);
}
return true; // add bnode to hash group
} if(property_exists($groups, $group_hash)) {
$groups->{$group_hash}[] = $bnode;
/** } else {
* Hashes all of the quads about a blank node. $groups->{$group_hash} = array($bnode);
* }
* @param string $id the ID of the bnode to hash quads for. }
* @param stdClass $bnodes the mapping of bnodes to quads. }
* @param UniqueNamer $namer the canonical bnode namer.
* // iterate over groups in sorted hash order
* @return string the new hash. $group_hashes = array_keys((array)$groups);
*/ sort($group_hashes);
protected function _hashQuads($id, $bnodes, $namer) foreach($group_hashes as $group_hash) {
{ // digest group hash
// return cached hash hash_update($md, $group_hash);
if (property_exists($bnodes->{$id}, 'hash')) {
return $bnodes->{$id}->hash; // choose a path and namer from the permutations
} $chosen_path = null;
$chosen_namer = null;
// serialize all of bnode's quads $permutator = new Permutator($groups->{$group_hash});
$quads = $bnodes->{$id}->quads; while($permutator->hasNext()) {
$nquads = []; $permutation = $permutator->next();
foreach ($quads as $quad) { $path_namer_copy = clone $path_namer;
$nquads[] = $this->toNQuad($quad, property_exists($quad, 'name') ?
$quad->name->value : null, $id); // build adjacent path
} $path = '';
$skipped = false;
// sort serialized quads $recurse = array();
sort($nquads); foreach($permutation as $bnode) {
// use canonical name if available
// cache and return hashed quads if($namer->isNamed($bnode)) {
$hash = $bnodes->{$id}->hash = hash('sha256', implode($nquads)); $path .= $namer->getName($bnode);
return $hash; } else {
} // recurse if bnode isn't named in the path yet
if(!$path_namer_copy->isNamed($bnode)) {
/** $recurse[] = $bnode;
* Produces a hash for the paths of adjacent bnodes for a bnode, }
* incorporating all information about its subgraph of bnodes. This $path .= $path_namer_copy->getName($bnode);
* method will recursively pick adjacent bnode permutations that produce the }
* lexicographically-least 'path' serializations.
* // skip permutation if path is already >= chosen path
* @param string $id the ID of the bnode to hash paths for. if($chosen_path !== null && strlen($path) >= strlen($chosen_path) &&
* @param stdClass $bnodes the map of bnode quads. $path > $chosen_path) {
* @param UniqueNamer $namer the canonical bnode namer. $skipped = true;
* @param UniqueNamer $path_namer the namer used to assign names to adjacent break;
* bnodes. }
* }
* @return stdClass the hash and path namer used.
*/ // recurse
protected function _hashPaths($id, $bnodes, $namer, $path_namer) if(!$skipped) {
{ foreach($recurse as $bnode) {
// create SHA-1 digest $result = $this->_hashPaths(
$md = hash_init('sha256'); $bnode, $bnodes, $namer, $path_namer_copy);
$path .= $path_namer_copy->getName($bnode);
// group adjacent bnodes by hash, keep properties and references separate $path .= "<{$result->hash}>";
$groups = new stdClass(); $path_namer_copy = $result->pathNamer;
$quads = $bnodes->{$id}->quads;
foreach ($quads as $quad) { // skip permutation if path is already >= chosen path
// get adjacent bnode if($chosen_path !== null &&
$bnode = $this->_getAdjacentBlankNodeName($quad->subject, $id); strlen($path) >= strlen($chosen_path) && $path > $chosen_path) {
if ($bnode !== null) { $skipped = true;
// normal property break;
$direction = 'p'; }
} else { }
$bnode = $this->_getAdjacentBlankNodeName($quad->object, $id); }
if ($bnode !== null) {
// reverse property if(!$skipped && ($chosen_path === null || $path < $chosen_path)) {
$direction = 'r'; $chosen_path = $path;
} $chosen_namer = $path_namer_copy;
} }
if ($bnode !== null) { }
// get bnode name (try canonical, path, then hash)
if ($namer->isNamed($bnode)) { // digest chosen path and update namer
$name = $namer->getName($bnode); hash_update($md, $chosen_path);
} else if ($path_namer->isNamed($bnode)) { $path_namer = $chosen_namer;
$name = $path_namer->getName($bnode); }
} else {
$name = $this->_hashQuads($bnode, $bnodes, $namer); // return SHA-1 hash and path namer
} return (object)array(
'hash' => hash_final($md), 'pathNamer' => $path_namer);
// hash direction, property, and bnode name/hash }
$group_md = hash_init('sha256');
hash_update($group_md, $direction); /**
hash_update($group_md, $quad->predicate->value); * A helper function that gets the blank node name from an RDF quad
hash_update($group_md, $name); * node (subject or object). If the node is not a blank node or its
$group_hash = hash_final($group_md); * value does not match the given blank node ID, it will be returned.
*
// add bnode to hash group * @param stdClass $node the RDF quad node.
if (property_exists($groups, $group_hash)) { * @param string $id the ID of the blank node to look next to.
$groups->{$group_hash}[] = $bnode; *
} else { * @return mixed the adjacent blank node name or null if none was found.
$groups->{$group_hash} = [$bnode]; */
} protected function _getAdjacentBlankNodeName($node, $id) {
} if($node->type === 'blank node' && $node->value !== $id) {
} return $node->value;
}
// iterate over groups in sorted hash order return null;
$group_hashes = array_keys((array) $groups); }
sort($group_hashes);
foreach ($group_hashes as $group_hash) { /**
// digest group hash * Compares two strings first based on length and then lexicographically.
hash_update($md, $group_hash); *
* @param string $a the first string.
// choose a path and namer from the permutations * @param string $b the second string.
$chosen_path = null; *
$chosen_namer = null; * @return integer -1 if a < b, 1 if a > b, 0 if a == b.
$permutator = new Permutator($groups->{$group_hash}); */
while ($permutator->hasNext()) { protected function _compareShortestLeast($a, $b) {
$permutation = $permutator->next(); $len_a = strlen($a);
$path_namer_copy = clone $path_namer; $len_b = strlen($b);
if($len_a < $len_b) {
// build adjacent path return -1;
$path = ''; }
$skipped = false; if($len_b < $len_a) {
$recurse = []; return 1;
foreach ($permutation as $bnode) { }
// use canonical name if available if($a === $b) {
if ($namer->isNamed($bnode)) { return 0;
$path .= $namer->getName($bnode); }
} else { return ($a < $b) ? -1 : 1;
// recurse if bnode isn't named in the path yet }
if (!$path_namer_copy->isNamed($bnode)) {
$recurse[] = $bnode; /**
} * Picks the preferred compaction term from the given inverse context entry.
$path .= $path_namer_copy->getName($bnode); *
} * @param active_ctx the active context.
* @param iri the IRI to pick the term for.
// skip permutation if path is already >= chosen path * @param value the value to pick the term for.
if ($chosen_path !== null && strlen($path) >= strlen($chosen_path) && * @param containers the preferred containers.
$path > $chosen_path) { * @param type_or_language either '@type' or '@language'.
$skipped = true; * @param type_or_language_value the preferred value for '@type' or
break; * '@language'.
} *
} * @return mixed the preferred term.
*/
// recurse protected function _selectTerm(
if (!$skipped) { $active_ctx, $iri, $value, $containers,
foreach ($recurse as $bnode) { $type_or_language, $type_or_language_value) {
$result = $this->_hashPaths( if($type_or_language_value === null) {
$bnode, $bnodes, $namer, $path_namer_copy); $type_or_language_value = '@null';
$path .= $path_namer_copy->getName($bnode); }
$path .= "<{$result->hash}>";
$path_namer_copy = $result->pathNamer; // options for the value of @type or @language
$prefs = array();
// skip permutation if path is already >= chosen path
if ($chosen_path !== null && // determine prefs for @id based on whether or not value compacts to a term
strlen($path) >= strlen($chosen_path) && $path > $chosen_path) { if(($type_or_language_value === '@id' ||
$skipped = true; $type_or_language_value === '@reverse') &&
break; self::_isSubjectReference($value)) {
} // prefer @reverse first
} if($type_or_language_value === '@reverse') {
} $prefs[] = '@reverse';
}
if (!$skipped && ($chosen_path === null || $path < $chosen_path)) { // try to compact value to a term
$chosen_path = $path; $term = $this->_compactIri(
$chosen_namer = $path_namer_copy; $active_ctx, $value->{'@id'}, null, array('vocab' => true));
} if(property_exists($active_ctx->mappings, $term) &&
} $active_ctx->mappings->{$term} &&
$active_ctx->mappings->{$term}->{'@id'} === $value->{'@id'}) {
// digest chosen path and update namer // prefer @vocab
hash_update($md, $chosen_path); array_push($prefs, '@vocab', '@id');
$path_namer = $chosen_namer; } else {
} // prefer @id
array_push($prefs, '@id', '@vocab');
// return SHA-1 hash and path namer }
return (object) [ } else {
'hash' => hash_final($md), 'pathNamer' => $path_namer]; $prefs[] = $type_or_language_value;
} }
$prefs[] = '@none';
/**
* A helper function that gets the blank node name from an RDF quad $container_map = $active_ctx->inverse->{$iri};
* node (subject or object). If the node is not a blank node or its foreach($containers as $container) {
* value does not match the given blank node ID, it will be returned. // if container not available in the map, continue
* if(!property_exists($container_map, $container)) {
* @param stdClass $node the RDF quad node. continue;
* @param string $id the ID of the blank node to look next to. }
*
* @return mixed the adjacent blank node name or null if none was found. $type_or_language_value_map =
*/ $container_map->{$container}->{$type_or_language};
protected function _getAdjacentBlankNodeName($node, $id) foreach($prefs as $pref) {
{ // if type/language option not available in the map, continue
if ($node->type === 'blank node' && $node->value !== $id) { if(!property_exists($type_or_language_value_map, $pref)) {
return $node->value; continue;
} }
return null;
} // select term
return $type_or_language_value_map->{$pref};
/** }
* Compares two strings first based on length and then lexicographically. }
* return null;
* @param string $a the first string. }
* @param string $b the second string.
* /**
* @return integer -1 if a < b, 1 if a > b, 0 if a == b. * Compacts an IRI or keyword into a term or prefix if it can be. If the
*/ * IRI has an associated value it may be passed.
protected function _compareShortestLeast($a, $b) *
{ * @param stdClass $active_ctx the active context to use.
$len_a = strlen($a); * @param string $iri the IRI to compact.
$len_b = strlen($b); * @param mixed $value the value to check or null.
if ($len_a < $len_b) { * @param assoc $relative_to options for how to compact IRIs:
return -1; * vocab: true to split after @vocab, false not to.
} * @param bool $reverse true if a reverse property is being compacted, false
if ($len_b < $len_a) { * if not.
return 1; *
} * @return string the compacted term, prefix, keyword alias, or original IRI.
if ($a === $b) { */
return 0; protected function _compactIri(
} $active_ctx, $iri, $value=null, $relative_to=array(), $reverse=false) {
return ($a < $b) ? -1 : 1; // can't compact null
} if($iri === null) {
return $iri;
/** }
* Picks the preferred compaction term from the given inverse context entry.
* // term is a keyword, default vocab to true
* @param active_ctx the active context. if(self::_isKeyword($iri)) {
* @param iri the IRI to pick the term for. $relative_to['vocab'] = true;
* @param value the value to pick the term for. } else if(!isset($relative_to['vocab'])) {
* @param containers the preferred containers. $relative_to['vocab'] = false;
* @param type_or_language either '@type' or '@language'. }
* @param type_or_language_value the preferred value for '@type' or
* '@language'. // use inverse context to pick a term if iri is relative to vocab
* if($relative_to['vocab'] &&
* @return mixed the preferred term. property_exists($this->_getInverseContext($active_ctx), $iri)) {
*/ $default_language = '@none';
protected function _selectTerm( if(property_exists($active_ctx, '@language')) {
$active_ctx, $iri, $value, $containers, $type_or_language, $type_or_language_value) $default_language = $active_ctx->{'@language'};
{ }
if ($type_or_language_value === null) {
$type_or_language_value = '@null'; // prefer @index if available in value
} $containers = array();
if(is_object($value) && property_exists($value, '@index')) {
// options for the value of @type or @language $containers[] = '@index';
$prefs = []; }
// determine prefs for @id based on whether or not value compacts to a term // defaults for term selection based on type/language
if (($type_or_language_value === '@id' || $type_or_language = '@language';
$type_or_language_value === '@reverse') && $type_or_language_value = '@null';
self::_isSubjectReference($value)) {
// prefer @reverse first if($reverse) {
if ($type_or_language_value === '@reverse') { $type_or_language = '@type';
$prefs[] = '@reverse'; $type_or_language_value = '@reverse';
} $containers[] = '@set';
// try to compact value to a term } else if(self::_isList($value)) {
$term = $this->_compactIri( // choose the most specific term that works for all elements in @list
$active_ctx, $value->{'@id'}, null, ['vocab' => true]); // only select @list containers if @index is NOT in value
if (property_exists($active_ctx->mappings, $term) && if(!property_exists($value, '@index')) {
$active_ctx->mappings->{$term} && $containers[] = '@list';
$active_ctx->mappings->{$term}->{'@id'} === $value->{'@id'}) { }
// prefer @vocab $list = $value->{'@list'};
array_push($prefs, '@vocab', '@id'); $common_language = (count($list) === 0) ? $default_language : null;
} else { $common_type = null;
// prefer @id foreach($list as $item) {
array_push($prefs, '@id', '@vocab'); $item_language = '@none';
} $item_type = '@none';
} else { if(self::_isValue($item)) {
$prefs[] = $type_or_language_value; if(property_exists($item, '@language')) {
} $item_language = $item->{'@language'};
$prefs[] = '@none'; } else if(property_exists($item, '@type')) {
$item_type = $item->{'@type'};
$container_map = $active_ctx->inverse->{$iri}; } else {
foreach ($containers as $container) { // plain literal
// if container not available in the map, continue $item_language = '@null';
if (!property_exists($container_map, $container)) { }
continue; } else {
} $item_type = '@id';
}
$type_or_language_value_map = $container_map->{$container}->{$type_or_language}; if($common_language === null) {
foreach ($prefs as $pref) { $common_language = $item_language;
// if type/language option not available in the map, continue } else if($item_language !== $common_language &&
if (!property_exists($type_or_language_value_map, $pref)) { self::_isValue($item)) {
continue; $common_language = '@none';
} }
if($common_type === null) {
// select term $common_type = $item_type;
return $type_or_language_value_map->{$pref}; } else if($item_type !== $common_type) {
} $common_type = '@none';
} }
return null; // there are different languages and types in the list, so choose
} // the most generic term, no need to keep iterating the list
if($common_language === '@none' && $common_type === '@none') {
/** break;
* Compacts an IRI or keyword into a term or prefix if it can be. If the }
* IRI has an associated value it may be passed. }
* if($common_language === null) {
* @param stdClass $active_ctx the active context to use. $common_language = '@none';
* @param string $iri the IRI to compact. }
* @param mixed $value the value to check or null. if($common_type === null) {
* @param assoc $relative_to options for how to compact IRIs: $common_type = '@none';
* vocab: true to split after @vocab, false not to. }
* @param bool $reverse true if a reverse property is being compacted, false if($common_type !== '@none') {
* if not. $type_or_language = '@type';
* $type_or_language_value = $common_type;
* @return string the compacted term, prefix, keyword alias, or original IRI. } else {
*/ $type_or_language_value = $common_language;
protected function _compactIri( }
$active_ctx, $iri, $value = null, $relative_to = [], $reverse = false) } else {
{ if(self::_isValue($value)) {
// can't compact null if(property_exists($value, '@language') &&
if ($iri === null) { !property_exists($value, '@index')) {
return $iri; $containers[] = '@language';
} $type_or_language_value = $value->{'@language'};
} else if(property_exists($value, '@type')) {
$inverse_ctx = $this->_getInverseContext($active_ctx); $type_or_language = '@type';
$type_or_language_value = $value->{'@type'};
if (self::_isKeyword($iri)) { }
// a keyword can only be compacted to simple alias } else {
if (property_exists($inverse_ctx, $iri)) { $type_or_language = '@type';
return $inverse_ctx->$iri->{'@none'}->{'@type'}->{'@none'}; $type_or_language_value = '@id';
} }
return $iri; $containers[] = '@set';
} }
if (!isset($relative_to['vocab'])) { // do term selection
$relative_to['vocab'] = false; $containers[] = '@none';
} $term = $this->_selectTerm(
$active_ctx, $iri, $value,
// use inverse context to pick a term if iri is relative to vocab $containers, $type_or_language, $type_or_language_value);
if ($relative_to['vocab'] && property_exists($inverse_ctx, $iri)) { if($term !== null) {
$default_language = '@none'; return $term;
if (property_exists($active_ctx, '@language')) { }
$default_language = $active_ctx->{'@language'}; }
}
// no term match, use @vocab if available
// prefer @index if available in value if($relative_to['vocab']) {
$containers = []; if(property_exists($active_ctx, '@vocab')) {
if (is_object($value) && property_exists($value, '@index')) { // determine if vocab is a prefix of the iri
$containers[] = '@index'; $vocab = $active_ctx->{'@vocab'};
} if(strpos($iri, $vocab) === 0 && $iri !== $vocab) {
// use suffix as relative iri if it is not a term in the active
// defaults for term selection based on type/language // context
$type_or_language = '@language'; $suffix = substr($iri, strlen($vocab));
$type_or_language_value = '@null'; if(!property_exists($active_ctx->mappings, $suffix)) {
return $suffix;
if ($reverse) { }
$type_or_language = '@type'; }
$type_or_language_value = '@reverse'; }
$containers[] = '@set'; }
} else if (self::_isList($value)) {
// choose the most specific term that works for all elements in @list // no term or @vocab match, check for possible CURIEs
// only select @list containers if @index is NOT in value $choice = null;
if (!property_exists($value, '@index')) { foreach($active_ctx->mappings as $term => $definition) {
$containers[] = '@list'; // skip terms with colons, they can't be prefixes
} if(strpos($term, ':') !== false) {
$list = $value->{'@list'}; continue;
$common_language = (count($list) === 0) ? $default_language : null; }
$common_type = null; // skip entries with @ids that are not partial matches
foreach ($list as $item) { if($definition === null ||
$item_language = '@none'; $definition->{'@id'} === $iri ||
$item_type = '@none'; strpos($iri, $definition->{'@id'}) !== 0) {
if (self::_isValue($item)) { continue;
if (property_exists($item, '@language')) { }
$item_language = $item->{'@language'};
} else if (property_exists($item, '@type')) { // a CURIE is usable if:
$item_type = $item->{'@type'}; // 1. it has no mapping, OR
} else { // 2. value is null, which means we're not compacting an @value, AND
// plain literal // the mapping matches the IRI)
$item_language = '@null'; $curie = $term . ':' . substr($iri, strlen($definition->{'@id'}));
} $is_usable_curie = (!property_exists($active_ctx->mappings, $curie) ||
} else { ($value === null && $active_ctx->mappings->{$curie} &&
$item_type = '@id'; $active_ctx->mappings->{$curie}->{'@id'} === $iri));
}
if ($common_language === null) { // select curie if it is shorter or the same length but lexicographically
$common_language = $item_language; // less than the current choice
} else if ($item_language !== $common_language && if($is_usable_curie && ($choice === null ||
self::_isValue($item)) { self::_compareShortestLeast($curie, $choice) < 0)) {
$common_language = '@none'; $choice = $curie;
} }
if ($common_type === null) { }
$common_type = $item_type;
} else if ($item_type !== $common_type) { // return chosen curie
$common_type = '@none'; if($choice !== null) {
} return $choice;
// there are different languages and types in the list, so choose }
// the most generic term, no need to keep iterating the list
if ($common_language === '@none' && $common_type === '@none') { // compact IRI relative to base
break; if(!$relative_to['vocab']) {
} return jsonld_remove_base($active_ctx->{'@base'}, $iri);
} }
if ($common_language === null) {
$common_language = '@none'; // return IRI as is
} return $iri;
if ($common_type === null) { }
$common_type = '@none';
} /**
if ($common_type !== '@none') { * Performs value compaction on an object with '@value' or '@id' as the only
$type_or_language = '@type'; * property.
$type_or_language_value = $common_type; *
} else { * @param stdClass $active_ctx the active context.
$type_or_language_value = $common_language; * @param string $active_property the active property that points to the
} * value.
} else { * @param mixed $value the value to compact.
if (self::_isValue($value)) { *
if (property_exists($value, '@language') && * @return mixed the compaction result.
!property_exists($value, '@index')) { */
$containers[] = '@language'; protected function _compactValue($active_ctx, $active_property, $value) {
$type_or_language_value = $value->{'@language'}; // value is a @value
} else if (property_exists($value, '@type')) { if(self::_isValue($value)) {
$type_or_language = '@type'; // get context rules
$type_or_language_value = $value->{'@type'}; $type = self::getContextValue($active_ctx, $active_property, '@type');
} $language = self::getContextValue(
} else { $active_ctx, $active_property, '@language');
$type_or_language = '@type'; $container = self::getContextValue(
$type_or_language_value = '@id'; $active_ctx, $active_property, '@container');
}
$containers[] = '@set'; // whether or not the value has an @index that must be preserved
} $preserve_index = (property_exists($value, '@index') &&
$container !== '@index');
// do term selection
$containers[] = '@none'; // if there's no @index to preserve
$term = $this->_selectTerm( if(!$preserve_index) {
$active_ctx, $iri, $value, $containers, $type_or_language, $type_or_language_value); // matching @type or @language specified in context, compact value
if ($term !== null) { if(self::_hasKeyValue($value, '@type', $type) ||
return $term; self::_hasKeyValue($value, '@language', $language)) {
} return $value->{'@value'};
} }
}
// no term match, use @vocab if available
if ($relative_to['vocab']) { // return just the value of @value if all are true:
if (property_exists($active_ctx, '@vocab')) { // 1. @value is the only key or @index isn't being preserved
// determine if vocab is a prefix of the iri // 2. there is no default language or @value is not a string or
$vocab = $active_ctx->{'@vocab'}; // the key has a mapping with a null @language
if (strpos($iri, $vocab) === 0 && $iri !== $vocab) { $key_count = count(array_keys((array)$value));
// use suffix as relative iri if it is not a term in the active $is_value_only_key = ($key_count === 1 ||
// context ($key_count === 2 && property_exists($value, '@index') &&
$suffix = substr($iri, strlen($vocab)); !$preserve_index));
if (!property_exists($active_ctx->mappings, $suffix)) { $has_default_language = property_exists($active_ctx, '@language');
return $suffix; $is_value_string = is_string($value->{'@value'});
} $has_null_mapping = (
} property_exists($active_ctx->mappings, $active_property) &&
} $active_ctx->mappings->{$active_property} !== null &&
} self::_hasKeyValue(
$active_ctx->mappings->{$active_property}, '@language', null));
// no term or @vocab match, check for possible CURIEs if($is_value_only_key &&
$choice = null; (!$has_default_language || !$is_value_string || $has_null_mapping)) {
$idx = 0; return $value->{'@value'};
$partial_matches = []; }
$iri_map = $active_ctx->fast_curie_map;
// check for partial matches of against `iri`, which means look until $rval = new stdClass();
// iri.length - 1, not full length
$max_partial_length = strlen($iri) - 1; // preserve @index
for (; $idx < $max_partial_length && isset($iri_map[$iri[$idx]]); ++$idx) { if($preserve_index) {
$iri_map = $iri_map[$iri[$idx]]; $rval->{$this->_compactIri($active_ctx, '@index')} = $value->{'@index'};
if (isset($iri_map[''])) { }
$entry = $iri_map[''][0];
$entry->iri_length = $idx + 1; // compact @type IRI
$partial_matches[] = $entry; if(property_exists($value, '@type')) {
} $rval->{$this->_compactIri($active_ctx, '@type')} = $this->_compactIri(
} $active_ctx, $value->{'@type'}, null, array('vocab' => true));
// check partial matches in reverse order to prefer longest ones first } else if(property_exists($value, '@language')) {
$partial_matches = array_reverse($partial_matches); // alias @language
foreach ($partial_matches as $entry) { $rval->{$this->_compactIri($active_ctx, '@language')} =
$terms = $entry->terms; $value->{'@language'};
foreach ($terms as $term) { }
// a CURIE is usable if:
// 1. it has no mapping, OR // alias @value
// 2. value is null, which means we're not compacting an @value, AND $rval->{$this->_compactIri($active_ctx, '@value')} = $value->{'@value'};
// the mapping matches the IRI
$curie = $term . ':' . substr($iri, $entry->iri_length); return $rval;
$is_usable_curie = (!property_exists($active_ctx->mappings, $curie) || }
($value === null &&
$active_ctx->mappings->{$curie}->{'@id'} === $iri)); // value is a subject reference
$expanded_property = $this->_expandIri(
// select curie if it is shorter or the same length but $active_ctx, $active_property, array('vocab' => true));
// lexicographically less than the current choice $type = self::getContextValue($active_ctx, $active_property, '@type');
if ($is_usable_curie && ($choice === null || $compacted = $this->_compactIri(
self::_compareShortestLeast($curie, $choice) < 0)) { $active_ctx, $value->{'@id'}, null,
$choice = $curie; array('vocab' => ($type === '@vocab')));
}
} // compact to scalar
} if($type === '@id' || $type === '@vocab' ||
$expanded_property === '@graph') {
// return chosen curie return $compacted;
if ($choice !== null) { }
return $choice;
} $rval = (object)array(
$this->_compactIri($active_ctx, '@id') => $compacted);
// compact IRI relative to base return $rval;
if (!$relative_to['vocab']) { }
return jsonld_remove_base($active_ctx->{'@base'}, $iri);
} /**
* Creates a term definition during context processing.
// return IRI as is *
return $iri; * @param stdClass $active_ctx the current active context.
} * @param stdClass $local_ctx the local context being processed.
* @param string $term the key in the local context to define the mapping for.
/** * @param stdClass $defined a map of defining/defined keys to detect cycles
* Performs value compaction on an object with '@value' or '@id' as the only * and prevent double definitions.
* property. */
* protected function _createTermDefinition(
* @param stdClass $active_ctx the active context. $active_ctx, $local_ctx, $term, $defined) {
* @param string $active_property the active property that points to the if(property_exists($defined, $term)) {
* value. // term already defined
* @param mixed $value the value to compact. if($defined->{$term}) {
* return;
* @return mixed the compaction result. }
*/ // cycle detected
protected function _compactValue($active_ctx, $active_property, $value) throw new JsonLdException(
{ 'Cyclical context definition detected.',
// value is a @value 'jsonld.CyclicalContext', 'cyclic IRI mapping',
if (self::_isValue($value)) { array('context' => $local_ctx, 'term' => $term));
// get context rules }
$type = self::getContextValue($active_ctx, $active_property, '@type');
$language = self::getContextValue( // now defining term
$active_ctx, $active_property, '@language'); $defined->{$term} = false;
$container = self::getContextValue(
$active_ctx, $active_property, '@container'); if(self::_isKeyword($term)) {
throw new JsonLdException(
// whether or not the value has an @index that must be preserved 'Invalid JSON-LD syntax; keywords cannot be overridden.',
$preserve_index = (property_exists($value, '@index') && 'jsonld.SyntaxError', 'keyword redefinition',
$container !== '@index'); array('context' => $local_ctx, 'term' => $term));
}
// if there's no @index to preserve
if (!$preserve_index) { // remove old mapping
// matching @type or @language specified in context, compact value if(property_exists($active_ctx->mappings, $term)) {
if (self::_hasKeyValue($value, '@type', $type) || unset($active_ctx->mappings->{$term});
self::_hasKeyValue($value, '@language', $language)) { }
return $value->{'@value'};
} // get context term value
} $value = $local_ctx->{$term};
// return just the value of @value if all are true: // clear context entry
// 1. @value is the only key or @index isn't being preserved if($value === null || (is_object($value) &&
// 2. there is no default language or @value is not a string or self::_hasKeyValue($value, '@id', null))) {
// the key has a mapping with a null @language $active_ctx->mappings->{$term} = null;
$key_count = count(array_keys((array) $value)); $defined->{$term} = true;
$is_value_only_key = ($key_count === 1 || return;
($key_count === 2 && property_exists($value, '@index') && }
!$preserve_index));
$has_default_language = property_exists($active_ctx, '@language'); // convert short-hand value to object w/@id
$is_value_string = is_string($value->{'@value'}); if(is_string($value)) {
$has_null_mapping = ( $value = (object)array('@id' => $value);
property_exists($active_ctx->mappings, $active_property) && }
$active_ctx->mappings->{$active_property} !== null &&
self::_hasKeyValue( if(!is_object($value)) {
$active_ctx->mappings->{$active_property}, '@language', null)); throw new JsonLdException(
if ($is_value_only_key && 'Invalid JSON-LD syntax; @context property values must be ' .
(!$has_default_language || !$is_value_string || $has_null_mapping)) { 'strings or objects.', 'jsonld.SyntaxError', 'invalid term definition',
return $value->{'@value'}; array('context' => $local_ctx));
} }
$rval = new stdClass(); // create new mapping
$mapping = $active_ctx->mappings->{$term} = new stdClass();
// preserve @index $mapping->reverse = false;
if ($preserve_index) {
$rval->{$this->_compactIri($active_ctx, '@index')} = $value->{'@index'}; if(property_exists($value, '@reverse')) {
} if(property_exists($value, '@id')) {
throw new JsonLdException(
// compact @type IRI 'Invalid JSON-LD syntax; a @reverse term definition must not ' +
if (property_exists($value, '@type')) { 'contain @id.', 'jsonld.SyntaxError', 'invalid reverse property',
$rval->{$this->_compactIri($active_ctx, '@type')} = $this->_compactIri( array('context' => $local_ctx));
$active_ctx, $value->{'@type'}, null, ['vocab' => true]); }
} else if (property_exists($value, '@language')) { $reverse = $value->{'@reverse'};
// alias @language if(!is_string($reverse)) {
$rval->{$this->_compactIri($active_ctx, '@language')} = $value->{'@language'}; throw new JsonLdException(
} 'Invalid JSON-LD syntax; a @context @reverse value must be a string.',
'jsonld.SyntaxError', 'invalid IRI mapping',
// alias @value array('context' => $local_ctx));
$rval->{$this->_compactIri($active_ctx, '@value')} = $value->{'@value'}; }
return $rval; // expand and add @id mapping
} $id = $this->_expandIri(
$active_ctx, $reverse, array('vocab' => true, 'base' => false),
// value is a subject reference $local_ctx, $defined);
$expanded_property = $this->_expandIri( if(!self::_isAbsoluteIri($id)) {
$active_ctx, $active_property, ['vocab' => true]); throw new JsonLdException(
$type = self::getContextValue($active_ctx, $active_property, '@type'); 'Invalid JSON-LD syntax; @context @reverse value must be ' .
$compacted = $this->_compactIri( 'an absolute IRI or a blank node identifier.',
$active_ctx, $value->{'@id'}, null, ['vocab' => ($type === '@vocab')]); 'jsonld.SyntaxError', 'invalid IRI mapping',
array('context' => $local_ctx));
// compact to scalar }
if ($type === '@id' || $type === '@vocab' || $mapping->{'@id'} = $id;
$expanded_property === '@graph') { $mapping->reverse = true;
return $compacted; } else if(property_exists($value, '@id')) {
} $id = $value->{'@id'};
if(!is_string($id)) {
$rval = (object) [ throw new JsonLdException(
$this->_compactIri($active_ctx, '@id') => $compacted]; 'Invalid JSON-LD syntax; @context @id value must be a string.',
return $rval; 'jsonld.SyntaxError', 'invalid IRI mapping',
} array('context' => $local_ctx));
}
/** if($id !== $term) {
* Creates a term definition during context processing. // add @id to mapping
* $id = $this->_expandIri(
* @param stdClass $active_ctx the current active context. $active_ctx, $id, array('vocab' => true, 'base' => false),
* @param stdClass $local_ctx the local context being processed. $local_ctx, $defined);
* @param string $term the key in the local context to define the mapping for. if(!self::_isAbsoluteIri($id) && !self::_isKeyword($id)) {
* @param stdClass $defined a map of defining/defined keys to detect cycles throw new JsonLdException(
* and prevent double definitions. 'Invalid JSON-LD syntax; @context @id value must be an ' .
*/ 'absolute IRI, a blank node identifier, or a keyword.',
protected function _createTermDefinition( 'jsonld.SyntaxError', 'invalid IRI mapping',
$active_ctx, $local_ctx, $term, $defined) array('context' => $local_ctx));
{ }
if (property_exists($defined, $term)) { $mapping->{'@id'} = $id;
// term already defined }
if ($defined->{$term}) { }
return;
} if(!property_exists($mapping, '@id')) {
// cycle detected // see if the term has a prefix
throw new JsonLdException( $colon = strpos($term, ':');
'Cyclical context definition detected.', 'jsonld.CyclicalContext', 'cyclic IRI mapping', ['context' => $local_ctx, 'term' => $term]); if($colon !== false) {
} $prefix = substr($term, 0, $colon);
if(property_exists($local_ctx, $prefix)) {
// now defining term // define parent prefix
$defined->{$term} = false; $this->_createTermDefinition(
$active_ctx, $local_ctx, $prefix, $defined);
if (self::_isKeyword($term)) { }
throw new JsonLdException(
'Invalid JSON-LD syntax; keywords cannot be overridden.', 'jsonld.SyntaxError', 'keyword redefinition', ['context' => $local_ctx, 'term' => $term]); if(property_exists($active_ctx->mappings, $prefix) &&
} $active_ctx->mappings->{$prefix}) {
// set @id based on prefix parent
// remove old mapping $suffix = substr($term, $colon + 1);
if (property_exists($active_ctx->mappings, $term)) { $mapping->{'@id'} = $active_ctx->mappings->{$prefix}->{'@id'} .
unset($active_ctx->mappings->{$term}); $suffix;
} } else {
// term is an absolute IRI
// get context term value $mapping->{'@id'} = $term;
$value = $local_ctx->{$term}; }
} else {
// clear context entry // non-IRIs *must* define @ids if @vocab is not available
if ($value === null || (is_object($value) && if(!property_exists($active_ctx, '@vocab')) {
self::_hasKeyValue($value, '@id', null))) { throw new JsonLdException(
$active_ctx->mappings->{$term} = null; 'Invalid JSON-LD syntax; @context terms must define an @id.',
$defined->{$term} = true; 'jsonld.SyntaxError', 'invalid IRI mapping',
return; array('context' => $local_ctx, 'term' => $term));
} }
// prepend vocab to term
// convert short-hand value to object w/@id $mapping->{'@id'} = $active_ctx->{'@vocab'} . $term;
if (is_string($value)) { }
$value = (object) ['@id' => $value]; }
}
// IRI mapping now defined
if (!is_object($value)) { $defined->{$term} = true;
throw new JsonLdException(
'Invalid JSON-LD syntax; @context property values must be ' . if(property_exists($value, '@type')) {
'strings or objects.', 'jsonld.SyntaxError', 'invalid term definition', ['context' => $local_ctx]); $type = $value->{'@type'};
} if(!is_string($type)) {
throw new JsonLdException(
// create new mapping 'Invalid JSON-LD syntax; @context @type values must be strings.',
$mapping = $active_ctx->mappings->{$term} = new stdClass(); 'jsonld.SyntaxError', 'invalid type mapping',
$mapping->reverse = false; array('context' => $local_ctx));
}
if (property_exists($value, '@reverse')) {
if (property_exists($value, '@id')) { if($type !== '@id' && $type !== '@vocab') {
throw new JsonLdException( // expand @type to full IRI
'Invalid JSON-LD syntax; a @reverse term definition must not ' + $type = $this->_expandIri(
'contain @id.', 'jsonld.SyntaxError', 'invalid reverse property', ['context' => $local_ctx]); $active_ctx, $type, array('vocab' => true), $local_ctx, $defined);
} if(!self::_isAbsoluteIri($type)) {
$reverse = $value->{'@reverse'}; throw new JsonLdException(
if (!is_string($reverse)) { 'Invalid JSON-LD syntax; an @context @type value must ' .
throw new JsonLdException( 'be an absolute IRI.', 'jsonld.SyntaxError',
'Invalid JSON-LD syntax; a @context @reverse value must be a string.', 'jsonld.SyntaxError', 'invalid IRI mapping', ['context' => $local_ctx]); 'invalid type mapping', array('context' => $local_ctx));
} }
if(strpos($type, '_:') === 0) {
// expand and add @id mapping throw new JsonLdException(
$id = $this->_expandIri( 'Invalid JSON-LD syntax; an @context @type values must ' .
$active_ctx, $reverse, ['vocab' => true, 'base' => false], $local_ctx, $defined); 'be an IRI, not a blank node identifier.',
if (!self::_isAbsoluteIri($id)) { 'jsonld.SyntaxError', 'invalid type mapping',
throw new JsonLdException( array('context' => $local_ctx));
'Invalid JSON-LD syntax; @context @reverse value must be ' . }
'an absolute IRI or a blank node identifier.', 'jsonld.SyntaxError', 'invalid IRI mapping', ['context' => $local_ctx]); }
}
$mapping->{'@id'} = $id; // add @type to mapping
$mapping->reverse = true; $mapping->{'@type'} = $type;
} else if (property_exists($value, '@id')) { }
$id = $value->{'@id'};
if (!is_string($id)) { if(property_exists($value, '@container')) {
throw new JsonLdException( $container = $value->{'@container'};
'Invalid JSON-LD syntax; @context @id value must be a string.', 'jsonld.SyntaxError', 'invalid IRI mapping', ['context' => $local_ctx]); if($container !== '@list' && $container !== '@set' &&
} $container !== '@index' && $container !== '@language') {
if ($id !== $term) { throw new JsonLdException(
// add @id to mapping 'Invalid JSON-LD syntax; @context @container value must be ' .
$id = $this->_expandIri( 'one of the following: @list, @set, @index, or @language.',
$active_ctx, $id, ['vocab' => true, 'base' => false], $local_ctx, $defined); 'jsonld.SyntaxError', 'invalid container mapping',
if (!self::_isAbsoluteIri($id) && !self::_isKeyword($id)) { array('context' => $local_ctx));
throw new JsonLdException( }
'Invalid JSON-LD syntax; @context @id value must be an ' . if($mapping->reverse && $container !== '@index' &&
'absolute IRI, a blank node identifier, or a keyword.', 'jsonld.SyntaxError', 'invalid IRI mapping', ['context' => $local_ctx]); $container !== '@set' && $container !== null) {
} throw new JsonLdException(
$mapping->{'@id'} = $id; 'Invalid JSON-LD syntax; @context @container value for a @reverse ' +
} 'type definition must be @index or @set.',
} 'jsonld.SyntaxError', 'invalid reverse property',
array('context' => $local_ctx));
// always compute whether term has a colon as an optimization for }
// _compactIri
$colon = strpos($term, ':'); // add @container to mapping
$mapping->_term_has_colon = ($colon !== false); $mapping->{'@container'} = $container;
}
if (!property_exists($mapping, '@id')) {
// see if the term has a prefix if(property_exists($value, '@language') &&
if ($mapping->_term_has_colon) { !property_exists($value, '@type')) {
$prefix = substr($term, 0, $colon); $language = $value->{'@language'};
if (property_exists($local_ctx, $prefix)) { if($language !== null && !is_string($language)) {
// define parent prefix throw new JsonLdException(
$this->_createTermDefinition( 'Invalid JSON-LD syntax; @context @language value must be ' .
$active_ctx, $local_ctx, $prefix, $defined); 'a string or null.', 'jsonld.SyntaxError',
} 'invalid language mapping', array('context' => $local_ctx));
}
if (property_exists($active_ctx->mappings, $prefix) &&
$active_ctx->mappings->{$prefix}) { // add @language to mapping
// set @id based on prefix parent if($language !== null) {
$suffix = substr($term, $colon + 1); $language = strtolower($language);
$mapping->{'@id'} = $active_ctx->mappings->{$prefix}->{'@id'} . }
$suffix; $mapping->{'@language'} = $language;
} else { }
// term is an absolute IRI
$mapping->{'@id'} = $term; // disallow aliasing @context and @preserve
} $id = $mapping->{'@id'};
} else { if($id === '@context' || $id === '@preserve') {
// non-IRIs *must* define @ids if @vocab is not available throw new JsonLdException(
if (!property_exists($active_ctx, '@vocab')) { 'Invalid JSON-LD syntax; @context and @preserve cannot be aliased.',
throw new JsonLdException( 'jsonld.SyntaxError', 'invalid keyword alias',
'Invalid JSON-LD syntax; @context terms must define an @id.', 'jsonld.SyntaxError', 'invalid IRI mapping', ['context' => $local_ctx, 'term' => $term]); array('context' => $local_ctx));
} }
// prepend vocab to term }
$mapping->{'@id'} = $active_ctx->{'@vocab'} . $term;
} /**
} * Expands a string to a full IRI. The string may be a term, a prefix, a
* relative IRI, or an absolute IRI. The associated absolute IRI will be
// optimization to store length of @id once for _compactIri * returned.
$mapping->_id_length = strlen($mapping->{'@id'}); *
* @param stdClass $active_ctx the current active context.
// IRI mapping now defined * @param string $value the string to expand.
$defined->{$term} = true; * @param assoc $relative_to options for how to resolve relative IRIs:
* base: true to resolve against the base IRI, false not to.
if (property_exists($value, '@type')) { * vocab: true to concatenate after @vocab, false not to.
$type = $value->{'@type'}; * @param stdClass $local_ctx the local context being processed (only given
if (!is_string($type)) { * if called during document processing).
throw new JsonLdException( * @param defined a map for tracking cycles in context definitions (only given
'Invalid JSON-LD syntax; @context @type values must be strings.', 'jsonld.SyntaxError', 'invalid type mapping', ['context' => $local_ctx]); * if called during document processing).
} *
* @return mixed the expanded value.
if ($type !== '@id' && $type !== '@vocab') { */
// expand @type to full IRI function _expandIri(
$type = $this->_expandIri( $active_ctx, $value, $relative_to=array(), $local_ctx=null, $defined=null) {
$active_ctx, $type, ['vocab' => true], $local_ctx, $defined); // already expanded
if (!self::_isAbsoluteIri($type)) { if($value === null || self::_isKeyword($value)) {
throw new JsonLdException( return $value;
'Invalid JSON-LD syntax; an @context @type value must ' . }
'be an absolute IRI.', 'jsonld.SyntaxError', 'invalid type mapping', ['context' => $local_ctx]);
} // define term dependency if not defined
if (strpos($type, '_:') === 0) { if($local_ctx !== null && property_exists($local_ctx, $value) &&
throw new JsonLdException( !self::_hasKeyValue($defined, $value, true)) {
'Invalid JSON-LD syntax; an @context @type values must ' . $this->_createTermDefinition($active_ctx, $local_ctx, $value, $defined);
'be an IRI, not a blank node identifier.', 'jsonld.SyntaxError', 'invalid type mapping', ['context' => $local_ctx]); }
}
} if(isset($relative_to['vocab']) && $relative_to['vocab']) {
if(property_exists($active_ctx->mappings, $value)) {
// add @type to mapping $mapping = $active_ctx->mappings->{$value};
$mapping->{'@type'} = $type;
} // value is explicitly ignored with a null mapping
if($mapping === null) {
if (property_exists($value, '@container')) { return null;
$container = $value->{'@container'}; }
if ($container !== '@list' && $container !== '@set' &&
$container !== '@index' && $container !== '@language') { // value is a term
throw new JsonLdException( return $mapping->{'@id'};
'Invalid JSON-LD syntax; @context @container value must be ' . }
'one of the following: @list, @set, @index, or @language.', 'jsonld.SyntaxError', 'invalid container mapping', ['context' => $local_ctx]); }
}
if ($mapping->reverse && $container !== '@index' && // split value into prefix:suffix
$container !== '@set' && $container !== null) { $colon = strpos($value, ':');
throw new JsonLdException( if($colon !== false) {
'Invalid JSON-LD syntax; @context @container value for a @reverse ' + $prefix = substr($value, 0, $colon);
'type definition must be @index or @set.', 'jsonld.SyntaxError', 'invalid reverse property', ['context' => $local_ctx]); $suffix = substr($value, $colon + 1);
}
// do not expand blank nodes (prefix of '_') or already-absolute
// add @container to mapping // IRIs (suffix of '//')
$mapping->{'@container'} = $container; if($prefix === '_' || strpos($suffix, '//') === 0) {
} return $value;
}
if (property_exists($value, '@language') &&
!property_exists($value, '@type')) { // prefix dependency not defined, define it
$language = $value->{'@language'}; if($local_ctx !== null && property_exists($local_ctx, $prefix)) {
if ($language !== null && !is_string($language)) { $this->_createTermDefinition(
throw new JsonLdException( $active_ctx, $local_ctx, $prefix, $defined);
'Invalid JSON-LD syntax; @context @language value must be ' . }
'a string or null.', 'jsonld.SyntaxError', 'invalid language mapping', ['context' => $local_ctx]);
} // use mapping if prefix is defined
if(property_exists($active_ctx->mappings, $prefix)) {
// add @language to mapping $mapping = $active_ctx->mappings->{$prefix};
if ($language !== null) { if($mapping) {
$language = strtolower($language); return $mapping->{'@id'} . $suffix;
} }
$mapping->{'@language'} = $language; }
}
// already absolute IRI
// disallow aliasing @context and @preserve return $value;
$id = $mapping->{'@id'}; }
if ($id === '@context' || $id === '@preserve') {
throw new JsonLdException( // prepend vocab
'Invalid JSON-LD syntax; @context and @preserve cannot be aliased.', 'jsonld.SyntaxError', 'invalid keyword alias', ['context' => $local_ctx]); if(isset($relative_to['vocab']) && $relative_to['vocab'] &&
} property_exists($active_ctx, '@vocab')) {
} return $active_ctx->{'@vocab'} . $value;
}
/**
* Expands a string to a full IRI. The string may be a term, a prefix, a // prepend base
* relative IRI, or an absolute IRI. The associated absolute IRI will be $rval = $value;
* returned. if(isset($relative_to['base']) && $relative_to['base']) {
* $rval = jsonld_prepend_base($active_ctx->{'@base'}, $rval);
* @param stdClass $active_ctx the current active context. }
* @param string $value the string to expand.
* @param assoc $relative_to options for how to resolve relative IRIs: return $rval;
* base: true to resolve against the base IRI, false not to. }
* vocab: true to concatenate after @vocab, false not to.
* @param stdClass $local_ctx the local context being processed (only given /**
* if called during document processing). * Finds all @context URLs in the given JSON-LD input.
* @param defined a map for tracking cycles in context definitions (only given *
* if called during document processing). * @param mixed $input the JSON-LD input.
* * @param stdClass $urls a map of URLs (url => false/@contexts).
* @return mixed the expanded value. * @param bool $replace true to replace the URLs in the given input with
*/ * the @contexts from the urls map, false not to.
function _expandIri( * @param string $base the base URL to resolve relative URLs with.
$active_ctx, $value, $relative_to = [], $local_ctx = null, $defined = null) */
{ protected function _findContextUrls($input, $urls, $replace, $base) {
// already expanded if(is_array($input)) {
if ($value === null || self::_isKeyword($value)) { foreach($input as $e) {
return $value; $this->_findContextUrls($e, $urls, $replace, $base);
} }
} else if(is_object($input)) {
// define term dependency if not defined foreach($input as $k => &$v) {
if ($local_ctx !== null && property_exists($local_ctx, $value) && if($k !== '@context') {
!self::_hasKeyValue($defined, $value, true)) { $this->_findContextUrls($v, $urls, $replace, $base);
$this->_createTermDefinition($active_ctx, $local_ctx, $value, $defined); continue;
} }
if (isset($relative_to['vocab']) && $relative_to['vocab']) { // array @context
if (property_exists($active_ctx->mappings, $value)) { if(is_array($v)) {
$mapping = $active_ctx->mappings->{$value}; $length = count($v);
for($i = 0; $i < $length; ++$i) {
// value is explicitly ignored with a null mapping if(is_string($v[$i])) {
if ($mapping === null) { $url = jsonld_prepend_base($base, $v[$i]);
return null; // replace w/@context if requested
} if($replace) {
$ctx = $urls->{$url};
// value is a term if(is_array($ctx)) {
return $mapping->{'@id'}; // add flattened context
} array_splice($v, $i, 1, $ctx);
} $i += count($ctx);
$length += count($ctx);
// split value into prefix:suffix } else {
$colon = strpos($value, ':'); $v[$i] = $ctx;
if ($colon !== false) { }
$prefix = substr($value, 0, $colon); } else if(!property_exists($urls, $url)) {
$suffix = substr($value, $colon + 1); // @context URL found
$urls->{$url} = false;
// do not expand blank nodes (prefix of '_') or already-absolute }
// IRIs (suffix of '//') }
if ($prefix === '_' || strpos($suffix, '//') === 0) { }
return $value; } else if(is_string($v)) {
} // string @context
$v = jsonld_prepend_base($base, $v);
// prefix dependency not defined, define it // replace w/@context if requested
if ($local_ctx !== null && property_exists($local_ctx, $prefix)) { if($replace) {
$this->_createTermDefinition( $input->{$k} = $urls->{$v};
$active_ctx, $local_ctx, $prefix, $defined); } else if(!property_exists($urls, $v)) {
} // @context URL found
$urls->{$v} = false;
// use mapping if prefix is defined }
if (property_exists($active_ctx->mappings, $prefix)) { }
$mapping = $active_ctx->mappings->{$prefix}; }
if ($mapping) { }
return $mapping->{'@id'} . $suffix; }
}
} /**
* Retrieves external @context URLs using the given document loader. Each
// already absolute IRI * instance of @context in the input that refers to a URL will be replaced
return $value; * with the JSON @context found at that URL.
} *
* @param mixed $input the JSON-LD input with possible contexts.
// prepend vocab * @param stdClass $cycles an object for tracking context cycles.
if (isset($relative_to['vocab']) && $relative_to['vocab'] && * @param callable $load_document(url) the document loader.
property_exists($active_ctx, '@vocab')) { * @param base $base the base URL to resolve relative URLs against.
return $active_ctx->{'@vocab'} . $value; *
} * @return mixed the result.
*/
// prepend base protected function _retrieveContextUrls(
$rval = $value; &$input, $cycles, $load_document, $base='') {
if (isset($relative_to['base']) && $relative_to['base']) { if(count(get_object_vars($cycles)) > self::MAX_CONTEXT_URLS) {
$rval = jsonld_prepend_base($active_ctx->{'@base'}, $rval); throw new JsonLdException(
} 'Maximum number of @context URLs exceeded.',
'jsonld.ContextUrlError', 'loading remote context failed',
return $rval; array('max' => self::MAX_CONTEXT_URLS));
} }
/** // for tracking the URLs to retrieve
* Finds all @context URLs in the given JSON-LD input. $urls = new stdClass();
*
* @param mixed $input the JSON-LD input. // regex for validating URLs
* @param stdClass $urls a map of URLs (url => false/@contexts). $regex = '/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/';
* @param bool $replace true to replace the URLs in the given input with
* the @contexts from the urls map, false not to. // find all URLs in the given input
* @param string $base the base URL to resolve relative URLs with. $this->_findContextUrls($input, $urls, false, $base);
*/
protected function _findContextUrls($input, $urls, $replace, $base) // queue all unretrieved URLs
{ $queue = array();
if (is_array($input)) { foreach($urls as $url => $ctx) {
foreach ($input as $e) { if($ctx === false) {
$this->_findContextUrls($e, $urls, $replace, $base); // validate URL
} if(!preg_match($regex, $url)) {
} else if (is_object($input)) { throw new JsonLdException(
foreach ($input as $k => &$v) { 'Malformed or unsupported URL.', 'jsonld.InvalidUrl',
if ($k !== '@context') { 'loading remote context failed', array('url' => $url));
$this->_findContextUrls($v, $urls, $replace, $base); }
continue; $queue[] = $url;
} }
}
// array @context
if (is_array($v)) { // retrieve URLs in queue
$length = count($v); foreach($queue as $url) {
for ($i = 0; $i < $length; ++$i) { // check for context URL cycle
if (is_string($v[$i])) { if(property_exists($cycles, $url)) {
$url = jsonld_prepend_base($base, $v[$i]); throw new JsonLdException(
// replace w/@context if requested 'Cyclical @context URLs detected.',
if ($replace) { 'jsonld.ContextUrlError', 'recursive context inclusion',
$ctx = $urls->{$url}; array('url' => $url));
if (is_array($ctx)) { }
// add flattened context $_cycles = self::copy($cycles);
array_splice($v, $i, 1, $ctx); $_cycles->{$url} = true;
$i += count($ctx) - 1;
$length = count($v); // retrieve URL
} else { $remote_doc = call_user_func($load_document, $url);
$v[$i] = $ctx; $ctx = $remote_doc->document;
}
} else if (!property_exists($urls, $url)) { // parse string context as JSON
// @context URL found if(is_string($ctx)) {
$urls->{$url} = false; try {
} $ctx = self::_parse_json($ctx);
} } catch(Exception $e) {
} throw new JsonLdException(
} else if (is_string($v)) { 'Could not parse JSON from URL.',
// string @context 'jsonld.ParseError', 'loading remote context failed',
$v = jsonld_prepend_base($base, $v); array('url' => $url), $e);
// replace w/@context if requested }
if ($replace) { }
$input->{$k} = $urls->{$v};
} else if (!property_exists($urls, $v)) { // ensure ctx is an object
// @context URL found if(!is_object($ctx)) {
$urls->{$v} = false; throw new JsonLdException(
} 'Derefencing a URL did not result in a valid JSON-LD object.',
} 'jsonld.InvalidUrl', 'invalid remote context', array('url' => $url));
} }
}
} // use empty context if no @context key is present
if(!property_exists($ctx, '@context')) {
/** $ctx = (object)array('@context' => new stdClass());
* Retrieves external @context URLs using the given document loader. Each } else {
* instance of @context in the input that refers to a URL will be replaced $ctx = (object)array('@context' => $ctx->{'@context'});
* with the JSON @context found at that URL. }
*
* @param mixed $input the JSON-LD input with possible contexts. // append context URL to context if given
* @param stdClass $cycles an object for tracking context cycles. if($remote_doc->contextUrl !== null) {
* @param callable $load_document(url) the document loader. $ctx->{'@context'} = self::arrayify($ctx->{'@context'});
* @param base $base the base URL to resolve relative URLs against. $ctx->{'@context'}[] = $remote_doc->contextUrl;
* }
* @return mixed the result.
*/ // recurse
protected function _retrieveContextUrls( $this->_retrieveContextUrls($ctx, $_cycles, $load_document, $url);
&$input, $cycles, $load_document, $base = '') $urls->{$url} = $ctx->{'@context'};
{ }
if (count(get_object_vars($cycles)) > self::MAX_CONTEXT_URLS) {
throw new JsonLdException( // replace all URLS in the input
'Maximum number of @context URLs exceeded.', 'jsonld.ContextUrlError', 'loading remote context failed', ['max' => self::MAX_CONTEXT_URLS]); $this->_findContextUrls($input, $urls, true, $base);
} }
// for tracking the URLs to retrieve /**
$urls = new stdClass(); * Gets the initial context.
*
// regex for validating URLs * @param assoc $options the options to use.
$regex = '/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/'; * base the document base IRI.
*
// find all URLs in the given input * @return stdClass the initial context.
$this->_findContextUrls($input, $urls, false, $base); */
protected function _getInitialContext($options) {
// queue all unretrieved URLs return (object)array(
$queue = []; '@base' => jsonld_parse_url($options['base']),
foreach ($urls as $url => $ctx) { 'mappings' => new stdClass(),
if ($ctx === false) { 'inverse' => null);
// validate URL }
if (!preg_match($regex, $url)) {
throw new JsonLdException( /**
'Malformed or unsupported URL.', 'jsonld.InvalidUrl', 'loading remote context failed', ['url' => $url]); * Generates an inverse context for use in the compaction algorithm, if
} * not already generated for the given active context.
$queue[] = $url; *
} * @param stdClass $active_ctx the active context to use.
} *
* @return stdClass the inverse context.
// retrieve URLs in queue */
foreach ($queue as $url) { protected function _getInverseContext($active_ctx) {
// check for context URL cycle // inverse context already generated
if (property_exists($cycles, $url)) { if($active_ctx->inverse) {
throw new JsonLdException( return $active_ctx->inverse;
'Cyclical @context URLs detected.', 'jsonld.ContextUrlError', 'recursive context inclusion', ['url' => $url]); }
}
$_cycles = self::copy($cycles); $inverse = $active_ctx->inverse = new stdClass();
$_cycles->{$url} = true;
// handle default language
// retrieve URL $default_language = '@none';
$remote_doc = call_user_func($load_document, $url); if(property_exists($active_ctx, '@language')) {
$ctx = $remote_doc->document; $default_language = $active_ctx->{'@language'};
}
// parse string context as JSON
if (is_string($ctx)) { // create term selections for each mapping in the context, ordered by
try { // shortest and then lexicographically least
$ctx = self::_parse_json($ctx); $mappings = $active_ctx->mappings;
} catch (Exception $e) { $terms = array_keys((array)$mappings);
throw new JsonLdException( usort($terms, array($this, '_compareShortestLeast'));
'Could not parse JSON from URL.', 'jsonld.ParseError', 'loading remote context failed', ['url' => $url], $e); foreach($terms as $term) {
} $mapping = $mappings->{$term};
} if($mapping === null) {
continue;
// ensure ctx is an object }
if (!is_object($ctx)) {
throw new JsonLdException( // add term selection where it applies
'Derefencing a URL did not result in a valid JSON-LD object.', 'jsonld.InvalidUrl', 'invalid remote context', ['url' => $url]); if(property_exists($mapping, '@container')) {
} $container = $mapping->{'@container'};
} else {
// use empty context if no @context key is present $container = '@none';
if (!property_exists($ctx, '@context')) { }
$ctx = (object) ['@context' => new stdClass()];
} else { // iterate over every IRI in the mapping
$ctx = (object) ['@context' => $ctx->{'@context'}]; $iris = $mapping->{'@id'};
} $iris = self::arrayify($iris);
foreach($iris as $iri) {
// append context URL to context if given // initialize container map
if ($remote_doc->contextUrl !== null) { if(!property_exists($inverse, $iri)) {
$ctx->{'@context'} = self::arrayify($ctx->{'@context'}); $inverse->{$iri} = new stdClass();
$ctx->{'@context'}[] = $remote_doc->contextUrl; }
} $container_map = $inverse->{$iri};
// recurse // add new entry
$this->_retrieveContextUrls($ctx, $_cycles, $load_document, $url); if(!property_exists($container_map, $container)) {
$urls->{$url} = $ctx->{'@context'}; $container_map->{$container} = (object)array(
} '@language' => new stdClass(),
'@type' => new stdClass());
// replace all URLS in the input }
$this->_findContextUrls($input, $urls, true, $base); $entry = $container_map->{$container};
}
if($mapping->reverse) {
/** // term is preferred for values using @reverse
* Gets the initial context. $this->_addPreferredTerm(
* $mapping, $term, $entry->{'@type'}, '@reverse');
* @param assoc $options the options to use. } else if(property_exists($mapping, '@type')) {
* base the document base IRI. // term is preferred for values using specific type
* $this->_addPreferredTerm(
* @return stdClass the initial context. $mapping, $term, $entry->{'@type'}, $mapping->{'@type'});
*/ } else if(property_exists($mapping, '@language')) {
protected function _getInitialContext($options) // term is preferred for values using specific language
{ $language = $mapping->{'@language'};
return (object) [ if($language === null) {
'@base' => jsonld_parse_url($options['base']), $language = '@null';
'mappings' => new stdClass(), }
'inverse' => null]; $this->_addPreferredTerm(
} $mapping, $term, $entry->{'@language'}, $language);
} else {
/** // term is preferred for values w/default language or no type and
* Generates an inverse context for use in the compaction algorithm, if // no language
* not already generated for the given active context. // add an entry for the default language
* $this->_addPreferredTerm(
* @param stdClass $active_ctx the active context to use. $mapping, $term, $entry->{'@language'}, $default_language);
*
* @return stdClass the inverse context. // add entries for no type and no language
*/ $this->_addPreferredTerm(
protected function _getInverseContext($active_ctx) $mapping, $term, $entry->{'@type'}, '@none');
{ $this->_addPreferredTerm(
// inverse context already generated $mapping, $term, $entry->{'@language'}, '@none');
if ($active_ctx->inverse) { }
return $active_ctx->inverse; }
} }
$inverse = $active_ctx->inverse = new stdClass(); return $inverse;
}
// variables for building fast CURIE map
$fast_curie_map = $active_ctx->fast_curie_map = new ArrayObject(); /**
$iris_to_terms = []; * Adds the term for the given entry if not already added.
*
// handle default language * @param stdClass $mapping the term mapping.
$default_language = '@none'; * @param string $term the term to add.
if (property_exists($active_ctx, '@language')) { * @param stdClass $entry the inverse context type_or_language entry to
$default_language = $active_ctx->{'@language'}; * add to.
} * @param string $type_or_language_value the key in the entry to add to.
*/
// create term selections for each mapping in the context, ordered by function _addPreferredTerm($mapping, $term, $entry, $type_or_language_value) {
// shortest and then lexicographically least if(!property_exists($entry, $type_or_language_value)) {
$mappings = $active_ctx->mappings; $entry->{$type_or_language_value} = $term;
$terms = array_keys((array) $mappings); }
usort($terms, [$this, '_compareShortestLeast']); }
foreach ($terms as $term) {
$mapping = $mappings->{$term}; /**
if ($mapping === null) { * Clones an active context, creating a child active context.
continue; *
} * @return stdClass a clone (child) of the active context.
*/
// add term selection where it applies protected function _cloneActiveContext($active_ctx) {
if (property_exists($mapping, '@container')) { $child = new stdClass();
$container = $mapping->{'@container'}; $child->{'@base'} = $active_ctx->{'@base'};
} else { $child->mappings = self::copy($active_ctx->mappings);
$container = '@none'; $child->inverse = null;
} if(property_exists($active_ctx, '@language')) {
$child->{'@language'} = $active_ctx->{'@language'};
// iterate over every IRI in the mapping }
$iris = $mapping->{'@id'}; if(property_exists($active_ctx, '@vocab')) {
$iris = self::arrayify($iris); $child->{'@vocab'} = $active_ctx->{'@vocab'};
foreach ($iris as $iri) { }
$is_keyword = self::_isKeyword($iri); return $child;
}
// initialize container map
if (!property_exists($inverse, $iri)) { /**
$inverse->{$iri} = new stdClass(); * Returns whether or not the given value is a keyword.
if (!$is_keyword && !$mapping->_term_has_colon) { *
// init IRI to term map and fast CURIE map * @param string $v the value to check.
$iris_to_terms[$iri] = new ArrayObject(); *
$iris_to_terms[$iri][] = $term; * @return bool true if the value is a keyword, false if not.
$fast_curie_entry = (object) [ */
'iri' => $iri, 'terms' => $iris_to_terms[$iri]]; protected static function _isKeyword($v) {
if (!array_key_exists($iri[0], (array) $fast_curie_map)) { if(!is_string($v)) {
$fast_curie_map[$iri[0]] = new ArrayObject(); return false;
} }
$fast_curie_map[$iri[0]][] = $fast_curie_entry; switch($v) {
} case '@base':
} else if (!$is_keyword && !$mapping->_term_has_colon) { case '@context':
// add IRI to term match case '@container':
$iris_to_terms[$iri][] = $term; case '@default':
} case '@embed':
$container_map = $inverse->{$iri}; case '@explicit':
case '@graph':
// add new entry case '@id':
if (!property_exists($container_map, $container)) { case '@index':
$container_map->{$container} = (object) [ case '@language':
'@language' => new stdClass(), case '@list':
'@type' => new stdClass()]; case '@omitDefault':
} case '@preserve':
$entry = $container_map->{$container}; case '@reverse':
case '@set':
if ($mapping->reverse) { case '@type':
// term is preferred for values using @reverse case '@value':
$this->_addPreferredTerm( case '@vocab':
$mapping, $term, $entry->{'@type'}, '@reverse'); return true;
} else if (property_exists($mapping, '@type')) { }
// term is preferred for values using specific type return false;
$this->_addPreferredTerm( }
$mapping, $term, $entry->{'@type'}, $mapping->{'@type'});
} else if (property_exists($mapping, '@language')) { /**
// term is preferred for values using specific language * Returns true if the given value is an empty Object.
$language = $mapping->{'@language'}; *
if ($language === null) { * @param mixed $v the value to check.
$language = '@null'; *
} * @return bool true if the value is an empty Object, false if not.
$this->_addPreferredTerm( */
$mapping, $term, $entry->{'@language'}, $language); protected static function _isEmptyObject($v) {
} else { return is_object($v) && count(get_object_vars($v)) === 0;
// term is preferred for values w/default language or no type and }
// no language
// add an entry for the default language /**
$this->_addPreferredTerm( * Throws an exception if the given value is not a valid @type value.
$mapping, $term, $entry->{'@language'}, $default_language); *
* @param mixed $v the value to check.
// add entries for no type and no language */
$this->_addPreferredTerm( protected static function _validateTypeValue($v) {
$mapping, $term, $entry->{'@type'}, '@none'); // must be a string or empty object
$this->_addPreferredTerm( if(is_string($v) || self::_isEmptyObject($v)) {
$mapping, $term, $entry->{'@language'}, '@none'); return;
} }
}
} // must be an array
$is_valid = false;
// build fast CURIE map if(is_array($v)) {
foreach ($fast_curie_map as $key => $value) { // must contain only strings
$this->_buildIriMap($fast_curie_map, $key, 1); $is_valid = true;
} foreach($v as $e) {
if(!(is_string($e))) {
return $inverse; $is_valid = false;
} break;
}
/** }
* Runs a recursive algorithm to build a lookup map for quickly finding }
* potential CURIEs.
* if(!$is_valid) {
* @param ArrayObject $iri_map the map to build. throw new JsonLdException(
* @param string $key the current key in the map to work on. 'Invalid JSON-LD syntax; "@type" value must a string, an array ' .
* @param int $idx the index into the IRI to compare. 'of strings, or an empty object.',
*/ 'jsonld.SyntaxError', 'invalid type value', array('value' => $v));
function _buildIriMap($iri_map, $key, $idx) }
{ }
$entries = $iri_map[$key];
$next = $iri_map[$key] = new ArrayObject(); /**
* Returns true if the given value is a subject with properties.
foreach ($entries as $entry) { *
$iri = $entry->iri; * @param mixed $v the value to check.
if ($idx >= strlen($iri)) { *
$letter = ''; * @return bool true if the value is a subject with properties, false if not.
} else { */
$letter = $iri[$idx]; protected static function _isSubject($v) {
} // Note: A value is a subject if all of these hold true:
if (!isset($next[$letter])) { // 1. It is an Object.
$next[$letter] = new ArrayObject(); // 2. It is not a @value, @set, or @list.
} // 3. It has more than 1 key OR any existing key is not @id.
$next[$letter][] = $entry; $rval = false;
} if(is_object($v) &&
!property_exists($v, '@value') &&
foreach ($next as $key => $value) { !property_exists($v, '@set') &&
if ($key === '') { !property_exists($v, '@list')) {
continue; $count = count(get_object_vars($v));
} $rval = ($count > 1 || !property_exists($v, '@id'));
$this->_buildIriMap($next, $key, $idx + 1); }
} return $rval;
} }
/** /**
* Adds the term for the given entry if not already added. * Returns true if the given value is a subject reference.
* *
* @param stdClass $mapping the term mapping. * @param mixed $v the value to check.
* @param string $term the term to add. *
* @param stdClass $entry the inverse context type_or_language entry to * @return bool true if the value is a subject reference, false if not.
* add to. */
* @param string $type_or_language_value the key in the entry to add to. protected static function _isSubjectReference($v) {
*/ // Note: A value is a subject reference if all of these hold true:
function _addPreferredTerm($mapping, $term, $entry, $type_or_language_value) // 1. It is an Object.
{ // 2. It has a single key: @id.
if (!property_exists($entry, $type_or_language_value)) { return (is_object($v) && count(get_object_vars($v)) === 1 &&
$entry->{$type_or_language_value} = $term; property_exists($v, '@id'));
} }
}
/**
/** * Returns true if the given value is a @value.
* Clones an active context, creating a child active context. *
* * @param mixed $v the value to check.
* @return stdClass a clone (child) of the active context. *
*/ * @return bool true if the value is a @value, false if not.
protected function _cloneActiveContext($active_ctx) */
{ protected static function _isValue($v) {
$child = new stdClass(); // Note: A value is a @value if all of these hold true:
$child->{'@base'} = $active_ctx->{'@base'}; // 1. It is an Object.
$child->mappings = self::copy($active_ctx->mappings); // 2. It has the @value property.
$child->inverse = null; return is_object($v) && property_exists($v, '@value');
if (property_exists($active_ctx, '@language')) { }
$child->{'@language'} = $active_ctx->{'@language'};
} /**
if (property_exists($active_ctx, '@vocab')) { * Returns true if the given value is a @list.
$child->{'@vocab'} = $active_ctx->{'@vocab'}; *
} * @param mixed $v the value to check.
return $child; *
} * @return bool true if the value is a @list, false if not.
*/
/** protected static function _isList($v) {
* Returns whether or not the given value is a keyword. // Note: A value is a @list if all of these hold true:
* // 1. It is an Object.
* @param string $v the value to check. // 2. It has the @list property.
* return is_object($v) && property_exists($v, '@list');
* @return bool true if the value is a keyword, false if not. }
*/
protected static function _isKeyword($v) /**
{ * Returns true if the given value is a blank node.
if (!is_string($v)) { *
return false; * @param mixed $v the value to check.
} *
switch ($v) { * @return bool true if the value is a blank node, false if not.
case '@base': */
case '@context': protected static function _isBlankNode($v) {
case '@container': // Note: A value is a blank node if all of these hold true:
case '@default': // 1. It is an Object.
case '@embed': // 2. If it has an @id key its value begins with '_:'.
case '@explicit': // 3. It has no keys OR is not a @value, @set, or @list.
case '@graph': $rval = false;
case '@id': if(is_object($v)) {
case '@index': if(property_exists($v, '@id')) {
case '@language': $rval = (strpos($v->{'@id'}, '_:') === 0);
case '@list': } else {
case '@omitDefault': $rval = (count(get_object_vars($v)) === 0 ||
case '@preserve': !(property_exists($v, '@value') ||
case '@requireAll': property_exists($v, '@set') ||
case '@reverse': property_exists($v, '@list')));
case '@set': }
case '@type': }
case '@value': return $rval;
case '@vocab': }
return true;
} /**
return false; * Returns true if the given value is an absolute IRI, false if not.
} *
* @param string $v the value to check.
/** *
* Returns true if the given value is an empty Object. * @return bool true if the value is an absolute IRI, false if not.
* */
* @param mixed $v the value to check. protected static function _isAbsoluteIri($v) {
* return strpos($v, ':') !== false;
* @return bool true if the value is an empty Object, false if not. }
*/
protected static function _isEmptyObject($v) /**
{ * Returns true if the given target has the given key and its
return is_object($v) && count(get_object_vars($v)) === 0; * value equals is the given value.
} *
* @param stdClass $target the target object.
/** * @param string key the key to check.
* Throws an exception if the given value is not a valid @type value. * @param mixed $value the value to check.
* *
* @param mixed $v the value to check. * @return bool true if the target has the given key and its value matches.
*/ */
protected static function _validateTypeValue($v) protected static function _hasKeyValue($target, $key, $value) {
{ return (property_exists($target, $key) && $target->{$key} === $value);
// must be a string or empty object }
if (is_string($v) || self::_isEmptyObject($v)) {
return; /**
} * Returns true if both of the given objects have the same value for the
* given key or if neither of the objects contain the given key.
// must be an array *
$is_valid = false; * @param stdClass $o1 the first object.
if (is_array($v)) { * @param stdClass $o2 the second object.
// must contain only strings * @param string key the key to check.
$is_valid = true; *
foreach ($v as $e) { * @return bool true if both objects have the same value for the key or
if (!(is_string($e))) { * neither has the key.
$is_valid = false; */
break; protected static function _compareKeyValues($o1, $o2, $key) {
} if(property_exists($o1, $key)) {
} return property_exists($o2, $key) && $o1->{$key} === $o2->{$key};
} }
return !property_exists($o2, $key);
if (!$is_valid) { }
throw new JsonLdException(
'Invalid JSON-LD syntax; "@type" value must a string, an array ' . /**
'of strings, or an empty object.', 'jsonld.SyntaxError', 'invalid type value', ['value' => $v]); * Parses JSON and sets an appropriate exception message on error.
} *
} * @param string $json the JSON to parse.
*
/** * @return mixed the parsed JSON object or array.
* Returns true if the given value is a subject with properties. */
* protected static function _parse_json($json) {
* @param mixed $v the value to check. $rval = json_decode($json);
* $error = json_last_error();
* @return bool true if the value is a subject with properties, false if not. if($error === JSON_ERROR_NONE && $rval === null) {
*/ $error = JSON_ERROR_SYNTAX;
protected static function _isSubject($v) }
{ switch($error) {
// Note: A value is a subject if all of these hold true: case JSON_ERROR_NONE:
// 1. It is an Object. break;
// 2. It is not a @value, @set, or @list. case JSON_ERROR_DEPTH:
// 3. It has more than 1 key OR any existing key is not @id. throw new JsonLdException(
$rval = false; 'Could not parse JSON; the maximum stack depth has been exceeded.',
if (is_object($v) && 'jsonld.ParseError');
!property_exists($v, '@value') && case JSON_ERROR_STATE_MISMATCH:
!property_exists($v, '@set') && throw new JsonLdException(
!property_exists($v, '@list')) { 'Could not parse JSON; invalid or malformed JSON.',
$count = count(get_object_vars($v)); 'jsonld.ParseError');
$rval = ($count > 1 || !property_exists($v, '@id')); case JSON_ERROR_CTRL_CHAR:
} case JSON_ERROR_SYNTAX:
return $rval; throw new JsonLdException(
} 'Could not parse JSON; syntax error, malformed JSON.',
'jsonld.ParseError');
/** case JSON_ERROR_UTF8:
* Returns true if the given value is a subject reference. throw new JsonLdException(
* 'Could not parse JSON from URL; malformed UTF-8 characters.',
* @param mixed $v the value to check. 'jsonld.ParseError');
* default:
* @return bool true if the value is a subject reference, false if not. throw new JsonLdException(
*/ 'Could not parse JSON from URL; unknown error.',
protected static function _isSubjectReference($v) 'jsonld.ParseError');
{ }
// Note: A value is a subject reference if all of these hold true: return $rval;
// 1. It is an Object. }
// 2. It has a single key: @id.
return (is_object($v) && count(get_object_vars($v)) === 1 &&
property_exists($v, '@id'));
}
/**
* Returns true if the given value is a @value.
*
* @param mixed $v the value to check.
*
* @return bool true if the value is a @value, false if not.
*/
protected static function _isValue($v)
{
// Note: A value is a @value if all of these hold true:
// 1. It is an Object.
// 2. It has the @value property.
return is_object($v) && property_exists($v, '@value');
}
/**
* Returns true if the given value is a @list.
*
* @param mixed $v the value to check.
*
* @return bool true if the value is a @list, false if not.
*/
protected static function _isList($v)
{
// Note: A value is a @list if all of these hold true:
// 1. It is an Object.
// 2. It has the @list property.
return is_object($v) && property_exists($v, '@list');
}
/**
* Returns true if the given value is a blank node.
*
* @param mixed $v the value to check.
*
* @return bool true if the value is a blank node, false if not.
*/
protected static function _isBlankNode($v)
{
// Note: A value is a blank node if all of these hold true:
// 1. It is an Object.
// 2. If it has an @id key its value begins with '_:'.
// 3. It has no keys OR is not a @value, @set, or @list.
$rval = false;
if (is_object($v)) {
if (property_exists($v, '@id')) {
$rval = (strpos($v->{'@id'}, '_:') === 0);
} else {
$rval = (count(get_object_vars($v)) === 0 ||
!(property_exists($v, '@value') ||
property_exists($v, '@set') ||
property_exists($v, '@list')));
}
}
return $rval;
}
/**
* Returns true if the given value is an absolute IRI, false if not.
*
* @param string $v the value to check.
*
* @return bool true if the value is an absolute IRI, false if not.
*/
protected static function _isAbsoluteIri($v)
{
return strpos($v, ':') !== false;
}
/**
* Returns true if the given target has the given key and its
* value equals is the given value.
*
* @param stdClass $target the target object.
* @param string key the key to check.
* @param mixed $value the value to check.
*
* @return bool true if the target has the given key and its value matches.
*/
protected static function _hasKeyValue($target, $key, $value)
{
return (property_exists($target, $key) && $target->{$key} === $value);
}
/**
* Returns true if both of the given objects have the same value for the
* given key or if neither of the objects contain the given key.
*
* @param stdClass $o1 the first object.
* @param stdClass $o2 the second object.
* @param string key the key to check.
*
* @return bool true if both objects have the same value for the key or
* neither has the key.
*/
protected static function _compareKeyValues($o1, $o2, $key)
{
if (property_exists($o1, $key)) {
return property_exists($o2, $key) && $o1->{$key} === $o2->{$key};
}
return !property_exists($o2, $key);
}
/**
* Parses JSON and sets an appropriate exception message on error.
*
* @param string $json the JSON to parse.
*
* @return mixed the parsed JSON object or array.
*/
protected static function _parse_json($json)
{
$rval = json_decode($json);
$error = json_last_error();
if ($error === JSON_ERROR_NONE && $rval === null) {
$error = JSON_ERROR_SYNTAX;
}
switch ($error) {
case JSON_ERROR_NONE:
break;
case JSON_ERROR_DEPTH:
throw new JsonLdException(
'Could not parse JSON; the maximum stack depth has been exceeded.', 'jsonld.ParseError');
case JSON_ERROR_STATE_MISMATCH:
throw new JsonLdException(
'Could not parse JSON; invalid or malformed JSON.', 'jsonld.ParseError');
case JSON_ERROR_CTRL_CHAR:
case JSON_ERROR_SYNTAX:
throw new JsonLdException(
'Could not parse JSON; syntax error, malformed JSON.', 'jsonld.ParseError');
case JSON_ERROR_UTF8:
throw new JsonLdException(
'Could not parse JSON from URL; malformed UTF-8 characters.', 'jsonld.ParseError');
default:
throw new JsonLdException(
'Could not parse JSON from URL; unknown error.', 'jsonld.ParseError');
}
return $rval;
}
} }
// register the N-Quads RDF parser // register the N-Quads RDF parser
jsonld_register_rdf_parser( jsonld_register_rdf_parser(
'application/nquads', ['JsonLdProcessor', 'parseNQuads']); 'application/nquads', array('JsonLdProcessor', 'parseNQuads'));
/** /**
* A JSON-LD Exception. * A JSON-LD Exception.
*/ */
class JsonLdException extends Exception class JsonLdException extends Exception {
{ public function __construct(
$msg, $type, $code='error', $details=null, $previous=null) {
public function __construct( $this->type = $type;
$msg, $type, $code = 'error', $details = null, $previous = null) $this->code = $code;
{ $this->details = $details;
$this->type = $type; $this->cause = $previous;
$this->code = $code; parent::__construct($msg, 0, $previous);
$this->details = $details; }
$this->cause = $previous; public function __toString() {
parent::__construct($msg, 0, $previous); $rval = __CLASS__ . ": [{$this->type}]: {$this->message}\n";
} if($this->code) {
$rval .= 'Code: ' . $this->code . "\n";
public function __toString() }
{ if($this->details) {
$rval = __CLASS__ . ": [{$this->type}]: {$this->message}\n"; $rval .= 'Details: ' . print_r($this->details, true) . "\n";
if ($this->code) { }
$rval .= 'Code: ' . $this->code . "\n"; if($this->cause) {
} $rval .= 'Cause: ' . $this->cause;
if ($this->details) { }
$rval .= 'Details: ' . print_r($this->details, true) . "\n"; $rval .= $this->getTraceAsString() . "\n";
} return $rval;
if ($this->cause) { }
$rval .= 'Cause: ' . $this->cause; };
}
$rval .= $this->getTraceAsString() . "\n";
return $rval;
}
}
;
/** /**
* A UniqueNamer issues unique names, keeping track of any previously issued * A UniqueNamer issues unique names, keeping track of any previously issued
* names. * names.
*/ */
class UniqueNamer class UniqueNamer {
{ /**
* Constructs a new UniqueNamer.
*
* @param prefix the prefix to use ('<prefix><counter>').
*/
public function __construct($prefix) {
$this->prefix = $prefix;
$this->counter = 0;
$this->existing = new stdClass();
$this->order = array();
}
/** /**
* Constructs a new UniqueNamer. * Clones this UniqueNamer.
* */
* @param prefix the prefix to use ('<prefix><counter>'). public function __clone() {
*/ $this->existing = clone $this->existing;
public function __construct($prefix) }
{
$this->prefix = $prefix;
$this->counter = 0;
$this->existing = new stdClass();
$this->order = [];
}
/** /**
* Clones this UniqueNamer. * Gets the new name for the given old name, where if no old name is given
*/ * a new name will be generated.
public function __clone() *
{ * @param mixed [$old_name] the old name to get the new name for.
$this->existing = clone $this->existing; *
} * @return string the new name.
*/
public function getName($old_name=null) {
// return existing old name
if($old_name && property_exists($this->existing, $old_name)) {
return $this->existing->{$old_name};
}
/** // get next name
* Gets the new name for the given old name, where if no old name is given $name = $this->prefix . $this->counter;
* a new name will be generated. $this->counter += 1;
*
* @param mixed [$old_name] the old name to get the new name for.
*
* @return string the new name.
*/
public function getName($old_name = null)
{
// return existing old name
if ($old_name && property_exists($this->existing, $old_name)) {
return $this->existing->{$old_name};
}
// get next name // save mapping
$name = $this->prefix . $this->counter; if($old_name !== null) {
$this->counter += 1; $this->existing->{$old_name} = $name;
$this->order[] = $old_name;
}
// save mapping return $name;
if ($old_name !== null) { }
$this->existing->{$old_name} = $name;
$this->order[] = $old_name;
}
return $name;
}
/**
* Returns true if the given old name has already been assigned a new name.
*
* @param string $old_name the old name to check.
*
* @return true if the old name has been assigned a new name, false if not.
*/
public function isNamed($old_name)
{
return property_exists($this->existing, $old_name);
}
/**
* Returns true if the given old name has already been assigned a new name.
*
* @param string $old_name the old name to check.
*
* @return true if the old name has been assigned a new name, false if not.
*/
public function isNamed($old_name) {
return property_exists($this->existing, $old_name);
}
} }
/** /**
* A Permutator iterates over all possible permutations of the given array * A Permutator iterates over all possible permutations of the given array
* of elements. * of elements.
*/ */
class Permutator class Permutator {
{ /**
* Constructs a new Permutator.
*
* @param array $list the array of elements to iterate over.
*/
public function __construct($list) {
// original array
$this->list = $list;
sort($this->list);
// indicates whether there are more permutations
$this->done = false;
// directional info for permutation algorithm
$this->left = new stdClass();
foreach($list as $v) {
$this->left->{$v} = true;
}
}
/** /**
* Constructs a new Permutator. * Returns true if there is another permutation.
* *
* @param array $list the array of elements to iterate over. * @return bool true if there is another permutation, false if not.
*/ */
public function __construct($list) public function hasNext() {
{ return !$this->done;
// original array }
$this->list = $list;
sort($this->list);
// indicates whether there are more permutations
$this->done = false;
// directional info for permutation algorithm
$this->left = new stdClass();
foreach ($list as $v) {
$this->left->{$v} = true;
}
}
/** /**
* Returns true if there is another permutation. * Gets the next permutation. Call hasNext() to ensure there is another one
* * first.
* @return bool true if there is another permutation, false if not. *
*/ * @return array the next permutation.
public function hasNext() */
{ public function next() {
return !$this->done; // copy current permutation
} $rval = $this->list;
/** /* Calculate the next permutation using the Steinhaus-Johnson-Trotter
* Gets the next permutation. Call hasNext() to ensure there is another one permutation algorithm. */
* first.
*
* @return array the next permutation.
*/
public function next()
{
// copy current permutation
$rval = $this->list;
/* Calculate the next permutation using the Steinhaus-Johnson-Trotter // get largest mobile element k
permutation algorithm. */ // (mobile: element is greater than the one it is looking at)
$k = null;
$pos = 0;
$length = count($this->list);
for($i = 0; $i < $length; ++$i) {
$element = $this->list[$i];
$left = $this->left->{$element};
if(($k === null || $element > $k) &&
(($left && $i > 0 && $element > $this->list[$i - 1]) ||
(!$left && $i < ($length - 1) && $element > $this->list[$i + 1]))) {
$k = $element;
$pos = $i;
}
}
// get largest mobile element k // no more permutations
// (mobile: element is greater than the one it is looking at) if($k === null) {
$k = null; $this->done = true;
$pos = 0; } else {
$length = count($this->list); // swap k and the element it is looking at
for ($i = 0; $i < $length; ++$i) { $swap = $this->left->{$k} ? $pos - 1 : $pos + 1;
$element = $this->list[$i]; $this->list[$pos] = $this->list[$swap];
$left = $this->left->{$element}; $this->list[$swap] = $k;
if (($k === null || $element > $k) &&
(($left && $i > 0 && $element > $this->list[$i - 1]) ||
(!$left && $i < ($length - 1) && $element > $this->list[$i + 1]))) {
$k = $element;
$pos = $i;
}
}
// no more permutations // reverse the direction of all elements larger than k
if ($k === null) { for($i = 0; $i < $length; ++$i) {
$this->done = true; if($this->list[$i] > $k) {
} else { $this->left->{$this->list[$i]} = !$this->left->{$this->list[$i]};
// swap k and the element it is looking at }
$swap = $this->left->{$k} ? $pos - 1 : $pos + 1; }
$this->list[$pos] = $this->list[$swap]; }
$this->list[$swap] = $k;
// reverse the direction of all elements larger than k
for ($i = 0; $i < $length; ++$i) {
if ($this->list[$i] > $k) {
$this->left->{$this->list[$i]} = !$this->left->{$this->list[$i]};
}
}
}
return $rval;
}
return $rval;
}
} }
/** /**
* An ActiveContextCache caches active contexts so they can be reused without * An ActiveContextCache caches active contexts so they can be reused without
* the overhead of recomputing them. * the overhead of recomputing them.
*/ */
class ActiveContextCache class ActiveContextCache {
{ /**
/** * Constructs a new ActiveContextCache.
* Constructs a new ActiveContextCache. *
* * @param int size the maximum size of the cache, defaults to 100.
* @param int size the maximum size of the cache, defaults to 100. */
*/ public function __construct($size=100) {
public function __construct($size = 100) $this->order = array();
{ $this->cache = new stdClass();
$this->order = []; $this->size = $size;
$this->cache = new stdClass(); }
$this->size = $size;
}
/** /**
* Gets an active context from the cache based on the current active * Gets an active context from the cache based on the current active
* context and the new local context. * context and the new local context.
* *
* @param stdClass $active_ctx the current active context. * @param stdClass $active_ctx the current active context.
* @param stdClass $local_ctx the new local context. * @param stdClass $local_ctx the new local context.
* *
* @return mixed a shared copy of the cached active context or null. * @return mixed a shared copy of the cached active context or null.
*/ */
public function get($active_ctx, $local_ctx) public function get($active_ctx, $local_ctx) {
{ $key1 = serialize($active_ctx);
$key1 = serialize($active_ctx); $key2 = serialize($local_ctx);
$key2 = serialize($local_ctx); if(property_exists($this->cache, $key1)) {
if (property_exists($this->cache, $key1)) { $level1 = $this->cache->{$key1};
$level1 = $this->cache->{$key1}; if(property_exists($level1, $key2)) {
if (property_exists($level1, $key2)) { return $level1->{$key2};
return $level1->{$key2}; }
} }
} return null;
}
return null; /**
} * Sets an active context in the cache based on the previous active
* context and the just-processed local context.
/** *
* Sets an active context in the cache based on the previous active * @param stdClass $active_ctx the previous active context.
* context and the just-processed local context. * @param stdClass $local_ctx the just-processed local context.
* * @param stdClass $result the resulting active context.
* @param stdClass $active_ctx the previous active context. */
* @param stdClass $local_ctx the just-processed local context. public function set($active_ctx, $local_ctx, $result) {
* @param stdClass $result the resulting active context. if(count($this->order) === $this->size) {
*/ $entry = array_shift($this->order);
public function set($active_ctx, $local_ctx, $result) unset($this->cache->{$entry->activeCtx}->{$entry->localCtx});
{ }
if (count($this->order) === $this->size) { $key1 = serialize($active_ctx);
$entry = array_shift($this->order); $key2 = serialize($local_ctx);
unset($this->cache->{$entry->activeCtx}->{$entry->localCtx}); $this->order[] = (object)array(
} 'activeCtx' => $key1, 'localCtx' => $key2);
if(!property_exists($this->cache, $key1)) {
$key1 = serialize($active_ctx); $this->cache->{$key1} = new stdClass();
$key2 = serialize($local_ctx); }
$this->order[] = (object) [ $this->cache->{$key1}->{$key2} = JsonLdProcessor::copy($result);
'activeCtx' => $key1, 'localCtx' => $key2]; }
if (!property_exists($this->cache, $key1)) {
$this->cache->{$key1} = new stdClass();
}
$this->cache->{$key1}->{$key2} = JsonLdProcessor::copy($result);
}
} }
/* end of file, omit ?> */ /* end of file, omit ?> */

1303
test.php
View file

@ -1,5 +1,4 @@
<?php <?php
/** /**
* PHP unit tests for JSON-LD. * PHP unit tests for JSON-LD.
* *
@ -9,841 +8,639 @@
*/ */
require_once('jsonld.php'); require_once('jsonld.php');
class JsonLdTestCase extends PHPUnit_Framework_TestCase class JsonLdTestCase extends PHPUnit_Framework_TestCase {
{ /**
/** * Runs this test case. Overridden to attach to EARL report w/o need for
* Runs this test case. Overridden to attach to EARL report w/o need for * an external XML configuration file.
* an external XML configuration file. *
* * @param PHPUnit_Framework_TestResult $result the test result.
* @param PHPUnit_Framework_TestResult $result the test result. */
*/ public function run(PHPUnit_Framework_TestResult $result = NULL) {
public function run(PHPUnit_Framework_TestResult $result = NULL) global $EARL;
{ $EARL->attach($result);
global $EARL; $this->result = $result;
$EARL->attach($result); parent::run($result);
$this->result = $result; }
parent::run($result);
}
/** /**
* Tests expansion. * Tests expansion.
* *
* @param JsonLdTest $test the test to run. * @param JsonLdTest $test the test to run.
* *
* @group expand * @group expand
* @group json-ld.org * @dataProvider expandProvider
* @dataProvider expandProvider */
*/ public function testExpand($test) {
public function testExpand($test) $this->test = $test;
{ $input = $test->readUrl('input');
$this->test = $test; $options = $test->createOptions();
$input = $test->readUrl('input'); $test->run('jsonld_expand', array($input, $options));
$options = $test->createOptions(); }
$test->run('jsonld_expand', [$input, $options]);
}
/** /**
* Tests compaction. * Tests compaction.
* *
* @param JsonLdTest $test the test to run. * @param JsonLdTest $test the test to run.
* *
* @group compact * @group compact
* @group json-ld.org * @dataProvider compactProvider
* @dataProvider compactProvider */
*/ public function testCompact($test) {
public function testCompact($test) $this->test = $test;
{ $input = $test->readUrl('input');
$this->test = $test; $context = $test->readProperty('context');
$input = $test->readUrl('input'); $options = $test->createOptions();
$context = $test->readProperty('context'); $test->run('jsonld_compact', array($input, $context, $options));
$options = $test->createOptions(); }
$test->run('jsonld_compact', [$input, $context, $options]);
}
/** /**
* Tests flatten. * Tests flatten.
* *
* @param JsonLdTest $test the test to run. * @param JsonLdTest $test the test to run.
* *
* @group flatten * @group flatten
* @group json-ld.org * @dataProvider flattenProvider
* @dataProvider flattenProvider */
*/ public function testFlatten($test) {
public function testFlatten($test) $this->test = $test;
{ $input = $test->readUrl('input');
$this->test = $test; $context = $test->readProperty('context');
$input = $test->readUrl('input'); $options = $test->createOptions();
$context = $test->readProperty('context'); $test->run('jsonld_flatten', array($input, $context, $options));
$options = $test->createOptions(); }
$test->run('jsonld_flatten', [$input, $context, $options]);
}
/** /**
* Tests serialization to RDF. * Tests serialization to RDF.
* *
* @param JsonLdTest $test the test to run. * @param JsonLdTest $test the test to run.
* *
* @group toRdf * @group toRdf
* @group json-ld.org * @dataProvider toRdfProvider
* @dataProvider toRdfProvider */
*/ public function testToRdf($test) {
public function testToRdf($test) $this->test = $test;
{ $input = $test->readUrl('input');
$this->test = $test; $options = $test->createOptions(array('format' => 'application/nquads'));
$input = $test->readUrl('input'); $test->run('jsonld_to_rdf', array($input, $options));
$options = $test->createOptions(['format' => 'application/nquads']); }
$test->run('jsonld_to_rdf', [$input, $options]);
}
/** /**
* Tests deserialization from RDF. * Tests deserialization from RDF.
* *
* @param JsonLdTest $test the test to run. * @param JsonLdTest $test the test to run.
* *
* @group fromRdf * @group fromRdf
* @group json-ld.org * @dataProvider fromRdfProvider
* @dataProvider fromRdfProvider */
*/ public function testFromRdf($test) {
public function testFromRdf($test) $this->test = $test;
{ $input = $test->readProperty('input');
$this->test = $test; $options = $test->createOptions(array('format' => 'application/nquads'));
$input = $test->readProperty('input'); $test->run('jsonld_from_rdf', array($input, $options));
$options = $test->createOptions(['format' => 'application/nquads']); }
$test->run('jsonld_from_rdf', [$input, $options]);
}
/** /**
* Tests framing. * Tests framing.
* *
* @param JsonLdTest $test the test to run. * @param JsonLdTest $test the test to run.
* *
* @group frame * @group frame
* @group json-ld.org * @dataProvider frameProvider
* @dataProvider frameProvider */
*/ public function testFrame($test) {
public function testFrame($test) $this->test = $test;
{ $input = $test->readUrl('input');
$this->test = $test; $frame = $test->readProperty('frame');
$input = $test->readUrl('input'); $options = $test->createOptions();
$frame = $test->readProperty('frame'); $test->run('jsonld_frame', array($input, $frame, $options));
$options = $test->createOptions(); }
$test->run('jsonld_frame', [$input, $frame, $options]);
}
/** /**
* Tests normalization. * Tests normalization.
* *
* @param JsonLdTest $test the test to run. * @param JsonLdTest $test the test to run.
* *
* @group normalize * @group normalize
* @group json-ld.org * @dataProvider normalizeProvider
* @dataProvider normalizeProvider */
*/ public function testNormalize($test) {
public function testNormalize($test) $this->test = $test;
{ $input = $test->readUrl('input');
$this->test = $test; $options = $test->createOptions(array('format' => 'application/nquads'));
$input = $test->readUrl('input'); $test->run('jsonld_normalize', array($input, $options));
$options = $test->createOptions(['format' => 'application/nquads']); }
$test->run('jsonld_normalize', [$input, $options]);
}
/** public function expandProvider() {
* Tests URGNA2012 normalization. return new JsonLdTestIterator('jld:ExpandTest');
* }
* @param JsonLdTest $test the test to run.
*
* @group normalize
* @group normalization
* @dataProvider urgna2012Provider
*/
public function testUrgna2012($test)
{
$this->test = $test;
$input = $test->readProperty('action');
$options = $test->createOptions([
'algorithm' => 'URGNA2012',
'inputFormat' => 'application/nquads',
'format' => 'application/nquads']);
$test->run('jsonld_normalize', [$input, $options]);
}
/** public function compactProvider() {
* Tests URDNA2015 normalization. return new JsonLdTestIterator('jld:CompactTest');
* }
* @param JsonLdTest $test the test to run.
*
* @group normalize
* @group normalization
* @dataProvider urdna2015Provider
*/
public function testUrdna2015($test)
{
$this->test = $test;
$input = $test->readProperty('action');
$options = $test->createOptions([
'algorithm' => 'URDNA2015',
'inputFormat' => 'application/nquads',
'format' => 'application/nquads']);
$test->run('jsonld_normalize', [$input, $options]);
}
public function expandProvider() public function flattenProvider() {
{ return new JsonLdTestIterator('jld:FlattenTest');
return new JsonLdTestIterator('jld:ExpandTest'); }
}
public function compactProvider() public function toRdfProvider() {
{ return new JsonLdTestIterator('jld:ToRDFTest');
return new JsonLdTestIterator('jld:CompactTest'); }
}
public function flattenProvider() public function fromRdfProvider() {
{ return new JsonLdTestIterator('jld:FromRDFTest');
return new JsonLdTestIterator('jld:FlattenTest'); }
}
public function toRdfProvider() public function normalizeProvider() {
{ return new JsonLdTestIterator('jld:NormalizeTest');
return new JsonLdTestIterator('jld:ToRDFTest'); }
}
public function fromRdfProvider() public function frameProvider() {
{ return new JsonLdTestIterator('jld:FrameTest');
return new JsonLdTestIterator('jld:FromRDFTest'); }
}
public function normalizeProvider()
{
return new JsonLdTestIterator('jld:NormalizeTest');
}
public function frameProvider()
{
return new JsonLdTestIterator('jld:FrameTest');
}
public function urgna2012Provider()
{
return new JsonLdTestIterator('rdfn:Urgna2012EvalTest');
}
public function urdna2015Provider()
{
return new JsonLdTestIterator('rdfn:Urdna2015EvalTest');
}
} }
class JsonLdManifest class JsonLdManifest {
{ public function __construct($data, $filename) {
public function __construct($data, $filename) $this->data = $data;
{ $this->filename = $filename;
$this->data = $data; $this->dirname = dirname($filename);
$this->filename = $filename; }
$this->dirname = dirname($filename);
}
public function load(&$tests) public function load(&$tests) {
{ $sequence = JsonLdProcessor::getValues($this->data, 'sequence');
$entries = array_merge( foreach($sequence as $entry) {
JsonLdProcessor::getValues($this->data, 'sequence'), JsonLdProcessor::getValues($this->data, 'entries')); if(is_string($entry)) {
$includes = JsonLdProcessor::getValues($this->data, 'include'); $filename = join(
foreach ($includes as $include) { DIRECTORY_SEPARATOR, array($this->dirname, $entry));
array_push($entries, $include . '.jsonld'); $entry = Util::readJson($filename);
} } else {
foreach ($entries as $entry) { $filename = $this->filename;
if (is_string($entry)) { }
$filename = join(
DIRECTORY_SEPARATOR, [$this->dirname, $entry]);
$entry = Util::readJson($filename);
} else {
$filename = $this->filename;
}
if (JsonLdProcessor::hasValue($entry, '@type', 'mf:Manifest') || if(JsonLdProcessor::hasValue($entry, '@type', 'mf:Manifest')) {
JsonLdProcessor::hasValue($entry, 'type', 'mf:Manifest')) { // entry is another manifest
// entry is another manifest $manifest = new JsonLdManifest($entry, $filename);
$manifest = new JsonLdManifest($entry, $filename); $manifest->load($tests);
$manifest->load($tests); } else {
} else { // assume entry is a test
// assume entry is a test $test = new JsonLdTest($this, $entry, $filename);
$test = new JsonLdTest($this, $entry, $filename); $types = JsonLdProcessor::getValues($test->data, '@type');
$types = array_merge( foreach($types as $type) {
JsonLdProcessor::getValues($test->data, '@type'), JsonLdProcessor::getValues($test->data, 'type')); if(!isset($tests[$type])) {
foreach ($types as $type) { $tests[$type] = array();
if (!isset($tests[$type])) { }
$tests[$type] = []; $tests[$type][] = $test;
} }
$tests[$type][] = $test; }
} }
} }
}
}
} }
class JsonLdTest class JsonLdTest {
{ public function __construct($manifest, $data, $filename) {
public function __construct($manifest, $data, $filename) $this->manifest = $manifest;
{ $this->data = $data;
$this->manifest = $manifest; $this->filename = $filename;
$this->data = $data; $this->dirname = dirname($filename);
$this->filename = $filename; $this->isPositive = JsonLdProcessor::hasValue(
$this->dirname = dirname($filename); $data, '@type', 'jld:PositiveEvaluationTest');
$this->isPositive = JsonLdProcessor::hasValue( $this->isNegative = JsonLdProcessor::hasValue(
$data, '@type', 'jld:PositiveEvaluationTest') || $data, '@type', 'jld:NegativeEvaluationTest');
JsonLdProcessor::hasValue(
$data, 'type', 'jld:PositiveEvaluationTest');
$this->isNegative = JsonLdProcessor::hasValue(
$data, '@type', 'jld:NegativeEvaluationTest') ||
JsonLdProcessor::hasValue(
$data, 'type', 'jld:NegativeEvaluationTest');
// generate test name // generate test name
if (isset($manifest->data->name)) { $this->name = $manifest->data->name . ' ' . substr($data->{'@id'}, 2) .
$manifestLabel = $manifest->data->name; ' - ' . $this->data->name;
} else if (isset($manifest->data->label)) {
$manifestLabel = $manifest->data->label;
} else {
$manifestLabel = 'UNNAMED';
}
if (isset($this->data->id)) {
$testId = $this->data->id;
} else {
$testId = $this->data->{'@id'};
}
if (isset($this->data->name)) {
$testLabel = $this->data->name;
} else if (isset($this->data->label)) {
$testLabel = $this->data->label;
} else {
$testLabel = 'UNNAMED';
}
$this->name = $manifestLabel . ' ' . $testId . ' - ' . $testLabel; // expand @id and input base
$data->{'@id'} = ($manifest->data->baseIri .
basename($manifest->filename) . $data->{'@id'});
$this->base = $manifest->data->baseIri . $data->input;
}
// expand @id and input base public function run($fn, $params) {
if (isset($manifest->data->baseIri)) { // read expected data
$data->{'@id'} = ($manifest->data->baseIri . if($this->isNegative) {
basename($manifest->filename) . $data->{'@id'}); $this->expected = $this->data->expect;
$this->base = $manifest->data->baseIri . $data->input; } else {
} $this->expected = $this->readProperty('expect');
} }
private function _getResultProperty() try {
{ $this->actual = call_user_func_array($fn, $params);
if (isset($this->data->expect)) { if($this->isNegative) {
return 'expect'; throw new Exception('Expected an error; one was not raised.');
} else if (isset($this->data->result)) { }
return 'result'; PHPUnit_Framework_TestCase::assertEquals($this->expected, $this->actual);
} else { } catch(Exception $e) {
throw new Exception('No test result property found.'); if($this->isPositive) {
} throw $e;
} }
$this->actual = $this->getJsonLdErrorCode($e);
PHPUnit_Framework_TestCase::assertEquals($this->expected, $this->actual);
}
}
public function run($fn, $params) public function readUrl($property) {
{ if(!property_exists($this->data, $property)) {
// read expected data return null;
if ($this->isNegative) { }
$this->expected = $this->data->expect; return $this->manifest->data->baseIri . $this->data->{$property};
} else { }
$this->expected = $this->readProperty($this->_getResultProperty());
}
try { public function readProperty($property) {
$this->actual = call_user_func_array($fn, $params); $data = $this->data;
if ($this->isNegative) { if(!property_exists($data, $property)) {
throw new Exception('Expected an error; one was not raised.'); return null;
} }
PHPUnit_Framework_TestCase::assertEquals($this->expected, $this->actual); $filename = join(
} catch (Exception $e) { DIRECTORY_SEPARATOR, array($this->dirname, $data->{$property}));
// assume positive test $extension = pathinfo($filename, PATHINFO_EXTENSION);
if ($this->isNegative) { if($extension === 'jsonld') {
$this->actual = $this->getJsonLdErrorCode($e); return Util::readJson($filename);
PHPUnit_Framework_TestCase::assertEquals( }
$this->expected, $this->actual); return Util::readFile($filename);
} else { }
throw $e;
}
}
}
public function readUrl($property) public function createOptions($opts=array()) {
{ $http_options = array(
if (!property_exists($this->data, $property)) { 'contentType', 'httpLink', 'httpStatus', 'redirectTo');
return null; $test_options = (property_exists($this->data, 'option') ?
} $this->data->option : array());
return $this->manifest->data->baseIri . $this->data->{$property}; $options = array();
} foreach($test_options as $k => $v) {
if(!in_array($k, $http_options)) {
$options[$k] = $v;
}
}
$options['documentLoader'] = $this->createDocumentLoader();
$options = array_merge($options, $opts);
if(isset($options['expandContext'])) {
$filename = join(
DIRECTORY_SEPARATOR, array($this->dirname, $options['expandContext']));
$options['expandContext'] = Util::readJson($filename);
}
return $options;
}
public function readProperty($property) public function createDocumentLoader() {
{ $base = 'http://json-ld.org/test-suite';
$data = $this->data; $test = $this;
if (!property_exists($data, $property)) {
return null;
}
$filename = join(
DIRECTORY_SEPARATOR, [$this->dirname, $data->{$property}]);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
if ($extension === 'jsonld') {
return Util::readJson($filename);
}
return Util::readFile($filename); $load_locally = function($url) use ($test, $base) {
} $doc = (object)array(
'contextUrl' => null, 'documentUrl' => $url, 'document' => null);
$options = (property_exists($test->data, 'option') ?
$test->data->option : null);
if($options and $url === $test->base) {
if(property_exists($options, 'redirectTo') &&
property_exists($options, 'httpStatus') &&
$options->httpStatus >= '300') {
$doc->documentUrl = ($test->manifest->data->{'baseIri'} .
$options->redirectTo);
} else if(property_exists($options, 'httpLink')) {
$content_type = (property_exists($options, 'contentType') ?
$options->contentType : null);
$extension = pathinfo($url, PATHINFO_EXTENSION);
if(!$content_type && $extension === 'jsonld') {
$content_type = 'application/ld+json';
}
$link_header = $options->httpLink;
if(is_array($link_header)) {
$link_header = join(',', $link_header);
}
$link_header = jsonld_parse_link_header($link_header);
if(isset($link_header['http://www.w3.org/ns/json-ld#context'])) {
$link_header = $link_header['http://www.w3.org/ns/json-ld#context'];
} else {
$link_header = null;
}
if($link_header && $content_type !== 'application/ld+json') {
if(is_array($link_header)) {
throw new Exception('multiple context link headers');
}
$doc->{'contextUrl'} = $link_header->target;
}
}
}
global $ROOT_MANIFEST_DIR;
$filename = $ROOT_MANIFEST_DIR .
substr($doc->{'documentUrl'}, strlen($base));
try {
$doc->{'document'} = Util::readJson($filename);
} catch(Exception $e) {
throw new Exception('loading document failed');
}
return $doc;
};
public function createOptions($opts = []) $local_loader = function($url) use ($test, $base, $load_locally) {
{ // always load remote-doc and non-base tests remotely
$http_options = [ if(strpos($url, $base) !== 0 ||
'contentType', 'httpLink', 'httpStatus', 'redirectTo']; $test->manifest->data->name === 'Remote document') {
$test_options = (property_exists($this->data, 'option') ? return call_user_func('jsonld_default_document_loader', $url);
$this->data->option : []); }
$options = [];
foreach ($test_options as $k => $v) {
if (!in_array($k, $http_options)) {
$options[$k] = $v;
}
}
$options['documentLoader'] = $this->createDocumentLoader(); // attempt to load locally
$options = array_merge($options, $opts); return call_user_func($load_locally, $url);
if (isset($options['expandContext'])) { };
$filename = join(
DIRECTORY_SEPARATOR, [$this->dirname, $options['expandContext']]);
$options['expandContext'] = Util::readJson($filename);
}
return $options; return $local_loader;
} }
public function createDocumentLoader()
{
$base = 'http://json-ld.org/test-suite';
$test = $this;
$load_locally = function($url) use ($test, $base) {
$doc = (object) [
'contextUrl' => null, 'documentUrl' => $url, 'document' => null];
$options = (property_exists($test->data, 'option') ?
$test->data->option : null);
if ($options and $url === $test->base) {
if (property_exists($options, 'redirectTo') &&
property_exists($options, 'httpStatus') &&
$options->httpStatus >= '300'
) {
$doc->documentUrl = ($test->manifest->data->baseIri .
$options->redirectTo);
} else if (property_exists($options, 'httpLink')) {
$content_type = (property_exists($options, 'contentType') ?
$options->contentType : null);
$extension = pathinfo($url, PATHINFO_EXTENSION);
if (!$content_type && $extension === 'jsonld') {
$content_type = 'application/ld+json';
}
$link_header = $options->httpLink;
if (is_array($link_header)) {
$link_header = join(',', $link_header);
}
$link_header = jsonld_parse_link_header($link_header);
if (isset($link_header['http://www.w3.org/ns/json-ld#context'])) {
$link_header = $link_header['http://www.w3.org/ns/json-ld#context'];
} else {
$link_header = null;
}
if ($link_header && $content_type !== 'application/ld+json') {
if (is_array($link_header)) {
throw new Exception('multiple context link headers');
}
$doc->contextUrl = $link_header->target;
}
}
}
global $ROOT_MANIFEST_DIR;
if (strpos($doc->documentUrl, ':') === false) {
$filename = join(
DIRECTORY_SEPARATOR, [
$ROOT_MANIFEST_DIR, $doc->documentUrl]);
$doc->documentUrl = 'file://' . $filename;
} else {
$filename = join(
DIRECTORY_SEPARATOR, [
$ROOT_MANIFEST_DIR, substr($doc->documentUrl, strlen($base))]);
}
try {
$doc->document = Util::readJson($filename);
} catch (Exception $e) {
throw new Exception('loading document failed');
}
return $doc;
};
$local_loader = function($url) use ($test, $base, $load_locally) {
// always load remote-doc and non-base tests remotely
if ((strpos($url, $base) !== 0 && strpos($url, ':') !== false) ||
$test->manifest->data->name === 'Remote document') {
return call_user_func('jsonld_default_document_loader', $url);
}
// attempt to load locally
return call_user_func($load_locally, $url);
};
return $local_loader;
}
public function getJsonLdErrorCode($err)
{
if ($err instanceof JsonLdException) {
if ($err->getCode()) {
return $err->getCode();
}
if ($err->cause) {
return $this->getJsonLdErrorCode($err->cause);
}
}
return $err->getMessage();
}
public function getJsonLdErrorCode($err) {
if($err instanceof JsonLdException) {
if($err->getCode()) {
return $err->getCode();
}
if($err->cause) {
return $this->getJsonLdErrorCode($err->cause);
}
}
return $err->getMessage();
}
} }
class JsonLdTestIterator implements Iterator class JsonLdTestIterator implements Iterator {
{ /**
/** * The current test index.
* The current test index. */
*/ protected $index = 0;
protected $index = 0;
/** /**
* The total number of tests. * The total number of tests.
*/ */
protected $count = 0; protected $count = 0;
/** /**
* Creates a TestIterator. * Creates a TestIterator.
* *
* @param string $type the type of tests to iterate over. * @param string $type the type of tests to iterate over.
*/ */
public function __construct($type) public function __construct($type) {
{ global $TESTS;
global $TESTS; if(isset($TESTS[$type])) {
if (isset($TESTS[$type])) { $this->tests = $TESTS[$type];
$this->tests = $TESTS[$type]; } else {
} else { $this->tests = array();
$this->tests = []; }
} $this->count = count($this->tests);
}
$this->count = count($this->tests); /**
} * Gets the parameters for the next test.
*
* @return assoc the parameters for the next test.
*/
public function current() {
return array('test' => $this->tests[$this->index]);
}
/** /**
* Gets the parameters for the next test. * Gets the current test number.
* *
* @return assoc the parameters for the next test. * @return int the current test number.
*/ */
public function current() public function key() {
{ return $this->index;
return ['test' => $this->tests[$this->index]]; }
}
/** /**
* Gets the current test number. * Proceeds to the next test.
* */
* @return int the current test number. public function next() {
*/ $this->index += 1;
public function key() }
{
return $this->index;
}
/** /**
* Proceeds to the next test. * Rewinds to the first test.
*/ */
public function next() public function rewind() {
{ $this->index = 0;
$this->index += 1; }
}
/** /**
* Rewinds to the first test. * Returns true if there are more tests to be run.
*/ *
public function rewind() * @return bool true if there are more tests to be run.
{ */
$this->index = 0; public function valid() {
} return $this->index < $this->count;
}
/**
* Returns true if there are more tests to be run.
*
* @return bool true if there are more tests to be run.
*/
public function valid()
{
return $this->index < $this->count;
}
} }
class EarlReport extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener class EarlReport extends PHPUnit_Util_Printer
{ implements PHPUnit_Framework_TestListener {
public function __construct() public function __construct() {
{ $this->filename = null;
$this->filename = null; $this->attached = false;
$this->attached = false; $this->report = (object)array(
$this->report = (object) [ '@context' => (object)array(
'@context' => (object) [ 'doap' => 'http://usefulinc.com/ns/doap#',
'doap' => 'http://usefulinc.com/ns/doap#', 'foaf' => 'http://xmlns.com/foaf/0.1/',
'foaf' => 'http://xmlns.com/foaf/0.1/', 'dc' => 'http://purl.org/dc/terms/',
'dc' => 'http://purl.org/dc/terms/', 'earl' => 'http://www.w3.org/ns/earl#',
'earl' => 'http://www.w3.org/ns/earl#', 'xsd' => 'http://www.w3.org/2001/XMLSchema#',
'xsd' => 'http://www.w3.org/2001/XMLSchema#', 'doap:homepage' => (object)array('@type' => '@id'),
'doap:homepage' => (object) ['@type' => '@id'], 'doap:license' => (object)array('@type' => '@id'),
'doap:license' => (object) ['@type' => '@id'], 'dc:creator' => (object)array('@type' => '@id'),
'dc:creator' => (object) ['@type' => '@id'], 'foaf:homepage' => (object)array('@type' => '@id'),
'foaf:homepage' => (object) ['@type' => '@id'], 'subjectOf' => (object)array('@reverse' => 'earl:subject'),
'subjectOf' => (object) ['@reverse' => 'earl:subject'], 'earl:assertedBy' => (object)array('@type' => '@id'),
'earl:assertedBy' => (object) ['@type' => '@id'], 'earl:mode' => (object)array('@type' => '@id'),
'earl:mode' => (object) ['@type' => '@id'], 'earl:test' => (object)array('@type' => '@id'),
'earl:test' => (object) ['@type' => '@id'], 'earl:outcome' => (object)array('@type' => '@id'),
'earl:outcome' => (object) ['@type' => '@id'], 'dc:date' => (object)array('@type' => 'xsd:date')
'dc:date' => (object) ['@type' => 'xsd:date'] ),
], '@id' => 'https://github.com/digitalbazaar/php-json-ld',
'@id' => 'https://github.com/digitalbazaar/php-json-ld', '@type' => array('doap:Project', 'earl:TestSubject', 'earl:Software'),
'@type' => ['doap:Project', 'earl:TestSubject', 'earl:Software'], 'doap:name' => 'php-json-ld',
'doap:name' => 'php-json-ld', 'dc:title' => 'php-json-ld',
'dc:title' => 'php-json-ld', 'doap:homepage' => 'https://github.com/digitalbazaar/php-json-ld',
'doap:homepage' => 'https://github.com/digitalbazaar/php-json-ld', 'doap:license' => 'https://github.com/digitalbazaar/php-json-ld/blob/master/LICENSE',
'doap:license' => 'https://github.com/digitalbazaar/php-json-ld/blob/master/LICENSE', 'doap:description' => 'A JSON-LD processor for PHP',
'doap:description' => 'A JSON-LD processor for PHP', 'doap:programming-language' => 'PHP',
'doap:programming-language' => 'PHP', 'dc:creator' => 'https://github.com/dlongley',
'dc:creator' => 'https://github.com/dlongley', 'doap:developer' => (object)array(
'doap:developer' => (object) [ '@id' => 'https://github.com/dlongley',
'@id' => 'https://github.com/dlongley', '@type' => array('foaf:Person', 'earl:Assertor'),
'@type' => ['foaf:Person', 'earl:Assertor'], 'foaf:name' => 'Dave Longley',
'foaf:name' => 'Dave Longley', 'foaf:homepage' => 'https://github.com/dlongley'
'foaf:homepage' => 'https://github.com/dlongley' ),
], 'dc:date' => array(
'dc:date' => [ '@value' => gmdate('Y-m-d'),
'@value' => gmdate('Y-m-d'), '@type' => 'xsd:date'
'@type' => 'xsd:date' ),
], 'subjectOf' => array()
'subjectOf' => [] );
]; }
}
/** /**
* Attaches to the given test result, if not yet attached. * Attaches to the given test result, if not yet attached.
* *
* @param PHPUnit_Framework_Test $result the result to attach to. * @param PHPUnit_Framework_Test $result the result to attach to.
*/ */
public function attach(PHPUnit_Framework_TestResult $result) public function attach(PHPUnit_Framework_TestResult $result) {
{ if(!$this->attached && $this->filename) {
if (!$this->attached && $this->filename) { $this->attached = true;
$this->attached = true; $result->addListener($this);
$result->addListener($this); }
} }
}
/** /**
* Adds an assertion to this EARL report. * Adds an assertion to this EARL report.
* *
* @param JsonLdTest $test the JsonLdTest for the assertion is for. * @param JsonLdTest $test the JsonLdTest for the assertion is for.
* @param bool $passed whether or not the test passed. * @param bool $passed whether or not the test passed.
*/ */
public function addAssertion($test, $passed) public function addAssertion($test, $passed) {
{ $this->report->{'subjectOf'}[] = (object)array(
$this->report->{'subjectOf'}[] = (object) [ '@type' => 'earl:Assertion',
'@type' => 'earl:Assertion', 'earl:assertedBy' => $this->report->{'doap:developer'}->{'@id'},
'earl:assertedBy' => $this->report->{'doap:developer'}->{'@id'}, 'earl:mode' => 'earl:automatic',
'earl:mode' => 'earl:automatic', 'earl:test' => $test->data->{'@id'},
'earl:test' => $test->data->{'@id'}, 'earl:result' => (object)array(
'earl:result' => (object) [ '@type' => 'earl:TestResult',
'@type' => 'earl:TestResult', 'dc:date' => gmdate(DateTime::ISO8601),
'dc:date' => gmdate(DateTime::ISO8601), 'earl:outcome' => $passed ? 'earl:passed' : 'earl:failed'
'earl:outcome' => $passed ? 'earl:passed' : 'earl:failed' )
] );
]; return $this;
}
return $this; /**
} * Writes this EARL report to a file.
*/
public function flush() {
if($this->filename) {
printf("\nWriting EARL report to: %s\n", $this->filename);
$fd = fopen($this->filename, 'w');
fwrite($fd, Util::jsonldEncode($this->report));
fclose($fd);
}
}
/** public function endTest(PHPUnit_Framework_Test $test, $time) {
* Writes this EARL report to a file. $this->addAssertion($test->test, true);
*/ }
public function flush()
{
if ($this->filename) {
printf("\nWriting EARL report to: %s\n", $this->filename);
$fd = fopen($this->filename, 'w');
fwrite($fd, Util::jsonldEncode($this->report));
fclose($fd);
}
}
public function endTest(PHPUnit_Framework_Test $test, $time) public function addError(
{ PHPUnit_Framework_Test $test, Exception $e, $time) {
$this->addAssertion($test->test, true); $this->addAssertion($test->test, false);
} }
public function addError( public function addFailure(
PHPUnit_Framework_Test $test, Exception $e, $time) PHPUnit_Framework_Test $test,
{ PHPUnit_Framework_AssertionFailedError $e, $time) {
$this->addAssertion($test->test, false); $this->addAssertion($test->test, false);
} if($test->result->shouldStop()) {
printf("\n\nFAILED\n");
printf("Test: %s\n", $test->test->name);
printf("Purpose: %s\n", $test->test->data->purpose);
printf("EXPECTED: %s\n", Util::jsonldEncode($test->test->expected));
printf("ACTUAL: %s\n", Util::jsonldEncode($test->test->actual));
}
}
public function addFailure( public function addIncompleteTest(
PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) PHPUnit_Framework_Test $test, Exception $e, $time) {
{ $this->addAssertion($test->test, false);
$this->addAssertion($test->test, false); }
if ($test->result->shouldStop()) {
if (isset($test->test->name)) {
$name = $test->test->name;
} else if (isset($test->test->label)) {
$name = $test->test->label;
} else {
$name = 'UNNAMED';
}
printf("\n\nFAILED\n");
printf("Test: %s\n", $name);
printf("Purpose: %s\n", $test->test->data->purpose);
printf("EXPECTED: %s\n", Util::jsonldEncode($test->test->expected));
printf("ACTUAL: %s\n", Util::jsonldEncode($test->test->actual));
}
}
public function addIncompleteTest( public function addRiskyTest(
PHPUnit_Framework_Test $test, Exception $e, $time) PHPUnit_Framework_Test $test, Exception $e, $time) {}
{ public function addSkippedTest(
$this->addAssertion($test->test, false); PHPUnit_Framework_Test $test, Exception $e, $time) {}
} public function startTest(PHPUnit_Framework_Test $test) {}
public function startTestSuite(PHPUnit_Framework_TestSuite $suite) {}
public function addRiskyTest( public function endTestSuite(PHPUnit_Framework_TestSuite $suite) {}
PHPUnit_Framework_Test $test, Exception $e, $time)
{
}
public function addSkippedTest(
PHPUnit_Framework_Test $test, Exception $e, $time)
{
}
public function startTest(PHPUnit_Framework_Test $test)
{
}
public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
{
}
public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
{
}
} }
class Util class Util {
{ public static function readFile($filename) {
public static function readFile($filename) $rval = @file_get_contents($filename);
{ if($rval === false) {
$rval = @file_get_contents($filename); throw new Exception('File read error: ' . $filename);
if ($rval === false) { }
throw new Exception('File read error: ' . $filename); return $rval;
} }
return $rval; public static function readJson($filename) {
} $rval = json_decode(self::readFile($filename));
if($rval === null) {
throw new Exception('JSON parse error');
}
return $rval;
}
public static function readJson($filename) public static function readNQuads($filename) {
{ return self::readFile($filename);
$rval = json_decode(self::readFile($filename)); }
if ($rval === null) {
throw new Exception('JSON parse error');
}
return $rval; public static function jsonldEncode($input) {
} // newer PHP has a flag to avoid escaped '/'
if(defined('JSON_UNESCAPED_SLASHES')) {
public static function readNQuads($filename) $options = JSON_UNESCAPED_SLASHES;
{ if(defined('JSON_PRETTY_PRINT')) {
return self::readFile($filename); $options |= JSON_PRETTY_PRINT;
} }
$json = json_encode($input, $options);
public static function jsonldEncode($input) } else {
{ // use a simple string replacement of '\/' to '/'.
// newer PHP has a flag to avoid escaped '/' $json = str_replace('\\/', '/', json_encode($input));
if (defined('JSON_UNESCAPED_SLASHES')) { }
$options = JSON_UNESCAPED_SLASHES; return $json;
if (defined('JSON_PRETTY_PRINT')) { }
$options |= JSON_PRETTY_PRINT;
}
$json = json_encode($input, $options);
} else {
// use a simple string replacement of '\/' to '/'.
$json = str_replace('\\/', '/', json_encode($input));
}
return $json;
}
} }
// tests to skip // tests to skip
$SKIP_TESTS = []; $SKIP_TESTS = array();
// root manifest directory // root manifest directory
$ROOT_MANIFEST_DIR; $ROOT_MANIFEST_DIR;
// parsed tests; keyed by type // parsed tests; keyed by type
$TESTS = []; $TESTS = array();
// parsed command line options // parsed command line options
$OPTIONS = []; $OPTIONS = array();
// parse command line options // parse command line options
global $argv; global $argv;
$args = $argv; $args = $argv;
$total = count($args); $total = count($args);
$start = false; $start = false;
for ($i = 0; $i < $total; ++$i) { for($i = 0; $i < $total; ++$i) {
$arg = $args[$i]; $arg = $args[$i];
if (!$start) { if(!$start) {
if (realpath($arg) === realpath(__FILE__)) { if(realpath($arg) === realpath(__FILE__)) {
$start = true; $start = true;
} }
continue; continue;
} }
if ($arg[0] !== '-') { if($arg[0] !== '-') {
break; break;
} }
$i += 1; $i += 1;
$OPTIONS[$arg] = $args[$i]; $OPTIONS[$arg] = $args[$i];
} }
if(!isset($OPTIONS['-d'])) {
if (!isset($OPTIONS['-d'])) { $dvar = 'path to json-ld.org/test-suite';
$dvar = 'path to json-ld.org/test-suite'; $evar = 'file to write EARL report to';
$evar = 'file to write EARL report to'; echo "php-json-ld Tests\n";
echo "php-json-ld Tests\n"; echo "Usage: phpunit test.php -d <$dvar> [-e <$evar>]\n\n";
echo "Usage: phpunit test.php -d <$dvar> [-e <$evar>]\n\n"; exit(0);
exit(0);
} }
// EARL Report // EARL Report
$EARL = new EarlReport(); $EARL = new EarlReport();
if (isset($OPTIONS['-e'])) { if(isset($OPTIONS['-e'])) {
$EARL->filename = $OPTIONS['-e']; $EARL->filename = $OPTIONS['-e'];
} }
// load root manifest // load root manifest
$ROOT_MANIFEST_DIR = realpath($OPTIONS['-d']); $ROOT_MANIFEST_DIR = realpath($OPTIONS['-d']);
$filename = join( $filename = join(
DIRECTORY_SEPARATOR, [$ROOT_MANIFEST_DIR, 'manifest.jsonld']); DIRECTORY_SEPARATOR, array($ROOT_MANIFEST_DIR, 'manifest.jsonld'));
$root_manifest = Util::readJson($filename); $root_manifest = Util::readJson($filename);
$manifest = new JsonLdManifest($root_manifest, $filename); $manifest = new JsonLdManifest($root_manifest, $filename);
$manifest->load($TESTS); $manifest->load($TESTS);