2011-07-14 06:47:40 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-23 18:57:06 +02:00
|
|
|
* PHP implementation of the JSON-LD API.
|
2015-02-08 02:08:44 +01:00
|
|
|
* Version: 0.4.7-dev
|
2011-07-14 06:47:40 +02:00
|
|
|
*
|
|
|
|
* @author Dave Longley
|
|
|
|
*
|
2012-04-23 18:57:06 +02:00
|
|
|
* BSD 3-Clause License
|
2014-04-24 20:39:30 +02:00
|
|
|
* Copyright (c) 2011-2014 Digital Bazaar, Inc.
|
2012-04-23 18:57:06 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* Neither the name of the Digital Bazaar, Inc. nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
|
|
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
|
|
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2011-07-14 06:47:40 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-04-23 18:57:06 +02:00
|
|
|
* Performs JSON-LD compaction.
|
|
|
|
*
|
|
|
|
* @param mixed $input the JSON-LD object to compact.
|
|
|
|
* @param mixed $ctx the context to compact with.
|
|
|
|
* @param assoc [$options] options to use:
|
|
|
|
* [base] the base IRI to use.
|
|
|
|
* [graph] true to always output a top-level graph (default: false).
|
2013-08-06 19:52:15 +02:00
|
|
|
* [documentLoader(url)] the document loader.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return mixed the compacted JSON-LD output.
|
2011-07-14 06:47:40 +02:00
|
|
|
*/
|
2012-04-23 18:57:06 +02:00
|
|
|
function jsonld_compact($input, $ctx, $options=array()) {
|
|
|
|
$p = new JsonLdProcessor();
|
|
|
|
return $p->compact($input, $ctx, $options);
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
|
2011-10-28 01:00:01 +02:00
|
|
|
/**
|
2012-04-23 18:57:06 +02:00
|
|
|
* Performs JSON-LD expansion.
|
2011-10-28 01:00:01 +02:00
|
|
|
*
|
2012-04-23 18:57:06 +02:00
|
|
|
* @param mixed $input the JSON-LD object to expand.
|
|
|
|
* @param assoc[$options] the options to use:
|
|
|
|
* [base] the base IRI to use.
|
2013-08-06 19:52:15 +02:00
|
|
|
* [documentLoader(url)] the document loader.
|
2011-10-28 01:00:01 +02:00
|
|
|
*
|
2012-04-23 18:57:06 +02:00
|
|
|
* @return array the expanded JSON-LD output.
|
2011-10-28 01:00:01 +02:00
|
|
|
*/
|
2012-04-23 18:57:06 +02:00
|
|
|
function jsonld_expand($input, $options=array()) {
|
|
|
|
$p = new JsonLdProcessor();
|
|
|
|
return $p->expand($input, $options);
|
2011-10-28 01:00:01 +02:00
|
|
|
}
|
|
|
|
|
2013-02-15 22:25:30 +01:00
|
|
|
/**
|
|
|
|
* Performs JSON-LD flattening.
|
|
|
|
*
|
|
|
|
* @param mixed $input the JSON-LD to flatten.
|
2013-02-07 23:19:19 +01:00
|
|
|
* @param mixed $ctx the context to use to compact the flattened output, or
|
2013-02-15 22:25:30 +01:00
|
|
|
* null.
|
|
|
|
* @param [options] the options to use:
|
|
|
|
* [base] the base IRI to use.
|
2013-08-06 19:52:15 +02:00
|
|
|
* [documentLoader(url)] the document loader.
|
2013-02-07 23:19:19 +01:00
|
|
|
*
|
2013-02-15 22:25:30 +01:00
|
|
|
* @return mixed the flattened JSON-LD output.
|
|
|
|
*/
|
2013-02-07 23:19:19 +01:00
|
|
|
function jsonld_flatten($input, $ctx, $options=array()) {
|
2013-02-15 22:25:30 +01:00
|
|
|
$p = new JsonLdProcessor();
|
|
|
|
return $p->flatten($input, $ctx, $options);
|
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
|
2011-10-28 01:00:01 +02:00
|
|
|
/**
|
2012-04-23 18:57:06 +02:00
|
|
|
* Performs JSON-LD framing.
|
|
|
|
*
|
|
|
|
* @param mixed $input the JSON-LD object to frame.
|
|
|
|
* @param stdClass $frame the JSON-LD frame to use.
|
|
|
|
* @param assoc [$options] the framing options.
|
|
|
|
* [base] the base IRI to use.
|
|
|
|
* [embed] default @embed flag (default: true).
|
|
|
|
* [explicit] default @explicit flag (default: false).
|
2014-07-29 19:40:31 +02:00
|
|
|
* [requireAll] default @requireAll flag (default: true).
|
2012-04-23 18:57:06 +02:00
|
|
|
* [omitDefault] default @omitDefault flag (default: false).
|
2013-08-06 19:52:15 +02:00
|
|
|
* [documentLoader(url)] the document loader.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return stdClass the framed JSON-LD output.
|
2011-10-28 01:00:01 +02:00
|
|
|
*/
|
2012-04-23 18:57:06 +02:00
|
|
|
function jsonld_frame($input, $frame, $options=array()) {
|
|
|
|
$p = new JsonLdProcessor();
|
|
|
|
return $p->frame($input, $frame, $options);
|
2011-10-28 01:00:01 +02:00
|
|
|
}
|
|
|
|
|
2014-12-04 20:45:44 +01:00
|
|
|
/**
|
|
|
|
* **Experimental**
|
|
|
|
*
|
|
|
|
* 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);
|
|
|
|
};
|
|
|
|
|
2011-07-14 06:47:40 +02:00
|
|
|
/**
|
2013-02-20 22:55:36 +01:00
|
|
|
* Performs RDF dataset normalization on the given JSON-LD input. The output
|
|
|
|
* is an RDF dataset unless the 'format' option is used.
|
2011-07-14 06:47:40 +02:00
|
|
|
*
|
2012-04-23 18:57:06 +02:00
|
|
|
* @param mixed $input the JSON-LD object to normalize.
|
|
|
|
* @param assoc [$options] the options to use:
|
|
|
|
* [base] the base IRI to use.
|
2012-05-05 22:03:30 +02:00
|
|
|
* [format] the format if output is a string:
|
2013-07-18 18:07:32 +02:00
|
|
|
* 'application/nquads' for N-Quads.
|
2013-08-06 19:52:15 +02:00
|
|
|
* [documentLoader(url)] the document loader.
|
2011-07-14 06:47:40 +02:00
|
|
|
*
|
2013-02-20 22:55:36 +01:00
|
|
|
* @return mixed the normalized output.
|
2011-07-14 06:47:40 +02:00
|
|
|
*/
|
2012-04-23 18:57:06 +02:00
|
|
|
function jsonld_normalize($input, $options=array()) {
|
|
|
|
$p = new JsonLdProcessor();
|
|
|
|
return $p->normalize($input, $options);
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
|
2012-04-30 23:10:27 +02:00
|
|
|
/**
|
2013-02-20 22:55:36 +01:00
|
|
|
* Converts an RDF dataset to JSON-LD.
|
2012-04-30 23:10:27 +02:00
|
|
|
*
|
2013-02-20 22:55:36 +01:00
|
|
|
* @param mixed $input a serialized string of RDF in a format specified
|
|
|
|
* by the format option or an RDF dataset to convert.
|
2012-04-30 23:43:38 +02:00
|
|
|
* @param assoc [$options] the options to use:
|
2012-05-10 04:15:28 +02:00
|
|
|
* [format] the format if input not an array:
|
2012-04-30 23:10:27 +02:00
|
|
|
* 'application/nquads' for N-Quads (default).
|
2012-09-26 22:36:51 +02:00
|
|
|
* [useRdfType] true to use rdf:type, false to use @type
|
|
|
|
* (default: false).
|
|
|
|
* [useNativeTypes] true to convert XSD types into native types
|
2013-06-07 18:06:39 +02:00
|
|
|
* (boolean, integer, double), false not to (default: false).
|
2012-04-30 23:10:27 +02:00
|
|
|
*
|
|
|
|
* @return array the JSON-LD output.
|
|
|
|
*/
|
|
|
|
function jsonld_from_rdf($input, $options=array()) {
|
|
|
|
$p = new JsonLdProcessor();
|
|
|
|
return $p->fromRDF($input, $options);
|
|
|
|
}
|
|
|
|
|
2011-07-14 06:47:40 +02:00
|
|
|
/**
|
2013-02-20 22:55:36 +01:00
|
|
|
* Outputs the RDF dataset found in the given JSON-LD object.
|
2011-07-14 06:47:40 +02:00
|
|
|
*
|
2012-04-23 18:57:06 +02:00
|
|
|
* @param mixed $input the JSON-LD object.
|
|
|
|
* @param assoc [$options] the options to use:
|
2012-04-30 23:43:38 +02:00
|
|
|
* [base] the base IRI to use.
|
|
|
|
* [format] the format to use to output a string:
|
2013-07-18 18:07:32 +02:00
|
|
|
* 'application/nquads' for N-Quads.
|
2013-08-06 19:52:15 +02:00
|
|
|
* [produceGeneralizedRdf] true to output generalized RDF, false
|
|
|
|
* to produce only standard RDF (default: false).
|
|
|
|
* [documentLoader(url)] the document loader.
|
2011-07-14 06:47:40 +02:00
|
|
|
*
|
2013-02-20 22:55:36 +01:00
|
|
|
* @return mixed the resulting RDF dataset (or a serialization of it).
|
2011-07-14 06:47:40 +02:00
|
|
|
*/
|
2012-04-23 18:57:06 +02:00
|
|
|
function jsonld_to_rdf($input, $options=array()) {
|
|
|
|
$p = new JsonLdProcessor();
|
2012-04-30 23:10:27 +02:00
|
|
|
return $p->toRDF($input, $options);
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
|
2014-03-18 17:10:44 +01:00
|
|
|
/**
|
|
|
|
* JSON-encodes (with unescaped slashes) the given stdClass or array.
|
|
|
|
*
|
|
|
|
* @param mixed $input the native PHP stdClass or array which will be
|
|
|
|
* converted to JSON by json_encode().
|
2014-03-20 19:03:22 +01:00
|
|
|
* @param int $options the options to use.
|
|
|
|
* [JSON_PRETTY_PRINT] pretty print.
|
|
|
|
* @param int $depth the maximum depth to use.
|
2014-03-18 17:10:44 +01:00
|
|
|
*
|
|
|
|
* @return the encoded JSON data.
|
|
|
|
*/
|
2014-03-20 19:03:22 +01:00
|
|
|
function jsonld_encode($input, $options=0, $depth=512) {
|
2014-03-18 17:10:44 +01:00
|
|
|
// newer PHP has a flag to avoid escaped '/'
|
|
|
|
if(defined('JSON_UNESCAPED_SLASHES')) {
|
2014-03-20 19:03:22 +01:00
|
|
|
return json_encode($input, JSON_UNESCAPED_SLASHES | $options, $depth);
|
2014-03-18 17:10:44 +01:00
|
|
|
}
|
|
|
|
// use a simple string replacement of '\/' to '/'.
|
2014-03-20 19:03:22 +01:00
|
|
|
return str_replace('\\/', '/', json_encode($input, $options, $depth));
|
2014-03-18 17:10:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decodes a serialized JSON-LD object.
|
|
|
|
*
|
|
|
|
* @param string $input the JSON-LD input.
|
|
|
|
*
|
|
|
|
* @return mixed the resolved JSON-LD object, null on error.
|
|
|
|
*/
|
|
|
|
function jsonld_decode($input) {
|
|
|
|
return json_decode($input);
|
|
|
|
}
|
|
|
|
|
2013-09-13 19:07:18 +02:00
|
|
|
/**
|
|
|
|
* Parses a link header. The results will be key'd by the value of "rel".
|
|
|
|
*
|
|
|
|
* Link: <http://json-ld.org/contexts/person.jsonld>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"
|
|
|
|
*
|
|
|
|
* Parses as: {
|
|
|
|
* 'http://www.w3.org/ns/json-ld#context': {
|
|
|
|
* target: http://json-ld.org/contexts/person.jsonld,
|
|
|
|
* type: 'application/ld+json'
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* If there is more than one "rel" with the same IRI, then entries in the
|
2013-09-14 20:45:41 +02:00
|
|
|
* resulting map for that "rel" will be arrays of objects, otherwise they will
|
|
|
|
* be single objects.
|
2013-09-13 19:07:18 +02:00
|
|
|
*
|
|
|
|
* @param string $header the link header to parse.
|
|
|
|
*
|
|
|
|
* @return assoc the parsed result.
|
|
|
|
*/
|
|
|
|
function jsonld_parse_link_header($header) {
|
|
|
|
$rval = array();
|
|
|
|
// split on unbracketed/unquoted commas
|
2013-09-14 20:45:41 +02:00
|
|
|
if(!preg_match_all(
|
|
|
|
'/(?:<[^>]*?>|"[^"]*?"|[^,])+/', $header, $entries, PREG_SET_ORDER)) {
|
2013-09-13 19:07:18 +02:00
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
$r_link_header = '/\s*<([^>]*?)>\s*(?:;\s*(.*))?/';
|
|
|
|
foreach($entries as $entry) {
|
2013-09-14 20:45:41 +02:00
|
|
|
if(!preg_match($r_link_header, $entry[0], $match)) {
|
2013-09-13 19:07:18 +02:00
|
|
|
continue;
|
|
|
|
}
|
2013-09-14 20:45:41 +02:00
|
|
|
$result = (object)array('target' => $match[1]);
|
2013-09-13 19:07:18 +02:00
|
|
|
$params = $match[2];
|
|
|
|
$r_params = '/(.*?)=(?:(?:"([^"]*?)")|([^"]*?))\s*(?:(?:;\s*)|$)/';
|
2013-09-14 20:45:41 +02:00
|
|
|
preg_match_all($r_params, $params, $matches, PREG_SET_ORDER);
|
2013-09-13 19:07:18 +02:00
|
|
|
foreach($matches as $match) {
|
2013-09-14 20:45:41 +02:00
|
|
|
$result->{$match[1]} = $match[2] ?: $match[3];
|
2013-09-13 19:07:18 +02:00
|
|
|
}
|
2013-09-14 20:45:41 +02:00
|
|
|
$rel = property_exists($result, 'rel') ? $result->rel : '';
|
|
|
|
if(!isset($rval[$rel])) {
|
2013-09-13 19:07:18 +02:00
|
|
|
$rval[$rel] = $result;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(is_array($rval[$rel])) {
|
2013-09-13 19:07:18 +02:00
|
|
|
$rval[$rel][] = $result;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-09-13 19:07:18 +02:00
|
|
|
$rval[$rel] = array($rval[$rel], $result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
2013-02-15 18:16:26 +01:00
|
|
|
/**
|
|
|
|
* Relabels all blank nodes in the given JSON-LD input.
|
|
|
|
*
|
|
|
|
* @param mixed input the JSON-LD input.
|
|
|
|
*/
|
|
|
|
function jsonld_relabel_blank_nodes($input) {
|
|
|
|
$p = new JsonLdProcessor();
|
|
|
|
return $p->_labelBlankNodes(new UniqueNamer('_:b'), $input);
|
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
/** JSON-LD shared in-memory cache. */
|
|
|
|
global $jsonld_cache;
|
|
|
|
$jsonld_cache = new stdClass();
|
|
|
|
|
|
|
|
/** The default active context cache. */
|
2013-02-08 06:15:45 +01:00
|
|
|
$jsonld_cache->activeCtx = new ActiveContextCache();
|
2013-02-07 23:19:19 +01:00
|
|
|
|
2013-07-15 18:30:28 +02:00
|
|
|
/** Stores the default JSON-LD document loader. */
|
|
|
|
global $jsonld_default_load_document;
|
2013-09-15 21:54:54 +02:00
|
|
|
$jsonld_default_load_document = 'jsonld_default_document_loader';
|
2012-05-07 23:12:10 +02:00
|
|
|
|
|
|
|
/**
|
2013-07-15 18:30:28 +02:00
|
|
|
* Sets the default JSON-LD document loader.
|
2012-05-07 23:12:10 +02:00
|
|
|
*
|
2013-07-15 18:30:28 +02:00
|
|
|
* @param callable load_document(url) the document loader.
|
2012-05-07 23:12:10 +02:00
|
|
|
*/
|
2013-07-15 18:30:28 +02:00
|
|
|
function jsonld_set_document_loader($load_document) {
|
|
|
|
global $jsonld_default_load_document;
|
|
|
|
$jsonld_default_load_document = $load_document;
|
2012-05-07 23:12:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves JSON-LD at the given URL.
|
|
|
|
*
|
2013-03-31 01:42:23 +01:00
|
|
|
* @param string $url the URL to retrieve.
|
2012-05-07 23:12:10 +02:00
|
|
|
*
|
|
|
|
* @return the JSON-LD.
|
|
|
|
*/
|
2013-02-15 21:38:37 +01:00
|
|
|
function jsonld_get_url($url) {
|
2013-07-15 18:30:28 +02:00
|
|
|
global $jsonld_default_load_document;
|
|
|
|
if($jsonld_default_load_document !== null) {
|
|
|
|
$document_loader = $jsonld_default_load_document;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2014-04-24 21:27:03 +02:00
|
|
|
$document_loader = 'jsonld_default_document_loader';
|
2012-05-07 23:12:10 +02:00
|
|
|
}
|
|
|
|
|
2013-08-09 20:08:31 +02:00
|
|
|
$remote_doc = call_user_func($document_loader, $url);
|
2013-07-15 18:30:28 +02:00
|
|
|
if($remote_doc) {
|
|
|
|
return $remote_doc->document;
|
|
|
|
}
|
|
|
|
return null;
|
2012-10-29 14:57:24 +01:00
|
|
|
}
|
|
|
|
|
2013-02-15 22:25:30 +01:00
|
|
|
/**
|
|
|
|
* The default implementation to retrieve JSON-LD at the given URL.
|
|
|
|
*
|
|
|
|
* @param string $url the URL to to retrieve.
|
|
|
|
*
|
2013-07-15 18:30:28 +02:00
|
|
|
* @return stdClass the RemoteDocument object.
|
2013-02-15 22:25:30 +01:00
|
|
|
*/
|
2013-07-15 18:30:28 +02:00
|
|
|
function jsonld_default_document_loader($url) {
|
2013-09-15 21:54:54 +02:00
|
|
|
$doc = (object)array(
|
|
|
|
'contextUrl' => null, 'document' => null, 'documentUrl' => $url);
|
2013-07-15 18:30:28 +02:00
|
|
|
$redirects = array();
|
|
|
|
|
2013-02-18 22:34:14 +01:00
|
|
|
$opts = array(
|
|
|
|
'http' => array(
|
2013-07-15 18:30:28 +02:00
|
|
|
'method' => 'GET',
|
2013-02-15 22:25:30 +01:00
|
|
|
'header' =>
|
2014-03-19 23:55:18 +01:00
|
|
|
"Accept: application/ld+json\r\n"),
|
2014-04-24 21:15:31 +02:00
|
|
|
/* Note: Use jsonld_default_secure_document_loader for security. */
|
|
|
|
'ssl' => array(
|
|
|
|
'verify_peer' => false,
|
|
|
|
'allow_self_signed' => true)
|
|
|
|
);
|
|
|
|
|
2013-09-15 21:54:54 +02:00
|
|
|
$context = stream_context_create($opts);
|
|
|
|
$content_type = null;
|
|
|
|
stream_context_set_params($context, array('notification' =>
|
|
|
|
function($notification_code, $severity, $message) use (
|
|
|
|
&$redirects, &$content_type) {
|
2013-07-15 18:30:28 +02:00
|
|
|
switch($notification_code) {
|
|
|
|
case STREAM_NOTIFY_REDIRECTED:
|
|
|
|
$redirects[] = $message;
|
|
|
|
break;
|
2013-09-15 21:54:54 +02:00
|
|
|
case STREAM_NOTIFY_MIME_TYPE_IS:
|
|
|
|
$content_type = $message;
|
|
|
|
break;
|
2013-07-15 18:30:28 +02:00
|
|
|
};
|
|
|
|
}));
|
2014-04-24 21:42:17 +02:00
|
|
|
$result = @file_get_contents($url, false, $context);
|
2013-02-15 22:25:30 +01:00
|
|
|
if($result === false) {
|
2013-09-14 19:58:40 +02:00
|
|
|
throw new JsonLdException(
|
2014-04-24 20:58:10 +02:00
|
|
|
'Could not retrieve a JSON-LD document from the URL: ' . $url,
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.LoadDocumentError', 'loading document failed');
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
2013-09-15 21:54:54 +02:00
|
|
|
$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'];
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-09-15 21:54:54 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update document url based on redirects
|
2013-07-15 18:30:28 +02:00
|
|
|
$redirs = count($redirects);
|
|
|
|
if($redirs > 0) {
|
|
|
|
$url = $redirects[$redirs - 1];
|
|
|
|
}
|
2013-09-15 21:54:54 +02:00
|
|
|
$doc->document = $result;
|
|
|
|
$doc->documentUrl = $url;
|
|
|
|
return $doc;
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
2012-05-07 23:12:10 +02:00
|
|
|
|
2013-03-01 21:07:40 +01:00
|
|
|
/**
|
|
|
|
* The default implementation to retrieve JSON-LD at the given secure URL.
|
|
|
|
*
|
|
|
|
* @param string $url the secure URL to to retrieve.
|
|
|
|
*
|
2013-07-15 18:30:28 +02:00
|
|
|
* @return stdClass the RemoteDocument object.
|
2013-03-01 21:07:40 +01:00
|
|
|
*/
|
2013-07-15 18:30:28 +02:00
|
|
|
function jsonld_default_secure_document_loader($url) {
|
2013-03-01 21:07:40 +01:00
|
|
|
if(strpos($url, 'https') !== 0) {
|
2013-09-15 06:24:09 +02:00
|
|
|
throw new JsonLdException(
|
|
|
|
"Could not GET url: '$url'; 'https' is required.",
|
|
|
|
'jsonld.LoadDocumentError', 'loading document failed');
|
2013-02-18 22:34:14 +01:00
|
|
|
}
|
|
|
|
|
2013-09-15 21:54:54 +02:00
|
|
|
$doc = (object)array(
|
|
|
|
'contextUrl' => null, 'document' => null, 'documentUrl' => $url);
|
2013-03-01 21:07:40 +01:00
|
|
|
$redirects = array();
|
|
|
|
|
|
|
|
// default JSON-LD https GET implementation
|
2013-02-18 22:34:14 +01:00
|
|
|
$opts = array(
|
2014-03-19 23:55:18 +01:00
|
|
|
'http' => array(
|
|
|
|
'method' => 'GET',
|
2013-03-01 21:07:40 +01:00
|
|
|
'header' =>
|
2014-03-19 23:55:18 +01:00
|
|
|
"Accept: application/ld+json\r\n"),
|
2014-04-24 21:15:31 +02:00
|
|
|
'ssl' => array(
|
|
|
|
'verify_peer' => true,
|
|
|
|
'allow_self_signed' => false,
|
|
|
|
'cafile' => '/etc/ssl/certs/ca-certificates.crt'));
|
2013-09-15 21:54:54 +02:00
|
|
|
$context = stream_context_create($opts);
|
|
|
|
$content_type = null;
|
|
|
|
stream_context_set_params($context, array('notification' =>
|
|
|
|
function($notification_code, $severity, $message) use (
|
|
|
|
&$redirects, &$content_type) {
|
2013-02-18 22:34:14 +01:00
|
|
|
switch($notification_code) {
|
|
|
|
case STREAM_NOTIFY_REDIRECTED:
|
|
|
|
$redirects[] = $message;
|
|
|
|
break;
|
2013-09-15 21:54:54 +02:00
|
|
|
case STREAM_NOTIFY_MIME_TYPE_IS:
|
|
|
|
$content_type = $message;
|
|
|
|
break;
|
2013-02-18 22:34:14 +01:00
|
|
|
};
|
2013-09-15 21:54:54 +02:00
|
|
|
}));
|
2014-04-24 21:42:17 +02:00
|
|
|
$result = @file_get_contents($url, false, $context);
|
2013-03-01 21:07:40 +01:00
|
|
|
if($result === false) {
|
2013-09-15 06:24:09 +02:00
|
|
|
throw new JsonLdException(
|
2014-04-24 20:58:10 +02:00
|
|
|
'Could not retrieve a JSON-LD document from the URL: ' + $url,
|
2013-09-15 06:24:09 +02:00
|
|
|
'jsonld.LoadDocumentError', 'loading document failed');
|
2013-02-18 22:34:14 +01:00
|
|
|
}
|
2013-09-15 21:54:54 +02:00
|
|
|
$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'];
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-09-15 21:54:54 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update document url based on redirects
|
2013-02-18 22:34:14 +01:00
|
|
|
foreach($redirects as $redirect) {
|
2013-03-01 21:07:40 +01:00
|
|
|
if(strpos($redirect, 'https') !== 0) {
|
2013-09-15 06:24:09 +02:00
|
|
|
throw new JsonLdException(
|
|
|
|
"Could not GET redirected url: '$redirect'; 'https' is required.",
|
|
|
|
'jsonld.LoadDocumentError', 'loading document failed');
|
2013-03-01 21:07:40 +01:00
|
|
|
}
|
2013-07-15 18:30:28 +02:00
|
|
|
$url = $redirect;
|
2013-02-18 22:34:14 +01:00
|
|
|
}
|
2013-09-15 21:54:54 +02:00
|
|
|
$doc->document = $result;
|
|
|
|
$doc->documentUrl = $url;
|
|
|
|
return $doc;
|
2013-02-18 22:34:14 +01:00
|
|
|
}
|
|
|
|
|
2013-02-20 22:55:36 +01:00
|
|
|
/** Registered global RDF dataset parsers hashed by content-type. */
|
2013-02-15 22:25:30 +01:00
|
|
|
global $jsonld_rdf_parsers;
|
|
|
|
$jsonld_rdf_parsers = new stdClass();
|
|
|
|
|
|
|
|
/**
|
2013-02-20 22:55:36 +01:00
|
|
|
* Registers a global RDF dataset parser by content-type, for use with
|
2012-05-09 18:28:26 +02:00
|
|
|
* jsonld_from_rdf. Global parsers will be used by JsonLdProcessors that do
|
2013-02-15 22:25:30 +01:00
|
|
|
* not register their own parsers.
|
|
|
|
*
|
|
|
|
* @param string $content_type the content-type for the parser.
|
2012-05-09 18:28:26 +02:00
|
|
|
* @param callable $parser(input) the parser function (takes a string as
|
2013-07-15 18:30:28 +02:00
|
|
|
* a parameter and returns an RDF dataset).
|
2013-02-15 22:25:30 +01:00
|
|
|
*/
|
2012-05-09 18:28:26 +02:00
|
|
|
function jsonld_register_rdf_parser($content_type, $parser) {
|
2013-02-15 22:25:30 +01:00
|
|
|
global $jsonld_rdf_parsers;
|
|
|
|
$jsonld_rdf_parsers->{$content_type} = $parser;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-20 22:55:36 +01:00
|
|
|
* Unregisters a global RDF dataset parser by content-type.
|
2013-02-15 22:25:30 +01:00
|
|
|
*
|
|
|
|
* @param string $content_type the content-type for the parser.
|
|
|
|
*/
|
2012-05-09 18:28:26 +02:00
|
|
|
function jsonld_unregister_rdf_parser($content_type) {
|
2012-05-09 22:40:22 +02:00
|
|
|
global $jsonld_rdf_parsers;
|
2013-02-15 22:25:30 +01:00
|
|
|
if(property_exists($jsonld_rdf_parsers, $content_type)) {
|
2012-05-09 22:40:22 +02:00
|
|
|
unset($jsonld_rdf_parsers->{$content_type});
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
2012-10-09 19:17:25 +02:00
|
|
|
}
|
|
|
|
|
2013-02-15 22:25:30 +01:00
|
|
|
/**
|
|
|
|
* Parses a URL into its component parts.
|
|
|
|
*
|
|
|
|
* @param string $url the URL to parse.
|
|
|
|
*
|
|
|
|
* @return assoc the parsed URL.
|
|
|
|
*/
|
2013-02-07 23:19:19 +01:00
|
|
|
function jsonld_parse_url($url) {
|
2013-06-07 18:06:39 +02:00
|
|
|
if($url === null) {
|
|
|
|
$url = '';
|
|
|
|
}
|
|
|
|
|
2014-12-03 08:05:03 +01:00
|
|
|
$keys = array(
|
|
|
|
'href', 'protocol', 'scheme', '?authority', 'authority',
|
|
|
|
'?auth', 'auth', 'user', 'pass', 'host', '?port', 'port', 'path',
|
|
|
|
'?query', 'query', '?fragment', 'fragment');
|
|
|
|
$regex = "/^(([^:\/?#]+):)?(\/\/(((([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(:(\d*))?))?([^?#]*)(\?([^#]*))?(#(.*))?/";
|
|
|
|
preg_match($regex, $url, $match);
|
2013-03-06 17:36:14 +01:00
|
|
|
|
2014-12-03 08:05:03 +01:00
|
|
|
$rval = array();
|
|
|
|
$flags = array();
|
|
|
|
$len = count($keys);
|
|
|
|
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];
|
|
|
|
}
|
2013-03-06 17:36:14 +01:00
|
|
|
}
|
|
|
|
|
2014-12-03 08:05:03 +01:00
|
|
|
if(!$flags['authority']) {
|
|
|
|
$rval['authority'] = null;
|
2013-03-06 17:36:14 +01:00
|
|
|
}
|
2014-12-03 08:05:03 +01:00
|
|
|
if(!$flags['auth']) {
|
|
|
|
$rval['auth'] = $rval['user'] = $rval['pass'] = null;
|
2013-02-08 21:51:19 +01:00
|
|
|
}
|
2014-12-03 08:05:03 +01:00
|
|
|
if(!$flags['port']) {
|
|
|
|
$rval['port'] = null;
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
2014-12-03 08:05:03 +01:00
|
|
|
if(!$flags['query']) {
|
|
|
|
$rval['query'] = null;
|
2013-02-15 06:58:23 +01:00
|
|
|
}
|
2014-12-03 08:05:03 +01:00
|
|
|
if(!$flags['fragment']) {
|
|
|
|
$rval['fragment'] = null;
|
2013-03-06 17:36:14 +01:00
|
|
|
}
|
2014-12-03 08:05:03 +01:00
|
|
|
|
2013-03-06 17:36:14 +01:00
|
|
|
$rval['normalizedPath'] = jsonld_remove_dot_segments(
|
2014-12-03 08:05:03 +01:00
|
|
|
$rval['path'], !!$rval['authority']);
|
2013-03-06 17:36:14 +01:00
|
|
|
|
2013-02-08 04:46:28 +01:00
|
|
|
return $rval;
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
|
|
|
|
2013-03-27 15:52:25 +01:00
|
|
|
/**
|
|
|
|
* Removes dot segments from a URL path.
|
|
|
|
*
|
|
|
|
* @param string $path the path to remove dot segments from.
|
|
|
|
* @param bool $has_authority true if the URL has an authority, false if not.
|
|
|
|
*/
|
|
|
|
function jsonld_remove_dot_segments($path, $has_authority) {
|
|
|
|
$rval = '';
|
|
|
|
|
|
|
|
if(strpos($path, '/') === 0) {
|
|
|
|
$rval = '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
// RFC 3986 5.2.4 (reworked)
|
|
|
|
$input = explode('/', $path);
|
|
|
|
$output = array();
|
|
|
|
while(count($input) > 0) {
|
|
|
|
if($input[0] === '.' || ($input[0] === '' && count($input) > 1)) {
|
|
|
|
array_shift($input);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if($input[0] === '..') {
|
|
|
|
array_shift($input);
|
|
|
|
if($has_authority ||
|
|
|
|
(count($output) > 0 && $output[count($output) - 1] !== '..')) {
|
|
|
|
array_pop($output);
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
|
|
|
// leading relative URL '..'
|
2013-03-27 15:52:25 +01:00
|
|
|
$output[] = '..';
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$output[] = array_shift($input);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rval . implode('/', $output);
|
|
|
|
}
|
2013-03-06 17:36:14 +01:00
|
|
|
|
2012-10-09 19:17:25 +02:00
|
|
|
/**
|
2013-02-07 23:19:19 +01:00
|
|
|
* Prepends a base IRI to the given relative IRI.
|
2012-10-09 19:17:25 +02:00
|
|
|
*
|
2013-02-07 23:19:19 +01:00
|
|
|
* @param mixed $base a string or the parsed base IRI.
|
|
|
|
* @param string $iri the relative IRI.
|
2012-10-09 19:17:25 +02:00
|
|
|
*
|
2013-02-07 23:19:19 +01:00
|
|
|
* @return string the absolute IRI.
|
2012-10-09 19:17:25 +02:00
|
|
|
*/
|
2013-02-07 23:19:19 +01:00
|
|
|
function jsonld_prepend_base($base, $iri) {
|
2014-01-07 16:53:24 +01:00
|
|
|
// skip IRI processing
|
|
|
|
if($base === null) {
|
|
|
|
return $iri;
|
|
|
|
}
|
|
|
|
|
2013-02-15 22:25:30 +01:00
|
|
|
// already an absolute IRI
|
|
|
|
if(strpos($iri, ':') !== false) {
|
|
|
|
return $iri;
|
2013-02-08 21:48:23 +01:00
|
|
|
}
|
|
|
|
|
2013-02-28 23:10:21 +01:00
|
|
|
// parse base if it is a string
|
2013-02-07 23:19:19 +01:00
|
|
|
if(is_string($base)) {
|
|
|
|
$base = jsonld_parse_url($base);
|
|
|
|
}
|
2013-02-28 23:10:21 +01:00
|
|
|
|
2013-03-06 17:36:14 +01:00
|
|
|
// parse given IRI
|
2013-02-07 23:19:19 +01:00
|
|
|
$rel = jsonld_parse_url($iri);
|
2013-02-15 22:25:30 +01:00
|
|
|
|
2014-12-03 08:05:03 +01:00
|
|
|
// per RFC3986 5.2.2
|
|
|
|
$transform = array('protocol' => $base['protocol']);
|
2013-02-07 23:19:19 +01:00
|
|
|
|
2014-12-03 08:05:03 +01:00
|
|
|
if($rel['authority'] !== null) {
|
|
|
|
$transform['authority'] = $rel['authority'];
|
|
|
|
$transform['path'] = $rel['path'];
|
|
|
|
$transform['query'] = $rel['query'];
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2014-12-03 08:05:03 +01:00
|
|
|
$transform['authority'] = $base['authority'];
|
|
|
|
|
|
|
|
if($rel['path'] === '') {
|
|
|
|
$transform['path'] = $base['path'];
|
|
|
|
if($rel['query'] !== null) {
|
|
|
|
$transform['query'] = $rel['query'];
|
|
|
|
} else {
|
|
|
|
$transform['query'] = $base['query'];
|
2013-02-28 22:50:43 +01:00
|
|
|
}
|
2014-12-03 08:05:03 +01:00
|
|
|
} 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
|
|
|
|
if($rel['path'] !== '') {
|
|
|
|
$idx = strrpos($path, '/');
|
|
|
|
$idx = ($idx === false) ? 0 : $idx + 1;
|
|
|
|
$path = substr($path, 0, $idx);
|
|
|
|
if(strlen($path) > 0 && substr($path, -1) !== '/') {
|
|
|
|
$path .= '/';
|
|
|
|
}
|
|
|
|
$path .= $rel['path'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$transform['path'] = $path;
|
|
|
|
}
|
|
|
|
$transform['query'] = $rel['query'];
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-06 17:36:14 +01:00
|
|
|
// remove slashes and dots in path
|
2014-12-03 08:05:03 +01:00
|
|
|
$transform['path'] = jsonld_remove_dot_segments(
|
|
|
|
$transform['path'], !!$transform['authority']);
|
2013-02-15 22:25:30 +01:00
|
|
|
|
2014-12-03 08:05:03 +01:00
|
|
|
// construct URL
|
|
|
|
$rval = $transform['protocol'];
|
|
|
|
if($transform['authority'] !== null) {
|
|
|
|
$rval .= '//' . $transform['authority'];
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
2014-12-03 08:05:03 +01:00
|
|
|
$rval .= $transform['path'];
|
|
|
|
if($transform['query'] !== null) {
|
|
|
|
$rval .= '?' . $transform['query'];
|
|
|
|
}
|
|
|
|
if($rel['fragment'] !== null) {
|
|
|
|
$rval .= '#' . $rel['fragment'];
|
2013-02-08 21:48:23 +01:00
|
|
|
}
|
|
|
|
|
2014-12-03 08:05:03 +01:00
|
|
|
// handle empty base
|
2013-03-06 17:36:14 +01:00
|
|
|
if($rval === '') {
|
|
|
|
$rval = './';
|
2013-02-08 21:48:23 +01:00
|
|
|
}
|
2013-02-15 22:25:30 +01:00
|
|
|
|
2013-03-06 17:36:14 +01:00
|
|
|
return $rval;
|
2012-10-09 19:17:25 +02:00
|
|
|
}
|
2012-05-09 18:28:26 +02:00
|
|
|
|
2013-02-15 06:58:23 +01:00
|
|
|
/**
|
|
|
|
* Removes a base IRI from the given absolute IRI.
|
|
|
|
*
|
|
|
|
* @param mixed $base the base IRI.
|
|
|
|
* @param string $iri the absolute IRI.
|
|
|
|
*
|
|
|
|
* @return string the relative IRI if relative to base, otherwise the absolute
|
|
|
|
* IRI.
|
|
|
|
*/
|
|
|
|
function jsonld_remove_base($base, $iri) {
|
2014-01-07 16:53:24 +01:00
|
|
|
// skip IRI processing
|
|
|
|
if($base === null) {
|
|
|
|
return $iri;
|
|
|
|
}
|
|
|
|
|
2013-02-15 06:58:23 +01:00
|
|
|
if(is_string($base)) {
|
|
|
|
$base = jsonld_parse_url($base);
|
|
|
|
}
|
|
|
|
|
2013-02-28 22:42:32 +01:00
|
|
|
// establish base root
|
|
|
|
$root = '';
|
2013-03-06 17:36:14 +01:00
|
|
|
if($base['href'] !== '') {
|
|
|
|
$root .= "{$base['protocol']}//{$base['authority']}";
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(strpos($iri, '//') === false) {
|
|
|
|
// support network-path reference with empty base
|
2013-03-06 17:36:14 +01:00
|
|
|
$root .= '//';
|
2013-02-15 06:58:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// IRI not relative to base
|
2013-03-08 19:50:18 +01:00
|
|
|
if($root === '' || strpos($iri, $root) !== 0) {
|
2013-02-15 06:58:23 +01:00
|
|
|
return $iri;
|
|
|
|
}
|
|
|
|
|
2013-02-28 21:49:42 +01:00
|
|
|
// remove root from IRI
|
|
|
|
$rel = jsonld_parse_url(substr($iri, strlen($root)));
|
|
|
|
|
2013-10-05 06:04:17 +02:00
|
|
|
// remove path segments that match (do not remove last segment unless there
|
|
|
|
// is a hash or query)
|
2013-03-06 17:36:14 +01:00
|
|
|
$base_segments = explode('/', $base['normalizedPath']);
|
|
|
|
$iri_segments = explode('/', $rel['normalizedPath']);
|
2014-12-03 08:05:03 +01:00
|
|
|
$last = ($rel['query'] || $rel['fragment']) ? 0 : 1;
|
2013-10-05 06:04:17 +02:00
|
|
|
while(count($base_segments) > 0 && count($iri_segments) > $last) {
|
2013-02-15 06:58:23 +01:00
|
|
|
if($base_segments[0] !== $iri_segments[0]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
array_shift($base_segments);
|
|
|
|
array_shift($iri_segments);
|
|
|
|
}
|
|
|
|
|
|
|
|
// use '../' for each non-matching base segment
|
|
|
|
$rval = '';
|
|
|
|
if(count($base_segments) > 0) {
|
2014-02-21 18:18:18 +01:00
|
|
|
// 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)
|
|
|
|
array_pop($base_segments);
|
2013-02-15 06:58:23 +01:00
|
|
|
foreach($base_segments as $segment) {
|
|
|
|
$rval .= '../';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepend remaining segments
|
|
|
|
$rval .= implode('/', $iri_segments);
|
|
|
|
|
2013-03-27 15:52:25 +01:00
|
|
|
// add query and hash
|
2014-12-03 08:05:03 +01:00
|
|
|
if($rel['query'] !== null) {
|
2013-03-27 15:52:25 +01:00
|
|
|
$rval .= "?{$rel['query']}";
|
|
|
|
}
|
2014-12-03 08:05:03 +01:00
|
|
|
if($rel['fragment'] !== null) {
|
2013-03-27 15:52:25 +01:00
|
|
|
$rval .= "#{$rel['fragment']}";
|
|
|
|
}
|
2013-02-28 21:49:42 +01:00
|
|
|
|
|
|
|
if($rval === '') {
|
|
|
|
$rval = './';
|
|
|
|
}
|
|
|
|
|
2013-02-15 06:58:23 +01:00
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-18 20:43:10 +01:00
|
|
|
/**
|
2012-04-23 18:57:06 +02:00
|
|
|
* A JSON-LD processor.
|
2011-07-14 06:47:40 +02:00
|
|
|
*/
|
2012-04-23 18:57:06 +02:00
|
|
|
class JsonLdProcessor {
|
|
|
|
/** XSD constants */
|
|
|
|
const XSD_BOOLEAN = 'http://www.w3.org/2001/XMLSchema#boolean';
|
|
|
|
const XSD_DOUBLE = 'http://www.w3.org/2001/XMLSchema#double';
|
|
|
|
const XSD_INTEGER = 'http://www.w3.org/2001/XMLSchema#integer';
|
2012-09-26 22:36:51 +02:00
|
|
|
const XSD_STRING = 'http://www.w3.org/2001/XMLSchema#string';
|
2012-04-23 18:57:06 +02:00
|
|
|
|
|
|
|
/** RDF constants */
|
2013-08-09 19:32:26 +02:00
|
|
|
const RDF_LIST = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#List';
|
2012-04-23 18:57:06 +02:00
|
|
|
const RDF_FIRST = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first';
|
|
|
|
const RDF_REST = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest';
|
|
|
|
const RDF_NIL = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil';
|
|
|
|
const RDF_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';
|
2013-02-20 22:55:36 +01:00
|
|
|
const RDF_LANGSTRING =
|
|
|
|
'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString';
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2012-05-07 23:12:10 +02:00
|
|
|
/** Restraints */
|
|
|
|
const MAX_CONTEXT_URLS = 10;
|
|
|
|
|
2013-02-20 22:55:36 +01:00
|
|
|
/** Processor-specific RDF dataset parsers. */
|
2012-05-09 18:28:26 +02:00
|
|
|
protected $rdfParsers = null;
|
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
/**
|
|
|
|
* Constructs a JSON-LD processor.
|
|
|
|
*/
|
|
|
|
public function __construct() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs JSON-LD compaction.
|
|
|
|
*
|
|
|
|
* @param mixed $input the JSON-LD object to compact.
|
|
|
|
* @param mixed $ctx the context to compact with.
|
|
|
|
* @param assoc $options the compaction options.
|
2013-02-07 23:19:19 +01:00
|
|
|
* [base] the base IRI to use.
|
2013-02-21 22:06:33 +01:00
|
|
|
* [compactArrays] true to compact arrays to single values when
|
|
|
|
* appropriate, false not to (default: true).
|
|
|
|
* [graph] true to always output a top-level graph (default: false).
|
2013-02-07 23:19:19 +01:00
|
|
|
* [skipExpansion] true to assume the input is expanded and skip
|
|
|
|
* expansion, false not to, defaults to false.
|
2012-04-23 18:57:06 +02:00
|
|
|
* [activeCtx] true to also return the active context used.
|
2013-08-06 19:52:15 +02:00
|
|
|
* [documentLoader(url)] the document loader.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return mixed the compacted JSON-LD output.
|
|
|
|
*/
|
|
|
|
public function compact($input, $ctx, $options) {
|
2014-04-24 21:27:03 +02:00
|
|
|
global $jsonld_default_load_document;
|
|
|
|
|
2013-04-09 17:34:31 +02:00
|
|
|
if($ctx === null) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'The compaction context must not be null.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.CompactError', 'invalid local context');
|
2013-04-09 17:34:31 +02:00
|
|
|
}
|
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
// nothing to compact
|
|
|
|
if($input === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-07-15 18:30:28 +02:00
|
|
|
self::setdefaults($options, array(
|
|
|
|
'base' => is_string($input) ? $input : '',
|
|
|
|
'compactArrays' => true,
|
|
|
|
'graph' => false,
|
|
|
|
'skipExpansion' => false,
|
|
|
|
'activeCtx' => false,
|
2014-12-04 20:45:44 +01:00
|
|
|
'documentLoader' => $jsonld_default_load_document,
|
|
|
|
'link' => false));
|
|
|
|
if($options['link']) {
|
|
|
|
// force skip expansion when linking, "link" is not part of the
|
|
|
|
// public API, it should only be called from framing
|
|
|
|
$options['skipExpansion'] = true;
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
if($options['skipExpansion'] === true) {
|
|
|
|
$expanded = $input;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-07 23:19:19 +01:00
|
|
|
// expand input
|
|
|
|
try {
|
|
|
|
$expanded = $this->expand($input, $options);
|
2014-04-24 20:39:30 +02:00
|
|
|
} catch(JsonLdException $e) {
|
2013-02-07 23:19:19 +01:00
|
|
|
throw new JsonLdException(
|
|
|
|
'Could not expand input before compaction.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.CompactError', null, null, $e);
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// process context
|
2013-02-07 23:19:19 +01:00
|
|
|
$active_ctx = $this->_getInitialContext($options);
|
2012-04-23 18:57:06 +02:00
|
|
|
try {
|
|
|
|
$active_ctx = $this->processContext($active_ctx, $ctx, $options);
|
2014-04-24 20:39:30 +02:00
|
|
|
} catch(JsonLdException $e) {
|
2012-04-23 18:57:06 +02:00
|
|
|
throw new JsonLdException(
|
2012-04-24 20:37:21 +02:00
|
|
|
'Could not process context before compaction.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.CompactError', null, null, $e);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// do compaction
|
2012-04-23 21:52:56 +02:00
|
|
|
$compacted = $this->_compact($active_ctx, null, $expanded, $options);
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-02-21 22:06:33 +01:00
|
|
|
if($options['compactArrays'] &&
|
|
|
|
!$options['graph'] && is_array($compacted)) {
|
2013-02-08 05:47:55 +01:00
|
|
|
if(count($compacted) === 1) {
|
2014-04-24 20:39:30 +02:00
|
|
|
// simplify to a single item
|
2013-02-08 05:47:55 +01:00
|
|
|
$compacted = $compacted[0];
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(count($compacted) === 0) {
|
|
|
|
// simplify to an empty object
|
2013-02-08 05:47:55 +01:00
|
|
|
$compacted = new stdClass();
|
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if($options['graph']) {
|
|
|
|
// always use array if graph option is on
|
2013-02-15 22:25:30 +01:00
|
|
|
$compacted = self::arrayify($compacted);
|
2012-04-23 21:52:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// follow @context key
|
|
|
|
if(is_object($ctx) && property_exists($ctx, '@context')) {
|
|
|
|
$ctx = $ctx->{'@context'};
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// build output context
|
|
|
|
$ctx = self::copy($ctx);
|
|
|
|
$ctx = self::arrayify($ctx);
|
|
|
|
|
|
|
|
// remove empty contexts
|
|
|
|
$tmp = $ctx;
|
|
|
|
$ctx = array();
|
2012-04-25 02:29:40 +02:00
|
|
|
foreach($tmp as $v) {
|
2013-02-08 04:46:28 +01:00
|
|
|
if(!is_object($v) || count(array_keys((array)$v)) > 0) {
|
2012-04-23 18:57:06 +02:00
|
|
|
$ctx[] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove array if only one context
|
|
|
|
$ctx_length = count($ctx);
|
|
|
|
$has_context = ($ctx_length > 0);
|
|
|
|
if($ctx_length === 1) {
|
|
|
|
$ctx = $ctx[0];
|
|
|
|
}
|
|
|
|
|
2013-04-09 17:31:26 +02:00
|
|
|
// add context and/or @graph
|
|
|
|
if(is_array($compacted)) {
|
|
|
|
// use '@graph' keyword
|
|
|
|
$kwgraph = $this->_compactIri($active_ctx, '@graph');
|
|
|
|
$graph = $compacted;
|
|
|
|
$compacted = new stdClass();
|
|
|
|
if($has_context) {
|
2012-04-23 18:57:06 +02:00
|
|
|
$compacted->{'@context'} = $ctx;
|
2013-04-09 17:31:26 +02:00
|
|
|
}
|
|
|
|
$compacted->{$kwgraph} = $graph;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(is_object($compacted) && $has_context) {
|
2013-04-09 17:31:26 +02:00
|
|
|
// reorder keys so @context is first
|
|
|
|
$graph = $compacted;
|
|
|
|
$compacted = new stdClass();
|
|
|
|
$compacted->{'@context'} = $ctx;
|
|
|
|
foreach($graph as $k => $v) {
|
|
|
|
$compacted->{$k} = $v;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($options['activeCtx']) {
|
2013-02-07 23:19:19 +01:00
|
|
|
return array('compacted' => $compacted, 'activeCtx' => $active_ctx);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
|
|
|
|
return $compacted;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs JSON-LD expansion.
|
|
|
|
*
|
|
|
|
* @param mixed $input the JSON-LD object to expand.
|
|
|
|
* @param assoc $options the options to use:
|
|
|
|
* [base] the base IRI to use.
|
2013-07-15 18:30:28 +02:00
|
|
|
* [expandContext] a context to expand with.
|
2013-02-07 23:19:19 +01:00
|
|
|
* [keepFreeFloatingNodes] true to keep free-floating nodes,
|
|
|
|
* false not to, defaults to false.
|
2013-08-06 19:52:15 +02:00
|
|
|
* [documentLoader(url)] the document loader.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return array the expanded JSON-LD output.
|
|
|
|
*/
|
|
|
|
public function expand($input, $options) {
|
2014-04-24 21:27:03 +02:00
|
|
|
global $jsonld_default_load_document;
|
2013-07-15 18:30:28 +02:00
|
|
|
self::setdefaults($options, array(
|
|
|
|
'keepFreeFloatingNodes' => false,
|
2014-04-24 21:27:03 +02:00
|
|
|
'documentLoader' => $jsonld_default_load_document));
|
2013-07-15 18:30:28 +02:00
|
|
|
|
|
|
|
// if input is a string, attempt to dereference remote document
|
|
|
|
if(is_string($input)) {
|
2013-09-14 20:12:19 +02:00
|
|
|
$remote_doc = call_user_func($options['documentLoader'], $input);
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-07-15 18:30:28 +02:00
|
|
|
$remote_doc = (object)array(
|
|
|
|
'contextUrl' => null,
|
|
|
|
'documentUrl' => null,
|
|
|
|
'document' => $input);
|
|
|
|
}
|
|
|
|
|
2013-09-14 20:12:19 +02:00
|
|
|
try {
|
|
|
|
if($remote_doc->document === null) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'No remote document found at the given URL.',
|
|
|
|
'jsonld.NullRemoteDocument');
|
|
|
|
}
|
|
|
|
if(is_string($remote_doc->document)) {
|
2013-09-15 04:02:37 +02:00
|
|
|
$remote_doc->document = self::_parse_json($remote_doc->document);
|
2013-09-14 20:12:19 +02:00
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} catch(Exception $e) {
|
2013-09-14 20:12:19 +02:00
|
|
|
throw new JsonLdException(
|
2014-04-24 21:42:17 +02:00
|
|
|
'Could not retrieve a JSON-LD document from the URL.',
|
2013-09-14 20:12:19 +02:00
|
|
|
'jsonld.LoadDocumentError', 'loading document failed',
|
|
|
|
array('remoteDoc' => $remote_doc), $e);
|
|
|
|
}
|
|
|
|
|
2013-09-14 20:19:27 +02:00
|
|
|
// set default base
|
|
|
|
self::setdefault($options, 'base', $remote_doc->documentUrl ?: '');
|
|
|
|
|
2013-07-15 18:30:28 +02:00
|
|
|
// build meta-object and retrieve all @context urls
|
|
|
|
$input = (object)array(
|
2013-09-14 20:12:19 +02:00
|
|
|
'document' => self::copy($remote_doc->document),
|
2013-07-15 18:30:28 +02:00
|
|
|
'remoteContext' => (object)array(
|
|
|
|
'@context' => $remote_doc->contextUrl));
|
2013-07-16 21:24:11 +02:00
|
|
|
if(isset($options['expandContext'])) {
|
|
|
|
$expand_context = self::copy($options['expandContext']);
|
|
|
|
if(is_object($expand_context) &&
|
|
|
|
property_exists($expand_context, '@context')) {
|
|
|
|
$input->expandContext = $expand_context;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-07-16 21:24:11 +02:00
|
|
|
$input->expandContext = (object)array('@context' => $expand_context);
|
|
|
|
}
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-02-15 21:34:21 +01:00
|
|
|
// retrieve all @context URLs in the input
|
2012-05-07 23:12:10 +02:00
|
|
|
try {
|
2013-02-15 21:34:21 +01:00
|
|
|
$this->_retrieveContextUrls(
|
2013-07-24 22:05:22 +02:00
|
|
|
$input, new stdClass(), $options['documentLoader'], $options['base']);
|
2014-04-24 20:39:30 +02:00
|
|
|
} catch(Exception $e) {
|
2013-02-15 22:25:30 +01:00
|
|
|
throw new JsonLdException(
|
|
|
|
'Could not perform JSON-LD expansion.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.ExpandError', null, null, $e);
|
2012-05-07 23:12:10 +02:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
$active_ctx = $this->_getInitialContext($options);
|
2013-07-15 18:30:28 +02:00
|
|
|
$document = $input->document;
|
|
|
|
$remote_context = $input->remoteContext->{'@context'};
|
|
|
|
|
|
|
|
// process optional expandContext
|
2013-07-16 21:24:11 +02:00
|
|
|
if(property_exists($input, 'expandContext')) {
|
|
|
|
$active_ctx = self::_processContext(
|
|
|
|
$active_ctx, $input->expandContext, $options);
|
2013-07-15 18:30:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// process remote context from HTTP Link Header
|
|
|
|
if($remote_context) {
|
2013-07-16 21:24:11 +02:00
|
|
|
$active_ctx = self::_processContext(
|
|
|
|
$active_ctx, $remote_context, $options);
|
2013-07-15 18:30:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// do expansion
|
|
|
|
$expanded = $this->_expand($active_ctx, null, $document, $options, false);
|
2012-04-23 18:57:06 +02:00
|
|
|
|
|
|
|
// optimize away @graph with no other properties
|
|
|
|
if(is_object($expanded) && property_exists($expanded, '@graph') &&
|
2013-02-07 23:19:19 +01:00
|
|
|
count(array_keys((array)$expanded)) === 1) {
|
2012-04-23 18:57:06 +02:00
|
|
|
$expanded = $expanded->{'@graph'};
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if($expanded === null) {
|
2013-02-07 23:19:19 +01:00
|
|
|
$expanded = array();
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
// normalize to an array
|
|
|
|
return self::arrayify($expanded);
|
|
|
|
}
|
|
|
|
|
2013-02-15 22:25:30 +01:00
|
|
|
/**
|
|
|
|
* Performs JSON-LD flattening.
|
|
|
|
*
|
2013-02-07 23:19:19 +01:00
|
|
|
* @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:
|
|
|
|
* [base] the base IRI to use.
|
2013-07-15 18:30:28 +02:00
|
|
|
* [expandContext] a context to expand with.
|
2013-08-06 19:52:15 +02:00
|
|
|
* [documentLoader(url)] the document loader.
|
2013-02-15 22:25:30 +01:00
|
|
|
*
|
|
|
|
* @return array the flattened output.
|
|
|
|
*/
|
2013-02-07 23:19:19 +01:00
|
|
|
public function flatten($input, $ctx, $options) {
|
2014-04-24 21:27:03 +02:00
|
|
|
global $jsonld_default_load_document;
|
2013-07-15 18:30:28 +02:00
|
|
|
self::setdefaults($options, array(
|
|
|
|
'base' => is_string($input) ? $input : '',
|
2014-04-24 21:27:03 +02:00
|
|
|
'documentLoader' => $jsonld_default_load_document));
|
2013-02-07 23:19:19 +01:00
|
|
|
|
2013-02-15 22:25:30 +01:00
|
|
|
try {
|
|
|
|
// expand input
|
|
|
|
$expanded = $this->expand($input, $options);
|
2014-04-24 20:39:30 +02:00
|
|
|
} catch(Exception $e) {
|
2013-02-15 22:25:30 +01:00
|
|
|
throw new JsonLdException(
|
|
|
|
'Could not expand input before flattening.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.FlattenError', null, null, $e);
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// do flattening
|
2013-02-07 23:19:19 +01:00
|
|
|
$flattened = $this->_flatten($expanded);
|
|
|
|
|
|
|
|
if($ctx === null) {
|
|
|
|
return $flattened;
|
|
|
|
}
|
|
|
|
|
2013-02-15 22:25:30 +01:00
|
|
|
// compact result (force @graph option to true, skip expansion)
|
|
|
|
$options['graph'] = true;
|
2013-02-07 23:19:19 +01:00
|
|
|
$options['skipExpansion'] = true;
|
|
|
|
try {
|
|
|
|
$compacted = $this->compact($flattened, $ctx, $options);
|
2014-04-24 20:39:30 +02:00
|
|
|
} catch(Exception $e) {
|
2013-02-15 22:25:30 +01:00
|
|
|
throw new JsonLdException(
|
2013-02-07 23:19:19 +01:00
|
|
|
'Could not compact flattened output.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.FlattenError', null, null, $e);
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
|
|
|
|
2013-02-15 22:25:30 +01:00
|
|
|
return $compacted;
|
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
/**
|
|
|
|
* Performs JSON-LD framing.
|
|
|
|
*
|
|
|
|
* @param mixed $input the JSON-LD object to frame.
|
|
|
|
* @param stdClass $frame the JSON-LD frame to use.
|
|
|
|
* @param $options the framing options.
|
|
|
|
* [base] the base IRI to use.
|
2013-07-15 18:30:28 +02:00
|
|
|
* [expandContext] a context to expand with.
|
2014-12-04 20:45:44 +01:00
|
|
|
* [embed] default @embed flag: '@last', '@always', '@never', '@link'
|
|
|
|
* (default: '@last').
|
2012-04-23 18:57:06 +02:00
|
|
|
* [explicit] default @explicit flag (default: false).
|
2014-07-29 19:40:31 +02:00
|
|
|
* [requireAll] default @requireAll flag (default: true).
|
2012-04-23 18:57:06 +02:00
|
|
|
* [omitDefault] default @omitDefault flag (default: false).
|
2013-08-06 19:52:15 +02:00
|
|
|
* [documentLoader(url)] the document loader.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return stdClass the framed JSON-LD output.
|
|
|
|
*/
|
|
|
|
public function frame($input, $frame, $options) {
|
2014-04-24 21:27:03 +02:00
|
|
|
global $jsonld_default_load_document;
|
2013-07-15 18:30:28 +02:00
|
|
|
self::setdefaults($options, array(
|
|
|
|
'base' => is_string($input) ? $input : '',
|
|
|
|
'compactArrays' => true,
|
2014-12-04 20:45:44 +01:00
|
|
|
'embed' => '@last',
|
2013-07-15 18:30:28 +02:00
|
|
|
'explicit' => false,
|
2014-07-29 19:40:31 +02:00
|
|
|
'requireAll' => true,
|
2013-07-15 18:30:28 +02:00
|
|
|
'omitDefault' => false,
|
2014-04-24 21:27:03 +02:00
|
|
|
'documentLoader' => $jsonld_default_load_document));
|
2013-07-15 18:30:28 +02:00
|
|
|
|
|
|
|
// if frame is a string, attempt to dereference remote document
|
|
|
|
if(is_string($frame)) {
|
2013-09-14 20:12:19 +02:00
|
|
|
$remote_frame = call_user_func($options['documentLoader'], $frame);
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-07-15 18:30:28 +02:00
|
|
|
$remote_frame = (object)array(
|
|
|
|
'contextUrl' => null,
|
|
|
|
'documentUrl' => null,
|
|
|
|
'document' => $frame);
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-09-14 20:12:19 +02:00
|
|
|
try {
|
|
|
|
if($remote_frame->document === null) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'No remote document found at the given URL.',
|
|
|
|
'jsonld.NullRemoteDocument');
|
|
|
|
}
|
|
|
|
if(is_string($remote_frame->document)) {
|
2013-09-15 04:02:37 +02:00
|
|
|
$remote_frame->document = self::_parse_json($remote_frame->document);
|
2013-09-14 20:12:19 +02:00
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} catch(Exception $e) {
|
2013-09-14 20:12:19 +02:00
|
|
|
throw new JsonLdException(
|
2014-04-24 21:42:17 +02:00
|
|
|
'Could not retrieve a JSON-LD document from the URL.',
|
2013-09-14 20:12:19 +02:00
|
|
|
'jsonld.LoadDocumentError', 'loading document failed',
|
2014-04-24 21:42:17 +02:00
|
|
|
array('remoteDoc' => $remote_frame), $e);
|
2013-09-14 20:12:19 +02:00
|
|
|
}
|
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
// preserve frame context
|
2013-07-15 18:30:28 +02:00
|
|
|
$frame = $remote_frame->document;
|
|
|
|
if($frame !== null) {
|
|
|
|
$ctx = (property_exists($frame, '@context') ?
|
|
|
|
$frame->{'@context'} : new stdClass());
|
|
|
|
if($remote_frame->contextUrl !== null) {
|
|
|
|
if($ctx !== null) {
|
|
|
|
$ctx = $remote_frame->contextUrl;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-07-15 18:30:28 +02:00
|
|
|
$ctx = self::arrayify($ctx);
|
|
|
|
$ctx[] = $remote_frame->contextUrl;
|
|
|
|
}
|
|
|
|
$frame->{'@context'} = $ctx;
|
|
|
|
}
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
// expand input
|
2013-02-07 23:19:19 +01:00
|
|
|
$expanded = $this->expand($input, $options);
|
2014-04-24 20:39:30 +02:00
|
|
|
} catch(Exception $e) {
|
2012-04-23 18:57:06 +02:00
|
|
|
throw new JsonLdException(
|
|
|
|
'Could not expand input before framing.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.FrameError', null, null, $e);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// expand frame
|
2013-09-15 03:43:35 +02:00
|
|
|
$opts = $options;
|
2013-02-07 23:19:19 +01:00
|
|
|
$opts['keepFreeFloatingNodes'] = true;
|
|
|
|
$expanded_frame = $this->expand($frame, $opts);
|
2014-04-24 20:39:30 +02:00
|
|
|
} catch(Exception $e) {
|
2012-04-23 18:57:06 +02:00
|
|
|
throw new JsonLdException(
|
|
|
|
'Could not expand frame before framing.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.FrameError', null, null, $e);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// do framing
|
2013-02-07 23:19:19 +01:00
|
|
|
$framed = $this->_frame($expanded, $expanded_frame, $options);
|
2012-04-23 18:57:06 +02:00
|
|
|
|
|
|
|
try {
|
2014-12-04 20:45:44 +01:00
|
|
|
// compact result (force @graph option to true, skip expansion, check
|
|
|
|
// for linked embeds)
|
2012-04-23 18:57:06 +02:00
|
|
|
$options['graph'] = true;
|
2013-02-07 23:19:19 +01:00
|
|
|
$options['skipExpansion'] = true;
|
2014-12-04 20:45:44 +01:00
|
|
|
$options['link'] = new ArrayObject();
|
2012-04-23 18:57:06 +02:00
|
|
|
$options['activeCtx'] = true;
|
|
|
|
$result = $this->compact($framed, $ctx, $options);
|
2014-04-24 20:39:30 +02:00
|
|
|
} catch(Exception $e) {
|
2012-04-23 18:57:06 +02:00
|
|
|
throw new JsonLdException(
|
|
|
|
'Could not compact framed output.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.FrameError', null, null, $e);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$compacted = $result['compacted'];
|
2013-02-07 23:19:19 +01:00
|
|
|
$active_ctx = $result['activeCtx'];
|
2012-04-23 18:57:06 +02:00
|
|
|
|
|
|
|
// get graph alias
|
2013-02-07 23:19:19 +01:00
|
|
|
$graph = $this->_compactIri($active_ctx, '@graph');
|
2012-04-23 18:57:06 +02:00
|
|
|
// remove @preserve from results
|
2014-12-04 20:45:44 +01:00
|
|
|
$options['link'] = new ArrayObject();
|
2013-02-07 23:19:19 +01:00
|
|
|
$compacted->{$graph} = $this->_removePreserve(
|
2013-02-21 22:06:33 +01:00
|
|
|
$active_ctx, $compacted->{$graph}, $options);
|
2012-04-23 18:57:06 +02:00
|
|
|
return $compacted;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs JSON-LD normalization.
|
|
|
|
*
|
|
|
|
* @param mixed $input the JSON-LD object to normalize.
|
|
|
|
* @param assoc $options the options to use:
|
|
|
|
* [base] the base IRI to use.
|
2013-07-15 18:30:28 +02:00
|
|
|
* [expandContext] a context to expand with.
|
2013-02-20 22:55:36 +01:00
|
|
|
* [format] the format if output is a string:
|
|
|
|
* 'application/nquads' for N-Quads.
|
2013-08-06 19:52:15 +02:00
|
|
|
* [documentLoader(url)] the document loader.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
2013-02-20 22:55:36 +01:00
|
|
|
* @return mixed the normalized output.
|
2012-04-23 18:57:06 +02:00
|
|
|
*/
|
|
|
|
public function normalize($input, $options) {
|
2014-04-24 21:27:03 +02:00
|
|
|
global $jsonld_default_load_document;
|
2013-07-15 18:30:28 +02:00
|
|
|
self::setdefaults($options, array(
|
|
|
|
'base' => is_string($input) ? $input : '',
|
2014-04-24 21:27:03 +02:00
|
|
|
'documentLoader' => $jsonld_default_load_document));
|
2012-04-23 18:57:06 +02:00
|
|
|
|
|
|
|
try {
|
2013-02-20 22:55:36 +01:00
|
|
|
// convert to RDF dataset then do normalization
|
2013-09-15 03:43:35 +02:00
|
|
|
$opts = $options;
|
2013-02-20 22:55:36 +01:00
|
|
|
if(isset($opts['format'])) {
|
|
|
|
unset($opts['format']);
|
|
|
|
}
|
2013-08-06 20:06:30 +02:00
|
|
|
$opts['produceGeneralizedRdf'] = false;
|
2013-02-20 22:55:36 +01:00
|
|
|
$dataset = $this->toRDF($input, $opts);
|
2014-04-24 20:39:30 +02:00
|
|
|
} catch(Exception $e) {
|
2012-04-23 18:57:06 +02:00
|
|
|
throw new JsonLdException(
|
2013-02-20 22:55:36 +01:00
|
|
|
'Could not convert input to RDF dataset before normalization.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.NormalizeError', null, null, $e);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// do normalization
|
2013-02-20 22:55:36 +01:00
|
|
|
return $this->_normalize($dataset, $options);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2012-04-30 23:10:27 +02:00
|
|
|
/**
|
2013-02-20 22:55:36 +01:00
|
|
|
* Converts an RDF dataset to JSON-LD.
|
2012-04-30 23:10:27 +02:00
|
|
|
*
|
2013-02-20 22:55:36 +01:00
|
|
|
* @param mixed $dataset a serialized string of RDF in a format specified
|
|
|
|
* by the format option or an RDF dataset to convert.
|
2012-04-30 23:43:38 +02:00
|
|
|
* @param assoc $options the options to use:
|
2012-04-30 23:10:27 +02:00
|
|
|
* [format] the format if input is a string:
|
|
|
|
* 'application/nquads' for N-Quads (default).
|
2012-09-26 22:36:51 +02:00
|
|
|
* [useRdfType] true to use rdf:type, false to use @type
|
|
|
|
* (default: false).
|
|
|
|
* [useNativeTypes] true to convert XSD types into native types
|
2013-06-07 18:06:39 +02:00
|
|
|
* (boolean, integer, double), false not to (default: false).
|
2012-04-30 23:10:27 +02:00
|
|
|
*
|
|
|
|
* @return array the JSON-LD output.
|
|
|
|
*/
|
2013-02-20 22:55:36 +01:00
|
|
|
public function fromRDF($dataset, $options) {
|
2012-05-09 18:28:26 +02:00
|
|
|
global $jsonld_rdf_parsers;
|
|
|
|
|
2013-07-15 18:30:28 +02:00
|
|
|
self::setdefaults($options, array(
|
|
|
|
'useRdfType' => false,
|
|
|
|
'useNativeTypes' => false));
|
2012-04-30 23:10:27 +02:00
|
|
|
|
2013-02-20 22:55:36 +01:00
|
|
|
if(!isset($options['format']) && is_string($dataset)) {
|
|
|
|
// set default format to nquads
|
|
|
|
$options['format'] = 'application/nquads';
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle special format
|
|
|
|
if(isset($options['format']) && $options['format']) {
|
2012-05-09 18:28:26 +02:00
|
|
|
// supported formats (processor-specific and global)
|
|
|
|
if(($this->rdfParsers !== null &&
|
|
|
|
!property_exists($this->rdfParsers, $options['format'])) ||
|
|
|
|
$this->rdfParsers === null &&
|
|
|
|
!property_exists($jsonld_rdf_parsers, $options['format'])) {
|
2013-02-15 22:25:30 +01:00
|
|
|
throw new JsonLdException(
|
|
|
|
'Unknown input format.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.UnknownFormat', null, array('format' => $options['format']));
|
2012-05-09 18:28:26 +02:00
|
|
|
}
|
|
|
|
if($this->rdfParsers !== null) {
|
|
|
|
$callable = $this->rdfParsers->{$options['format']};
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2012-05-09 18:28:26 +02:00
|
|
|
$callable = $jsonld_rdf_parsers->{$options['format']};
|
2012-04-30 23:10:27 +02:00
|
|
|
}
|
2013-08-09 20:08:31 +02:00
|
|
|
$dataset = call_user_func($callable, $dataset);
|
2012-04-30 23:10:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// convert from RDF
|
2013-02-20 22:55:36 +01:00
|
|
|
return $this->_fromRDF($dataset, $options);
|
2012-04-30 23:10:27 +02:00
|
|
|
}
|
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
/**
|
2013-02-20 22:55:36 +01:00
|
|
|
* Outputs the RDF dataset found in the given JSON-LD object.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @param mixed $input the JSON-LD object.
|
|
|
|
* @param assoc $options the options to use:
|
2012-04-30 23:43:38 +02:00
|
|
|
* [base] the base IRI to use.
|
2013-07-15 18:30:28 +02:00
|
|
|
* [expandContext] a context to expand with.
|
2012-04-30 23:43:38 +02:00
|
|
|
* [format] the format to use to output a string:
|
2013-07-18 18:07:32 +02:00
|
|
|
* 'application/nquads' for N-Quads.
|
2013-08-06 19:52:15 +02:00
|
|
|
* [produceGeneralizedRdf] true to output generalized RDF, false
|
|
|
|
* to produce only standard RDF (default: false).
|
|
|
|
* [documentLoader(url)] the document loader.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
2013-02-20 22:55:36 +01:00
|
|
|
* @return mixed the resulting RDF dataset (or a serialization of it).
|
2012-04-23 18:57:06 +02:00
|
|
|
*/
|
|
|
|
public function toRDF($input, $options) {
|
2014-04-24 21:27:03 +02:00
|
|
|
global $jsonld_default_load_document;
|
2013-07-15 18:30:28 +02:00
|
|
|
self::setdefaults($options, array(
|
|
|
|
'base' => is_string($input) ? $input : '',
|
2013-08-06 19:52:15 +02:00
|
|
|
'produceGeneralizedRdf' => false,
|
2014-04-24 21:27:03 +02:00
|
|
|
'documentLoader' => $jsonld_default_load_document));
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2012-04-30 23:43:38 +02:00
|
|
|
try {
|
|
|
|
// expand input
|
|
|
|
$expanded = $this->expand($input, $options);
|
2014-04-24 20:39:30 +02:00
|
|
|
} catch(JsonLdException $e) {
|
2012-04-30 23:43:38 +02:00
|
|
|
throw new JsonLdException(
|
2013-08-06 20:02:08 +02:00
|
|
|
'Could not expand input before serialization to RDF.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.RdfError', null, null, $e);
|
2012-04-30 23:43:38 +02:00
|
|
|
}
|
|
|
|
|
2013-02-20 22:55:36 +01:00
|
|
|
// create node map for default graph (and any named graphs)
|
|
|
|
$namer = new UniqueNamer('_:b');
|
|
|
|
$node_map = (object)array('@default' => new stdClass());
|
|
|
|
$this->_createNodeMap($expanded, $node_map, '@default', $namer);
|
|
|
|
|
|
|
|
// output RDF dataset
|
|
|
|
$dataset = new stdClass();
|
2013-06-07 04:00:52 +02:00
|
|
|
$graph_names = array_keys((array)$node_map);
|
|
|
|
sort($graph_names);
|
|
|
|
foreach($graph_names as $graph_name) {
|
|
|
|
$graph = $node_map->{$graph_name};
|
2013-09-13 19:16:10 +02:00
|
|
|
// skip relative IRIs
|
|
|
|
if($graph_name === '@default' || self::_isAbsoluteIri($graph_name)) {
|
|
|
|
$dataset->{$graph_name} = $this->_graphToRDF($graph, $namer, $options);
|
|
|
|
}
|
2013-02-20 22:55:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$rval = $dataset;
|
2012-04-30 23:43:38 +02:00
|
|
|
|
|
|
|
// convert to output format
|
2013-02-20 22:55:36 +01:00
|
|
|
if(isset($options['format']) && $options['format']) {
|
2012-04-30 23:43:38 +02:00
|
|
|
// supported formats
|
|
|
|
if($options['format'] === 'application/nquads') {
|
2013-02-20 22:55:36 +01:00
|
|
|
$rval = self::toNQuads($dataset);
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2012-04-30 23:43:38 +02:00
|
|
|
throw new JsonLdException(
|
2013-09-14 19:58:40 +02:00
|
|
|
'Unknown output format.', 'jsonld.UnknownFormat',
|
|
|
|
null, array('format' => $options['format']));
|
2012-04-30 23:43:38 +02:00
|
|
|
}
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-02-20 22:55:36 +01:00
|
|
|
return $rval;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes a local context, resolving any URLs as necessary, and returns a
|
|
|
|
* new active context in its callback.
|
|
|
|
*
|
|
|
|
* @param stdClass $active_ctx the current active context.
|
|
|
|
* @param mixed $local_ctx the local context to process.
|
|
|
|
* @param assoc $options the options to use:
|
2013-08-06 19:52:15 +02:00
|
|
|
* [documentLoader(url)] the document loader.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return stdClass the new active context.
|
|
|
|
*/
|
2012-04-27 06:16:17 +02:00
|
|
|
public function processContext($active_ctx, $local_ctx, $options) {
|
2014-04-24 21:27:03 +02:00
|
|
|
global $jsonld_default_load_document;
|
2013-07-15 18:30:28 +02:00
|
|
|
self::setdefaults($options, array(
|
2013-08-06 19:54:59 +02:00
|
|
|
'base' => '',
|
2014-04-24 21:27:03 +02:00
|
|
|
'documentLoader' => $jsonld_default_load_document));
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-02-15 22:25:30 +01:00
|
|
|
// return initial context early for null context
|
|
|
|
if($local_ctx === null) {
|
|
|
|
return $this->_getInitialContext($options);
|
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
|
2013-02-15 21:34:21 +01:00
|
|
|
// retrieve URLs in local_ctx
|
2013-04-04 08:54:16 +02:00
|
|
|
$local_ctx = self::copy($local_ctx);
|
|
|
|
if(is_string($local_ctx) or (
|
|
|
|
is_object($local_ctx) && !property_exists($local_ctx, '@context'))) {
|
|
|
|
$local_ctx = (object)array('@context' => $local_ctx);
|
2012-05-07 23:12:10 +02:00
|
|
|
}
|
|
|
|
try {
|
2013-02-15 21:34:21 +01:00
|
|
|
$this->_retrieveContextUrls(
|
2013-07-24 22:05:22 +02:00
|
|
|
$local_ctx, new stdClass(),
|
|
|
|
$options['documentLoader'], $options['base']);
|
2014-04-24 20:39:30 +02:00
|
|
|
} catch(Exception $e) {
|
2013-02-15 22:25:30 +01:00
|
|
|
throw new JsonLdException(
|
|
|
|
'Could not process JSON-LD context.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.ContextError', null, null, $e);
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
|
|
|
// process context
|
2013-04-04 08:54:16 +02:00
|
|
|
return $this->_processContext($active_ctx, $local_ctx, $options);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the given subject has the given property.
|
|
|
|
*
|
|
|
|
* @param stdClass $subject the subject to check.
|
|
|
|
* @param string $property the property to look for.
|
|
|
|
*
|
|
|
|
* @return bool true if the subject has the given property, false if not.
|
|
|
|
*/
|
|
|
|
public static function hasProperty($subject, $property) {
|
|
|
|
$rval = false;
|
|
|
|
if(property_exists($subject, $property)) {
|
|
|
|
$value = $subject->{$property};
|
|
|
|
$rval = (!is_array($value) || count($value) > 0);
|
|
|
|
}
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the given value is a property of the given subject.
|
|
|
|
*
|
|
|
|
* @param stdClass $subject the subject to check.
|
|
|
|
* @param string $property the property to check.
|
|
|
|
* @param mixed $value the value to check.
|
|
|
|
*
|
|
|
|
* @return bool true if the value exists, false if not.
|
|
|
|
*/
|
|
|
|
public static function hasValue($subject, $property, $value) {
|
|
|
|
$rval = false;
|
|
|
|
if(self::hasProperty($subject, $property)) {
|
|
|
|
$val = $subject->{$property};
|
2012-04-27 02:46:41 +02:00
|
|
|
$is_list = self::_isList($val);
|
|
|
|
if(is_array($val) || $is_list) {
|
|
|
|
if($is_list) {
|
2012-04-23 18:57:06 +02:00
|
|
|
$val = $val->{'@list'};
|
|
|
|
}
|
|
|
|
foreach($val as $v) {
|
|
|
|
if(self::compareValues($value, $v)) {
|
|
|
|
$rval = true;
|
2011-12-20 03:29:11 +01:00
|
|
|
break;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(!is_array($value)) {
|
|
|
|
// avoid matching the set of values with an array value parameter
|
2012-04-23 18:57:06 +02:00
|
|
|
$rval = self::compareValues($value, $val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-01 00:27:09 +02:00
|
|
|
* Adds a value to a subject. If the value is an array, all values in the
|
|
|
|
* array will be added.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param stdClass $subject the subject to add the value to.
|
|
|
|
* @param string $property the property that relates the value to the subject.
|
|
|
|
* @param mixed $value the value to add.
|
2012-07-01 00:27:09 +02:00
|
|
|
* @param assoc [$options] the options to use:
|
|
|
|
* [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)
|
|
|
|
* (default: true).
|
2012-04-23 18:57:06 +02:00
|
|
|
*/
|
|
|
|
public static function addValue(
|
2012-07-01 00:27:09 +02:00
|
|
|
$subject, $property, $value, $options=array()) {
|
2013-07-15 18:30:28 +02:00
|
|
|
self::setdefaults($options, array(
|
|
|
|
'allowDuplicate' => true,
|
|
|
|
'propertyIsArray' => false));
|
2012-05-22 21:09:26 +02:00
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
if(is_array($value)) {
|
2012-07-01 00:27:09 +02:00
|
|
|
if(count($value) === 0 && $options['propertyIsArray'] &&
|
2012-04-23 18:57:06 +02:00
|
|
|
!property_exists($subject, $property)) {
|
|
|
|
$subject->{$property} = array();
|
|
|
|
}
|
|
|
|
foreach($value as $v) {
|
2012-07-01 00:27:09 +02:00
|
|
|
self::addValue($subject, $property, $v, $options);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(property_exists($subject, $property)) {
|
2012-07-01 00:27:09 +02:00
|
|
|
// check if subject already has value if duplicates not allowed
|
|
|
|
$has_value = (!$options['allowDuplicate'] &&
|
2012-05-22 21:09:26 +02:00
|
|
|
self::hasValue($subject, $property, $value));
|
2012-04-23 18:57:06 +02:00
|
|
|
|
|
|
|
// make property an array if value not present or always an array
|
2012-04-23 21:52:56 +02:00
|
|
|
if(!is_array($subject->{$property}) &&
|
2012-07-01 00:27:09 +02:00
|
|
|
(!$has_value || $options['propertyIsArray'])) {
|
2012-04-23 18:57:06 +02:00
|
|
|
$subject->{$property} = array($subject->{$property});
|
|
|
|
}
|
|
|
|
|
|
|
|
// add new value
|
2012-04-23 21:52:56 +02:00
|
|
|
if(!$has_value) {
|
2012-04-23 18:57:06 +02:00
|
|
|
$subject->{$property}[] = $value;
|
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2012-04-23 18:57:06 +02:00
|
|
|
// add new value as set or single value
|
2012-07-01 00:27:09 +02:00
|
|
|
$subject->{$property} = ($options['propertyIsArray'] ?
|
|
|
|
array($value) : $value);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all of the values for a subject's property as an array.
|
|
|
|
*
|
|
|
|
* @param stdClass $subject the subject.
|
|
|
|
* @param string $property the property.
|
|
|
|
*
|
|
|
|
* @return array all of the values for a subject's property as an array.
|
|
|
|
*/
|
|
|
|
public static function getValues($subject, $property) {
|
2012-08-15 21:39:55 +02:00
|
|
|
$rval = (property_exists($subject, $property) ?
|
|
|
|
$subject->{$property} : array());
|
2012-04-23 18:57:06 +02:00
|
|
|
return self::arrayify($rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a property from a subject.
|
|
|
|
*
|
|
|
|
* @param stdClass $subject the subject.
|
|
|
|
* @param string $property the property.
|
|
|
|
*/
|
|
|
|
public static function removeProperty($subject, $property) {
|
|
|
|
unset($subject->{$property});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a value from a subject.
|
|
|
|
*
|
|
|
|
* @param stdClass $subject the subject.
|
|
|
|
* @param string $property the property that relates the value to the subject.
|
|
|
|
* @param mixed $value the value to remove.
|
2012-07-01 00:27:09 +02:00
|
|
|
* @param assoc [$options] the options to use:
|
|
|
|
* [propertyIsArray] true if the property is always an array,
|
2012-04-23 18:57:06 +02:00
|
|
|
* false if not (default: false).
|
|
|
|
*/
|
|
|
|
public static function removeValue(
|
2012-07-01 00:27:09 +02:00
|
|
|
$subject, $property, $value, $options=array()) {
|
2013-07-15 18:30:28 +02:00
|
|
|
self::setdefaults($options, array(
|
|
|
|
'propertyIsArray' => false));
|
2012-04-23 18:57:06 +02:00
|
|
|
|
|
|
|
// filter out value
|
|
|
|
$filter = function($e) use ($value) {
|
|
|
|
return !self::compareValues($e, $value);
|
|
|
|
};
|
|
|
|
$values = self::getValues($subject, $property);
|
2013-02-08 04:46:28 +01:00
|
|
|
$values = array_values(array_filter($values, $filter));
|
2012-04-23 18:57:06 +02:00
|
|
|
|
|
|
|
if(count($values) === 0) {
|
|
|
|
self::removeProperty($subject, $property);
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(count($values) === 1 && !$options['propertyIsArray']) {
|
2012-04-23 18:57:06 +02:00
|
|
|
$subject->{$property} = $values[0];
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2012-04-23 18:57:06 +02:00
|
|
|
$subject->{$property} = $values;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares two JSON-LD values for equality. Two JSON-LD values will be
|
|
|
|
* considered equal if:
|
|
|
|
*
|
|
|
|
* 1. They are both primitives of the same type and value.
|
2013-02-07 23:19:19 +01:00
|
|
|
* 2. They are both @values with the same @value, @type, @language,
|
|
|
|
* and @index, OR
|
2013-04-06 06:15:39 +02:00
|
|
|
* 3. They both have @ids that are the same.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @param mixed $v1 the first value.
|
|
|
|
* @param mixed $v2 the second value.
|
|
|
|
*
|
|
|
|
* @return bool true if v1 and v2 are considered equal, false if not.
|
|
|
|
*/
|
|
|
|
public static function compareValues($v1, $v2) {
|
|
|
|
// 1. equal primitives
|
|
|
|
if($v1 === $v2) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. equal @values
|
2013-02-10 20:28:28 +01:00
|
|
|
if(self::_isValue($v1) && self::_isValue($v2)) {
|
2013-02-07 23:19:19 +01:00
|
|
|
return (
|
|
|
|
self::_compareKeyValues($v1, $v2, '@value') &&
|
|
|
|
self::_compareKeyValues($v1, $v2, '@type') &&
|
|
|
|
self::_compareKeyValues($v1, $v2, '@language') &&
|
|
|
|
self::_compareKeyValues($v1, $v2, '@index'));
|
|
|
|
}
|
|
|
|
|
2013-04-06 06:15:39 +02:00
|
|
|
// 3. equal @ids
|
2012-04-23 18:57:06 +02:00
|
|
|
if(is_object($v1) && property_exists($v1, '@id') &&
|
|
|
|
is_object($v2) && property_exists($v2, '@id')) {
|
|
|
|
return $v1->{'@id'} === $v2->{'@id'};
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the value for the given active context key and type, null if none is
|
|
|
|
* set.
|
|
|
|
*
|
|
|
|
* @param stdClass $ctx the active context.
|
|
|
|
* @param string $key the context key.
|
|
|
|
* @param string [$type] the type of value to get (eg: '@id', '@type'), if not
|
|
|
|
* specified gets the entire entry for a key, null if not found.
|
|
|
|
*
|
|
|
|
* @return mixed the value.
|
|
|
|
*/
|
|
|
|
public static function getContextValue($ctx, $key, $type) {
|
|
|
|
$rval = null;
|
2011-07-14 06:47:40 +02:00
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
// return null for invalid key
|
|
|
|
if($key === null) {
|
|
|
|
return $rval;
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
// get default language
|
|
|
|
if($type === '@language' && property_exists($ctx, $type)) {
|
|
|
|
$rval = $ctx->{$type};
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
// get specific entry information
|
|
|
|
if(property_exists($ctx->mappings, $key)) {
|
|
|
|
$entry = $ctx->mappings->{$key};
|
2013-02-07 23:19:19 +01:00
|
|
|
if($entry === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
if($type === null) {
|
2014-04-24 20:39:30 +02:00
|
|
|
// return whole entry
|
2012-04-23 18:57:06 +02:00
|
|
|
$rval = $entry;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(property_exists($entry, $type)) {
|
|
|
|
// return entry value for type
|
2012-04-23 18:57:06 +02:00
|
|
|
$rval = $entry->{$type};
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
return $rval;
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
|
2013-02-15 22:25:30 +01:00
|
|
|
/**
|
2013-02-20 22:55:36 +01:00
|
|
|
* Parses RDF in the form of N-Quads.
|
2013-02-15 22:25:30 +01:00
|
|
|
*
|
|
|
|
* @param string $input the N-Quads input to parse.
|
|
|
|
*
|
2013-02-20 22:55:36 +01:00
|
|
|
* @return stdClass an RDF dataset.
|
2013-02-15 22:25:30 +01:00
|
|
|
*/
|
|
|
|
public static function parseNQuads($input) {
|
|
|
|
// define partial regexes
|
|
|
|
$iri = '(?:<([^:]+:[^>]*)>)';
|
|
|
|
$bnode = '(_:(?:[A-Za-z][A-Za-z0-9]*))';
|
|
|
|
$plain = '"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"';
|
|
|
|
$datatype = "(?:\\^\\^$iri)";
|
|
|
|
$language = '(?:@([a-z]+(?:-[a-z0-9]+)*))';
|
|
|
|
$literal = "(?:$plain(?:$datatype|$language)?)";
|
|
|
|
$ws = '[ \\t]';
|
|
|
|
$eoln = '/(?:\r\n)|(?:\n)|(?:\r)/';
|
|
|
|
$empty = "/^$ws*$/";
|
|
|
|
|
|
|
|
// define quad part regexes
|
|
|
|
$subject = "(?:$iri|$bnode)$ws+";
|
|
|
|
$property = "$iri$ws+";
|
|
|
|
$object = "(?:$iri|$bnode|$literal)$ws*";
|
2013-02-20 22:55:36 +01:00
|
|
|
$graph_name = "(?:\\.|(?:(?:$iri|$bnode)$ws*\\.))";
|
2013-02-15 22:25:30 +01:00
|
|
|
|
|
|
|
// full quad regex
|
2013-02-20 22:55:36 +01:00
|
|
|
$quad = "/^$ws*$subject$property$object$graph_name$ws*$/";
|
2013-02-15 22:25:30 +01:00
|
|
|
|
2013-02-20 22:55:36 +01:00
|
|
|
// build RDF dataset
|
|
|
|
$dataset = new stdClass();
|
2013-02-15 22:25:30 +01:00
|
|
|
|
|
|
|
// split N-Quad input into lines
|
|
|
|
$lines = preg_split($eoln, $input);
|
|
|
|
$line_number = 0;
|
|
|
|
foreach($lines as $line) {
|
|
|
|
$line_number += 1;
|
|
|
|
|
|
|
|
// skip empty lines
|
|
|
|
if(preg_match($empty, $line)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse quad
|
|
|
|
if(!preg_match($quad, $line, $match)) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Error while parsing N-Quads; invalid quad.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.ParseError', null, array('line' => $line_number));
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
|
|
|
|
2013-02-20 22:55:36 +01:00
|
|
|
// create RDF triple
|
|
|
|
$triple = (object)array(
|
2013-02-15 22:25:30 +01:00
|
|
|
'subject' => new stdClass(),
|
2013-02-20 22:55:36 +01:00
|
|
|
'predicate' => new stdClass(),
|
2013-02-15 22:25:30 +01:00
|
|
|
'object' => new stdClass());
|
|
|
|
|
|
|
|
// get subject
|
|
|
|
if($match[1] !== '') {
|
2013-02-20 22:55:36 +01:00
|
|
|
$triple->subject->type = 'IRI';
|
|
|
|
$triple->subject->value = $match[1];
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-20 22:55:36 +01:00
|
|
|
$triple->subject->type = 'blank node';
|
|
|
|
$triple->subject->value = $match[2];
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
|
|
|
|
2013-02-20 22:55:36 +01:00
|
|
|
// get predicate
|
|
|
|
$triple->predicate->type = 'IRI';
|
|
|
|
$triple->predicate->value = $match[3];
|
2013-02-15 22:25:30 +01:00
|
|
|
|
|
|
|
// get object
|
|
|
|
if($match[4] !== '') {
|
2013-02-20 22:55:36 +01:00
|
|
|
$triple->object->type = 'IRI';
|
|
|
|
$triple->object->value = $match[4];
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if($match[5] !== '') {
|
2013-02-20 22:55:36 +01:00
|
|
|
$triple->object->type = 'blank node';
|
|
|
|
$triple->object->value = $match[5];
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-20 22:55:36 +01:00
|
|
|
$triple->object->type = 'literal';
|
2013-02-15 22:25:30 +01:00
|
|
|
$unescaped = str_replace(
|
2013-02-20 22:55:36 +01:00
|
|
|
array('\"', '\t', '\n', '\r', '\\\\'),
|
|
|
|
array('"', "\t", "\n", "\r", '\\'),
|
|
|
|
$match[6]);
|
2013-02-15 22:25:30 +01:00
|
|
|
if(isset($match[7]) && $match[7] !== '') {
|
2013-02-20 22:55:36 +01:00
|
|
|
$triple->object->datatype = $match[7];
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(isset($match[8]) && $match[8] !== '') {
|
2013-02-20 22:55:36 +01:00
|
|
|
$triple->object->datatype = self::RDF_LANGSTRING;
|
|
|
|
$triple->object->language = $match[8];
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-20 22:55:36 +01:00
|
|
|
$triple->object->datatype = self::XSD_STRING;
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
2013-02-20 22:55:36 +01:00
|
|
|
$triple->object->value = $unescaped;
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
|
|
|
|
2013-02-20 22:55:36 +01:00
|
|
|
// get graph name ('@default' is used for the default graph)
|
|
|
|
$name = '@default';
|
2013-02-15 22:25:30 +01:00
|
|
|
if(isset($match[9]) && $match[9] !== '') {
|
2013-02-20 22:55:36 +01:00
|
|
|
$name = $match[9];
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(isset($match[10]) && $match[10] !== '') {
|
2013-02-20 22:55:36 +01:00
|
|
|
$name = $match[10];
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
|
|
|
|
2013-02-20 22:55:36 +01:00
|
|
|
// initialize graph in dataset
|
|
|
|
if(!property_exists($dataset, $name)) {
|
|
|
|
$dataset->{$name} = array($triple);
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
|
|
|
// add triple if unique to its graph
|
2013-02-20 22:55:36 +01:00
|
|
|
$unique = true;
|
|
|
|
$triples = &$dataset->{$name};
|
|
|
|
foreach($triples as $t) {
|
|
|
|
if(self::_compareRDFTriples($t, $triple)) {
|
|
|
|
$unique = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($unique) {
|
|
|
|
$triples[] = $triple;
|
|
|
|
}
|
|
|
|
}
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
|
|
|
|
2013-02-20 22:55:36 +01:00
|
|
|
return $dataset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts an RDF dataset to N-Quads.
|
|
|
|
*
|
|
|
|
* @param stdClass $dataset the RDF dataset to convert.
|
|
|
|
*
|
|
|
|
* @return string the N-Quads string.
|
|
|
|
*/
|
|
|
|
public static function toNQuads($dataset) {
|
|
|
|
$quads = array();
|
|
|
|
foreach($dataset as $graph_name => $triples) {
|
|
|
|
foreach($triples as $triple) {
|
|
|
|
if($graph_name === '@default') {
|
|
|
|
$graph_name = null;
|
|
|
|
}
|
|
|
|
$quads[] = self::toNQuad($triple, $graph_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort($quads);
|
|
|
|
return implode($quads);
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-20 22:55:36 +01:00
|
|
|
* Converts an RDF triple and graph name to an N-Quad string (a single quad).
|
2013-02-15 22:25:30 +01:00
|
|
|
*
|
2013-02-20 22:55:36 +01:00
|
|
|
* @param stdClass $triple the RDF triple to convert.
|
|
|
|
* @param mixed $graph_name the name of the graph containing the triple, null
|
|
|
|
* for the default graph.
|
|
|
|
* @param string $bnode the bnode the quad is mapped to (optional, for
|
|
|
|
* use during normalization only).
|
2013-02-15 22:25:30 +01:00
|
|
|
*
|
2013-02-20 22:55:36 +01:00
|
|
|
* @return string the N-Quad string.
|
2013-02-15 22:25:30 +01:00
|
|
|
*/
|
2013-02-20 22:55:36 +01:00
|
|
|
public static function toNQuad($triple, $graph_name, $bnode=null) {
|
|
|
|
$s = $triple->subject;
|
|
|
|
$p = $triple->predicate;
|
|
|
|
$o = $triple->object;
|
|
|
|
$g = $graph_name;
|
2013-02-15 22:25:30 +01:00
|
|
|
|
|
|
|
$quad = '';
|
|
|
|
|
2013-06-07 04:00:52 +02:00
|
|
|
// subject is an IRI
|
2013-02-20 22:55:36 +01:00
|
|
|
if($s->type === 'IRI') {
|
|
|
|
$quad .= "<{$s->value}>";
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if($bnode !== null) {
|
|
|
|
// bnode normalization mode
|
2013-02-20 22:55:36 +01:00
|
|
|
$quad .= ($s->value === $bnode) ? '_:a' : '_:z';
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
|
|
|
// bnode normal mode
|
2013-02-20 22:55:36 +01:00
|
|
|
$quad .= $s->value;
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
2013-06-07 04:00:52 +02:00
|
|
|
$quad .= ' ';
|
2013-02-15 22:25:30 +01:00
|
|
|
|
2013-06-07 04:00:52 +02:00
|
|
|
// predicate is an IRI
|
|
|
|
if($p->type === 'IRI') {
|
|
|
|
$quad .= "<{$p->value}>";
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if($bnode !== null) {
|
|
|
|
// FIXME: TBD what to do with bnode predicates during normalization
|
|
|
|
// bnode normalization mode
|
2013-06-07 04:00:52 +02:00
|
|
|
$quad .= '_:p';
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
|
|
|
// bnode normal mode
|
2013-06-07 04:00:52 +02:00
|
|
|
$quad .= $p->value;
|
|
|
|
}
|
|
|
|
$quad .= ' ';
|
2013-02-15 22:25:30 +01:00
|
|
|
|
|
|
|
// object is IRI, bnode, or literal
|
2013-02-20 22:55:36 +01:00
|
|
|
if($o->type === 'IRI') {
|
|
|
|
$quad .= "<{$o->value}>";
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if($o->type === 'blank node') {
|
2013-02-15 22:25:30 +01:00
|
|
|
if($bnode !== null) {
|
2014-04-24 20:39:30 +02:00
|
|
|
// normalization mode
|
2013-02-20 22:55:36 +01:00
|
|
|
$quad .= ($o->value === $bnode) ? '_:a' : '_:z';
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
|
|
|
// normal mode
|
2013-02-20 22:55:36 +01:00
|
|
|
$quad .= $o->value;
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-15 22:25:30 +01:00
|
|
|
$escaped = str_replace(
|
2013-02-20 22:55:36 +01:00
|
|
|
array('\\', "\t", "\n", "\r", '"'),
|
|
|
|
array('\\\\', '\t', '\n', '\r', '\"'),
|
|
|
|
$o->value);
|
2013-02-15 22:25:30 +01:00
|
|
|
$quad .= '"' . $escaped . '"';
|
2013-02-20 22:55:36 +01:00
|
|
|
if($o->datatype === self::RDF_LANGSTRING) {
|
2013-07-15 20:10:29 +02:00
|
|
|
if($o->language) {
|
|
|
|
$quad .= "@{$o->language}";
|
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if($o->datatype !== self::XSD_STRING) {
|
2013-02-20 22:55:36 +01:00
|
|
|
$quad .= "^^<{$o->datatype}>";
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// graph
|
2012-06-15 16:24:41 +02:00
|
|
|
if($g !== null) {
|
2013-02-20 22:55:36 +01:00
|
|
|
if(strpos($g, '_:') !== 0) {
|
|
|
|
$quad .= " <$g>";
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if($bnode) {
|
2013-02-15 20:04:29 +01:00
|
|
|
$quad .= ' _:g';
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-20 22:55:36 +01:00
|
|
|
$quad .= " $g";
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$quad .= " .\n";
|
|
|
|
return $quad;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-20 22:55:36 +01:00
|
|
|
* Registers a processor-specific RDF dataset parser by content-type.
|
2013-02-15 22:25:30 +01:00
|
|
|
* Global parsers will no longer be used by this processor.
|
|
|
|
*
|
|
|
|
* @param string $content_type the content-type for the parser.
|
|
|
|
* @param callable $parser(input) the parser function (takes a string as
|
2013-02-20 22:55:36 +01:00
|
|
|
* a parameter and returns an RDF dataset).
|
2013-02-15 22:25:30 +01:00
|
|
|
*/
|
2012-05-09 18:28:26 +02:00
|
|
|
public function registerRDFParser($content_type, $parser) {
|
|
|
|
if($this->rdfParsers === null) {
|
|
|
|
$this->rdfParsers = new stdClass();
|
|
|
|
}
|
2013-02-15 22:25:30 +01:00
|
|
|
$this->rdfParsers->{$content_type} = $parser;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-20 22:55:36 +01:00
|
|
|
* Unregisters a process-specific RDF dataset parser by content-type. If
|
2012-05-09 18:28:26 +02:00
|
|
|
* there are no remaining processor-specific parsers, then the global
|
2013-02-15 22:25:30 +01:00
|
|
|
* parsers will be re-enabled.
|
|
|
|
*
|
|
|
|
* @param string $content_type the content-type for the parser.
|
|
|
|
*/
|
2012-05-09 18:28:26 +02:00
|
|
|
public function unregisterRDFParser($content_type) {
|
|
|
|
if($this->rdfParsers !== null &&
|
|
|
|
property_exists($this->rdfParsers, $content_type)) {
|
|
|
|
unset($this->rdfParsers->{$content_type});
|
|
|
|
if(count(get_object_vars($content_type)) === 0) {
|
|
|
|
$this->rdfParsers = null;
|
|
|
|
}
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
|
|
|
}
|
2012-05-09 18:28:26 +02:00
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
/**
|
|
|
|
* If $value is an array, returns $value, otherwise returns an array
|
|
|
|
* containing $value as the only element.
|
|
|
|
*
|
|
|
|
* @param mixed $value the value.
|
|
|
|
*
|
2012-04-25 02:29:40 +02:00
|
|
|
* @return array an array.
|
2012-04-23 18:57:06 +02:00
|
|
|
*/
|
|
|
|
public static function arrayify($value) {
|
2012-04-25 02:29:40 +02:00
|
|
|
return is_array($value) ? $value : array($value);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clones an object, array, or string/number.
|
|
|
|
*
|
|
|
|
* @param mixed $value the value to clone.
|
|
|
|
*
|
|
|
|
* @return mixed the cloned value.
|
|
|
|
*/
|
|
|
|
public static function copy($value) {
|
|
|
|
if(is_object($value) || is_array($value)) {
|
|
|
|
return unserialize(serialize($value));
|
|
|
|
}
|
2013-09-15 03:43:35 +02:00
|
|
|
return $value;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-07-15 18:30:28 +02:00
|
|
|
/**
|
|
|
|
* Sets the value of a key for the given array if that property
|
|
|
|
* has not already been set.
|
|
|
|
*
|
|
|
|
* @param &assoc $arr the object to update.
|
|
|
|
* @param string $key the key to update.
|
|
|
|
* @param mixed $value the value to set.
|
|
|
|
*/
|
|
|
|
public static function setdefault(&$arr, $key, $value) {
|
|
|
|
isset($arr[$key]) or $arr[$key] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets default values for keys in the given array.
|
|
|
|
*
|
|
|
|
* @param &assoc $arr the object to update.
|
|
|
|
* @param assoc $defaults the default keys and values.
|
|
|
|
*/
|
|
|
|
public static function setdefaults(&$arr, $defaults) {
|
|
|
|
foreach($defaults as $key => $value) {
|
|
|
|
self::setdefault($arr, $key, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
/**
|
|
|
|
* Recursively compacts an element using the given active context. All values
|
|
|
|
* must be in expanded form before this method is called.
|
|
|
|
*
|
2013-02-07 23:19:19 +01:00
|
|
|
* @param stdClass $active_ctx the active context to use.
|
|
|
|
* @param mixed $active_property the compacted property with the element
|
|
|
|
* to compact, null for none.
|
2012-04-23 18:57:06 +02:00
|
|
|
* @param mixed $element the element to compact.
|
2013-02-07 23:19:19 +01:00
|
|
|
* @param assoc $options the compaction options.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return mixed the compacted value.
|
|
|
|
*/
|
2013-02-07 23:19:19 +01:00
|
|
|
protected function _compact(
|
|
|
|
$active_ctx, $active_property, $element, $options) {
|
2012-04-23 18:57:06 +02:00
|
|
|
// recursively compact array
|
|
|
|
if(is_array($element)) {
|
|
|
|
$rval = array();
|
|
|
|
foreach($element as $e) {
|
2013-02-07 23:19:19 +01:00
|
|
|
// compact, dropping any null values
|
|
|
|
$compacted = $this->_compact(
|
|
|
|
$active_ctx, $active_property, $e, $options);
|
|
|
|
if($compacted !== null) {
|
|
|
|
$rval[] = $compacted;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
2013-02-21 22:06:33 +01:00
|
|
|
if($options['compactArrays'] && count($rval) === 1) {
|
2012-04-23 18:57:06 +02:00
|
|
|
// use single element if no container is specified
|
2013-02-07 23:19:19 +01:00
|
|
|
$container = self::getContextValue(
|
|
|
|
$active_ctx, $active_property, '@container');
|
|
|
|
if($container === null) {
|
2012-04-23 18:57:06 +02:00
|
|
|
$rval = $rval[0];
|
|
|
|
}
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
|
|
|
return $rval;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// recursively compact object
|
|
|
|
if(is_object($element)) {
|
2014-12-04 20:45:44 +01:00
|
|
|
if($options['link'] && property_exists($element, '@id') &&
|
|
|
|
isset($options['link'][$element->{'@id'}])) {
|
|
|
|
// check for a linked element to reuse
|
|
|
|
$linked = $options['link'][$element->{'@id'}];
|
|
|
|
foreach($linked as $link) {
|
|
|
|
if($link['expanded'] === $element) {
|
|
|
|
return $link['compacted'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// do value compaction on @values and subject references
|
|
|
|
if(self::_isValue($element) || self::_isSubjectReference($element)) {
|
2014-12-04 20:45:44 +01:00
|
|
|
$rval = $this->_compactValue($active_ctx, $active_property, $element);
|
|
|
|
if($options['link'] && self::_isSubjectReference($element)) {
|
|
|
|
// store linked element
|
|
|
|
if(!isset($options['link'][$element->{'@id'}])) {
|
|
|
|
$options['link'][$element->{'@id'}] = array();
|
|
|
|
}
|
|
|
|
$options['link'][$element->{'@id'}][] = array(
|
|
|
|
'expanded' => $element, 'compacted' => $rval);
|
|
|
|
}
|
|
|
|
return $rval;
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-03-27 15:52:25 +01:00
|
|
|
// FIXME: avoid misuse of active property as an expanded property?
|
2013-03-12 17:20:11 +01:00
|
|
|
$inside_reverse = ($active_property === '@reverse');
|
|
|
|
|
2014-12-04 20:45:44 +01:00
|
|
|
$rval = new stdClass();
|
|
|
|
|
|
|
|
if($options['link'] && property_exists($element, '@id')) {
|
|
|
|
// store linked element
|
|
|
|
if(!isset($options['link'][$element->{'@id'}])) {
|
|
|
|
$options['link'][$element->{'@id'}] = array();
|
|
|
|
}
|
|
|
|
$options['link'][$element->{'@id'}][] = array(
|
|
|
|
'expanded' => $element, 'compacted' => $rval);
|
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// process element keys in order
|
|
|
|
$keys = array_keys((array)$element);
|
|
|
|
sort($keys);
|
|
|
|
foreach($keys as $expanded_property) {
|
|
|
|
$expanded_value = $element->{$expanded_property};
|
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
// compact @id and @type(s)
|
2013-02-07 23:19:19 +01:00
|
|
|
if($expanded_property === '@id' || $expanded_property === '@type') {
|
|
|
|
if(is_string($expanded_value)) {
|
2014-04-24 20:39:30 +02:00
|
|
|
// compact single @id
|
2013-02-07 23:19:19 +01:00
|
|
|
$compacted_value = $this->_compactIri(
|
2013-02-15 06:58:23 +01:00
|
|
|
$active_ctx, $expanded_value, null,
|
|
|
|
array('vocab' => ($expanded_property === '@type')));
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
|
|
|
// expanded value must be a @type array
|
2013-02-07 23:19:19 +01:00
|
|
|
$compacted_value = array();
|
|
|
|
foreach($expanded_value as $ev) {
|
|
|
|
$compacted_value[] = $this->_compactIri(
|
2013-02-11 22:00:44 +01:00
|
|
|
$active_ctx, $ev, null, array('vocab' => true));
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// use keyword alias and add value
|
|
|
|
$alias = $this->_compactIri($active_ctx, $expanded_property);
|
|
|
|
$is_array = (is_array($compacted_value) &&
|
|
|
|
count($expanded_value) === 0);
|
2012-07-01 00:27:09 +02:00
|
|
|
self::addValue(
|
2013-02-07 23:19:19 +01:00
|
|
|
$rval, $alias, $compacted_value,
|
|
|
|
array('propertyIsArray' => $is_array));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-03-27 15:52:25 +01:00
|
|
|
// handle @reverse
|
|
|
|
if($expanded_property === '@reverse') {
|
|
|
|
// recursively compact expanded value
|
|
|
|
$compacted_value = $this->_compact(
|
|
|
|
$active_ctx, '@reverse', $expanded_value, $options);
|
|
|
|
|
|
|
|
// handle double-reversed properties
|
|
|
|
foreach($compacted_value as $compacted_property => $value) {
|
2013-03-12 17:20:11 +01:00
|
|
|
if(property_exists($active_ctx->mappings, $compacted_property) &&
|
2013-03-27 15:52:25 +01:00
|
|
|
$active_ctx->mappings->{$compacted_property} &&
|
2013-03-12 17:20:11 +01:00
|
|
|
$active_ctx->mappings->{$compacted_property}->reverse) {
|
2013-06-18 19:26:27 +02:00
|
|
|
$container = self::getContextValue(
|
|
|
|
$active_ctx, $compacted_property, '@container');
|
|
|
|
$use_array = ($container === '@set' ||
|
|
|
|
!$options['compactArrays']);
|
|
|
|
self::addValue(
|
|
|
|
$rval, $compacted_property, $value,
|
|
|
|
array('propertyIsArray' => $use_array));
|
2013-03-27 15:52:25 +01:00
|
|
|
unset($compacted_value->{$compacted_property});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(count(array_keys((array)$compacted_value)) > 0) {
|
|
|
|
// use keyword alias and add value
|
|
|
|
$alias = $this->_compactIri($active_ctx, $expanded_property);
|
|
|
|
self::addValue($rval, $alias, $compacted_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
2013-03-12 17:20:11 +01:00
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// handle @index property
|
|
|
|
if($expanded_property === '@index') {
|
|
|
|
// drop @index if inside an @index container
|
|
|
|
$container = self::getContextValue(
|
|
|
|
$active_ctx, $active_property, '@container');
|
|
|
|
if($container === '@index') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// use keyword alias and add value
|
|
|
|
$alias = $this->_compactIri($active_ctx, $expanded_property);
|
|
|
|
self::addValue($rval, $alias, $expanded_value);
|
2012-04-23 18:57:06 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-10-02 01:58:06 +02:00
|
|
|
// skip array processing for keywords that aren't @graph or @list
|
|
|
|
if($expanded_property !== '@graph' && $expanded_property !== '@list' &&
|
|
|
|
self::_isKeyword($expanded_property)) {
|
|
|
|
// use keyword alias and add value as is
|
|
|
|
$alias = $this->_compactIri($active_ctx, $expanded_property);
|
|
|
|
self::addValue($rval, $alias, $expanded_value);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// Note: expanded value must be an array due to expansion algorithm.
|
2012-04-23 18:57:06 +02:00
|
|
|
|
|
|
|
// preserve empty arrays
|
2013-02-07 23:19:19 +01:00
|
|
|
if(count($expanded_value) === 0) {
|
2013-02-11 22:07:38 +01:00
|
|
|
$item_active_property = $this->_compactIri(
|
|
|
|
$active_ctx, $expanded_property, $expanded_value,
|
2013-03-12 17:20:11 +01:00
|
|
|
array('vocab' => true), $inside_reverse);
|
2012-07-01 00:27:09 +02:00
|
|
|
self::addValue(
|
2013-02-11 22:07:38 +01:00
|
|
|
$rval, $item_active_property, array(),
|
|
|
|
array('propertyIsArray' => true));
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// recusively process array values
|
2013-02-07 23:19:19 +01:00
|
|
|
foreach($expanded_value as $expanded_item) {
|
|
|
|
// compact property and get container type
|
2013-02-11 22:07:38 +01:00
|
|
|
$item_active_property = $this->_compactIri(
|
2013-02-07 23:19:19 +01:00
|
|
|
$active_ctx, $expanded_property, $expanded_item,
|
2013-03-12 17:20:11 +01:00
|
|
|
array('vocab' => true), $inside_reverse);
|
2013-02-07 23:19:19 +01:00
|
|
|
$container = self::getContextValue(
|
2013-02-11 22:07:38 +01:00
|
|
|
$active_ctx, $item_active_property, '@container');
|
2013-02-07 23:19:19 +01:00
|
|
|
|
|
|
|
// get @list value if appropriate
|
|
|
|
$is_list = self::_isList($expanded_item);
|
|
|
|
$list = null;
|
2012-04-27 02:46:41 +02:00
|
|
|
if($is_list) {
|
2013-02-07 23:19:19 +01:00
|
|
|
$list = $expanded_item->{'@list'};
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// recursively compact expanded item
|
|
|
|
$compacted_item = $this->_compact(
|
2013-02-11 22:07:38 +01:00
|
|
|
$active_ctx, $item_active_property,
|
|
|
|
$is_list ? $list : $expanded_item, $options);
|
2012-04-23 18:57:06 +02:00
|
|
|
|
|
|
|
// handle @list
|
2013-02-07 23:19:19 +01:00
|
|
|
if($is_list) {
|
2013-02-08 05:47:55 +01:00
|
|
|
// ensure @list value is an array
|
2013-02-07 23:19:19 +01:00
|
|
|
$compacted_item = self::arrayify($compacted_item);
|
|
|
|
|
|
|
|
if($container !== '@list') {
|
|
|
|
// wrap using @list alias
|
|
|
|
$compacted_item = (object)array(
|
2013-02-08 05:47:55 +01:00
|
|
|
$this->_compactIri($active_ctx, '@list') => $compacted_item);
|
2013-02-07 23:19:19 +01:00
|
|
|
|
|
|
|
// include @index from expanded @list, if any
|
|
|
|
if(property_exists($expanded_item, '@index')) {
|
|
|
|
$compacted_item->{$this->_compactIri($active_ctx, '@index')} =
|
|
|
|
$expanded_item->{'@index'};
|
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(property_exists($rval, $item_active_property)) {
|
|
|
|
// can't use @list container for more than 1 list
|
2012-04-23 18:57:06 +02:00
|
|
|
throw new JsonLdException(
|
2013-02-07 23:19:19 +01:00
|
|
|
'JSON-LD compact error; property has a "@list" @container ' .
|
|
|
|
'rule but there is more than a single @list that matches ' .
|
|
|
|
'the compacted term in the document. Compaction might mix ' .
|
2013-09-14 19:58:40 +02:00
|
|
|
'unwanted items into the list.', 'jsonld.SyntaxError',
|
|
|
|
'compaction to list of lists');
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// handle language and index maps
|
|
|
|
if($container === '@language' || $container === '@index') {
|
|
|
|
// get or create the map object
|
2013-02-11 22:07:38 +01:00
|
|
|
if(property_exists($rval, $item_active_property)) {
|
|
|
|
$map_object = $rval->{$item_active_property};
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-11 22:07:38 +01:00
|
|
|
$rval->{$item_active_property} = $map_object = new stdClass();
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// if container is a language map, simplify compacted value to
|
|
|
|
// a simple string
|
|
|
|
if($container === '@language' && self::_isValue($compacted_item)) {
|
|
|
|
$compacted_item = $compacted_item->{'@value'};
|
|
|
|
}
|
|
|
|
|
|
|
|
// add compact value to map object using key from expanded value
|
|
|
|
// based on the container type
|
|
|
|
self::addValue(
|
|
|
|
$map_object, $expanded_item->{$container}, $compacted_item);
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-21 22:06:33 +01:00
|
|
|
// use an array if: compactArrays flag is false,
|
2013-04-02 02:24:53 +02:00
|
|
|
// @container is @set or @list, value is an empty
|
2013-02-07 23:19:19 +01:00
|
|
|
// array, or key is @graph
|
2013-02-21 22:06:33 +01:00
|
|
|
$is_array = (!$options['compactArrays'] ||
|
|
|
|
$container === '@set' || $container === '@list' ||
|
2013-02-07 23:19:19 +01:00
|
|
|
(is_array($compacted_item) && count($compacted_item) === 0) ||
|
2013-02-11 17:01:05 +01:00
|
|
|
$expanded_property === '@list' ||
|
2013-02-07 23:19:19 +01:00
|
|
|
$expanded_property === '@graph');
|
|
|
|
|
|
|
|
// add compact value
|
|
|
|
self::addValue(
|
2013-02-11 22:07:38 +01:00
|
|
|
$rval, $item_active_property, $compacted_item,
|
2013-02-07 23:19:19 +01:00
|
|
|
array('propertyIsArray' => $is_array));
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
|
2011-08-22 07:53:12 +02:00
|
|
|
return $rval;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// only primitives remain which are already compact
|
|
|
|
return $element;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursively expands an element using the given context. Any context in
|
2013-02-15 21:34:21 +01:00
|
|
|
* the element will be removed. All context URLs must have been retrieved
|
2012-04-23 18:57:06 +02:00
|
|
|
* before calling this method.
|
|
|
|
*
|
2013-02-07 23:19:19 +01:00
|
|
|
* @param stdClass $active_ctx the active context to use.
|
|
|
|
* @param mixed $active_property the property for the element, null for none.
|
2012-04-23 18:57:06 +02:00
|
|
|
* @param mixed $element the element to expand.
|
2013-02-07 23:19:19 +01:00
|
|
|
* @param assoc $options the expansion options.
|
|
|
|
* @param bool $inside_list true if the property is a list, false if not.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return mixed the expanded value.
|
|
|
|
*/
|
|
|
|
protected function _expand(
|
2013-02-07 23:19:19 +01:00
|
|
|
$active_ctx, $active_property, $element, $options, $inside_list) {
|
|
|
|
// nothing to expand
|
|
|
|
if($element === null) {
|
|
|
|
return $element;
|
|
|
|
}
|
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
// recursively expand array
|
|
|
|
if(is_array($element)) {
|
2011-07-14 06:47:40 +02:00
|
|
|
$rval = array();
|
2013-09-13 22:42:10 +02:00
|
|
|
$container = self::getContextValue(
|
|
|
|
$active_ctx, $active_property, '@container');
|
|
|
|
$inside_list = $inside_list || $container === '@list';
|
2012-04-23 18:57:06 +02:00
|
|
|
foreach($element as $e) {
|
|
|
|
// expand element
|
2013-02-07 23:19:19 +01:00
|
|
|
$e = $this->_expand(
|
|
|
|
$active_ctx, $active_property, $e, $options, $inside_list);
|
|
|
|
if($inside_list && (is_array($e) || self::_isList($e))) {
|
2012-04-23 18:57:06 +02:00
|
|
|
// lists of lists are illegal
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; lists of lists are not permitted.',
|
2013-09-13 22:42:10 +02:00
|
|
|
'jsonld.SyntaxError', 'list of lists');
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
// drop null values
|
2013-09-13 22:42:10 +02:00
|
|
|
if($e !== null) {
|
2013-02-07 23:19:19 +01:00
|
|
|
if(is_array($e)) {
|
|
|
|
$rval = array_merge($rval, $e);
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-07 23:19:19 +01:00
|
|
|
$rval[] = $e;
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $rval;
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
|
2013-09-13 19:17:40 +02:00
|
|
|
if(!is_object($element)) {
|
2013-09-13 22:42:10 +02:00
|
|
|
// drop free-floating scalars that are not in lists
|
2013-09-13 19:17:40 +02:00
|
|
|
if(!$inside_list &&
|
|
|
|
($active_property === null ||
|
|
|
|
$this->_expandIri($active_ctx, $active_property,
|
|
|
|
array('vocab' => true)) === '@graph')) {
|
|
|
|
return null;
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-09-13 19:17:40 +02:00
|
|
|
// expand element according to value expansion rules
|
|
|
|
return $this->_expandValue($active_ctx, $active_property, $element);
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-09-13 19:17:40 +02:00
|
|
|
// recursively expand object:
|
2013-02-07 23:19:19 +01:00
|
|
|
|
2013-09-13 19:17:40 +02:00
|
|
|
// if element has a context, process it
|
|
|
|
if(property_exists($element, '@context')) {
|
|
|
|
$active_ctx = $this->_processContext(
|
|
|
|
$active_ctx, $element->{'@context'}, $options);
|
|
|
|
}
|
2013-02-12 22:11:00 +01:00
|
|
|
|
2013-09-13 19:17:40 +02:00
|
|
|
// expand the active property
|
|
|
|
$expanded_active_property = $this->_expandIri(
|
|
|
|
$active_ctx, $active_property, array('vocab' => true));
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-09-13 19:17:40 +02:00
|
|
|
$rval = new stdClass();
|
|
|
|
$keys = array_keys((array)$element);
|
|
|
|
sort($keys);
|
|
|
|
foreach($keys as $key) {
|
|
|
|
$value = $element->{$key};
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-09-13 19:17:40 +02:00
|
|
|
if($key === '@context') {
|
|
|
|
continue;
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-09-13 19:17:40 +02:00
|
|
|
// expand key to IRI
|
|
|
|
$expanded_property = $this->_expandIri(
|
|
|
|
$active_ctx, $key, array('vocab' => true));
|
2012-04-25 02:29:40 +02:00
|
|
|
|
2013-09-13 19:17:40 +02:00
|
|
|
// drop non-absolute IRI keys that aren't keywords
|
|
|
|
if($expanded_property === null ||
|
|
|
|
!(self::_isAbsoluteIri($expanded_property) ||
|
|
|
|
self::_isKeyword($expanded_property))) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-09-13 22:42:10 +02:00
|
|
|
if(self::_isKeyword($expanded_property)) {
|
|
|
|
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',
|
|
|
|
array('value' => $value));
|
|
|
|
}
|
|
|
|
if(property_exists($rval, $expanded_property)) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; colliding keywords detected.',
|
|
|
|
'jsonld.SyntaxError', 'colliding keywords',
|
|
|
|
array('keyword' => $expanded_property));
|
|
|
|
}
|
2013-09-13 19:17:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// syntax error if @id is not a string
|
|
|
|
if($expanded_property === '@id' && !is_string($value)) {
|
2013-09-15 03:29:23 +02:00
|
|
|
if(!isset($options['isFrame']) || !$options['isFrame']) {
|
2013-09-13 22:42:10 +02:00
|
|
|
throw new JsonLdException(
|
2013-09-13 19:17:40 +02:00
|
|
|
'Invalid JSON-LD syntax; "@id" value must a string.',
|
2013-09-13 22:42:10 +02:00
|
|
|
'jsonld.SyntaxError', 'invalid @id value',
|
|
|
|
array('value' => $value));
|
|
|
|
}
|
|
|
|
if(!is_object($value)) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; "@id" value must a string or an object.',
|
|
|
|
'jsonld.SyntaxError', 'invalid @id value',
|
|
|
|
array('value' => $value));
|
|
|
|
}
|
2013-09-13 19:17:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate @type value
|
|
|
|
if($expanded_property === '@type') {
|
|
|
|
$this->_validateTypeValue($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// @graph must be an array or an object
|
|
|
|
if($expanded_property === '@graph' &&
|
|
|
|
!(is_object($value) || is_array($value))) {
|
|
|
|
throw new JsonLdException(
|
2013-09-13 22:42:10 +02:00
|
|
|
'Invalid JSON-LD syntax; "@graph" value must not be an ' .
|
|
|
|
'object or an array.', 'jsonld.SyntaxError',
|
|
|
|
'invalid @graph value', array('value' => $value));
|
2013-09-13 19:17:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// @value must not be an object or an array
|
|
|
|
if($expanded_property === '@value' &&
|
|
|
|
(is_object($value) || is_array($value))) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; "@value" value must not be an ' .
|
2013-09-13 22:42:10 +02:00
|
|
|
'object or an array.', 'jsonld.SyntaxError',
|
|
|
|
'invalid value object value', array('value' => $value));
|
2013-09-13 19:17:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// @language must be a string
|
2013-09-13 22:42:10 +02:00
|
|
|
if($expanded_property === '@language') {
|
2015-02-08 02:07:49 +01:00
|
|
|
if($value === null) {
|
|
|
|
// drop null @language values, they expand as if they didn't exist
|
|
|
|
continue;
|
|
|
|
}
|
2013-09-13 22:42:10 +02:00
|
|
|
if(!is_string($value)) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; "@language" value must not be a string.',
|
|
|
|
'jsonld.SyntaxError', 'invalid language-tagged string',
|
|
|
|
array('value' => $value));
|
|
|
|
}
|
2013-09-13 19:17:40 +02:00
|
|
|
// ensure language value is lowercase
|
|
|
|
$value = strtolower($value);
|
|
|
|
}
|
2012-04-25 02:29:40 +02:00
|
|
|
|
2013-09-13 19:17:40 +02:00
|
|
|
// @index must be a string
|
|
|
|
if($expanded_property === '@index') {
|
|
|
|
if(!is_string($value)) {
|
2013-02-07 23:19:19 +01:00
|
|
|
throw new JsonLdException(
|
2013-09-13 19:17:40 +02:00
|
|
|
'Invalid JSON-LD syntax; "@index" value must be a string.',
|
2013-09-13 22:42:10 +02:00
|
|
|
'jsonld.SyntaxError', 'invalid @index value',
|
|
|
|
array('value' => $value));
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
2013-09-13 19:17:40 +02:00
|
|
|
}
|
2012-04-25 02:29:40 +02:00
|
|
|
|
2013-09-13 19:17:40 +02:00
|
|
|
// @reverse must be an object
|
|
|
|
if($expanded_property === '@reverse') {
|
|
|
|
if(!is_object($value)) {
|
2012-04-23 18:57:06 +02:00
|
|
|
throw new JsonLdException(
|
2013-09-13 19:17:40 +02:00
|
|
|
'Invalid JSON-LD syntax; "@reverse" value must be an object.',
|
2013-09-13 22:42:10 +02:00
|
|
|
'jsonld.SyntaxError', 'invalid @reverse value',
|
|
|
|
array('value' => $value));
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-09-13 19:17:40 +02:00
|
|
|
$expanded_value = $this->_expand(
|
|
|
|
$active_ctx, '@reverse', $value, $options, $inside_list);
|
2013-03-27 15:52:25 +01:00
|
|
|
|
2013-09-13 19:17:40 +02:00
|
|
|
// properties double-reversed
|
|
|
|
if(property_exists($expanded_value, '@reverse')) {
|
|
|
|
foreach($expanded_value->{'@reverse'} as $rproperty => $rvalue) {
|
|
|
|
self::addValue(
|
|
|
|
$rval, $rproperty, $rvalue, array('propertyIsArray' => true));
|
2013-03-27 15:52:25 +01:00
|
|
|
}
|
2013-09-13 19:17:40 +02:00
|
|
|
}
|
2013-03-27 15:52:25 +01:00
|
|
|
|
2013-09-13 19:17:40 +02:00
|
|
|
// FIXME: can this be merged with code below to simplify?
|
|
|
|
// merge in all reversed properties
|
|
|
|
if(property_exists($rval, '@reverse')) {
|
|
|
|
$reverse_map = $rval->{'@reverse'};
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-09-13 19:17:40 +02:00
|
|
|
$reverse_map = null;
|
|
|
|
}
|
|
|
|
foreach($expanded_value as $property => $items) {
|
|
|
|
if($property === '@reverse') {
|
|
|
|
continue;
|
2013-03-12 17:20:11 +01:00
|
|
|
}
|
2013-09-13 19:17:40 +02:00
|
|
|
if($reverse_map === null) {
|
|
|
|
$reverse_map = $rval->{'@reverse'} = new stdClass();
|
2013-03-27 15:52:25 +01:00
|
|
|
}
|
2013-09-13 19:17:40 +02:00
|
|
|
self::addValue(
|
|
|
|
$reverse_map, $property, array(),
|
|
|
|
array('propertyIsArray' => true));
|
|
|
|
foreach($items as $item) {
|
|
|
|
if(self::_isValue($item) || self::_isList($item)) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; "@reverse" value must not be a ' +
|
2013-09-13 22:42:10 +02:00
|
|
|
'@value or an @list.', 'jsonld.SyntaxError',
|
|
|
|
'invalid reverse property value',
|
2013-09-13 19:17:40 +02:00
|
|
|
array('value' => $expanded_value));
|
2013-03-27 15:52:25 +01:00
|
|
|
}
|
2013-03-12 17:20:11 +01:00
|
|
|
self::addValue(
|
2013-09-13 19:17:40 +02:00
|
|
|
$reverse_map, $property, $item,
|
2013-03-12 17:20:11 +01:00
|
|
|
array('propertyIsArray' => true));
|
2013-03-27 15:52:25 +01:00
|
|
|
}
|
2013-03-12 17:20:11 +01:00
|
|
|
}
|
|
|
|
|
2013-09-13 19:17:40 +02:00
|
|
|
continue;
|
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
|
2013-09-13 19:17:40 +02:00
|
|
|
$container = self::getContextValue($active_ctx, $key, '@container');
|
|
|
|
|
|
|
|
if($container === '@language' && is_object($value)) {
|
2014-04-24 20:39:30 +02:00
|
|
|
// handle language map container (skip if value is not an object)
|
2013-09-13 19:17:40 +02:00
|
|
|
$expanded_value = $this->_expandLanguageMap($value);
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if($container === '@index' && is_object($value)) {
|
|
|
|
// handle index container (skip if value is not an object)
|
2013-09-13 19:17:40 +02:00
|
|
|
$expanded_value = array();
|
|
|
|
$value_keys = array_keys((array)$value);
|
|
|
|
sort($value_keys);
|
|
|
|
foreach($value_keys as $value_key) {
|
|
|
|
$val = $value->{$value_key};
|
|
|
|
$val = self::arrayify($val);
|
|
|
|
$val = $this->_expand($active_ctx, $key, $val, $options, false);
|
|
|
|
foreach($val as $item) {
|
|
|
|
if(!property_exists($item, '@index')) {
|
|
|
|
$item->{'@index'} = $value_key;
|
2012-04-27 22:18:05 +02:00
|
|
|
}
|
2013-09-13 19:17:40 +02:00
|
|
|
$expanded_value[] = $item;
|
2013-08-09 20:19:41 +02:00
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-09-13 22:42:10 +02:00
|
|
|
// recurse into @list or @set
|
2013-09-13 19:17:40 +02:00
|
|
|
$is_list = ($expanded_property === '@list');
|
|
|
|
if($is_list || $expanded_property === '@set') {
|
|
|
|
$next_active_property = $active_property;
|
|
|
|
if($is_list && $expanded_active_property === '@graph') {
|
|
|
|
$next_active_property = null;
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
2013-09-13 19:17:40 +02:00
|
|
|
$expanded_value = $this->_expand(
|
|
|
|
$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.',
|
2013-09-13 22:42:10 +02:00
|
|
|
'jsonld.SyntaxError', 'list of lists');
|
2012-04-27 22:18:05 +02:00
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-09-13 19:17:40 +02:00
|
|
|
// recursively expand value with key as new active property
|
|
|
|
$expanded_value = $this->_expand(
|
|
|
|
$active_ctx, $key, $value, $options, false);
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
2013-09-13 19:17:40 +02:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-09-13 19:17:40 +02:00
|
|
|
// drop null values if property is not @value
|
|
|
|
if($expanded_value === null && $expanded_property !== '@value') {
|
|
|
|
continue;
|
|
|
|
}
|
2012-04-25 02:29:40 +02:00
|
|
|
|
2013-09-13 19:17:40 +02:00
|
|
|
// convert expanded value to @list if container specifies it
|
|
|
|
if($expanded_property !== '@list' && !self::_isList($expanded_value) &&
|
|
|
|
$container === '@list') {
|
|
|
|
// ensure expanded value is an array
|
|
|
|
$expanded_value = (object)array(
|
|
|
|
'@list' => self::arrayify($expanded_value));
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: can this be merged with code above to simplify?
|
|
|
|
// merge in reverse properties
|
|
|
|
if(property_exists($active_ctx->mappings, $key) &&
|
|
|
|
$active_ctx->mappings->{$key} &&
|
|
|
|
$active_ctx->mappings->{$key}->reverse) {
|
2014-07-08 18:59:37 +02:00
|
|
|
if(property_exists($rval, '@reverse')) {
|
|
|
|
$reverse_map = $rval->{'@reverse'};
|
|
|
|
} else {
|
|
|
|
$reverse_map = $rval->{'@reverse'} = new stdClass();
|
|
|
|
}
|
2013-09-13 19:17:40 +02:00
|
|
|
$expanded_value = self::arrayify($expanded_value);
|
|
|
|
foreach($expanded_value as $item) {
|
|
|
|
if(self::_isValue($item) || self::_isList($item)) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; "@reverse" value must not be a ' +
|
2013-09-13 22:42:10 +02:00
|
|
|
'@value or an @list.', 'jsonld.SyntaxError',
|
|
|
|
'invalid reverse property value',
|
|
|
|
array('value' => $expanded_value));
|
2013-03-27 15:52:25 +01:00
|
|
|
}
|
2013-09-13 19:17:40 +02:00
|
|
|
self::addValue(
|
|
|
|
$reverse_map, $expanded_property, $item,
|
|
|
|
array('propertyIsArray' => true));
|
2013-03-12 17:20:11 +01:00
|
|
|
}
|
2013-09-13 19:17:40 +02:00
|
|
|
continue;
|
2012-04-25 02:29:40 +02:00
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
|
2013-09-13 19:17:40 +02:00
|
|
|
// add value for property
|
|
|
|
// use an array except for certain keywords
|
|
|
|
$use_array = (!in_array(
|
|
|
|
$expanded_property, array(
|
|
|
|
'@index', '@id', '@type', '@value', '@language')));
|
|
|
|
self::addValue(
|
|
|
|
$rval, $expanded_property, $expanded_value,
|
|
|
|
array('propertyIsArray' => $use_array));
|
|
|
|
}
|
|
|
|
|
|
|
|
// get property count on expanded output
|
|
|
|
$keys = array_keys((array)$rval);
|
|
|
|
$count = count($keys);
|
2013-02-07 23:19:19 +01:00
|
|
|
|
2013-09-13 19:17:40 +02:00
|
|
|
// @value must only have @language or @type
|
|
|
|
if(property_exists($rval, '@value')) {
|
2013-02-07 23:19:19 +01:00
|
|
|
// @value must only have @language or @type
|
2013-09-13 19:17:40 +02:00
|
|
|
if(property_exists($rval, '@type') &&
|
|
|
|
property_exists($rval, '@language')) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; an element containing "@value" may not ' .
|
|
|
|
'contain both "@type" and "@language".',
|
2013-09-13 22:42:10 +02:00
|
|
|
'jsonld.SyntaxError', 'invalid value object',
|
|
|
|
array('element' => $rval));
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
2013-09-13 19:17:40 +02:00
|
|
|
$valid_count = $count - 1;
|
|
|
|
if(property_exists($rval, '@type')) {
|
|
|
|
$valid_count -= 1;
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
2013-09-13 19:17:40 +02:00
|
|
|
if(property_exists($rval, '@index')) {
|
|
|
|
$valid_count -= 1;
|
|
|
|
}
|
|
|
|
if(property_exists($rval, '@language')) {
|
|
|
|
$valid_count -= 1;
|
|
|
|
}
|
|
|
|
if($valid_count !== 0) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; an element containing "@value" may only ' .
|
|
|
|
'have an "@index" property and at most one other property ' .
|
|
|
|
'which can be "@type" or "@language".',
|
2013-09-13 22:42:10 +02:00
|
|
|
'jsonld.SyntaxError', 'invalid value object',
|
|
|
|
array('element' => $rval));
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
2013-09-13 19:17:40 +02:00
|
|
|
// drop null @values
|
|
|
|
if($rval->{'@value'} === null) {
|
2013-02-15 22:25:30 +01:00
|
|
|
$rval = null;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(property_exists($rval, '@language') &&
|
2013-09-13 19:17:40 +02:00
|
|
|
!is_string($rval->{'@value'})) {
|
2014-04-24 20:39:30 +02:00
|
|
|
// if @language is present, @value must be a string
|
2013-09-13 22:42:10 +02:00
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; only strings may be language-tagged.',
|
|
|
|
'jsonld.SyntaxError', 'invalid language-tagged value',
|
|
|
|
array('element' => $rval));
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(property_exists($rval, '@type') &&
|
2013-09-15 03:33:34 +02:00
|
|
|
(!self::_isAbsoluteIri($rval->{'@type'}) ||
|
|
|
|
strpos($rval->{'@type'}, '_:') === 0)) {
|
2013-09-13 22:42:10 +02:00
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; an element containing "@value" ' .
|
|
|
|
'and "@type" must have an absolute IRI for the value ' .
|
|
|
|
'of "@type".', 'jsonld.SyntaxError', 'invalid typed value',
|
|
|
|
array('element' => $rval));
|
2013-09-13 19:17:40 +02:00
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(property_exists($rval, '@type') && !is_array($rval->{'@type'})) {
|
|
|
|
// convert @type to an array
|
2013-09-13 19:17:40 +02:00
|
|
|
$rval->{'@type'} = array($rval->{'@type'});
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(property_exists($rval, '@set') ||
|
2013-09-13 19:17:40 +02:00
|
|
|
property_exists($rval, '@list')) {
|
2014-04-24 20:39:30 +02:00
|
|
|
// handle @set and @list
|
2013-09-14 20:14:26 +02:00
|
|
|
if($count > 1 && !($count === 2 && property_exists($rval, '@index'))) {
|
2013-09-13 19:17:40 +02:00
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; if an element has the property "@set" ' .
|
|
|
|
'or "@list", then it can have at most one other property that is ' .
|
2013-09-13 22:42:10 +02:00
|
|
|
'"@index".', 'jsonld.SyntaxError', 'invalid set or list object',
|
|
|
|
array('element' => $rval));
|
2013-09-13 19:17:40 +02:00
|
|
|
}
|
|
|
|
// optimize away @set
|
|
|
|
if(property_exists($rval, '@set')) {
|
|
|
|
$rval = $rval->{'@set'};
|
|
|
|
$keys = array_keys((array)$rval);
|
|
|
|
$count = count($keys);
|
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if($count === 1 && property_exists($rval, '@language')) {
|
|
|
|
// drop objects with only @language
|
2013-09-13 19:17:40 +02:00
|
|
|
$rval = null;
|
|
|
|
}
|
2013-02-15 22:25:30 +01:00
|
|
|
|
2013-09-13 19:17:40 +02:00
|
|
|
// drop certain top-level objects that do not occur in lists
|
|
|
|
if(is_object($rval) &&
|
|
|
|
!$options['keepFreeFloatingNodes'] && !$inside_list &&
|
|
|
|
($active_property === null || $expanded_active_property === '@graph')) {
|
2013-09-13 22:42:10 +02:00
|
|
|
// drop empty object or top-level @value/@list, or object with only @id
|
|
|
|
if($count === 0 || property_exists($rval, '@value') ||
|
|
|
|
property_exists($rval, '@list') ||
|
|
|
|
($count === 1 && property_exists($rval, '@id'))) {
|
2013-09-13 19:17:40 +02:00
|
|
|
$rval = null;
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-09-13 19:17:40 +02:00
|
|
|
return $rval;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-07 23:19:19 +01:00
|
|
|
* Performs JSON-LD flattening.
|
|
|
|
*
|
|
|
|
* @param array $input the expanded JSON-LD to flatten.
|
|
|
|
*
|
|
|
|
* @return array the flattened output.
|
|
|
|
*/
|
|
|
|
protected function _flatten($input) {
|
2013-02-15 22:25:30 +01:00
|
|
|
// produce a map of all subjects and name each bnode
|
2013-02-17 19:38:55 +01:00
|
|
|
$namer = new UniqueNamer('_:b');
|
2013-02-15 22:25:30 +01:00
|
|
|
$graphs = (object)array('@default' => new stdClass());
|
|
|
|
$this->_createNodeMap($input, $graphs, '@default', $namer);
|
|
|
|
|
|
|
|
// add all non-default graphs to default graph
|
2013-02-15 18:16:26 +01:00
|
|
|
$default_graph = $graphs->{'@default'};
|
|
|
|
$graph_names = array_keys((array)$graphs);
|
2013-02-15 22:25:30 +01:00
|
|
|
foreach($graph_names as $graph_name) {
|
|
|
|
if($graph_name === '@default') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$node_map = $graphs->{$graph_name};
|
|
|
|
if(!property_exists($default_graph, $graph_name)) {
|
|
|
|
$default_graph->{$graph_name} = (object)array(
|
|
|
|
'@id' => $graph_name, '@graph' => array());
|
|
|
|
}
|
|
|
|
$subject = $default_graph->{$graph_name};
|
|
|
|
if(!property_exists($subject, '@graph')) {
|
|
|
|
$subject->{'@graph'} = array();
|
|
|
|
}
|
|
|
|
$ids = array_keys((array)$node_map);
|
|
|
|
sort($ids);
|
|
|
|
foreach($ids as $id) {
|
2013-08-09 19:32:26 +02:00
|
|
|
$node = $node_map->{$id};
|
|
|
|
// only add full subjects
|
|
|
|
if(!self::_isSubjectReference($node)) {
|
|
|
|
$subject->{'@graph'}[] = $node;
|
|
|
|
}
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// produce flattened output
|
|
|
|
$flattened = array();
|
|
|
|
$keys = array_keys((array)$default_graph);
|
|
|
|
sort($keys);
|
|
|
|
foreach($keys as $key) {
|
2013-08-09 19:32:26 +02:00
|
|
|
$node = $default_graph->{$key};
|
|
|
|
// only add full subjects to top-level
|
2013-08-09 19:42:31 +02:00
|
|
|
if(!self::_isSubjectReference($node)) {
|
2013-08-09 19:32:26 +02:00
|
|
|
$flattened[] = $node;
|
|
|
|
}
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
|
|
|
return $flattened;
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs JSON-LD framing.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @param array $input the expanded JSON-LD to frame.
|
|
|
|
* @param array $frame the expanded JSON-LD frame to use.
|
2013-02-07 23:19:19 +01:00
|
|
|
* @param assoc $options the framing options.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return array the framed output.
|
|
|
|
*/
|
|
|
|
protected function _frame($input, $frame, $options) {
|
|
|
|
// create framing state
|
|
|
|
$state = (object)array(
|
|
|
|
'options' => $options,
|
2012-05-22 21:09:26 +02:00
|
|
|
'graphs' => (object)array(
|
|
|
|
'@default' => new stdClass(),
|
2014-12-04 20:45:44 +01:00
|
|
|
'@merged' => new stdClass()),
|
|
|
|
'subjectStack' => array(),
|
|
|
|
'link' => new stdClass());
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2012-05-22 21:09:26 +02:00
|
|
|
// produce a map of all graphs and name each bnode
|
|
|
|
// FIXME: currently uses subjects from @merged graph only
|
2013-02-17 19:38:55 +01:00
|
|
|
$namer = new UniqueNamer('_:b');
|
2013-02-08 06:15:45 +01:00
|
|
|
$this->_createNodeMap($input, $state->graphs, '@merged', $namer);
|
|
|
|
$state->subjects = $state->graphs->{'@merged'};
|
2012-04-23 18:57:06 +02:00
|
|
|
|
|
|
|
// frame the subjects
|
|
|
|
$framed = new ArrayObject();
|
2013-02-08 06:15:45 +01:00
|
|
|
$keys = array_keys((array)$state->subjects);
|
|
|
|
sort($keys);
|
|
|
|
$this->_matchFrame($state, $keys, $frame, $framed, null);
|
2012-04-23 18:57:06 +02:00
|
|
|
return (array)$framed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-20 22:55:36 +01:00
|
|
|
* Performs normalization on the given RDF dataset.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
2013-02-20 22:55:36 +01:00
|
|
|
* @param stdClass $dataset the RDF dataset to normalize.
|
2012-05-05 22:03:30 +02:00
|
|
|
* @param assoc $options the normalization options.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
2013-02-07 23:19:19 +01:00
|
|
|
* @return mixed the normalized output.
|
2012-04-23 18:57:06 +02:00
|
|
|
*/
|
2013-02-20 22:55:36 +01:00
|
|
|
protected function _normalize($dataset, $options) {
|
|
|
|
// create quads and map bnodes to their associated quads
|
|
|
|
$quads = array();
|
2012-04-23 18:57:06 +02:00
|
|
|
$bnodes = new stdClass();
|
2013-02-20 22:55:36 +01:00
|
|
|
foreach($dataset as $graph_name => $triples) {
|
|
|
|
if($graph_name === '@default') {
|
|
|
|
$graph_name = null;
|
|
|
|
}
|
|
|
|
foreach($triples as $triple) {
|
|
|
|
$quad = $triple;
|
|
|
|
if($graph_name !== null) {
|
|
|
|
if(strpos($graph_name, '_:') === 0) {
|
|
|
|
$quad->name = (object)array(
|
|
|
|
'type' => 'blank node', 'value' => $graph_name);
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-20 22:55:36 +01:00
|
|
|
$quad->name = (object)array(
|
|
|
|
'type' => 'IRI', 'value' => $graph_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$quads[] = $quad;
|
|
|
|
|
|
|
|
foreach(array('subject', 'object', 'name') as $attr) {
|
|
|
|
if(property_exists($quad, $attr) &&
|
|
|
|
$quad->{$attr}->type === 'blank node') {
|
|
|
|
$id = $quad->{$attr}->value;
|
|
|
|
if(property_exists($bnodes, $id)) {
|
|
|
|
$bnodes->{$id}->quads[] = $quad;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-20 22:55:36 +01:00
|
|
|
$bnodes->{$id} = (object)array('quads' => array($quad));
|
|
|
|
}
|
2012-05-05 22:03:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-15 22:25:30 +01:00
|
|
|
|
2013-02-20 22:55:36 +01:00
|
|
|
// mapping complete, start canonical naming
|
2012-04-23 18:57:06 +02:00
|
|
|
$namer = new UniqueNamer('_:c14n');
|
|
|
|
|
2013-02-20 22:55:36 +01:00
|
|
|
// continue to hash bnode quads while bnodes are assigned names
|
2012-04-23 18:57:06 +02:00
|
|
|
$unnamed = null;
|
2012-04-23 20:01:56 +02:00
|
|
|
$nextUnnamed = array_keys((array)$bnodes);
|
2012-04-23 18:57:06 +02:00
|
|
|
$duplicates = null;
|
|
|
|
do {
|
2012-04-23 20:01:56 +02:00
|
|
|
$unnamed = $nextUnnamed;
|
2012-04-23 18:57:06 +02:00
|
|
|
$nextUnnamed = array();
|
|
|
|
$duplicates = new stdClass();
|
|
|
|
$unique = new stdClass();
|
|
|
|
foreach($unnamed as $bnode) {
|
2013-02-20 22:55:36 +01:00
|
|
|
// hash quads for each unnamed bnode
|
|
|
|
$hash = $this->_hashQuads($bnode, $bnodes, $namer);
|
2012-04-23 18:57:06 +02:00
|
|
|
|
|
|
|
// store hash as unique or a duplicate
|
2012-04-23 20:01:56 +02:00
|
|
|
if(property_exists($duplicates, $hash)) {
|
2012-04-23 18:57:06 +02:00
|
|
|
$duplicates->{$hash}[] = $bnode;
|
|
|
|
$nextUnnamed[] = $bnode;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(property_exists($unique, $hash)) {
|
2012-04-23 18:57:06 +02:00
|
|
|
$duplicates->{$hash} = array($unique->{$hash}, $bnode);
|
|
|
|
$nextUnnamed[] = $unique->{$hash};
|
|
|
|
$nextUnnamed[] = $bnode;
|
|
|
|
unset($unique->{$hash});
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2012-04-23 18:57:06 +02:00
|
|
|
$unique->{$hash} = $bnode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// name unique bnodes in sorted hash order
|
2012-04-23 20:01:56 +02:00
|
|
|
$hashes = array_keys((array)$unique);
|
2012-04-23 18:57:06 +02:00
|
|
|
sort($hashes);
|
|
|
|
foreach($hashes as $hash) {
|
2012-04-23 21:00:13 +02:00
|
|
|
$namer->getName($unique->{$hash});
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while(count($unnamed) > count($nextUnnamed));
|
|
|
|
|
|
|
|
// enumerate duplicate hash groups in sorted order
|
2012-04-23 20:01:56 +02:00
|
|
|
$hashes = array_keys((array)$duplicates);
|
2012-04-23 18:57:06 +02:00
|
|
|
sort($hashes);
|
|
|
|
foreach($hashes as $hash) {
|
|
|
|
// process group
|
|
|
|
$group = $duplicates->{$hash};
|
|
|
|
$results = array();
|
|
|
|
foreach($group as $bnode) {
|
|
|
|
// skip already-named bnodes
|
|
|
|
if($namer->isNamed($bnode)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// hash bnode paths
|
2013-02-17 19:38:55 +01:00
|
|
|
$path_namer = new UniqueNamer('_:b');
|
2012-04-23 18:57:06 +02:00
|
|
|
$path_namer->getName($bnode);
|
2012-05-05 22:03:30 +02:00
|
|
|
$results[] = $this->_hashPaths($bnode, $bnodes, $namer, $path_namer);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// name bnodes in hash order
|
|
|
|
usort($results, function($a, $b) {
|
|
|
|
$a = $a->hash;
|
|
|
|
$b = $b->hash;
|
|
|
|
return ($a < $b) ? -1 : (($a > $b) ? 1 : 0);
|
|
|
|
});
|
|
|
|
foreach($results as $result) {
|
|
|
|
// name all bnodes in path namer in key-entry order
|
|
|
|
foreach($result->pathNamer->order as $bnode) {
|
|
|
|
$namer->getName($bnode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-05 22:03:30 +02:00
|
|
|
// create normalized array
|
|
|
|
$normalized = array();
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-02-20 22:55:36 +01:00
|
|
|
/* Note: At this point all bnodes in the set of RDF quads have been
|
|
|
|
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. */
|
|
|
|
|
|
|
|
// update bnode names in each quad and serialize
|
|
|
|
foreach($quads as $quad) {
|
|
|
|
foreach(array('subject', 'object', 'name') as $attr) {
|
|
|
|
if(property_exists($quad, $attr) &&
|
|
|
|
$quad->{$attr}->type === 'blank node' &&
|
|
|
|
strpos($quad->{$attr}->value, '_:c14n') !== 0) {
|
|
|
|
$quad->{$attr}->value = $namer->getName($quad->{$attr}->value);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
2013-02-20 22:55:36 +01:00
|
|
|
$normalized[] = $this->toNQuad($quad, property_exists($quad, 'name') ?
|
|
|
|
$quad->name->value : null);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2012-05-05 22:03:30 +02:00
|
|
|
// sort normalized output
|
|
|
|
sort($normalized);
|
|
|
|
|
|
|
|
// handle output format
|
2013-02-20 22:55:36 +01:00
|
|
|
if(isset($options['format']) && $options['format']) {
|
2012-05-05 22:03:30 +02:00
|
|
|
if($options['format'] === 'application/nquads') {
|
|
|
|
return implode($normalized);
|
|
|
|
}
|
2013-02-20 22:55:36 +01:00
|
|
|
throw new JsonLdException(
|
|
|
|
'Unknown output format.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.UnknownFormat', null, array('format' => $options['format']));
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-02-20 22:55:36 +01:00
|
|
|
// return RDF dataset
|
2012-05-09 22:21:46 +02:00
|
|
|
return $this->parseNQuads(implode($normalized));
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2012-04-30 23:10:27 +02:00
|
|
|
/**
|
2013-02-20 22:55:36 +01:00
|
|
|
* Converts an RDF dataset to JSON-LD.
|
2012-04-30 23:10:27 +02:00
|
|
|
*
|
2013-02-20 22:55:36 +01:00
|
|
|
* @param stdClass $dataset the RDF dataset.
|
2013-08-06 20:02:08 +02:00
|
|
|
* @param assoc $options the RDF serialization options.
|
2012-05-01 03:22:05 +02:00
|
|
|
*
|
|
|
|
* @return array the JSON-LD output.
|
2012-04-30 23:10:27 +02:00
|
|
|
*/
|
2013-02-20 22:55:36 +01:00
|
|
|
protected function _fromRDF($dataset, $options) {
|
2013-05-17 22:59:32 +02:00
|
|
|
$default_graph = new stdClass();
|
|
|
|
$graph_map = (object)array('@default' => $default_graph);
|
2014-09-15 23:04:50 +02:00
|
|
|
$referenced_once = (object)array();
|
2012-04-30 23:10:27 +02:00
|
|
|
|
2013-05-17 22:59:32 +02:00
|
|
|
foreach($dataset as $name => $graph) {
|
|
|
|
if(!property_exists($graph_map, $name)) {
|
|
|
|
$graph_map->{$name} = new stdClass();
|
|
|
|
}
|
|
|
|
if($name !== '@default' && !property_exists($default_graph, $name)) {
|
|
|
|
$default_graph->{$name} = (object)array('@id' => $name);
|
|
|
|
}
|
|
|
|
$node_map = $graph_map->{$name};
|
|
|
|
foreach($graph as $triple) {
|
2013-02-20 22:55:36 +01:00
|
|
|
// get subject, predicate, object
|
|
|
|
$s = $triple->subject->value;
|
|
|
|
$p = $triple->predicate->value;
|
|
|
|
$o = $triple->object;
|
2012-04-30 23:10:27 +02:00
|
|
|
|
2013-05-17 22:59:32 +02:00
|
|
|
if(!property_exists($node_map, $s)) {
|
|
|
|
$node_map->{$s} = (object)array('@id' => $s);
|
2012-04-30 23:10:27 +02:00
|
|
|
}
|
2013-05-17 22:59:32 +02:00
|
|
|
$node = $node_map->{$s};
|
2012-04-30 23:10:27 +02:00
|
|
|
|
2013-05-17 22:59:32 +02:00
|
|
|
$object_is_id = ($o->type === 'IRI' || $o->type === 'blank node');
|
2013-08-09 19:32:26 +02:00
|
|
|
if($object_is_id && !property_exists($node_map, $o->value)) {
|
2013-05-17 22:59:32 +02:00
|
|
|
$node_map->{$o->value} = (object)array('@id' => $o->value);
|
2012-04-30 23:10:27 +02:00
|
|
|
}
|
|
|
|
|
2013-10-02 21:58:28 +02:00
|
|
|
if($p === self::RDF_TYPE && !$options['useRdfType'] && $object_is_id) {
|
2013-05-17 22:59:32 +02:00
|
|
|
self::addValue(
|
|
|
|
$node, '@type', $o->value, array('propertyIsArray' => true));
|
2013-02-20 22:55:36 +01:00
|
|
|
continue;
|
|
|
|
}
|
2012-04-30 23:10:27 +02:00
|
|
|
|
2013-08-09 19:32:26 +02:00
|
|
|
$value = self::_RDFToObject($o, $options['useNativeTypes']);
|
2013-05-17 22:59:32 +02:00
|
|
|
self::addValue($node, $p, $value, array('propertyIsArray' => true));
|
2012-04-30 23:10:27 +02:00
|
|
|
|
2013-08-09 19:32:26 +02:00
|
|
|
// object may be an RDF list/partial list node but we can't know
|
|
|
|
// easily until all triples are read
|
|
|
|
if($object_is_id) {
|
2014-09-15 23:04:50 +02:00
|
|
|
if($o->value === self::RDF_NIL) {
|
|
|
|
$object = $node_map->{$o->value};
|
|
|
|
if(!property_exists($object, 'usages')) {
|
|
|
|
$object->usages = array();
|
|
|
|
}
|
|
|
|
$object->usages[] = (object)array(
|
|
|
|
'node' => $node,
|
|
|
|
'property' => $p,
|
|
|
|
'value' => $value);
|
|
|
|
} else if(property_exists($referenced_once, $o->value)) {
|
|
|
|
// object referenced more than once
|
|
|
|
$referenced_once->{$o->value} = false;
|
|
|
|
} else {
|
|
|
|
// track single reference
|
|
|
|
$referenced_once->{$o->value} = (object)array(
|
|
|
|
'node' => $node,
|
|
|
|
'property' => $p,
|
|
|
|
'value' => $value);
|
2012-04-30 23:10:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-17 22:59:32 +02:00
|
|
|
// convert linked lists to @list arrays
|
|
|
|
foreach($graph_map as $name => $graph_object) {
|
2013-08-09 19:32:26 +02:00
|
|
|
// no @lists to be converted, continue
|
|
|
|
if(!property_exists($graph_object, self::RDF_NIL)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate backwards through each RDF list
|
|
|
|
$nil = $graph_object->{self::RDF_NIL};
|
|
|
|
foreach($nil->usages as $usage) {
|
|
|
|
$node = $usage->node;
|
|
|
|
$property = $usage->property;
|
|
|
|
$head = $usage->value;
|
2013-08-09 19:55:54 +02:00
|
|
|
$list = array();
|
|
|
|
$list_nodes = array();
|
2013-08-09 19:32:26 +02:00
|
|
|
|
|
|
|
// ensure node is a well-formed list node; it must:
|
2014-09-15 23:04:50 +02:00
|
|
|
// 1. Be referenced only once.
|
2013-08-09 19:32:26 +02:00
|
|
|
// 2. Have an array for rdf:first that has 1 item.
|
|
|
|
// 3. Have an array for rdf:rest that has 1 item.
|
2014-09-15 23:04:50 +02:00
|
|
|
// 4. Have no keys other than: @id, rdf:first, rdf:rest, and,
|
2013-08-09 19:32:26 +02:00
|
|
|
// optionally, @type where the value is rdf:List.
|
|
|
|
$node_key_count = count(array_keys((array)$node));
|
2014-07-29 19:14:46 +02:00
|
|
|
while($property === self::RDF_REST &&
|
2014-09-15 23:04:50 +02:00
|
|
|
property_exists($referenced_once, $node->{'@id'}) &&
|
|
|
|
is_object($referenced_once->{$node->{'@id'}}) &&
|
2013-08-09 19:32:26 +02:00
|
|
|
property_exists($node, self::RDF_FIRST) &&
|
|
|
|
property_exists($node, self::RDF_REST) &&
|
|
|
|
is_array($node->{self::RDF_FIRST}) &&
|
|
|
|
is_array($node->{self::RDF_REST}) &&
|
|
|
|
count($node->{self::RDF_FIRST}) === 1 &&
|
|
|
|
count($node->{self::RDF_REST}) === 1 &&
|
2014-09-15 23:04:50 +02:00
|
|
|
($node_key_count === 3 || ($node_key_count === 4 &&
|
2013-08-09 19:32:26 +02:00
|
|
|
property_exists($node, '@type') && is_array($node->{'@type'}) &&
|
|
|
|
count($node->{'@type'}) === 1 &&
|
|
|
|
$node->{'@type'}[0] === self::RDF_LIST))) {
|
|
|
|
$list[] = $node->{self::RDF_FIRST}[0];
|
|
|
|
$list_nodes[] = $node->{'@id'};
|
|
|
|
|
|
|
|
// get next node, moving backwards through list
|
2014-09-15 23:04:50 +02:00
|
|
|
$usage = $referenced_once->{$node->{'@id'}};
|
2013-08-09 19:32:26 +02:00
|
|
|
$node = $usage->node;
|
|
|
|
$property = $usage->property;
|
|
|
|
$head = $usage->value;
|
|
|
|
$node_key_count = count(array_keys((array)$node));
|
2013-05-17 22:59:32 +02:00
|
|
|
|
2013-08-09 19:32:26 +02:00
|
|
|
// if node is not a blank node, then list head found
|
|
|
|
if(strpos($node->{'@id'}, '_:') !== 0) {
|
2013-05-17 22:59:32 +02:00
|
|
|
break;
|
2012-04-30 23:10:27 +02:00
|
|
|
}
|
2013-05-17 22:59:32 +02:00
|
|
|
}
|
|
|
|
|
2013-08-09 19:32:26 +02:00
|
|
|
// list is nested in another list
|
|
|
|
if($property === self::RDF_FIRST) {
|
|
|
|
// empty list
|
|
|
|
if($node->{'@id'} === self::RDF_NIL) {
|
|
|
|
// can't convert rdf:nil to a @list object because it would
|
|
|
|
// result in a list of lists which isn't supported
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// preserve list head
|
|
|
|
$head = $graph_object->{$head->{'@id'}}->{self::RDF_REST}[0];
|
|
|
|
array_pop($list);
|
|
|
|
array_pop($list_nodes);
|
2013-05-17 22:59:32 +02:00
|
|
|
}
|
|
|
|
|
2013-08-09 19:32:26 +02:00
|
|
|
// transform list into @list object
|
|
|
|
unset($head->{'@id'});
|
|
|
|
$head->{'@list'} = array_reverse($list);
|
|
|
|
foreach($list_nodes as $list_node) {
|
|
|
|
unset($graph_object->{$list_node});
|
2012-04-30 23:10:27 +02:00
|
|
|
}
|
|
|
|
}
|
2014-09-15 23:04:50 +02:00
|
|
|
|
|
|
|
unset($nil->usages);
|
2012-04-30 23:10:27 +02:00
|
|
|
}
|
|
|
|
|
2013-05-17 22:59:32 +02:00
|
|
|
$result = array();
|
|
|
|
$subjects = array_keys((array)$default_graph);
|
|
|
|
sort($subjects);
|
|
|
|
foreach($subjects as $subject) {
|
|
|
|
$node = $default_graph->{$subject};
|
|
|
|
if(property_exists($graph_map, $subject)) {
|
|
|
|
$node->{'@graph'} = array();
|
|
|
|
$graph_object = $graph_map->{$subject};
|
|
|
|
$subjects_ = array_keys((array)$graph_object);
|
|
|
|
sort($subjects_);
|
|
|
|
foreach($subjects_ as $subject_) {
|
|
|
|
$node_ = $graph_object->{$subject_};
|
2013-08-06 19:46:56 +02:00
|
|
|
// only add full subjects to top-level
|
|
|
|
if(!self::_isSubjectReference($node_)) {
|
|
|
|
$node->{'@graph'}[] = $node_;
|
|
|
|
}
|
2012-04-30 23:10:27 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-06 19:46:56 +02:00
|
|
|
// only add full subjects to top-level
|
|
|
|
if(!self::_isSubjectReference($node)) {
|
|
|
|
$result[] = $node;
|
|
|
|
}
|
2012-04-30 23:10:27 +02:00
|
|
|
}
|
2013-05-17 22:59:32 +02:00
|
|
|
|
|
|
|
return $result;
|
2012-04-30 23:10:27 +02:00
|
|
|
}
|
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
/**
|
|
|
|
* Processes a local context and returns a new active context.
|
|
|
|
*
|
|
|
|
* @param stdClass $active_ctx the current active context.
|
|
|
|
* @param mixed $local_ctx the local context to process.
|
2013-02-07 23:19:19 +01:00
|
|
|
* @param assoc $options the context processing options.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return stdClass the new active context.
|
|
|
|
*/
|
|
|
|
protected function _processContext($active_ctx, $local_ctx, $options) {
|
2013-02-07 23:19:19 +01:00
|
|
|
global $jsonld_cache;
|
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
// normalize local context to an array
|
2013-02-15 22:25:30 +01:00
|
|
|
if(is_object($local_ctx) && property_exists($local_ctx, '@context') &&
|
|
|
|
is_array($local_ctx->{'@context'})) {
|
|
|
|
$local_ctx = $local_ctx->{'@context'};
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
$ctxs = self::arrayify($local_ctx);
|
|
|
|
|
2013-06-07 18:06:39 +02:00
|
|
|
// no contexts in array, clone existing context
|
|
|
|
if(count($ctxs) === 0) {
|
|
|
|
return self::_cloneActiveContext($active_ctx);
|
|
|
|
}
|
|
|
|
|
2014-07-09 21:16:24 +02:00
|
|
|
// process each context in order, update active context
|
|
|
|
// on each iteration to ensure proper caching
|
2013-06-07 18:06:39 +02:00
|
|
|
$rval = $active_ctx;
|
2012-04-23 18:57:06 +02:00
|
|
|
foreach($ctxs as $ctx) {
|
|
|
|
// reset to initial context
|
|
|
|
if($ctx === null) {
|
2014-07-09 21:16:24 +02:00
|
|
|
$rval = $active_ctx = $this->_getInitialContext($options);
|
2012-04-23 18:57:06 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// dereference @context key if present
|
|
|
|
if(is_object($ctx) && property_exists($ctx, '@context')) {
|
|
|
|
$ctx = $ctx->{'@context'};
|
|
|
|
}
|
|
|
|
|
2013-02-15 21:34:21 +01:00
|
|
|
// context must be an object by now, all URLs retrieved before this call
|
2012-04-23 18:57:06 +02:00
|
|
|
if(!is_object($ctx)) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; @context must be an object.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.SyntaxError', 'invalid local context',
|
|
|
|
array('context' => $ctx));
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-06-07 18:06:39 +02:00
|
|
|
// get context from cache if available
|
|
|
|
if(property_exists($jsonld_cache, 'activeCtx')) {
|
|
|
|
$cached = $jsonld_cache->activeCtx->get($active_ctx, $ctx);
|
|
|
|
if($cached) {
|
2014-07-09 21:16:24 +02:00
|
|
|
$rval = $active_ctx = $cached;
|
2013-06-07 18:06:39 +02:00
|
|
|
$must_clone = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-09 21:16:24 +02:00
|
|
|
// update active context and clone new one before updating
|
|
|
|
$active_ctx = $rval;
|
|
|
|
$rval = self::_cloneActiveContext($rval);
|
2013-06-07 18:06:39 +02:00
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
// define context mappings for keys in local context
|
|
|
|
$defined = new stdClass();
|
2013-02-07 23:19:19 +01:00
|
|
|
|
2013-03-27 15:52:25 +01:00
|
|
|
// handle @base
|
|
|
|
if(property_exists($ctx, '@base')) {
|
|
|
|
$base = $ctx->{'@base'};
|
2013-02-28 22:03:11 +01:00
|
|
|
if($base === null) {
|
2013-06-07 04:00:52 +02:00
|
|
|
$base = null;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(!is_string($base)) {
|
2013-03-27 15:52:25 +01:00
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; the value of "@base" in a ' .
|
|
|
|
'@context must be a string or null.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.SyntaxError', 'invalid base IRI', array('context' => $ctx));
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if($base !== '' && !self::_isAbsoluteIri($base)) {
|
2013-03-27 15:52:25 +01:00
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; the value of "@base" in a ' .
|
|
|
|
'@context must be an absolute IRI or the empty string.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.SyntaxError', 'invalid base IRI', array('context' => $ctx));
|
2013-03-27 15:52:25 +01:00
|
|
|
}
|
2014-01-07 16:53:24 +01:00
|
|
|
if($base !== null) {
|
|
|
|
$base = jsonld_parse_url($base);
|
|
|
|
}
|
|
|
|
$rval->{'@base'} = $base;
|
2013-03-27 15:52:25 +01:00
|
|
|
$defined->{'@base'} = true;
|
2013-02-28 22:03:11 +01:00
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// handle @vocab
|
|
|
|
if(property_exists($ctx, '@vocab')) {
|
|
|
|
$value = $ctx->{'@vocab'};
|
|
|
|
if($value === null) {
|
|
|
|
unset($rval->{'@vocab'});
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(!is_string($value)) {
|
2013-02-07 23:19:19 +01:00
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; the value of "@vocab" in a ' .
|
|
|
|
'@context must be a string or null.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.SyntaxError', 'invalid vocab mapping',
|
|
|
|
array('context' => $ctx));
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(!self::_isAbsoluteIri($value)) {
|
2013-02-07 23:19:19 +01:00
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; the value of "@vocab" in a ' .
|
|
|
|
'@context must be an absolute IRI.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.SyntaxError', 'invalid vocab mapping',
|
|
|
|
array('context' => $ctx));
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-07 23:19:19 +01:00
|
|
|
$rval->{'@vocab'} = $value;
|
|
|
|
}
|
|
|
|
$defined->{'@vocab'} = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle @language
|
|
|
|
if(property_exists($ctx, '@language')) {
|
|
|
|
$value = $ctx->{'@language'};
|
|
|
|
if($value === null) {
|
|
|
|
unset($rval->{'@language'});
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(!is_string($value)) {
|
2013-02-07 23:19:19 +01:00
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; the value of "@language" in a ' .
|
|
|
|
'@context must be a string or null.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.SyntaxError', 'invalid default language',
|
|
|
|
array('context' => $ctx));
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-07 23:19:19 +01:00
|
|
|
$rval->{'@language'} = strtolower($value);
|
|
|
|
}
|
|
|
|
$defined->{'@language'} = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// process all other keys
|
2012-04-23 18:57:06 +02:00
|
|
|
foreach($ctx as $k => $v) {
|
2013-02-07 23:19:19 +01:00
|
|
|
$this->_createTermDefinition($rval, $ctx, $k, $defined);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-06-07 18:06:39 +02:00
|
|
|
// cache result
|
|
|
|
if(property_exists($jsonld_cache, 'activeCtx')) {
|
|
|
|
$jsonld_cache->activeCtx->set($active_ctx, $ctx, $rval);
|
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expands a language map.
|
|
|
|
*
|
|
|
|
* @param stdClass $language_map the language map to expand.
|
|
|
|
*
|
|
|
|
* @return array the expanded language map.
|
|
|
|
*/
|
|
|
|
protected function _expandLanguageMap($language_map) {
|
|
|
|
$rval = array();
|
|
|
|
$keys = array_keys((array)$language_map);
|
|
|
|
sort($keys);
|
|
|
|
foreach($keys as $key) {
|
|
|
|
$values = $language_map->{$key};
|
|
|
|
$values = self::arrayify($values);
|
|
|
|
foreach($values as $item) {
|
2015-09-14 15:39:23 +02:00
|
|
|
if($item === null) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
if(!is_string($item)) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; language map values must be strings.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.SyntaxError', 'invalid language map value',
|
|
|
|
array('languageMap', $language_map));
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
|
|
|
$rval[] = (object)array(
|
|
|
|
'@value' => $item,
|
|
|
|
'@language' => strtolower($key));
|
|
|
|
}
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
/**
|
|
|
|
* Labels the blank nodes in the given value using the given UniqueNamer.
|
|
|
|
*
|
|
|
|
* @param UniqueNamer $namer the UniqueNamer to use.
|
|
|
|
* @param mixed $element the element with blank nodes to rename.
|
|
|
|
*
|
|
|
|
* @return mixed the element.
|
|
|
|
*/
|
2013-02-15 18:16:26 +01:00
|
|
|
public function _labelBlankNodes($namer, $element) {
|
2013-02-07 23:19:19 +01:00
|
|
|
if(is_array($element)) {
|
2013-02-08 04:46:28 +01:00
|
|
|
$length = count($element);
|
|
|
|
for($i = 0; $i < $length; ++$i) {
|
2013-02-11 21:41:46 +01:00
|
|
|
$element[$i] = $this->_labelBlankNodes($namer, $element[$i]);
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(self::_isList($element)) {
|
2013-02-07 23:19:19 +01:00
|
|
|
$element->{'@list'} = $this->_labelBlankNodes(
|
2013-02-11 21:41:46 +01:00
|
|
|
$namer, $element->{'@list'});
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(is_object($element)) {
|
2013-02-07 23:19:19 +01:00
|
|
|
// rename blank node
|
|
|
|
if(self::_isBlankNode($element)) {
|
|
|
|
$name = null;
|
|
|
|
if(property_exists($element, '@id')) {
|
|
|
|
$name = $element->{'@id'};
|
|
|
|
}
|
|
|
|
$element->{'@id'} = $namer->getName($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// recursively apply to all keys
|
|
|
|
$keys = array_keys((array)$element);
|
|
|
|
sort($keys);
|
|
|
|
foreach($keys as $key) {
|
|
|
|
if($key !== '@id') {
|
2013-02-11 21:44:20 +01:00
|
|
|
$element->{$key} = $this->_labelBlankNodes($namer, $element->{$key});
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $element;
|
|
|
|
}
|
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
/**
|
|
|
|
* Expands the given value by using the coercion and keyword rules in the
|
|
|
|
* given context.
|
|
|
|
*
|
2013-02-07 23:19:19 +01:00
|
|
|
* @param stdClass $active_ctx the active context to use.
|
|
|
|
* @param string $active_property the property the value is associated with.
|
2012-04-23 18:57:06 +02:00
|
|
|
* @param mixed $value the value to expand.
|
|
|
|
*
|
|
|
|
* @return mixed the expanded value.
|
|
|
|
*/
|
2013-02-07 23:19:19 +01:00
|
|
|
protected function _expandValue($active_ctx, $active_property, $value) {
|
2012-05-09 00:07:28 +02:00
|
|
|
// nothing to expand
|
|
|
|
if($value === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
// special-case expand @id and @type (skips '@id' expansion)
|
2013-02-07 23:19:19 +01:00
|
|
|
$expanded_property = $this->_expandIri(
|
|
|
|
$active_ctx, $active_property, array('vocab' => true));
|
|
|
|
if($expanded_property === '@id') {
|
|
|
|
return $this->_expandIri($active_ctx, $value, array('base' => true));
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if($expanded_property === '@type') {
|
2013-02-07 23:19:19 +01:00
|
|
|
return $this->_expandIri(
|
|
|
|
$active_ctx, $value, array('vocab' => true, 'base' => true));
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// get type definition from context
|
|
|
|
$type = self::getContextValue($active_ctx, $active_property, '@type');
|
2012-05-09 00:07:28 +02:00
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// do @id expansion (automatic for @graph)
|
2013-02-14 18:40:24 +01:00
|
|
|
if($type === '@id' || ($expanded_property === '@graph' &&
|
|
|
|
is_string($value))) {
|
2013-02-07 23:19:19 +01:00
|
|
|
return (object)array('@id' => $this->_expandIri(
|
|
|
|
$active_ctx, $value, array('base' => true)));
|
|
|
|
}
|
2013-02-13 23:00:05 +01:00
|
|
|
// do @id expansion w/vocab
|
|
|
|
if($type === '@vocab') {
|
|
|
|
return (object)array('@id' => $this->_expandIri(
|
|
|
|
$active_ctx, $value, array('vocab' => true, 'base' => true)));
|
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
|
|
|
|
// do not expand keyword values
|
|
|
|
if(self::_isKeyword($expanded_property)) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
$rval = new stdClass();
|
|
|
|
|
|
|
|
// other type
|
|
|
|
if($type !== null) {
|
|
|
|
$rval->{'@type'} = $type;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(is_string($value)) {
|
|
|
|
// check for language tagging for strings
|
2013-02-07 23:19:19 +01:00
|
|
|
$language = self::getContextValue(
|
|
|
|
$active_ctx, $active_property, '@language');
|
|
|
|
if($language !== null) {
|
|
|
|
$rval->{'@language'} = $language;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$rval->{'@value'} = $value;
|
2012-04-23 18:57:06 +02:00
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
2012-04-30 23:10:27 +02:00
|
|
|
/**
|
2013-02-20 22:55:36 +01:00
|
|
|
* Creates an array of RDF triples for the given graph.
|
|
|
|
*
|
|
|
|
* @param stdClass $graph the graph to create RDF triples for.
|
|
|
|
* @param UniqueNamer $namer for assigning bnode names.
|
2013-08-06 20:07:21 +02:00
|
|
|
* @param assoc $options the RDF serialization options.
|
2013-02-20 22:55:36 +01:00
|
|
|
*
|
|
|
|
* @return array the array of RDF triples for the given graph.
|
|
|
|
*/
|
2013-08-06 20:07:21 +02:00
|
|
|
protected function _graphToRDF($graph, $namer, $options) {
|
2013-02-20 22:55:36 +01:00
|
|
|
$rval = array();
|
|
|
|
|
2013-06-07 04:12:49 +02:00
|
|
|
$ids = array_keys((array)$graph);
|
|
|
|
sort($ids);
|
|
|
|
foreach($ids as $id) {
|
|
|
|
$node = $graph->{$id};
|
2014-01-07 16:53:24 +01:00
|
|
|
if($id === '"') {
|
|
|
|
$id = '';
|
|
|
|
}
|
2013-06-07 04:12:49 +02:00
|
|
|
$properties = array_keys((array)$node);
|
|
|
|
sort($properties);
|
|
|
|
foreach($properties as $property) {
|
|
|
|
$items = $node->{$property};
|
2013-02-20 22:55:36 +01:00
|
|
|
if($property === '@type') {
|
|
|
|
$property = self::RDF_TYPE;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(self::_isKeyword($property)) {
|
2013-02-20 22:55:36 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($items as $item) {
|
2013-09-13 19:16:10 +02:00
|
|
|
// skip relative IRI subjects and predicates
|
|
|
|
if(!(self::_isAbsoluteIri($id) && self::_isAbsoluteIri($property))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-02-20 22:55:36 +01:00
|
|
|
// RDF subject
|
|
|
|
$subject = new stdClass();
|
2013-06-07 18:06:39 +02:00
|
|
|
$subject->type = (strpos($id, '_:') === 0) ? 'blank node' : 'IRI';
|
|
|
|
$subject->value = $id;
|
2013-02-20 22:55:36 +01:00
|
|
|
|
|
|
|
// RDF predicate
|
|
|
|
$predicate = new stdClass();
|
2013-06-07 18:06:39 +02:00
|
|
|
$predicate->type = (strpos($property, '_:') === 0 ?
|
|
|
|
'blank node' : 'IRI');
|
|
|
|
$predicate->value = $property;
|
2013-02-20 22:55:36 +01:00
|
|
|
|
2013-08-06 20:02:08 +02:00
|
|
|
// skip bnode predicates unless producing generalized RDF
|
|
|
|
if($predicate->type === 'blank node' &&
|
|
|
|
!$options['produceGeneralizedRdf']) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-02-20 22:55:36 +01:00
|
|
|
if(self::_isList($item)) {
|
2014-04-24 20:39:30 +02:00
|
|
|
// convert @list to triples
|
2013-02-20 22:55:36 +01:00
|
|
|
$this->_listToRDF(
|
|
|
|
$item->{'@list'}, $namer, $subject, $predicate, $rval);
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
|
|
|
// convert value or node object to triple
|
2013-06-07 18:06:39 +02:00
|
|
|
$object = $this->_objectToRDF($item);
|
2013-09-13 19:16:10 +02:00
|
|
|
// skip null objects (they are relative IRIs)
|
|
|
|
if($object) {
|
|
|
|
$rval[] = (object)array(
|
|
|
|
'subject' => $subject,
|
|
|
|
'predicate' => $predicate,
|
|
|
|
'object' => $object);
|
|
|
|
}
|
2013-02-20 22:55:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a @list value into linked list of blank node RDF triples
|
|
|
|
* (an RDF collection).
|
|
|
|
*
|
|
|
|
* @param array $list the @list value.
|
|
|
|
* @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.
|
|
|
|
* @param &array $triples the array of triples to append to.
|
|
|
|
*/
|
|
|
|
protected function _listToRDF(
|
|
|
|
$list, $namer, $subject, $predicate, &$triples) {
|
|
|
|
$first = (object)array('type' => 'IRI', 'value' => self::RDF_FIRST);
|
|
|
|
$rest = (object)array('type' => 'IRI', 'value' => self::RDF_REST);
|
|
|
|
$nil = (object)array('type' => 'IRI', 'value' => self::RDF_NIL);
|
|
|
|
|
|
|
|
foreach($list as $item) {
|
|
|
|
$blank_node = (object)array(
|
|
|
|
'type' => 'blank node', 'value' => $namer->getName());
|
|
|
|
$triples[] = (object)array(
|
|
|
|
'subject' => $subject,
|
|
|
|
'predicate' => $predicate,
|
|
|
|
'object' => $blank_node);
|
|
|
|
|
|
|
|
$subject = $blank_node;
|
|
|
|
$predicate = $first;
|
2013-06-07 18:06:39 +02:00
|
|
|
$object = $this->_objectToRDF($item);
|
2013-09-13 19:16:10 +02:00
|
|
|
// skip null objects (they are relative IRIs)
|
|
|
|
if($object) {
|
|
|
|
$triples[] = (object)array(
|
|
|
|
'subject' => $subject,
|
|
|
|
'predicate' => $predicate,
|
|
|
|
'object' => $object);
|
|
|
|
}
|
2013-02-20 22:55:36 +01:00
|
|
|
|
|
|
|
$predicate = $rest;
|
|
|
|
}
|
|
|
|
|
|
|
|
$triples[] = (object)array(
|
|
|
|
'subject' => $subject, 'predicate' => $predicate, 'object' => $nil);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a JSON-LD value object to an RDF literal or a JSON-LD string or
|
|
|
|
* node object to an RDF resource.
|
|
|
|
*
|
|
|
|
* @param mixed $item the JSON-LD value or node object.
|
2012-04-30 23:10:27 +02:00
|
|
|
*
|
2013-02-20 22:55:36 +01:00
|
|
|
* @return stdClass the RDF literal or RDF resource.
|
|
|
|
*/
|
2013-06-07 18:06:39 +02:00
|
|
|
protected function _objectToRDF($item) {
|
2013-02-20 22:55:36 +01:00
|
|
|
$object = new stdClass();
|
|
|
|
|
|
|
|
if(self::_isValue($item)) {
|
|
|
|
$object->type = 'literal';
|
|
|
|
$value = $item->{'@value'};
|
|
|
|
$datatype = property_exists($item, '@type') ? $item->{'@type'} : null;
|
|
|
|
|
|
|
|
// convert to XSD datatypes as appropriate
|
|
|
|
if(is_bool($value)) {
|
|
|
|
$object->value = ($value ? 'true' : 'false');
|
|
|
|
$object->datatype = $datatype ? $datatype : self::XSD_BOOLEAN;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(is_double($value) || $datatype == self::XSD_DOUBLE) {
|
2013-02-20 22:55:36 +01:00
|
|
|
// canonical double representation
|
|
|
|
$object->value = preg_replace(
|
|
|
|
'/(\d)0*E\+?/', '$1E', sprintf('%1.15E', $value));
|
|
|
|
$object->datatype = $datatype ? $datatype : self::XSD_DOUBLE;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(is_integer($value)) {
|
2013-02-20 22:55:36 +01:00
|
|
|
$object->value = strval($value);
|
|
|
|
$object->datatype = $datatype ? $datatype : self::XSD_INTEGER;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(property_exists($item, '@language')) {
|
2013-02-20 22:55:36 +01:00
|
|
|
$object->value = $value;
|
|
|
|
$object->datatype = $datatype ? $datatype : self::RDF_LANGSTRING;
|
|
|
|
$object->language = $item->{'@language'};
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-20 22:55:36 +01:00
|
|
|
$object->value = $value;
|
|
|
|
$object->datatype = $datatype ? $datatype : self::XSD_STRING;
|
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
|
|
|
// convert string/node object to RDF
|
2013-02-20 22:55:36 +01:00
|
|
|
$id = is_object($item) ? $item->{'@id'} : $item;
|
2013-06-07 18:06:39 +02:00
|
|
|
$object->type = (strpos($id, '_:') === 0) ? 'blank node' : 'IRI';
|
|
|
|
$object->value = $id;
|
2013-02-20 22:55:36 +01:00
|
|
|
}
|
|
|
|
|
2013-09-13 19:16:10 +02:00
|
|
|
// skip relative IRIs
|
|
|
|
if($object->type === 'IRI' && !self::_isAbsoluteIri($object->value)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-02-20 22:55:36 +01:00
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts an RDF triple object to a JSON-LD object.
|
|
|
|
*
|
|
|
|
* @param stdClass $o the RDF triple object to convert.
|
2012-09-26 22:36:51 +02:00
|
|
|
* @param bool $use_native_types true to output native types, false not to.
|
2012-04-30 23:10:27 +02:00
|
|
|
*
|
|
|
|
* @return stdClass the JSON-LD object.
|
|
|
|
*/
|
2013-02-20 22:55:36 +01:00
|
|
|
protected function _RDFToObject($o, $use_native_types) {
|
|
|
|
// convert IRI/blank node object to JSON-LD
|
|
|
|
if($o->type === 'IRI' || $o->type === 'blank node') {
|
|
|
|
return (object)array('@id' => $o->value);
|
2012-04-30 23:10:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// convert literal object to JSON-LD
|
2013-02-20 22:55:36 +01:00
|
|
|
$rval = (object)array('@value' => $o->value);
|
2012-04-30 23:10:27 +02:00
|
|
|
|
2013-02-20 22:55:36 +01:00
|
|
|
if(property_exists($o, 'language')) {
|
2014-04-24 20:39:30 +02:00
|
|
|
// add language
|
2013-02-20 22:55:36 +01:00
|
|
|
$rval->{'@language'} = $o->language;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
|
|
|
// add datatype
|
2013-02-20 22:55:36 +01:00
|
|
|
$type = $o->datatype;
|
2012-09-26 22:36:51 +02:00
|
|
|
// use native types for certain xsd types
|
|
|
|
if($use_native_types) {
|
|
|
|
if($type === self::XSD_BOOLEAN) {
|
|
|
|
if($rval->{'@value'} === 'true') {
|
|
|
|
$rval->{'@value'} = true;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if($rval->{'@value'} === 'false') {
|
2013-02-15 22:25:30 +01:00
|
|
|
$rval->{'@value'} = false;
|
2012-09-26 22:36:51 +02:00
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(is_numeric($rval->{'@value'})) {
|
2012-09-26 22:36:51 +02:00
|
|
|
if($type === self::XSD_INTEGER) {
|
|
|
|
$i = intval($rval->{'@value'});
|
|
|
|
if(strval($i) === $rval->{'@value'}) {
|
|
|
|
$rval->{'@value'} = $i;
|
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if($type === self::XSD_DOUBLE) {
|
2012-09-26 22:36:51 +02:00
|
|
|
$rval->{'@value'} = doubleval($rval->{'@value'});
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
2012-09-26 22:36:51 +02:00
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
// do not add native type
|
|
|
|
if(!in_array($type, array(
|
|
|
|
self::XSD_BOOLEAN, self::XSD_INTEGER, self::XSD_DOUBLE,
|
|
|
|
self::XSD_STRING))) {
|
2012-09-26 22:36:51 +02:00
|
|
|
$rval->{'@type'} = $type;
|
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if($type !== self::XSD_STRING) {
|
2012-09-26 22:36:51 +02:00
|
|
|
$rval->{'@type'} = $type;
|
|
|
|
}
|
2012-04-30 23:10:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
/**
|
2013-02-07 23:19:19 +01:00
|
|
|
* Recursively flattens the subjects in the given JSON-LD expanded input
|
|
|
|
* into a node map.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @param mixed $input the JSON-LD expanded input.
|
2012-05-22 21:09:26 +02:00
|
|
|
* @param stdClass $graphs a map of graph name to subject map.
|
2013-02-20 22:55:36 +01:00
|
|
|
* @param string $graph the name of the current graph.
|
2012-04-23 18:57:06 +02:00
|
|
|
* @param UniqueNamer $namer the blank node namer.
|
|
|
|
* @param mixed $name the name assigned to the current input if it is a bnode.
|
|
|
|
* @param mixed $list the list to append to, null for none.
|
|
|
|
*/
|
2013-02-07 23:19:19 +01:00
|
|
|
protected function _createNodeMap(
|
|
|
|
$input, $graphs, $graph, $namer, $name=null, $list=null) {
|
2012-04-23 18:57:06 +02:00
|
|
|
// recurse through array
|
|
|
|
if(is_array($input)) {
|
|
|
|
foreach($input as $e) {
|
2013-02-07 23:19:19 +01:00
|
|
|
$this->_createNodeMap($e, $graphs, $graph, $namer, null, $list);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2012-05-22 21:09:26 +02:00
|
|
|
return;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// add non-object to list
|
2013-02-15 22:25:30 +01:00
|
|
|
if(!is_object($input)) {
|
|
|
|
if($list !== null) {
|
|
|
|
$list[] = $input;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2012-05-22 21:09:26 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-03-28 17:12:13 +01:00
|
|
|
// add values to list
|
2013-02-07 23:19:19 +01:00
|
|
|
if(self::_isValue($input)) {
|
2013-03-28 17:12:13 +01:00
|
|
|
if(property_exists($input, '@type')) {
|
|
|
|
$type = $input->{'@type'};
|
|
|
|
// rename @type blank node
|
2013-08-06 19:28:25 +02:00
|
|
|
if(strpos($type, '_:') === 0) {
|
|
|
|
$type = $input->{'@type'} = $namer->getName($type);
|
|
|
|
}
|
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
if($list !== null) {
|
|
|
|
$list[] = $input;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-22 21:09:26 +02:00
|
|
|
// Note: At this point, input must be a subject.
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-06-07 18:06:39 +02:00
|
|
|
// spec requires @type to be named first, so assign names early
|
|
|
|
if(property_exists($input, '@type')) {
|
|
|
|
foreach($input->{'@type'} as $type) {
|
|
|
|
if(strpos($type, '_:') === 0) {
|
|
|
|
$namer->getName($type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-22 21:09:26 +02:00
|
|
|
// get name for subject
|
|
|
|
if($name === null) {
|
|
|
|
if(property_exists($input, '@id')) {
|
|
|
|
$name = $input->{'@id'};
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2012-05-22 21:09:26 +02:00
|
|
|
if(self::_isBlankNode($input)) {
|
|
|
|
$name = $namer->getName($name);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2012-05-22 21:09:26 +02:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2012-05-22 21:09:26 +02:00
|
|
|
// add subject reference to list
|
|
|
|
if($list !== null) {
|
|
|
|
$list[] = (object)array('@id' => $name);
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2012-05-22 21:09:26 +02:00
|
|
|
// create new subject or merge into existing one
|
|
|
|
if(!property_exists($graphs, $graph)) {
|
|
|
|
$graphs->{$graph} = new stdClass();
|
|
|
|
}
|
|
|
|
$subjects = $graphs->{$graph};
|
|
|
|
if(!property_exists($subjects, $name)) {
|
2014-01-07 16:53:24 +01:00
|
|
|
if($name === '') {
|
|
|
|
$subjects->{'"'} = new stdClass();
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2014-01-07 16:53:24 +01:00
|
|
|
$subjects->{$name} = new stdClass();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($name === '') {
|
|
|
|
$subject = $subjects->{'"'};
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2014-01-07 16:53:24 +01:00
|
|
|
$subject = $subjects->{$name};
|
2012-05-22 21:09:26 +02:00
|
|
|
}
|
|
|
|
$subject->{'@id'} = $name;
|
2013-02-07 23:19:19 +01:00
|
|
|
$properties = array_keys((array)$input);
|
|
|
|
sort($properties);
|
|
|
|
foreach($properties as $property) {
|
2012-05-22 21:09:26 +02:00
|
|
|
// skip @id
|
2013-02-07 23:19:19 +01:00
|
|
|
if($property === '@id') {
|
2012-05-22 21:09:26 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-03-27 15:52:25 +01:00
|
|
|
// handle reverse properties
|
|
|
|
if($property === '@reverse') {
|
|
|
|
$referenced_node = (object)array('@id' => $name);
|
|
|
|
$reverse_map = $input->{'@reverse'};
|
2013-03-12 17:20:11 +01:00
|
|
|
foreach($reverse_map as $reverse_property => $items) {
|
|
|
|
foreach($items as $item) {
|
2013-10-22 04:45:41 +02:00
|
|
|
$item_name = null;
|
|
|
|
if(property_exists($item, '@id')) {
|
|
|
|
$item_name = $item->{'@id'};
|
|
|
|
}
|
|
|
|
if(self::_isBlankNode($item)) {
|
|
|
|
$item_name = $namer->getName($item_name);
|
|
|
|
}
|
|
|
|
$this->_createNodeMap($item, $graphs, $graph, $namer, $item_name);
|
2014-01-07 16:53:24 +01:00
|
|
|
if($item_name === '') {
|
|
|
|
$item_name = '"';
|
|
|
|
}
|
2013-03-27 15:52:25 +01:00
|
|
|
self::addValue(
|
2013-10-22 04:45:41 +02:00
|
|
|
$subjects->{$item_name}, $reverse_property, $referenced_node,
|
2013-03-27 15:52:25 +01:00
|
|
|
array('propertyIsArray' => true, 'allowDuplicate' => false));
|
2013-03-12 17:20:11 +01:00
|
|
|
}
|
2013-03-27 15:52:25 +01:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2013-03-12 17:20:11 +01:00
|
|
|
|
2012-05-22 21:09:26 +02:00
|
|
|
// recurse into graph
|
2013-02-07 23:19:19 +01:00
|
|
|
if($property === '@graph') {
|
2012-05-22 21:09:26 +02:00
|
|
|
// add graph subjects map entry
|
|
|
|
if(!property_exists($graphs, $name)) {
|
2014-04-24 21:42:17 +02:00
|
|
|
// FIXME: temporary hack to avoid empty property bug
|
|
|
|
if(!$name) {
|
|
|
|
$name = '"';
|
|
|
|
}
|
2012-05-22 21:09:26 +02:00
|
|
|
$graphs->{$name} = new stdClass();
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2012-05-22 21:09:26 +02:00
|
|
|
$g = ($graph === '@merged') ? $graph : $name;
|
2013-02-07 23:19:19 +01:00
|
|
|
$this->_createNodeMap(
|
|
|
|
$input->{$property}, $graphs, $g, $namer, null, null);
|
2012-05-22 21:09:26 +02:00
|
|
|
continue;
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2012-05-22 21:09:26 +02:00
|
|
|
// copy non-@type keywords
|
2013-02-07 23:19:19 +01:00
|
|
|
if($property !== '@type' && self::_isKeyword($property)) {
|
2015-03-16 18:56:04 +01:00
|
|
|
if($property === '@index' && property_exists($subject, '@index') &&
|
|
|
|
($input->{'@index'} !== $subject->{'@index'} ||
|
|
|
|
$input->{'@index'}->{'@id'} !== $subject->{'@index'}->{'@id'})) {
|
2013-03-27 15:52:25 +01:00
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; conflicting @index property detected.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.SyntaxError', 'conflicting indexes',
|
|
|
|
array('subject' => $subject));
|
2013-03-27 15:52:25 +01:00
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
$subject->{$property} = $input->{$property};
|
2012-05-22 21:09:26 +02:00
|
|
|
continue;
|
|
|
|
}
|
2011-07-18 22:22:57 +02:00
|
|
|
|
2013-04-04 08:10:24 +02:00
|
|
|
// iterate over objects
|
2013-02-07 23:19:19 +01:00
|
|
|
$objects = $input->{$property};
|
2013-04-04 08:10:24 +02:00
|
|
|
|
|
|
|
// if property is a bnode, assign it a new id
|
|
|
|
if(strpos($property, '_:') === 0) {
|
|
|
|
$property = $namer->getName($property);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure property is added for empty arrays
|
2013-02-07 23:19:19 +01:00
|
|
|
if(count($objects) === 0) {
|
|
|
|
self::addValue(
|
|
|
|
$subject, $property, array(), array('propertyIsArray' => true));
|
|
|
|
continue;
|
|
|
|
}
|
2012-05-22 21:09:26 +02:00
|
|
|
foreach($objects as $o) {
|
2013-03-28 17:12:13 +01:00
|
|
|
if($property === '@type') {
|
|
|
|
// rename @type blank nodes
|
2013-08-06 19:28:25 +02:00
|
|
|
$o = (strpos($o, '_:') === 0) ? $namer->getName($o) : $o;
|
2013-03-28 17:12:13 +01:00
|
|
|
}
|
|
|
|
|
2012-05-22 21:09:26 +02:00
|
|
|
// handle embedded subject or subject reference
|
|
|
|
if(self::_isSubject($o) || self::_isSubjectReference($o)) {
|
|
|
|
// rename blank node @id
|
|
|
|
$id = property_exists($o, '@id') ? $o->{'@id'} : null;
|
|
|
|
if(self::_isBlankNode($o)) {
|
|
|
|
$id = $namer->getName($id);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2011-07-18 22:22:57 +02:00
|
|
|
|
2012-05-22 21:09:26 +02:00
|
|
|
// add reference and recurse
|
2012-07-01 00:27:09 +02:00
|
|
|
self::addValue(
|
2013-02-07 23:19:19 +01:00
|
|
|
$subject, $property, (object)array('@id' => $id),
|
2013-02-15 18:16:26 +01:00
|
|
|
array('propertyIsArray' => true, 'allowDuplicate' => false));
|
2013-02-07 23:19:19 +01:00
|
|
|
$this->_createNodeMap($o, $graphs, $graph, $namer, $id, null);
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(self::_isList($o)) {
|
|
|
|
// handle @list
|
2013-02-07 23:19:19 +01:00
|
|
|
$_list = new ArrayObject();
|
|
|
|
$this->_createNodeMap(
|
|
|
|
$o->{'@list'}, $graphs, $graph, $namer, $name, $_list);
|
|
|
|
$o = (object)array('@list' => (array)$_list);
|
|
|
|
self::addValue(
|
2013-02-15 18:16:26 +01:00
|
|
|
$subject, $property, $o,
|
|
|
|
array('propertyIsArray' => true, 'allowDuplicate' => false));
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
|
|
|
// handle @value
|
2013-02-07 23:19:19 +01:00
|
|
|
$this->_createNodeMap($o, $graphs, $graph, $namer, $name, null);
|
|
|
|
self::addValue(
|
2013-02-15 18:16:26 +01:00
|
|
|
$subject, $property, $o,
|
|
|
|
array('propertyIsArray' => true, 'allowDuplicate' => false));
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Frames subjects according to the given frame.
|
|
|
|
*
|
|
|
|
* @param stdClass $state the current framing state.
|
|
|
|
* @param array $subjects the subjects to filter.
|
|
|
|
* @param array $frame the frame.
|
|
|
|
* @param mixed $parent the parent subject or top-level array.
|
|
|
|
* @param mixed $property the parent property, initialized to null.
|
|
|
|
*/
|
2012-04-27 01:53:55 +02:00
|
|
|
protected function _matchFrame(
|
2012-04-23 18:57:06 +02:00
|
|
|
$state, $subjects, $frame, $parent, $property) {
|
|
|
|
// validate the frame
|
2014-12-04 20:45:44 +01:00
|
|
|
$this->_validateFrame($frame);
|
2012-04-23 18:57:06 +02:00
|
|
|
$frame = $frame[0];
|
|
|
|
|
|
|
|
// get flags for current frame
|
|
|
|
$options = $state->options;
|
2014-07-29 19:40:31 +02:00
|
|
|
$flags = array(
|
2014-12-04 20:45:44 +01:00
|
|
|
'embed' => $this->_getFrameFlag($frame, $options, 'embed'),
|
|
|
|
'explicit' => $this->_getFrameFlag($frame, $options, 'explicit'),
|
|
|
|
'requireAll' => $this->_getFrameFlag($frame, $options, 'requireAll'));
|
2014-07-29 19:40:31 +02:00
|
|
|
|
|
|
|
// filter out subjects that match the frame
|
|
|
|
$matches = $this->_filterSubjects($state, $subjects, $frame, $flags);
|
2012-04-23 18:57:06 +02:00
|
|
|
|
|
|
|
// add matches to output
|
|
|
|
foreach($matches as $id => $subject) {
|
2014-12-04 20:45:44 +01:00
|
|
|
if($flags['embed'] === '@link' && property_exists($state->link, $id)) {
|
|
|
|
// TODO: may want to also match an existing linked subject against
|
|
|
|
// 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
|
|
|
|
$this->_addFrameOutput($parent, $property, $state->link->{$id});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
/* Note: In order to treat each top-level match as a compartmentalized
|
2014-12-04 20:45:44 +01:00
|
|
|
result, clear the unique embedded subjects map when the property is null,
|
|
|
|
which only occurs at the top-level. */
|
2012-04-23 18:57:06 +02:00
|
|
|
if($property === null) {
|
2014-12-04 20:45:44 +01:00
|
|
|
$state->uniqueEmbeds = new stdClass();
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2014-12-04 20:45:44 +01:00
|
|
|
// start output for subject
|
2012-04-23 18:57:06 +02:00
|
|
|
$output = new stdClass();
|
|
|
|
$output->{'@id'} = $id;
|
2014-12-04 20:45:44 +01:00
|
|
|
$state->link->{$id} = $output;
|
|
|
|
|
|
|
|
// if embed is @never or if a circular reference would be created by an
|
|
|
|
// embed, the subject cannot be embedded, just add the reference;
|
|
|
|
// note that a circular reference won't occur when the embed flag is
|
|
|
|
// `@link` as the above check will short-circuit before reaching this point
|
|
|
|
if($flags['embed'] === '@never' ||
|
|
|
|
$this->_createsCircularReference($subject, $state->subjectStack)) {
|
|
|
|
$this->_addFrameOutput($parent, $property, $output);
|
|
|
|
continue;
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2014-12-04 20:45:44 +01:00
|
|
|
// if only the last match should be embedded
|
|
|
|
if($flags['embed'] === '@last') {
|
|
|
|
// remove any existing embed
|
|
|
|
if(property_exists($state->uniqueEmbeds, $id)) {
|
2012-04-23 18:57:06 +02:00
|
|
|
$this->_removeEmbed($state, $id);
|
|
|
|
}
|
2014-12-04 20:45:44 +01:00
|
|
|
$state->uniqueEmbeds->{$id} = array(
|
|
|
|
'parent' => $parent, 'property' => $property);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2014-12-04 20:45:44 +01:00
|
|
|
// push matching subject onto stack to enable circular embed checks
|
|
|
|
$state->subjectStack[] = $subject;
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2014-12-04 20:45:44 +01:00
|
|
|
// iterate over subject properties
|
|
|
|
$props = array_keys((array)$subject);
|
|
|
|
sort($props);
|
|
|
|
foreach($props as $prop) {
|
|
|
|
// copy keywords to output
|
|
|
|
if(self::_isKeyword($prop)) {
|
|
|
|
$output->{$prop} = self::copy($subject->{$prop});
|
|
|
|
continue;
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2014-12-04 20:45:44 +01:00
|
|
|
// explicit is on and property isn't in the frame, skip processing
|
|
|
|
if($flags['explicit'] && !property_exists($frame, $prop)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add objects
|
|
|
|
$objects = $subject->{$prop};
|
|
|
|
foreach($objects as $o) {
|
|
|
|
// recurse into list
|
|
|
|
if(self::_isList($o)) {
|
|
|
|
// add empty list
|
|
|
|
$list = (object)array('@list' => array());
|
|
|
|
$this->_addFrameOutput($output, $prop, $list);
|
|
|
|
|
|
|
|
// add list objects
|
|
|
|
$src = $o->{'@list'};
|
|
|
|
foreach($src as $o) {
|
|
|
|
if(self::_isSubjectReference($o)) {
|
|
|
|
// recurse into subject reference
|
|
|
|
$subframe = (property_exists($frame, $prop) ?
|
|
|
|
$frame->{$prop}[0]->{'@list'} :
|
|
|
|
$this->_createImplicitFrame($flags));
|
|
|
|
$this->_matchFrame(
|
|
|
|
$state, array($o->{'@id'}), $subframe, $list, '@list');
|
|
|
|
} else {
|
|
|
|
// include other values automatically
|
|
|
|
$this->_addFrameOutput($list, '@list', self::copy($o));
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
2014-12-04 20:45:44 +01:00
|
|
|
continue;
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
|
2014-12-04 20:45:44 +01:00
|
|
|
if(self::_isSubjectReference($o)) {
|
|
|
|
// recurse into subject reference
|
|
|
|
$subframe = (property_exists($frame, $prop) ?
|
|
|
|
$frame->{$prop} : $this->_createImplicitFrame($flags));
|
|
|
|
$this->_matchFrame(
|
|
|
|
$state, array($o->{'@id'}), $subframe, $output, $prop);
|
|
|
|
} else {
|
|
|
|
// include other values automatically
|
|
|
|
$this->_addFrameOutput($output, $prop, self::copy($o));
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
2014-12-04 20:45:44 +01:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2014-12-04 20:45:44 +01:00
|
|
|
// handle defaults
|
|
|
|
$props = array_keys((array)$frame);
|
|
|
|
sort($props);
|
|
|
|
foreach($props as $prop) {
|
|
|
|
// skip keywords
|
|
|
|
if(self::_isKeyword($prop)) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2014-12-04 20:45:44 +01:00
|
|
|
// if omit default is off, then include default values for properties
|
|
|
|
// that appear in the next frame but are not in the matching subject
|
|
|
|
$next = $frame->{$prop}[0];
|
|
|
|
$omit_default_on = $this->_getFrameFlag(
|
|
|
|
$next, $options, 'omitDefault');
|
|
|
|
if(!$omit_default_on && !property_exists($output, $prop)) {
|
|
|
|
$preserve = '@null';
|
|
|
|
if(property_exists($next, '@default')) {
|
|
|
|
$preserve = self::copy($next->{'@default'});
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2014-12-04 20:45:44 +01:00
|
|
|
$preserve = self::arrayify($preserve);
|
|
|
|
$output->{$prop} = array((object)array('@preserve' => $preserve));
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2014-12-04 20:45:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// add output to parent
|
|
|
|
$this->_addFrameOutput($parent, $property, $output);
|
|
|
|
|
|
|
|
// pop matching subject from circular ref-checking stack
|
|
|
|
array_pop($state->subjectStack);
|
|
|
|
}
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2014-12-04 20:45:44 +01:00
|
|
|
/**
|
|
|
|
* Creates an implicit frame when recursing through subject matches. If
|
|
|
|
* a frame doesn't have an explicit frame for a particular property, then
|
|
|
|
* a wildcard child frame will be created that uses the same flags that the
|
|
|
|
* parent frame used.
|
|
|
|
*
|
|
|
|
* @param assoc flags the current framing flags.
|
|
|
|
*
|
|
|
|
* @return array the implicit frame.
|
|
|
|
*/
|
|
|
|
function _createImplicitFrame($flags) {
|
|
|
|
$frame = new stdClass();
|
|
|
|
foreach($flags as $key => $value) {
|
|
|
|
$frame->{'@' . $key} = array($flags[$key]);
|
|
|
|
}
|
|
|
|
return array($frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* @param assoc subject_stack the current stack of subjects.
|
|
|
|
*
|
|
|
|
* @return bool true if a circular reference would be created, false if not.
|
|
|
|
*/
|
|
|
|
function _createsCircularReference($subject_to_embed, $subject_stack) {
|
|
|
|
for($i = count($subject_stack) - 1; $i >= 0; --$i) {
|
|
|
|
if($subject_stack[$i]->{'@id'} === $subject_to_embed->{'@id'}) {
|
|
|
|
return true;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
2014-12-04 20:45:44 +01:00
|
|
|
return false;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the frame flag value for the given flag name.
|
|
|
|
*
|
|
|
|
* @param stdClass $frame the frame.
|
|
|
|
* @param stdClass $options the framing options.
|
|
|
|
* @param string $name the flag name.
|
|
|
|
*
|
|
|
|
* @return mixed $the flag value.
|
|
|
|
*/
|
|
|
|
protected function _getFrameFlag($frame, $options, $name) {
|
2012-04-23 22:06:42 +02:00
|
|
|
$flag = "@$name";
|
2014-12-04 20:45:44 +01:00
|
|
|
$rval = (property_exists($frame, $flag) ?
|
2012-04-23 20:01:56 +02:00
|
|
|
$frame->{$flag}[0] : $options[$name]);
|
2014-12-04 20:45:44 +01:00
|
|
|
if($name === 'embed') {
|
|
|
|
// default is "@last"
|
|
|
|
// backwards-compatibility support for "embed" maps:
|
|
|
|
// true => "@last"
|
|
|
|
// false => "@never"
|
|
|
|
if($rval === true) {
|
|
|
|
$rval = '@last';
|
|
|
|
} else if($rval === false) {
|
|
|
|
$rval = '@never';
|
|
|
|
} else if($rval !== '@always' && $rval !== '@never' &&
|
|
|
|
$rval !== '@link') {
|
|
|
|
$rval = '@last';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $rval;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates a JSON-LD frame, throwing an exception if the frame is invalid.
|
|
|
|
*
|
|
|
|
* @param array $frame the frame to validate.
|
|
|
|
*/
|
2014-12-04 20:45:44 +01:00
|
|
|
protected function _validateFrame($frame) {
|
2012-04-23 18:57:06 +02:00
|
|
|
if(!is_array($frame) || count($frame) !== 1 || !is_object($frame[0])) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; a JSON-LD frame must be a single object.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.SyntaxError', null, array('frame' => $frame));
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a map of all of the subjects that match a parsed frame.
|
|
|
|
*
|
|
|
|
* @param stdClass $state the current framing state.
|
|
|
|
* @param array $subjects the set of subjects to filter.
|
|
|
|
* @param stdClass $frame the parsed frame.
|
2014-07-29 19:40:31 +02:00
|
|
|
* @param assoc $flags the frame flags.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return stdClass all of the matched subjects.
|
|
|
|
*/
|
2014-07-29 19:40:31 +02:00
|
|
|
protected function _filterSubjects($state, $subjects, $frame, $flags) {
|
2012-04-23 18:57:06 +02:00
|
|
|
$rval = new stdClass();
|
|
|
|
sort($subjects);
|
|
|
|
foreach($subjects as $id) {
|
|
|
|
$subject = $state->subjects->{$id};
|
2014-07-29 19:40:31 +02:00
|
|
|
if($this->_filterSubject($subject, $frame, $flags)) {
|
2012-04-23 18:57:06 +02:00
|
|
|
$rval->{$id} = $subject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the given subject matches the given frame.
|
|
|
|
*
|
|
|
|
* @param stdClass $subject the subject to check.
|
|
|
|
* @param stdClass $frame the frame to check.
|
2014-07-29 19:40:31 +02:00
|
|
|
* @param assoc $flags the frame flags.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return bool true if the subject matches, false if not.
|
|
|
|
*/
|
2014-07-29 19:40:31 +02:00
|
|
|
protected function _filterSubject($subject, $frame, $flags) {
|
2012-04-23 18:57:06 +02:00
|
|
|
// check @type (object value means 'any' type, fall through to ducktyping)
|
|
|
|
if(property_exists($frame, '@type') &&
|
|
|
|
!(count($frame->{'@type'}) === 1 && is_object($frame->{'@type'}[0]))) {
|
|
|
|
$types = $frame->{'@type'};
|
|
|
|
foreach($types as $type) {
|
|
|
|
// any matching @type is a match
|
|
|
|
if(self::hasValue($subject, '@type', $type)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check ducktype
|
2014-07-29 19:40:31 +02:00
|
|
|
$wildcard = true;
|
|
|
|
$matches_some = false;
|
2012-04-23 22:06:42 +02:00
|
|
|
foreach($frame as $k => $v) {
|
2014-07-29 19:40:31 +02:00
|
|
|
if(self::_isKeyword($k)) {
|
|
|
|
// skip non-@id and non-@type
|
|
|
|
if($k !== '@id' && $k !== '@type') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$wildcard = false;
|
|
|
|
|
|
|
|
// check @id for a specific @id value
|
|
|
|
if($k === '@id' && is_string($v)) {
|
|
|
|
if(!property_exists($subject, $k) || $subject->{$k} !== $v) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$matches_some = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$wildcard = false;
|
|
|
|
|
|
|
|
if(property_exists($subject, $k)) {
|
|
|
|
// $v === [] means do not match if property is present
|
|
|
|
if(is_array($v) && count($v) === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$matches_some = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// all properties must match to be a duck unless a @default is specified
|
|
|
|
$has_default = (is_array($v) && count($v) === 1 && is_object($v[0]) &&
|
|
|
|
property_exists($v[0], '@default'));
|
|
|
|
if($flags['requireAll'] && !$has_default) {
|
2012-04-23 18:57:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-07-29 19:40:31 +02:00
|
|
|
|
|
|
|
// return true if wildcard or subject matches some properties
|
|
|
|
return $wildcard || $matches_some;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes an existing embed.
|
|
|
|
*
|
|
|
|
* @param stdClass $state the current framing state.
|
|
|
|
* @param string $id the @id of the embed to remove.
|
|
|
|
*/
|
|
|
|
protected function _removeEmbed($state, $id) {
|
|
|
|
// get existing embed
|
2014-12-04 20:45:44 +01:00
|
|
|
$embeds = $state->uniqueEmbeds;
|
2012-04-23 18:57:06 +02:00
|
|
|
$embed = $embeds->{$id};
|
2015-02-25 03:56:17 +01:00
|
|
|
$property = $embed['property'];
|
2012-04-23 18:57:06 +02:00
|
|
|
|
|
|
|
// create reference to replace embed
|
2015-02-25 03:56:17 +01:00
|
|
|
$subject = (object)array('@id' => $id);
|
2012-04-23 18:57:06 +02:00
|
|
|
|
|
|
|
// remove existing embed
|
|
|
|
if(is_array($embed->parent)) {
|
|
|
|
// replace subject with reference
|
|
|
|
foreach($embed->parent as $i => $parent) {
|
|
|
|
if(self::compareValues($parent, $subject)) {
|
|
|
|
$embed->parent[$i] = $subject;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2012-04-23 18:57:06 +02:00
|
|
|
// replace subject with reference
|
2012-04-27 01:53:55 +02:00
|
|
|
$use_array = is_array($embed->parent->{$property});
|
2012-07-01 00:27:09 +02:00
|
|
|
self::removeValue($embed->parent, $property, $subject,
|
|
|
|
array('propertyIsArray' => $use_array));
|
|
|
|
self::addValue($embed->parent, $property, $subject,
|
|
|
|
array('propertyIsArray' => $use_array));
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// recursively remove dependent dangling embeds
|
|
|
|
$removeDependents = function($id) {
|
|
|
|
// get embed keys as a separate array to enable deleting keys in map
|
2012-04-23 20:01:56 +02:00
|
|
|
$ids = array_keys((array)$embeds);
|
2012-04-23 18:57:06 +02:00
|
|
|
foreach($ids as $next) {
|
|
|
|
if(property_exists($embeds, $next) &&
|
|
|
|
is_object($embeds->{$next}->parent) &&
|
|
|
|
$embeds->{$next}->parent->{'@id'} === $id) {
|
|
|
|
unset($embeds->{$next});
|
|
|
|
$removeDependents($next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
$removeDependents($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds framing output to the given parent.
|
|
|
|
*
|
|
|
|
* @param mixed $parent the parent to add to.
|
|
|
|
* @param string $property the parent property.
|
|
|
|
* @param mixed $output the output to add.
|
|
|
|
*/
|
2014-12-04 20:45:44 +01:00
|
|
|
protected function _addFrameOutput($parent, $property, $output) {
|
2012-04-23 22:06:42 +02:00
|
|
|
if(is_object($parent) && !($parent instanceof ArrayObject)) {
|
2012-07-01 00:27:09 +02:00
|
|
|
self::addValue(
|
|
|
|
$parent, $property, $output, array('propertyIsArray' => true));
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2012-04-23 18:57:06 +02:00
|
|
|
$parent[] = $output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the @preserve keywords as the last step of the framing algorithm.
|
|
|
|
*
|
|
|
|
* @param stdClass $ctx the active context used to compact the input.
|
|
|
|
* @param mixed $input the framed, compacted output.
|
2013-02-21 22:06:33 +01:00
|
|
|
* @param assoc $options the compaction options used.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return mixed the resulting output.
|
|
|
|
*/
|
2013-02-21 22:06:33 +01:00
|
|
|
protected function _removePreserve($ctx, $input, $options) {
|
2012-04-23 18:57:06 +02:00
|
|
|
// recurse through arrays
|
|
|
|
if(is_array($input)) {
|
|
|
|
$output = array();
|
|
|
|
foreach($input as $e) {
|
2013-02-21 22:06:33 +01:00
|
|
|
$result = $this->_removePreserve($ctx, $e, $options);
|
2012-04-23 18:57:06 +02:00
|
|
|
// drop nulls from arrays
|
|
|
|
if($result !== null) {
|
|
|
|
$output[] = $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$input = $output;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(is_object($input)) {
|
2012-04-23 18:57:06 +02:00
|
|
|
// remove @preserve
|
|
|
|
if(property_exists($input, '@preserve')) {
|
|
|
|
if($input->{'@preserve'} === '@null') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $input->{'@preserve'};
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip @values
|
|
|
|
if(self::_isValue($input)) {
|
|
|
|
return $input;
|
|
|
|
}
|
|
|
|
|
|
|
|
// recurse through @lists
|
2012-04-26 22:31:55 +02:00
|
|
|
if(self::_isList($input)) {
|
2013-02-21 22:06:33 +01:00
|
|
|
$input->{'@list'} = $this->_removePreserve(
|
|
|
|
$ctx, $input->{'@list'}, $options);
|
2012-04-23 18:57:06 +02:00
|
|
|
return $input;
|
|
|
|
}
|
|
|
|
|
2014-12-04 20:45:44 +01:00
|
|
|
// handle in-memory linked nodes
|
|
|
|
$id_alias = $this->_compactIri($ctx, '@id');
|
|
|
|
if(property_exists($input, $id_alias)) {
|
|
|
|
$id = $input->{$id_alias};
|
|
|
|
if(isset($options['link'][$id])) {
|
|
|
|
$idx = array_search($input, $options['link'][$id]);
|
|
|
|
if($idx === false) {
|
|
|
|
// prevent circular visitation
|
|
|
|
$options['link'][$id][] = $input;
|
|
|
|
} else {
|
|
|
|
// already visited
|
|
|
|
return $options['link'][$id][$idx];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// prevent circular visitation
|
2014-12-05 22:37:31 +01:00
|
|
|
$options['link'][$id] = array($input);
|
2014-12-04 20:45:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
// recurse through properties
|
|
|
|
foreach($input as $prop => $v) {
|
2013-02-21 22:06:33 +01:00
|
|
|
$result = $this->_removePreserve($ctx, $v, $options);
|
2012-04-23 18:57:06 +02:00
|
|
|
$container = self::getContextValue($ctx, $prop, '@container');
|
2013-02-21 22:06:33 +01:00
|
|
|
if($options['compactArrays'] &&
|
|
|
|
is_array($result) && count($result) === 1 &&
|
2012-04-23 18:57:06 +02:00
|
|
|
$container !== '@set' && $container !== '@list') {
|
|
|
|
$result = $result[0];
|
|
|
|
}
|
|
|
|
$input->{$prop} = $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $input;
|
|
|
|
}
|
|
|
|
|
2013-02-15 22:25:30 +01:00
|
|
|
/**
|
2013-02-20 22:55:36 +01:00
|
|
|
* Compares two RDF triples for equality.
|
|
|
|
*
|
|
|
|
* @param stdClass $t1 the first triple.
|
|
|
|
* @param stdClass $t2 the second triple.
|
2013-02-15 22:25:30 +01:00
|
|
|
*
|
2013-02-20 22:55:36 +01:00
|
|
|
* @return true if the triples are the same, false if not.
|
|
|
|
*/
|
|
|
|
protected static function _compareRDFTriples($t1, $t2) {
|
|
|
|
foreach(array('subject', 'predicate', 'object') as $attr) {
|
|
|
|
if($t1->{$attr}->type !== $t2->{$attr}->type ||
|
|
|
|
$t1->{$attr}->value !== $t2->{$attr}->value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(property_exists($t1->object, 'language') !==
|
|
|
|
property_exists($t1->object, 'language')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(property_exists($t1->object, 'language') &&
|
|
|
|
$t1->object->language !== $t2->object->language) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-09 19:42:31 +02:00
|
|
|
if(property_exists($t1->object, 'datatype') &&
|
|
|
|
$t1->object->datatype !== $t2->object->datatype) {
|
2013-02-20 22:55:36 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hashes all of the quads about a blank node.
|
|
|
|
*
|
|
|
|
* @param string $id the ID of the bnode to hash quads for.
|
|
|
|
* @param stdClass $bnodes the mapping of bnodes to quads.
|
2013-02-15 22:25:30 +01:00
|
|
|
* @param UniqueNamer $namer the canonical bnode namer.
|
|
|
|
*
|
|
|
|
* @return string the new hash.
|
|
|
|
*/
|
2013-02-20 22:55:36 +01:00
|
|
|
protected function _hashQuads($id, $bnodes, $namer) {
|
2013-02-15 22:25:30 +01:00
|
|
|
// return cached hash
|
|
|
|
if(property_exists($bnodes->{$id}, 'hash')) {
|
|
|
|
return $bnodes->{$id}->hash;
|
|
|
|
}
|
|
|
|
|
2013-02-20 22:55:36 +01:00
|
|
|
// serialize all of bnode's quads
|
|
|
|
$quads = $bnodes->{$id}->quads;
|
2013-02-15 22:25:30 +01:00
|
|
|
$nquads = array();
|
2013-02-20 22:55:36 +01:00
|
|
|
foreach($quads as $quad) {
|
|
|
|
$nquads[] = $this->toNQuad($quad, property_exists($quad, 'name') ?
|
|
|
|
$quad->name->value : null, $id);
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// sort serialized quads
|
|
|
|
sort($nquads);
|
|
|
|
|
|
|
|
// cache and return hashed quads
|
|
|
|
$hash = $bnodes->{$id}->hash = sha1(implode($nquads));
|
|
|
|
return $hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Produces a hash for the paths of adjacent bnodes for a bnode,
|
|
|
|
* incorporating all information about its subgraph of bnodes. This
|
|
|
|
* method will recursively pick adjacent bnode permutations that produce the
|
|
|
|
* lexicographically-least 'path' serializations.
|
|
|
|
*
|
|
|
|
* @param string $id the ID of the bnode to hash paths for.
|
2013-02-20 22:55:36 +01:00
|
|
|
* @param stdClass $bnodes the map of bnode quads.
|
2013-02-15 22:25:30 +01:00
|
|
|
* @param UniqueNamer $namer the canonical bnode namer.
|
|
|
|
* @param UniqueNamer $path_namer the namer used to assign names to adjacent
|
|
|
|
* bnodes.
|
|
|
|
*
|
|
|
|
* @return stdClass the hash and path namer used.
|
|
|
|
*/
|
|
|
|
protected function _hashPaths($id, $bnodes, $namer, $path_namer) {
|
|
|
|
// create SHA-1 digest
|
|
|
|
$md = hash_init('sha1');
|
|
|
|
|
|
|
|
// group adjacent bnodes by hash, keep properties and references separate
|
|
|
|
$groups = new stdClass();
|
2013-02-20 22:55:36 +01:00
|
|
|
$quads = $bnodes->{$id}->quads;
|
|
|
|
foreach($quads as $quad) {
|
2013-02-15 22:25:30 +01:00
|
|
|
// get adjacent bnode
|
2013-02-20 22:55:36 +01:00
|
|
|
$bnode = $this->_getAdjacentBlankNodeName($quad->subject, $id);
|
2013-02-15 22:25:30 +01:00
|
|
|
if($bnode !== null) {
|
2013-02-20 22:55:36 +01:00
|
|
|
// normal property
|
2013-02-15 22:25:30 +01:00
|
|
|
$direction = 'p';
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-20 22:55:36 +01:00
|
|
|
$bnode = $this->_getAdjacentBlankNodeName($quad->object, $id);
|
2013-02-15 22:25:30 +01:00
|
|
|
if($bnode !== null) {
|
2013-02-20 22:55:36 +01:00
|
|
|
// reverse property
|
2013-02-15 22:25:30 +01:00
|
|
|
$direction = 'r';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($bnode !== null) {
|
|
|
|
// get bnode name (try canonical, path, then hash)
|
|
|
|
if($namer->isNamed($bnode)) {
|
|
|
|
$name = $namer->getName($bnode);
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if($path_namer->isNamed($bnode)) {
|
2013-02-15 22:25:30 +01:00
|
|
|
$name = $path_namer->getName($bnode);
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-20 22:55:36 +01:00
|
|
|
$name = $this->_hashQuads($bnode, $bnodes, $namer);
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// hash direction, property, and bnode name/hash
|
|
|
|
$group_md = hash_init('sha1');
|
|
|
|
hash_update($group_md, $direction);
|
2013-02-20 22:55:36 +01:00
|
|
|
hash_update($group_md, $quad->predicate->value);
|
2013-02-15 22:25:30 +01:00
|
|
|
hash_update($group_md, $name);
|
|
|
|
$group_hash = hash_final($group_md);
|
|
|
|
|
|
|
|
// add bnode to hash group
|
|
|
|
if(property_exists($groups, $group_hash)) {
|
|
|
|
$groups->{$group_hash}[] = $bnode;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-15 22:25:30 +01:00
|
|
|
$groups->{$group_hash} = array($bnode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate over groups in sorted hash order
|
|
|
|
$group_hashes = array_keys((array)$groups);
|
|
|
|
sort($group_hashes);
|
|
|
|
foreach($group_hashes as $group_hash) {
|
|
|
|
// digest group hash
|
|
|
|
hash_update($md, $group_hash);
|
|
|
|
|
|
|
|
// choose a path and namer from the permutations
|
|
|
|
$chosen_path = null;
|
|
|
|
$chosen_namer = null;
|
|
|
|
$permutator = new Permutator($groups->{$group_hash});
|
|
|
|
while($permutator->hasNext()) {
|
|
|
|
$permutation = $permutator->next();
|
|
|
|
$path_namer_copy = clone $path_namer;
|
|
|
|
|
|
|
|
// build adjacent path
|
|
|
|
$path = '';
|
|
|
|
$skipped = false;
|
|
|
|
$recurse = array();
|
|
|
|
foreach($permutation as $bnode) {
|
|
|
|
// use canonical name if available
|
|
|
|
if($namer->isNamed($bnode)) {
|
|
|
|
$path .= $namer->getName($bnode);
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-15 22:25:30 +01:00
|
|
|
// recurse if bnode isn't named in the path yet
|
|
|
|
if(!$path_namer_copy->isNamed($bnode)) {
|
|
|
|
$recurse[] = $bnode;
|
|
|
|
}
|
|
|
|
$path .= $path_namer_copy->getName($bnode);
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip permutation if path is already >= chosen path
|
|
|
|
if($chosen_path !== null && strlen($path) >= strlen($chosen_path) &&
|
|
|
|
$path > $chosen_path) {
|
|
|
|
$skipped = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// recurse
|
|
|
|
if(!$skipped) {
|
|
|
|
foreach($recurse as $bnode) {
|
|
|
|
$result = $this->_hashPaths(
|
|
|
|
$bnode, $bnodes, $namer, $path_namer_copy);
|
|
|
|
$path .= $path_namer_copy->getName($bnode);
|
|
|
|
$path .= "<{$result->hash}>";
|
|
|
|
$path_namer_copy = $result->pathNamer;
|
|
|
|
|
|
|
|
// skip permutation if path is already >= chosen path
|
|
|
|
if($chosen_path !== null &&
|
|
|
|
strlen($path) >= strlen($chosen_path) && $path > $chosen_path) {
|
|
|
|
$skipped = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$skipped && ($chosen_path === null || $path < $chosen_path)) {
|
|
|
|
$chosen_path = $path;
|
|
|
|
$chosen_namer = $path_namer_copy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// digest chosen path and update namer
|
|
|
|
hash_update($md, $chosen_path);
|
|
|
|
$path_namer = $chosen_namer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return SHA-1 hash and path namer
|
|
|
|
return (object)array(
|
|
|
|
'hash' => hash_final($md), 'pathNamer' => $path_namer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-20 22:55:36 +01:00
|
|
|
* A helper function that gets the blank node name from an RDF quad
|
2013-02-15 22:25:30 +01:00
|
|
|
* node (subject or object). If the node is not a blank node or its
|
2013-02-20 22:55:36 +01:00
|
|
|
* value does not match the given blank node ID, it will be returned.
|
2013-02-15 22:25:30 +01:00
|
|
|
*
|
2013-02-20 22:55:36 +01:00
|
|
|
* @param stdClass $node the RDF quad node.
|
2013-02-15 22:25:30 +01:00
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
protected function _getAdjacentBlankNodeName($node, $id) {
|
2013-02-20 22:55:36 +01:00
|
|
|
if($node->type === 'blank node' && $node->value !== $id) {
|
|
|
|
return $node->value;
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
|
|
|
/**
|
2013-02-07 23:19:19 +01:00
|
|
|
* Compares two strings first based on length and then lexicographically.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
2013-02-07 23:19:19 +01:00
|
|
|
* @param string $a the first string.
|
|
|
|
* @param string $b the second string.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
2013-02-07 23:19:19 +01:00
|
|
|
* @return integer -1 if a < b, 1 if a > b, 0 if a == b.
|
2012-04-23 18:57:06 +02:00
|
|
|
*/
|
2013-02-07 23:19:19 +01:00
|
|
|
protected function _compareShortestLeast($a, $b) {
|
|
|
|
$len_a = strlen($a);
|
|
|
|
$len_b = strlen($b);
|
|
|
|
if($len_a < $len_b) {
|
|
|
|
return -1;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
if($len_b < $len_a) {
|
2013-02-07 23:19:19 +01:00
|
|
|
return 1;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
if($a === $b) {
|
2013-02-07 23:19:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ($a < $b) ? -1 : 1;
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
/**
|
|
|
|
* Picks the preferred compaction term from the given inverse context entry.
|
|
|
|
*
|
|
|
|
* @param active_ctx the active context.
|
|
|
|
* @param iri the IRI to pick the term for.
|
|
|
|
* @param value the value to pick the term for.
|
|
|
|
* @param containers the preferred containers.
|
|
|
|
* @param type_or_language either '@type' or '@language'.
|
|
|
|
* @param type_or_language_value the preferred value for '@type' or
|
|
|
|
* '@language'.
|
|
|
|
*
|
|
|
|
* @return mixed the preferred term.
|
|
|
|
*/
|
|
|
|
protected function _selectTerm(
|
2013-03-06 20:34:03 +01:00
|
|
|
$active_ctx, $iri, $value, $containers,
|
2013-02-07 23:19:19 +01:00
|
|
|
$type_or_language, $type_or_language_value) {
|
|
|
|
if($type_or_language_value === null) {
|
|
|
|
$type_or_language_value = '@null';
|
|
|
|
}
|
2013-02-14 18:40:24 +01:00
|
|
|
|
2013-03-12 17:20:11 +01:00
|
|
|
// options for the value of @type or @language
|
|
|
|
$prefs = array();
|
|
|
|
|
|
|
|
// determine prefs for @id based on whether or not value compacts to a term
|
|
|
|
if(($type_or_language_value === '@id' ||
|
|
|
|
$type_or_language_value === '@reverse') &&
|
|
|
|
self::_isSubjectReference($value)) {
|
|
|
|
// prefer @reverse first
|
|
|
|
if($type_or_language_value === '@reverse') {
|
|
|
|
$prefs[] = '@reverse';
|
|
|
|
}
|
2013-02-15 22:25:30 +01:00
|
|
|
// try to compact value to a term
|
|
|
|
$term = $this->_compactIri(
|
|
|
|
$active_ctx, $value->{'@id'}, null, array('vocab' => true));
|
2013-02-15 06:58:23 +01:00
|
|
|
if(property_exists($active_ctx->mappings, $term) &&
|
|
|
|
$active_ctx->mappings->{$term} &&
|
2013-02-15 22:25:30 +01:00
|
|
|
$active_ctx->mappings->{$term}->{'@id'} === $value->{'@id'}) {
|
2013-02-14 18:40:24 +01:00
|
|
|
// prefer @vocab
|
2013-03-12 17:20:11 +01:00
|
|
|
array_push($prefs, '@vocab', '@id');
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-14 18:40:24 +01:00
|
|
|
// prefer @id
|
2013-03-12 17:20:11 +01:00
|
|
|
array_push($prefs, '@id', '@vocab');
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-03-12 17:20:11 +01:00
|
|
|
$prefs[] = $type_or_language_value;
|
2013-02-14 18:40:24 +01:00
|
|
|
}
|
2013-03-12 17:20:11 +01:00
|
|
|
$prefs[] = '@none';
|
2013-02-14 18:40:24 +01:00
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
$container_map = $active_ctx->inverse->{$iri};
|
|
|
|
foreach($containers as $container) {
|
|
|
|
// if container not available in the map, continue
|
|
|
|
if(!property_exists($container_map, $container)) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
$type_or_language_value_map =
|
|
|
|
$container_map->{$container}->{$type_or_language};
|
2013-03-12 17:20:11 +01:00
|
|
|
foreach($prefs as $pref) {
|
2013-02-07 23:19:19 +01:00
|
|
|
// if type/language option not available in the map, continue
|
2013-03-12 17:20:11 +01:00
|
|
|
if(!property_exists($type_or_language_value_map, $pref)) {
|
2013-02-07 23:19:19 +01:00
|
|
|
continue;
|
|
|
|
}
|
2012-05-09 00:07:28 +02:00
|
|
|
|
2013-03-06 20:34:03 +01:00
|
|
|
// select term
|
2013-04-03 20:20:25 +02:00
|
|
|
return $type_or_language_value_map->{$pref};
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
2013-04-03 20:20:25 +02:00
|
|
|
return null;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2013-02-07 23:19:19 +01:00
|
|
|
* @param stdClass $active_ctx the active context to use.
|
2012-04-23 18:57:06 +02:00
|
|
|
* @param string $iri the IRI to compact.
|
|
|
|
* @param mixed $value the value to check or null.
|
2013-02-07 23:19:19 +01:00
|
|
|
* @param assoc $relative_to options for how to compact IRIs:
|
|
|
|
* vocab: true to split after @vocab, false not to.
|
2013-03-12 17:20:11 +01:00
|
|
|
* @param bool $reverse true if a reverse property is being compacted, false
|
|
|
|
* if not.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return string the compacted term, prefix, keyword alias, or original IRI.
|
|
|
|
*/
|
2013-02-07 23:19:19 +01:00
|
|
|
protected function _compactIri(
|
2013-03-12 17:20:11 +01:00
|
|
|
$active_ctx, $iri, $value=null, $relative_to=array(), $reverse=false) {
|
2012-04-23 18:57:06 +02:00
|
|
|
// can't compact null
|
|
|
|
if($iri === null) {
|
|
|
|
return $iri;
|
|
|
|
}
|
|
|
|
|
2013-03-11 22:04:24 +01:00
|
|
|
// term is a keyword, default vocab to true
|
2012-04-23 18:57:06 +02:00
|
|
|
if(self::_isKeyword($iri)) {
|
2013-03-11 22:04:24 +01:00
|
|
|
$relative_to['vocab'] = true;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(!isset($relative_to['vocab'])) {
|
2013-04-04 17:36:50 +02:00
|
|
|
$relative_to['vocab'] = false;
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-02-14 19:34:28 +01:00
|
|
|
// use inverse context to pick a term if iri is relative to vocab
|
2013-04-04 17:36:50 +02:00
|
|
|
if($relative_to['vocab'] &&
|
2013-02-14 19:34:28 +01:00
|
|
|
property_exists($this->_getInverseContext($active_ctx), $iri)) {
|
2013-02-15 22:25:30 +01:00
|
|
|
$default_language = '@none';
|
|
|
|
if(property_exists($active_ctx, '@language')) {
|
|
|
|
$default_language = $active_ctx->{'@language'};
|
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
|
|
|
|
// prefer @index if available in value
|
|
|
|
$containers = array();
|
|
|
|
if(is_object($value) && property_exists($value, '@index')) {
|
|
|
|
$containers[] = '@index';
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
|
|
|
|
// defaults for term selection based on type/language
|
|
|
|
$type_or_language = '@language';
|
|
|
|
$type_or_language_value = '@null';
|
|
|
|
|
2013-03-27 15:52:25 +01:00
|
|
|
if($reverse) {
|
|
|
|
$type_or_language = '@type';
|
|
|
|
$type_or_language_value = '@reverse';
|
|
|
|
$containers[] = '@set';
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(self::_isList($value)) {
|
|
|
|
// choose the most specific term that works for all elements in @list
|
2013-02-07 23:19:19 +01:00
|
|
|
// only select @list containers if @index is NOT in value
|
|
|
|
if(!property_exists($value, '@index')) {
|
|
|
|
$containers[] = '@list';
|
|
|
|
}
|
|
|
|
$list = $value->{'@list'};
|
|
|
|
$common_language = (count($list) === 0) ? $default_language : null;
|
|
|
|
$common_type = null;
|
|
|
|
foreach($list as $item) {
|
|
|
|
$item_language = '@none';
|
|
|
|
$item_type = '@none';
|
|
|
|
if(self::_isValue($item)) {
|
|
|
|
if(property_exists($item, '@language')) {
|
|
|
|
$item_language = $item->{'@language'};
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(property_exists($item, '@type')) {
|
2013-02-07 23:19:19 +01:00
|
|
|
$item_type = $item->{'@type'};
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
|
|
|
// plain literal
|
2013-02-07 23:19:19 +01:00
|
|
|
$item_language = '@null';
|
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-11 16:31:49 +01:00
|
|
|
$item_type = '@id';
|
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
if($common_language === null) {
|
|
|
|
$common_language = $item_language;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if($item_language !== $common_language &&
|
2013-02-07 23:19:19 +01:00
|
|
|
self::_isValue($item)) {
|
|
|
|
$common_language = '@none';
|
|
|
|
}
|
|
|
|
if($common_type === null) {
|
|
|
|
$common_type = $item_type;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if($item_type !== $common_type) {
|
2013-02-07 23:19:19 +01:00
|
|
|
$common_type = '@none';
|
|
|
|
}
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
2013-02-08 05:47:55 +01:00
|
|
|
if($common_language === null) {
|
|
|
|
$common_language = '@none';
|
|
|
|
}
|
|
|
|
if($common_type === null) {
|
|
|
|
$common_type = '@none';
|
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
if($common_type !== '@none') {
|
|
|
|
$type_or_language = '@type';
|
|
|
|
$type_or_language_value = $common_type;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-07 23:19:19 +01:00
|
|
|
$type_or_language_value = $common_language;
|
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-07 23:19:19 +01:00
|
|
|
if(self::_isValue($value)) {
|
2013-02-18 16:33:18 +01:00
|
|
|
if(property_exists($value, '@language') &&
|
|
|
|
!property_exists($value, '@index')) {
|
2013-02-07 23:19:19 +01:00
|
|
|
$containers[] = '@language';
|
|
|
|
$type_or_language_value = $value->{'@language'};
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(property_exists($value, '@type')) {
|
2013-02-07 23:19:19 +01:00
|
|
|
$type_or_language = '@type';
|
|
|
|
$type_or_language_value = $value->{'@type'};
|
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-07 23:19:19 +01:00
|
|
|
$type_or_language = '@type';
|
|
|
|
$type_or_language_value = '@id';
|
|
|
|
}
|
|
|
|
$containers[] = '@set';
|
|
|
|
}
|
|
|
|
|
|
|
|
// do term selection
|
2013-03-27 15:52:25 +01:00
|
|
|
$containers[] = '@none';
|
2013-02-07 23:19:19 +01:00
|
|
|
$term = $this->_selectTerm(
|
2013-03-06 20:34:03 +01:00
|
|
|
$active_ctx, $iri, $value,
|
2013-02-07 23:19:19 +01:00
|
|
|
$containers, $type_or_language, $type_or_language_value);
|
|
|
|
if($term !== null) {
|
|
|
|
return $term;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-04 17:36:50 +02:00
|
|
|
// no term match, use @vocab if available
|
|
|
|
if($relative_to['vocab']) {
|
|
|
|
if(property_exists($active_ctx, '@vocab')) {
|
|
|
|
// determine if vocab is a prefix of the iri
|
|
|
|
$vocab = $active_ctx->{'@vocab'};
|
|
|
|
if(strpos($iri, $vocab) === 0 && $iri !== $vocab) {
|
2013-07-15 18:30:28 +02:00
|
|
|
// use suffix as relative iri if it is not a term in the active
|
|
|
|
// context
|
2013-04-04 17:36:50 +02:00
|
|
|
$suffix = substr($iri, strlen($vocab));
|
|
|
|
if(!property_exists($active_ctx->mappings, $suffix)) {
|
|
|
|
return $suffix;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no term or @vocab match, check for possible CURIEs
|
2013-02-07 23:19:19 +01:00
|
|
|
$choice = null;
|
|
|
|
foreach($active_ctx->mappings as $term => $definition) {
|
|
|
|
// skip terms with colons, they can't be prefixes
|
|
|
|
if(strpos($term, ':') !== false) {
|
2012-04-23 18:57:06 +02:00
|
|
|
continue;
|
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
// skip entries with @ids that are not partial matches
|
2013-03-06 20:34:03 +01:00
|
|
|
if($definition === null ||
|
2013-02-07 23:19:19 +01:00
|
|
|
$definition->{'@id'} === $iri ||
|
|
|
|
strpos($iri, $definition->{'@id'}) !== 0) {
|
2012-04-23 18:57:06 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// a CURIE is usable if:
|
|
|
|
// 1. it has no mapping, OR
|
|
|
|
// 2. value is null, which means we're not compacting an @value, AND
|
|
|
|
// the mapping matches the IRI)
|
|
|
|
$curie = $term . ':' . substr($iri, strlen($definition->{'@id'}));
|
|
|
|
$is_usable_curie = (!property_exists($active_ctx->mappings, $curie) ||
|
|
|
|
($value === null && $active_ctx->mappings->{$curie} &&
|
|
|
|
$active_ctx->mappings->{$curie}->{'@id'} === $iri));
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// select curie if it is shorter or the same length but lexicographically
|
|
|
|
// less than the current choice
|
|
|
|
if($is_usable_curie && ($choice === null ||
|
|
|
|
self::_compareShortestLeast($curie, $choice) < 0)) {
|
|
|
|
$choice = $curie;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// return chosen curie
|
|
|
|
if($choice !== null) {
|
|
|
|
return $choice;
|
|
|
|
}
|
|
|
|
|
2013-02-15 06:58:23 +01:00
|
|
|
// compact IRI relative to base
|
2013-04-04 17:36:50 +02:00
|
|
|
if(!$relative_to['vocab']) {
|
2013-02-15 18:44:00 +01:00
|
|
|
return jsonld_remove_base($active_ctx->{'@base'}, $iri);
|
|
|
|
}
|
|
|
|
|
|
|
|
// return IRI as is
|
|
|
|
return $iri;
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
/**
|
|
|
|
* Performs value compaction on an object with '@value' or '@id' as the only
|
|
|
|
* property.
|
|
|
|
*
|
|
|
|
* @param stdClass $active_ctx the active context.
|
|
|
|
* @param string $active_property the active property that points to the
|
|
|
|
* value.
|
|
|
|
* @param mixed $value the value to compact.
|
|
|
|
*
|
|
|
|
* @return mixed the compaction result.
|
|
|
|
*/
|
|
|
|
protected function _compactValue($active_ctx, $active_property, $value) {
|
|
|
|
// value is a @value
|
|
|
|
if(self::_isValue($value)) {
|
|
|
|
// get context rules
|
|
|
|
$type = self::getContextValue($active_ctx, $active_property, '@type');
|
|
|
|
$language = self::getContextValue(
|
|
|
|
$active_ctx, $active_property, '@language');
|
|
|
|
$container = self::getContextValue(
|
|
|
|
$active_ctx, $active_property, '@container');
|
|
|
|
|
|
|
|
// whether or not the value has an @index that must be preserved
|
|
|
|
$preserve_index = (property_exists($value, '@index') &&
|
|
|
|
$container !== '@index');
|
|
|
|
|
2013-04-03 22:31:48 +02:00
|
|
|
// if there's no @index to preserve
|
2013-02-07 23:19:19 +01:00
|
|
|
if(!$preserve_index) {
|
|
|
|
// matching @type or @language specified in context, compact value
|
|
|
|
if(self::_hasKeyValue($value, '@type', $type) ||
|
|
|
|
self::_hasKeyValue($value, '@language', $language)) {
|
|
|
|
return $value->{'@value'};
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
|
|
|
|
// return just the value of @value if all are true:
|
|
|
|
// 1. @value is the only key or @index isn't being preserved
|
|
|
|
// 2. there is no default language or @value is not a string or
|
|
|
|
// the key has a mapping with a null @language
|
|
|
|
$key_count = count(array_keys((array)$value));
|
|
|
|
$is_value_only_key = ($key_count === 1 ||
|
|
|
|
($key_count === 2 && property_exists($value, '@index') &&
|
|
|
|
!$preserve_index));
|
|
|
|
$has_default_language = property_exists($active_ctx, '@language');
|
|
|
|
$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));
|
|
|
|
if($is_value_only_key &&
|
|
|
|
(!$has_default_language || !$is_value_string || $has_null_mapping)) {
|
|
|
|
return $value->{'@value'};
|
|
|
|
}
|
|
|
|
|
|
|
|
$rval = new stdClass();
|
|
|
|
|
|
|
|
// preserve @index
|
|
|
|
if($preserve_index) {
|
|
|
|
$rval->{$this->_compactIri($active_ctx, '@index')} = $value->{'@index'};
|
|
|
|
}
|
|
|
|
|
|
|
|
// compact @type IRI
|
|
|
|
if(property_exists($value, '@type')) {
|
|
|
|
$rval->{$this->_compactIri($active_ctx, '@type')} = $this->_compactIri(
|
2013-02-15 06:58:23 +01:00
|
|
|
$active_ctx, $value->{'@type'}, null, array('vocab' => true));
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(property_exists($value, '@language')) {
|
|
|
|
// alias @language
|
2013-02-07 23:19:19 +01:00
|
|
|
$rval->{$this->_compactIri($active_ctx, '@language')} =
|
|
|
|
$value->{'@language'};
|
|
|
|
}
|
|
|
|
|
|
|
|
// alias @value
|
|
|
|
$rval->{$this->_compactIri($active_ctx, '@value')} = $value->{'@value'};
|
|
|
|
|
|
|
|
return $rval;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// value is a subject reference
|
2013-02-15 20:13:46 +01:00
|
|
|
$expanded_property = $this->_expandIri(
|
|
|
|
$active_ctx, $active_property, array('vocab' => true));
|
2013-02-07 23:19:19 +01:00
|
|
|
$type = self::getContextValue($active_ctx, $active_property, '@type');
|
2013-02-15 06:58:23 +01:00
|
|
|
$compacted = $this->_compactIri(
|
2013-02-14 19:34:28 +01:00
|
|
|
$active_ctx, $value->{'@id'}, null,
|
2013-02-15 06:58:23 +01:00
|
|
|
array('vocab' => ($type === '@vocab')));
|
2013-02-07 23:19:19 +01:00
|
|
|
|
|
|
|
// compact to scalar
|
2013-02-14 18:40:24 +01:00
|
|
|
if($type === '@id' || $type === '@vocab' ||
|
|
|
|
$expanded_property === '@graph') {
|
2013-02-15 06:58:23 +01:00
|
|
|
return $compacted;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
$rval = (object)array(
|
2013-02-15 06:58:23 +01:00
|
|
|
$this->_compactIri($active_ctx, '@id') => $compacted);
|
2013-02-07 23:19:19 +01:00
|
|
|
return $rval;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
/**
|
|
|
|
* Creates a term definition during context processing.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @param stdClass $active_ctx the current active context.
|
2013-02-07 23:19:19 +01:00
|
|
|
* @param stdClass $local_ctx the local context being processed.
|
|
|
|
* @param string $term the key in the local context to define the mapping for.
|
2012-04-23 18:57:06 +02:00
|
|
|
* @param stdClass $defined a map of defining/defined keys to detect cycles
|
|
|
|
* and prevent double definitions.
|
|
|
|
*/
|
2013-02-07 23:19:19 +01:00
|
|
|
protected function _createTermDefinition(
|
|
|
|
$active_ctx, $local_ctx, $term, $defined) {
|
|
|
|
if(property_exists($defined, $term)) {
|
|
|
|
// term already defined
|
|
|
|
if($defined->{$term}) {
|
2012-04-23 18:57:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// cycle detected
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Cyclical context definition detected.',
|
2013-09-14 19:42:15 +02:00
|
|
|
'jsonld.CyclicalContext', 'cyclic IRI mapping',
|
|
|
|
array('context' => $local_ctx, 'term' => $term));
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// now defining term
|
|
|
|
$defined->{$term} = false;
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
if(self::_isKeyword($term)) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; keywords cannot be overridden.',
|
2013-09-15 03:08:01 +02:00
|
|
|
'jsonld.SyntaxError', 'keyword redefinition',
|
2014-07-09 21:16:24 +02:00
|
|
|
array('context' => $local_ctx, 'term' => $term));
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-03-11 22:04:24 +01:00
|
|
|
// remove old mapping
|
|
|
|
if(property_exists($active_ctx->mappings, $term)) {
|
|
|
|
unset($active_ctx->mappings->{$term});
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// get context term value
|
|
|
|
$value = $local_ctx->{$term};
|
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
// clear context entry
|
2013-02-07 23:19:19 +01:00
|
|
|
if($value === null || (is_object($value) &&
|
2013-02-12 21:10:45 +01:00
|
|
|
self::_hasKeyValue($value, '@id', null))) {
|
2013-02-07 23:19:19 +01:00
|
|
|
$active_ctx->mappings->{$term} = null;
|
|
|
|
$defined->{$term} = true;
|
2012-04-23 18:57:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-15 17:09:06 +02:00
|
|
|
// convert short-hand value to object w/@id
|
2012-04-23 18:57:06 +02:00
|
|
|
if(is_string($value)) {
|
2013-04-15 17:09:06 +02:00
|
|
|
$value = (object)array('@id' => $value);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!is_object($value)) {
|
|
|
|
throw new JsonLdException(
|
2013-02-07 23:19:19 +01:00
|
|
|
'Invalid JSON-LD syntax; @context property values must be ' .
|
2013-09-14 19:42:15 +02:00
|
|
|
'strings or objects.', 'jsonld.SyntaxError', 'invalid term definition',
|
|
|
|
array('context' => $local_ctx));
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// create new mapping
|
2013-04-16 17:11:09 +02:00
|
|
|
$mapping = $active_ctx->mappings->{$term} = new stdClass();
|
2013-03-12 17:20:11 +01:00
|
|
|
$mapping->reverse = false;
|
|
|
|
|
2013-03-27 15:52:25 +01:00
|
|
|
if(property_exists($value, '@reverse')) {
|
2013-06-18 19:26:27 +02:00
|
|
|
if(property_exists($value, '@id')) {
|
2013-03-27 15:52:25 +01:00
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; a @reverse term definition must not ' +
|
2013-09-14 19:42:15 +02:00
|
|
|
'contain @id.', 'jsonld.SyntaxError', 'invalid reverse property',
|
|
|
|
array('context' => $local_ctx));
|
2013-03-27 15:52:25 +01:00
|
|
|
}
|
|
|
|
$reverse = $value->{'@reverse'};
|
|
|
|
if(!is_string($reverse)) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; a @context @reverse value must be a string.',
|
2013-09-14 19:42:15 +02:00
|
|
|
'jsonld.SyntaxError', 'invalid IRI mapping',
|
|
|
|
array('context' => $local_ctx));
|
2013-03-27 15:52:25 +01:00
|
|
|
}
|
|
|
|
|
2013-06-18 19:26:27 +02:00
|
|
|
// expand and add @id mapping
|
2013-09-14 19:42:15 +02:00
|
|
|
$id = $this->_expandIri(
|
2013-05-03 16:41:27 +02:00
|
|
|
$active_ctx, $reverse, array('vocab' => true, 'base' => false),
|
2013-03-27 15:52:25 +01:00
|
|
|
$local_ctx, $defined);
|
2013-09-14 19:42:15 +02:00
|
|
|
if(!self::_isAbsoluteIri($id)) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; @context @reverse value must be ' .
|
|
|
|
'an absolute IRI or a blank node identifier.',
|
|
|
|
'jsonld.SyntaxError', 'invalid IRI mapping',
|
|
|
|
array('context' => $local_ctx));
|
|
|
|
}
|
|
|
|
$mapping->{'@id'} = $id;
|
2013-03-27 15:52:25 +01:00
|
|
|
$mapping->reverse = true;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(property_exists($value, '@id')) {
|
2012-04-23 18:57:06 +02:00
|
|
|
$id = $value->{'@id'};
|
2013-03-06 20:34:03 +01:00
|
|
|
if(!is_string($id)) {
|
2012-04-23 18:57:06 +02:00
|
|
|
throw new JsonLdException(
|
2013-04-03 22:31:48 +02:00
|
|
|
'Invalid JSON-LD syntax; @context @id value must be a string.',
|
2013-09-14 19:42:15 +02:00
|
|
|
'jsonld.SyntaxError', 'invalid IRI mapping',
|
|
|
|
array('context' => $local_ctx));
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2013-04-15 16:46:22 +02:00
|
|
|
if($id !== $term) {
|
|
|
|
// add @id to mapping
|
2013-09-14 19:42:15 +02:00
|
|
|
$id = $this->_expandIri(
|
2013-05-03 16:41:27 +02:00
|
|
|
$active_ctx, $id, array('vocab' => true, 'base' => false),
|
2013-04-15 16:46:22 +02:00
|
|
|
$local_ctx, $defined);
|
2013-09-14 19:42:15 +02:00
|
|
|
if(!self::_isAbsoluteIri($id) && !self::_isKeyword($id)) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; @context @id value must be an ' .
|
|
|
|
'absolute IRI, a blank node identifier, or a keyword.',
|
|
|
|
'jsonld.SyntaxError', 'invalid IRI mapping',
|
2013-09-14 19:43:50 +02:00
|
|
|
array('context' => $local_ctx));
|
2013-09-14 19:42:15 +02:00
|
|
|
}
|
|
|
|
$mapping->{'@id'} = $id;
|
2013-04-15 16:46:22 +02:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2013-04-15 16:46:22 +02:00
|
|
|
|
|
|
|
if(!property_exists($mapping, '@id')) {
|
2013-02-12 21:10:45 +01:00
|
|
|
// see if the term has a prefix
|
|
|
|
$colon = strpos($term, ':');
|
|
|
|
if($colon !== false) {
|
|
|
|
$prefix = substr($term, 0, $colon);
|
|
|
|
if(property_exists($local_ctx, $prefix)) {
|
|
|
|
// define parent prefix
|
|
|
|
$this->_createTermDefinition(
|
|
|
|
$active_ctx, $local_ctx, $prefix, $defined);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(property_exists($active_ctx->mappings, $prefix) &&
|
|
|
|
$active_ctx->mappings->{$prefix}) {
|
2014-04-24 20:39:30 +02:00
|
|
|
// set @id based on prefix parent
|
2013-02-12 21:10:45 +01:00
|
|
|
$suffix = substr($term, $colon + 1);
|
|
|
|
$mapping->{'@id'} = $active_ctx->mappings->{$prefix}->{'@id'} .
|
|
|
|
$suffix;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
|
|
|
// term is an absolute IRI
|
2013-02-12 21:10:45 +01:00
|
|
|
$mapping->{'@id'} = $term;
|
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-12 21:10:45 +01:00
|
|
|
// non-IRIs *must* define @ids if @vocab is not available
|
2013-02-07 23:19:19 +01:00
|
|
|
if(!property_exists($active_ctx, '@vocab')) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; @context terms must define an @id.',
|
2013-09-14 19:42:15 +02:00
|
|
|
'jsonld.SyntaxError', 'invalid IRI mapping',
|
2013-02-07 23:19:19 +01:00
|
|
|
array('context' => $local_ctx, 'term' => $term));
|
|
|
|
}
|
|
|
|
// prepend vocab to term
|
|
|
|
$mapping->{'@id'} = $active_ctx->{'@vocab'} . $term;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-16 17:11:09 +02:00
|
|
|
// IRI mapping now defined
|
|
|
|
$defined->{$term} = true;
|
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
if(property_exists($value, '@type')) {
|
|
|
|
$type = $value->{'@type'};
|
|
|
|
if(!is_string($type)) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; @context @type values must be strings.',
|
2013-09-14 19:42:15 +02:00
|
|
|
'jsonld.SyntaxError', 'invalid type mapping',
|
|
|
|
array('context' => $local_ctx));
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-09-14 19:42:15 +02:00
|
|
|
if($type !== '@id' && $type !== '@vocab') {
|
2012-04-23 18:57:06 +02:00
|
|
|
// expand @type to full IRI
|
2013-02-07 23:19:19 +01:00
|
|
|
$type = $this->_expandIri(
|
2013-09-14 20:14:26 +02:00
|
|
|
$active_ctx, $type, array('vocab' => true), $local_ctx, $defined);
|
2013-09-14 19:42:15 +02:00
|
|
|
if(!self::_isAbsoluteIri($type)) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; an @context @type value must ' .
|
|
|
|
'be an absolute IRI.', 'jsonld.SyntaxError',
|
|
|
|
'invalid type mapping', array('context' => $local_ctx));
|
|
|
|
}
|
|
|
|
if(strpos($type, '_:') === 0) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; an @context @type values must ' .
|
|
|
|
'be an IRI, not a blank node identifier.',
|
|
|
|
'jsonld.SyntaxError', 'invalid type mapping',
|
|
|
|
array('context' => $local_ctx));
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// add @type to mapping
|
|
|
|
$mapping->{'@type'} = $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(property_exists($value, '@container')) {
|
|
|
|
$container = $value->{'@container'};
|
2013-02-07 23:19:19 +01:00
|
|
|
if($container !== '@list' && $container !== '@set' &&
|
|
|
|
$container !== '@index' && $container !== '@language') {
|
2012-04-23 18:57:06 +02:00
|
|
|
throw new JsonLdException(
|
2013-02-07 23:19:19 +01:00
|
|
|
'Invalid JSON-LD syntax; @context @container value must be ' .
|
|
|
|
'one of the following: @list, @set, @index, or @language.',
|
2013-09-14 19:42:15 +02:00
|
|
|
'jsonld.SyntaxError', 'invalid container mapping',
|
|
|
|
array('context' => $local_ctx));
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2013-06-18 19:26:27 +02:00
|
|
|
if($mapping->reverse && $container !== '@index' &&
|
|
|
|
$container !== '@set' && $container !== null) {
|
2013-03-27 15:52:25 +01:00
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; @context @container value for a @reverse ' +
|
2013-06-18 19:26:27 +02:00
|
|
|
'type definition must be @index or @set.',
|
2013-09-14 19:42:15 +02:00
|
|
|
'jsonld.SyntaxError', 'invalid reverse property',
|
|
|
|
array('context' => $local_ctx));
|
2013-03-12 17:20:11 +01:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
|
|
|
// add @container to mapping
|
|
|
|
$mapping->{'@container'} = $container;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(property_exists($value, '@language') &&
|
|
|
|
!property_exists($value, '@type')) {
|
|
|
|
$language = $value->{'@language'};
|
|
|
|
if($language !== null && !is_string($language)) {
|
|
|
|
throw new JsonLdException(
|
2013-02-07 23:19:19 +01:00
|
|
|
'Invalid JSON-LD syntax; @context @language value must be ' .
|
2013-09-14 19:42:15 +02:00
|
|
|
'a string or null.', 'jsonld.SyntaxError',
|
|
|
|
'invalid language mapping', array('context' => $local_ctx));
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// add @language to mapping
|
2013-02-07 23:19:19 +01:00
|
|
|
if($language !== null) {
|
|
|
|
$language = strtolower($language);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
$mapping->{'@language'} = $language;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-04-15 17:09:06 +02:00
|
|
|
// disallow aliasing @context and @preserve
|
|
|
|
$id = $mapping->{'@id'};
|
|
|
|
if($id === '@context' || $id === '@preserve') {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Invalid JSON-LD syntax; @context and @preserve cannot be aliased.',
|
2013-09-14 19:42:15 +02:00
|
|
|
'jsonld.SyntaxError', 'invalid keyword alias',
|
|
|
|
array('context' => $local_ctx));
|
2013-04-15 17:09:06 +02:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-07 23:19:19 +01:00
|
|
|
* 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
|
|
|
|
* returned.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @param stdClass $active_ctx the current active context.
|
2013-02-07 23:19:19 +01:00
|
|
|
* @param string $value the string to expand.
|
|
|
|
* @param assoc $relative_to options for how to resolve relative IRIs:
|
|
|
|
* 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).
|
|
|
|
* @param defined a map for tracking cycles in context definitions (only given
|
|
|
|
* if called during document processing).
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return mixed the expanded value.
|
|
|
|
*/
|
2013-02-07 23:19:19 +01:00
|
|
|
function _expandIri(
|
|
|
|
$active_ctx, $value, $relative_to=array(), $local_ctx=null, $defined=null) {
|
2013-02-26 06:36:53 +01:00
|
|
|
// already expanded
|
|
|
|
if($value === null || self::_isKeyword($value)) {
|
|
|
|
return $value;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// define term dependency if not defined
|
|
|
|
if($local_ctx !== null && property_exists($local_ctx, $value) &&
|
|
|
|
!self::_hasKeyValue($defined, $value, true)) {
|
|
|
|
$this->_createTermDefinition($active_ctx, $local_ctx, $value, $defined);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-02-14 02:08:53 +01:00
|
|
|
if(isset($relative_to['vocab']) && $relative_to['vocab']) {
|
|
|
|
if(property_exists($active_ctx->mappings, $value)) {
|
|
|
|
$mapping = $active_ctx->mappings->{$value};
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-02-14 02:08:53 +01:00
|
|
|
// value is explicitly ignored with a null mapping
|
|
|
|
if($mapping === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-02-14 02:08:53 +01:00
|
|
|
// value is a term
|
2013-03-27 15:52:25 +01:00
|
|
|
return $mapping->{'@id'};
|
2013-02-14 02:08:53 +01:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-03-27 15:52:25 +01:00
|
|
|
// split value into prefix:suffix
|
|
|
|
$colon = strpos($value, ':');
|
|
|
|
if($colon !== false) {
|
|
|
|
$prefix = substr($value, 0, $colon);
|
|
|
|
$suffix = substr($value, $colon + 1);
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-03-27 15:52:25 +01:00
|
|
|
// do not expand blank nodes (prefix of '_') or already-absolute
|
|
|
|
// IRIs (suffix of '//')
|
2013-04-04 17:25:46 +02:00
|
|
|
if($prefix === '_' || strpos($suffix, '//') === 0) {
|
|
|
|
return $value;
|
|
|
|
}
|
2013-03-27 15:52:25 +01:00
|
|
|
|
2013-04-04 17:25:46 +02:00
|
|
|
// prefix dependency not defined, define it
|
|
|
|
if($local_ctx !== null && property_exists($local_ctx, $prefix)) {
|
|
|
|
$this->_createTermDefinition(
|
|
|
|
$active_ctx, $local_ctx, $prefix, $defined);
|
|
|
|
}
|
|
|
|
|
|
|
|
// use mapping if prefix is defined
|
|
|
|
if(property_exists($active_ctx->mappings, $prefix)) {
|
|
|
|
$mapping = $active_ctx->mappings->{$prefix};
|
|
|
|
if($mapping) {
|
|
|
|
return $mapping->{'@id'} . $suffix;
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-03-27 16:08:11 +01:00
|
|
|
// already absolute IRI
|
2013-03-27 15:52:25 +01:00
|
|
|
return $value;
|
2013-02-14 02:08:53 +01:00
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// prepend vocab
|
2013-03-27 15:52:25 +01:00
|
|
|
if(isset($relative_to['vocab']) && $relative_to['vocab'] &&
|
2013-02-07 23:19:19 +01:00
|
|
|
property_exists($active_ctx, '@vocab')) {
|
2013-03-27 16:08:11 +01:00
|
|
|
return $active_ctx->{'@vocab'} . $value;
|
2012-09-26 22:36:51 +02:00
|
|
|
}
|
2013-03-27 16:08:11 +01:00
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// prepend base
|
2013-03-27 16:08:11 +01:00
|
|
|
$rval = $value;
|
|
|
|
if(isset($relative_to['base']) && $relative_to['base']) {
|
2013-02-07 23:19:19 +01:00
|
|
|
$rval = jsonld_prepend_base($active_ctx->{'@base'}, $rval);
|
2012-09-26 22:36:51 +02:00
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
return $rval;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-02-15 22:25:30 +01:00
|
|
|
/**
|
|
|
|
* Finds all @context URLs in the given JSON-LD input.
|
|
|
|
*
|
|
|
|
* @param mixed $input the JSON-LD input.
|
|
|
|
* @param stdClass $urls a map of URLs (url => false/@contexts).
|
|
|
|
* @param bool $replace true to replace the URLs in the given input with
|
2012-10-09 19:17:25 +02:00
|
|
|
* the @contexts from the urls map, false not to.
|
2013-02-15 22:25:30 +01:00
|
|
|
* @param string $base the base URL to resolve relative URLs with.
|
|
|
|
*/
|
2013-02-08 21:48:23 +01:00
|
|
|
protected function _findContextUrls($input, $urls, $replace, $base) {
|
2013-02-15 22:25:30 +01:00
|
|
|
if(is_array($input)) {
|
|
|
|
foreach($input as $e) {
|
|
|
|
$this->_findContextUrls($e, $urls, $replace, $base);
|
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(is_object($input)) {
|
2013-02-15 22:25:30 +01:00
|
|
|
foreach($input as $k => &$v) {
|
|
|
|
if($k !== '@context') {
|
|
|
|
$this->_findContextUrls($v, $urls, $replace, $base);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// array @context
|
|
|
|
if(is_array($v)) {
|
|
|
|
$length = count($v);
|
|
|
|
for($i = 0; $i < $length; ++$i) {
|
|
|
|
if(is_string($v[$i])) {
|
2013-02-07 23:19:19 +01:00
|
|
|
$url = jsonld_prepend_base($base, $v[$i]);
|
2013-02-15 22:25:30 +01:00
|
|
|
// replace w/@context if requested
|
|
|
|
if($replace) {
|
|
|
|
$ctx = $urls->{$url};
|
|
|
|
if(is_array($ctx)) {
|
2012-05-07 23:12:10 +02:00
|
|
|
// add flattened context
|
2013-02-15 22:25:30 +01:00
|
|
|
array_splice($v, $i, 1, $ctx);
|
2014-12-03 20:56:23 +01:00
|
|
|
$i += count($ctx) - 1;
|
|
|
|
$length = count($v);
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-15 22:25:30 +01:00
|
|
|
$v[$i] = $ctx;
|
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(!property_exists($urls, $url)) {
|
|
|
|
// @context URL found
|
2013-02-15 22:25:30 +01:00
|
|
|
$urls->{$url} = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(is_string($v)) {
|
|
|
|
// string @context
|
2013-02-15 22:25:30 +01:00
|
|
|
$v = jsonld_prepend_base($base, $v);
|
|
|
|
// replace w/@context if requested
|
|
|
|
if($replace) {
|
|
|
|
$input->{$k} = $urls->{$v};
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(!property_exists($urls, $v)) {
|
|
|
|
// @context URL found
|
2013-02-15 22:25:30 +01:00
|
|
|
$urls->{$v} = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-15 18:30:28 +02:00
|
|
|
* Retrieves external @context URLs using the given document loader. Each
|
2013-02-15 22:25:30 +01:00
|
|
|
* instance of @context in the input that refers to a URL will be replaced
|
|
|
|
* with the JSON @context found at that URL.
|
|
|
|
*
|
2012-05-07 23:12:10 +02:00
|
|
|
* @param mixed $input the JSON-LD input with possible contexts.
|
|
|
|
* @param stdClass $cycles an object for tracking context cycles.
|
2013-07-15 18:30:28 +02:00
|
|
|
* @param callable $load_document(url) the document loader.
|
2012-10-09 19:17:25 +02:00
|
|
|
* @param base $base the base URL to resolve relative URLs against.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return mixed the result.
|
2013-02-15 22:25:30 +01:00
|
|
|
*/
|
2013-02-15 21:34:21 +01:00
|
|
|
protected function _retrieveContextUrls(
|
2013-07-15 18:30:28 +02:00
|
|
|
&$input, $cycles, $load_document, $base='') {
|
2013-02-15 22:25:30 +01:00
|
|
|
if(count(get_object_vars($cycles)) > self::MAX_CONTEXT_URLS) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Maximum number of @context URLs exceeded.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.ContextUrlError', 'loading remote context failed',
|
|
|
|
array('max' => self::MAX_CONTEXT_URLS));
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// for tracking the URLs to retrieve
|
2012-04-23 18:57:06 +02:00
|
|
|
$urls = new stdClass();
|
|
|
|
|
2013-09-15 03:27:55 +02:00
|
|
|
// regex for validating URLs
|
|
|
|
$regex = '/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/';
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// find all URLs in the given input
|
2012-10-09 19:17:25 +02:00
|
|
|
$this->_findContextUrls($input, $urls, false, $base);
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-02-15 22:25:30 +01:00
|
|
|
// queue all unretrieved URLs
|
|
|
|
$queue = array();
|
|
|
|
foreach($urls as $url => $ctx) {
|
2012-05-07 23:12:10 +02:00
|
|
|
if($ctx === false) {
|
2013-09-15 03:27:55 +02:00
|
|
|
// validate URL
|
|
|
|
if(!preg_match($regex, $url)) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Malformed or unsupported URL.', 'jsonld.InvalidUrl',
|
|
|
|
'loading remote context failed', array('url' => $url));
|
|
|
|
}
|
2012-05-07 23:12:10 +02:00
|
|
|
$queue[] = $url;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2012-05-07 23:12:10 +02:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-02-15 22:25:30 +01:00
|
|
|
// retrieve URLs in queue
|
2012-05-07 23:12:10 +02:00
|
|
|
foreach($queue as $url) {
|
2013-02-15 22:25:30 +01:00
|
|
|
// check for context URL cycle
|
|
|
|
if(property_exists($cycles, $url)) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Cyclical @context URLs detected.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.ContextUrlError', 'recursive context inclusion',
|
|
|
|
array('url' => $url));
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
|
|
|
$_cycles = self::copy($cycles);
|
2012-05-07 23:12:10 +02:00
|
|
|
$_cycles->{$url} = true;
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-02-15 22:25:30 +01:00
|
|
|
// retrieve URL
|
2013-08-09 20:08:31 +02:00
|
|
|
$remote_doc = call_user_func($load_document, $url);
|
2013-07-15 18:30:28 +02:00
|
|
|
$ctx = $remote_doc->document;
|
2012-04-23 18:57:06 +02:00
|
|
|
|
2013-02-15 22:25:30 +01:00
|
|
|
// parse string context as JSON
|
|
|
|
if(is_string($ctx)) {
|
2013-09-14 20:12:19 +02:00
|
|
|
try {
|
2013-09-15 04:02:37 +02:00
|
|
|
$ctx = self::_parse_json($ctx);
|
2014-04-24 20:39:30 +02:00
|
|
|
} catch(Exception $e) {
|
2013-09-14 20:12:19 +02:00
|
|
|
throw new JsonLdException(
|
|
|
|
'Could not parse JSON from URL.',
|
2013-09-15 03:27:55 +02:00
|
|
|
'jsonld.ParseError', 'loading remote context failed',
|
2013-09-14 20:12:19 +02:00
|
|
|
array('url' => $url), $e);
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure ctx is an object
|
|
|
|
if(!is_object($ctx)) {
|
|
|
|
throw new JsonLdException(
|
|
|
|
'Derefencing a URL did not result in a valid JSON-LD object.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.InvalidUrl', 'invalid remote context', array('url' => $url));
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// use empty context if no @context key is present
|
|
|
|
if(!property_exists($ctx, '@context')) {
|
|
|
|
$ctx = (object)array('@context' => new stdClass());
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-07-15 18:30:28 +02:00
|
|
|
$ctx = (object)array('@context' => $ctx->{'@context'});
|
|
|
|
}
|
|
|
|
|
|
|
|
// append context URL to context if given
|
|
|
|
if($remote_doc->contextUrl !== null) {
|
|
|
|
$ctx->{'@context'} = self::arrayify($ctx->{'@context'});
|
|
|
|
$ctx->{'@context'}[] = $remote_doc->contextUrl;
|
|
|
|
}
|
2013-02-15 22:25:30 +01:00
|
|
|
|
|
|
|
// recurse
|
2013-07-15 18:30:28 +02:00
|
|
|
$this->_retrieveContextUrls($ctx, $_cycles, $load_document, $url);
|
2013-02-15 22:25:30 +01:00
|
|
|
$urls->{$url} = $ctx->{'@context'};
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-02-15 22:25:30 +01:00
|
|
|
// replace all URLS in the input
|
|
|
|
$this->_findContextUrls($input, $urls, true, $base);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the initial context.
|
|
|
|
*
|
2013-02-07 23:19:19 +01:00
|
|
|
* @param assoc $options the options to use.
|
|
|
|
* base the document base IRI.
|
|
|
|
*
|
2012-04-23 18:57:06 +02:00
|
|
|
* @return stdClass the initial context.
|
|
|
|
*/
|
2013-02-07 23:19:19 +01:00
|
|
|
protected function _getInitialContext($options) {
|
2012-04-23 18:57:06 +02:00
|
|
|
return (object)array(
|
2013-02-07 23:19:19 +01:00
|
|
|
'@base' => jsonld_parse_url($options['base']),
|
2012-04-23 18:57:06 +02:00
|
|
|
'mappings' => new stdClass(),
|
2013-02-07 23:19:19 +01:00
|
|
|
'inverse' => null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates an inverse context for use in the compaction algorithm, if
|
|
|
|
* not already generated for the given active context.
|
|
|
|
*
|
|
|
|
* @param stdClass $active_ctx the active context to use.
|
|
|
|
*
|
|
|
|
* @return stdClass the inverse context.
|
|
|
|
*/
|
|
|
|
protected function _getInverseContext($active_ctx) {
|
2013-04-03 23:33:17 +02:00
|
|
|
// inverse context already generated
|
|
|
|
if($active_ctx->inverse) {
|
|
|
|
return $active_ctx->inverse;
|
|
|
|
}
|
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
$inverse = $active_ctx->inverse = new stdClass();
|
|
|
|
|
|
|
|
// handle default language
|
|
|
|
$default_language = '@none';
|
|
|
|
if(property_exists($active_ctx, '@language')) {
|
2013-02-15 22:25:30 +01:00
|
|
|
$default_language = $active_ctx->{'@language'};
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// create term selections for each mapping in the context, ordered by
|
|
|
|
// shortest and then lexicographically least
|
|
|
|
$mappings = $active_ctx->mappings;
|
|
|
|
$terms = array_keys((array)$mappings);
|
|
|
|
usort($terms, array($this, '_compareShortestLeast'));
|
|
|
|
foreach($terms as $term) {
|
|
|
|
$mapping = $mappings->{$term};
|
|
|
|
if($mapping === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-02-15 22:25:30 +01:00
|
|
|
// add term selection where it applies
|
|
|
|
if(property_exists($mapping, '@container')) {
|
|
|
|
$container = $mapping->{'@container'};
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2013-02-15 22:25:30 +01:00
|
|
|
$container = '@none';
|
|
|
|
}
|
2013-02-11 22:24:26 +01:00
|
|
|
|
2013-02-07 23:19:19 +01:00
|
|
|
// iterate over every IRI in the mapping
|
|
|
|
$iris = $mapping->{'@id'};
|
|
|
|
$iris = self::arrayify($iris);
|
|
|
|
foreach($iris as $iri) {
|
|
|
|
// initialize container map
|
|
|
|
if(!property_exists($inverse, $iri)) {
|
|
|
|
$inverse->{$iri} = new stdClass();
|
|
|
|
}
|
|
|
|
$container_map = $inverse->{$iri};
|
|
|
|
|
|
|
|
// add new entry
|
|
|
|
if(!property_exists($container_map, $container)) {
|
|
|
|
$container_map->{$container} = (object)array(
|
|
|
|
'@language' => new stdClass(),
|
|
|
|
'@type' => new stdClass());
|
|
|
|
}
|
|
|
|
$entry = $container_map->{$container};
|
|
|
|
|
2013-03-27 15:52:25 +01:00
|
|
|
if($mapping->reverse) {
|
2014-04-24 20:39:30 +02:00
|
|
|
// term is preferred for values using @reverse
|
2013-03-12 17:20:11 +01:00
|
|
|
$this->_addPreferredTerm(
|
2013-03-27 15:52:25 +01:00
|
|
|
$mapping, $term, $entry->{'@type'}, '@reverse');
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(property_exists($mapping, '@type')) {
|
|
|
|
// term is preferred for values using specific type
|
2013-03-12 17:20:11 +01:00
|
|
|
$this->_addPreferredTerm(
|
2013-03-27 15:52:25 +01:00
|
|
|
$mapping, $term, $entry->{'@type'}, $mapping->{'@type'});
|
2014-04-24 20:39:30 +02:00
|
|
|
} else if(property_exists($mapping, '@language')) {
|
|
|
|
// term is preferred for values using specific language
|
2013-03-12 17:20:11 +01:00
|
|
|
$language = $mapping->{'@language'};
|
2013-03-27 15:52:25 +01:00
|
|
|
if($language === null) {
|
|
|
|
$language = '@null';
|
|
|
|
}
|
2013-03-12 17:20:11 +01:00
|
|
|
$this->_addPreferredTerm(
|
2013-03-27 15:52:25 +01:00
|
|
|
$mapping, $term, $entry->{'@language'}, $language);
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
|
|
|
// term is preferred for values w/default language or no type and
|
|
|
|
// no language
|
2013-03-27 15:52:25 +01:00
|
|
|
// add an entry for the default language
|
2013-03-12 17:20:11 +01:00
|
|
|
$this->_addPreferredTerm(
|
2013-03-27 15:52:25 +01:00
|
|
|
$mapping, $term, $entry->{'@language'}, $default_language);
|
|
|
|
|
|
|
|
// add entries for no type and no language
|
2013-03-12 17:20:11 +01:00
|
|
|
$this->_addPreferredTerm(
|
2013-03-27 15:52:25 +01:00
|
|
|
$mapping, $term, $entry->{'@type'}, '@none');
|
2013-03-12 17:20:11 +01:00
|
|
|
$this->_addPreferredTerm(
|
2013-03-27 15:52:25 +01:00
|
|
|
$mapping, $term, $entry->{'@language'}, '@none');
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $inverse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-06 20:34:03 +01:00
|
|
|
* Adds the term for the given entry if not already added.
|
2013-02-07 23:19:19 +01:00
|
|
|
*
|
|
|
|
* @param stdClass $mapping the term mapping.
|
|
|
|
* @param string $term the term to add.
|
|
|
|
* @param stdClass $entry the inverse context type_or_language entry to
|
|
|
|
* add to.
|
|
|
|
* @param string $type_or_language_value the key in the entry to add to.
|
|
|
|
*/
|
|
|
|
function _addPreferredTerm($mapping, $term, $entry, $type_or_language_value) {
|
|
|
|
if(!property_exists($entry, $type_or_language_value)) {
|
2013-03-06 20:34:03 +01:00
|
|
|
$entry->{$type_or_language_value} = $term;
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clones an active context, creating a child active context.
|
|
|
|
*
|
|
|
|
* @return stdClass a clone (child) of the active context.
|
|
|
|
*/
|
|
|
|
protected function _cloneActiveContext($active_ctx) {
|
|
|
|
$child = new stdClass();
|
|
|
|
$child->{'@base'} = $active_ctx->{'@base'};
|
|
|
|
$child->mappings = self::copy($active_ctx->mappings);
|
|
|
|
$child->inverse = null;
|
2013-02-21 01:32:50 +01:00
|
|
|
if(property_exists($active_ctx, '@language')) {
|
|
|
|
$child->{'@language'} = $active_ctx->{'@language'};
|
|
|
|
}
|
|
|
|
if(property_exists($active_ctx, '@vocab')) {
|
|
|
|
$child->{'@vocab'} = $active_ctx->{'@vocab'};
|
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
return $child;
|
|
|
|
}
|
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
/**
|
2013-02-07 23:19:19 +01:00
|
|
|
* Returns whether or not the given value is a keyword.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
2012-04-27 22:18:05 +02:00
|
|
|
* @param string $v the value to check.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return bool true if the value is a keyword, false if not.
|
|
|
|
*/
|
2013-02-07 23:19:19 +01:00
|
|
|
protected static function _isKeyword($v) {
|
|
|
|
if(!is_string($v)) {
|
|
|
|
return false;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
switch($v) {
|
2013-03-05 03:28:24 +01:00
|
|
|
case '@base':
|
2013-02-07 23:19:19 +01:00
|
|
|
case '@context':
|
|
|
|
case '@container':
|
|
|
|
case '@default':
|
|
|
|
case '@embed':
|
|
|
|
case '@explicit':
|
|
|
|
case '@graph':
|
|
|
|
case '@id':
|
|
|
|
case '@index':
|
|
|
|
case '@language':
|
|
|
|
case '@list':
|
|
|
|
case '@omitDefault':
|
|
|
|
case '@preserve':
|
2014-07-29 19:40:31 +02:00
|
|
|
case '@requireAll':
|
2013-03-11 22:04:24 +01:00
|
|
|
case '@reverse':
|
2013-02-07 23:19:19 +01:00
|
|
|
case '@set':
|
|
|
|
case '@type':
|
|
|
|
case '@value':
|
|
|
|
case '@vocab':
|
|
|
|
return true;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-27 22:18:05 +02:00
|
|
|
* Returns true if the given value is an empty Object.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
2012-04-27 22:18:05 +02:00
|
|
|
* @param mixed $v the value to check.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
2012-04-27 22:18:05 +02:00
|
|
|
* @return bool true if the value is an empty Object, false if not.
|
2012-04-23 18:57:06 +02:00
|
|
|
*/
|
2012-04-27 22:18:05 +02:00
|
|
|
protected static function _isEmptyObject($v) {
|
|
|
|
return is_object($v) && count(get_object_vars($v)) === 0;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-27 22:18:05 +02:00
|
|
|
* Throws an exception if the given value is not a valid @type value.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
2012-04-27 22:18:05 +02:00
|
|
|
* @param mixed $v the value to check.
|
2012-04-23 18:57:06 +02:00
|
|
|
*/
|
2012-04-27 22:18:05 +02:00
|
|
|
protected static function _validateTypeValue($v) {
|
2013-02-07 23:19:19 +01:00
|
|
|
// must be a string or empty object
|
|
|
|
if(is_string($v) || self::_isEmptyObject($v)) {
|
2012-04-27 22:18:05 +02:00
|
|
|
return;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2012-04-27 22:18:05 +02:00
|
|
|
|
|
|
|
// must be an array
|
|
|
|
$is_valid = false;
|
|
|
|
if(is_array($v)) {
|
2013-02-07 23:19:19 +01:00
|
|
|
// must contain only strings
|
2012-04-27 22:18:05 +02:00
|
|
|
$is_valid = true;
|
|
|
|
foreach($v as $e) {
|
2013-02-07 23:19:19 +01:00
|
|
|
if(!(is_string($e))) {
|
2012-04-27 22:18:05 +02:00
|
|
|
$is_valid = false;
|
|
|
|
break;
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
2012-04-27 22:18:05 +02:00
|
|
|
|
|
|
|
if(!$is_valid) {
|
|
|
|
throw new JsonLdException(
|
2013-02-07 23:19:19 +01:00
|
|
|
'Invalid JSON-LD syntax; "@type" value must a string, an array ' .
|
2012-04-27 22:18:05 +02:00
|
|
|
'of strings, or an empty object.',
|
2013-09-14 19:58:40 +02:00
|
|
|
'jsonld.SyntaxError', 'invalid type value', array('value' => $v));
|
2012-04-27 22:18:05 +02:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the given value is a subject with properties.
|
|
|
|
*
|
2012-04-27 22:18:05 +02:00
|
|
|
* @param mixed $v the value to check.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return bool true if the value is a subject with properties, false if not.
|
|
|
|
*/
|
2012-04-27 22:18:05 +02:00
|
|
|
protected static function _isSubject($v) {
|
2012-04-23 18:57:06 +02:00
|
|
|
// Note: A value is a subject if all of these hold true:
|
|
|
|
// 1. It is an Object.
|
|
|
|
// 2. It is not a @value, @set, or @list.
|
|
|
|
// 3. It has more than 1 key OR any existing key is not @id.
|
2012-04-27 22:18:05 +02:00
|
|
|
$rval = false;
|
|
|
|
if(is_object($v) &&
|
|
|
|
!property_exists($v, '@value') &&
|
|
|
|
!property_exists($v, '@set') &&
|
|
|
|
!property_exists($v, '@list')) {
|
|
|
|
$count = count(get_object_vars($v));
|
|
|
|
$rval = ($count > 1 || !property_exists($v, '@id'));
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the given value is a subject reference.
|
|
|
|
*
|
2012-04-27 22:18:05 +02:00
|
|
|
* @param mixed $v the value to check.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return bool true if the value is a subject reference, false if not.
|
|
|
|
*/
|
2012-04-27 22:18:05 +02:00
|
|
|
protected static function _isSubjectReference($v) {
|
2012-04-23 18:57:06 +02:00
|
|
|
// Note: A value is a subject reference if all of these hold true:
|
|
|
|
// 1. It is an Object.
|
|
|
|
// 2. It has a single key: @id.
|
2012-04-27 22:18:05 +02:00
|
|
|
return (is_object($v) && count(get_object_vars($v)) === 1 &&
|
|
|
|
property_exists($v, '@id'));
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the given value is a @value.
|
|
|
|
*
|
2012-04-27 22:18:05 +02:00
|
|
|
* @param mixed $v the value to check.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return bool true if the value is a @value, false if not.
|
|
|
|
*/
|
2012-04-27 22:18:05 +02:00
|
|
|
protected static function _isValue($v) {
|
2012-04-23 18:57:06 +02:00
|
|
|
// Note: A value is a @value if all of these hold true:
|
|
|
|
// 1. It is an Object.
|
|
|
|
// 2. It has the @value property.
|
2012-04-27 22:18:05 +02:00
|
|
|
return is_object($v) && property_exists($v, '@value');
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the given value is a @list.
|
|
|
|
*
|
2012-04-27 22:18:05 +02:00
|
|
|
* @param mixed $v the value to check.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return bool true if the value is a @list, false if not.
|
|
|
|
*/
|
2012-04-27 22:18:05 +02:00
|
|
|
protected static function _isList($v) {
|
2012-04-23 18:57:06 +02:00
|
|
|
// Note: A value is a @list if all of these hold true:
|
|
|
|
// 1. It is an Object.
|
|
|
|
// 2. It has the @list property.
|
2012-04-27 22:18:05 +02:00
|
|
|
return is_object($v) && property_exists($v, '@list');
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the given value is a blank node.
|
|
|
|
*
|
2012-04-27 22:18:05 +02:00
|
|
|
* @param mixed $v the value to check.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return bool true if the value is a blank node, false if not.
|
|
|
|
*/
|
2012-04-27 22:18:05 +02:00
|
|
|
protected static function _isBlankNode($v) {
|
2012-04-23 18:57:06 +02:00
|
|
|
// 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.
|
2012-04-27 22:18:05 +02:00
|
|
|
$rval = false;
|
|
|
|
if(is_object($v)) {
|
|
|
|
if(property_exists($v, '@id')) {
|
|
|
|
$rval = (strpos($v->{'@id'}, '_:') === 0);
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2012-04-27 22:18:05 +02:00
|
|
|
$rval = (count(get_object_vars($v)) === 0 ||
|
|
|
|
!(property_exists($v, '@value') ||
|
|
|
|
property_exists($v, '@set') ||
|
|
|
|
property_exists($v, '@list')));
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the given value is an absolute IRI, false if not.
|
|
|
|
*
|
2012-04-27 22:18:05 +02:00
|
|
|
* @param string $v the value to check.
|
2012-04-23 18:57:06 +02:00
|
|
|
*
|
|
|
|
* @return bool true if the value is an absolute IRI, false if not.
|
|
|
|
*/
|
2012-04-27 22:18:05 +02:00
|
|
|
protected static function _isAbsoluteIri($v) {
|
|
|
|
return strpos($v, ':') !== false;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2013-09-14 20:12:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2013-09-15 21:54:54 +02:00
|
|
|
$error = json_last_error();
|
|
|
|
if($error === JSON_ERROR_NONE && $rval === null) {
|
|
|
|
$error = JSON_ERROR_SYNTAX;
|
|
|
|
}
|
|
|
|
switch($error) {
|
2013-09-14 20:12:19 +02:00
|
|
|
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;
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
|
2013-02-15 22:25:30 +01:00
|
|
|
// register the N-Quads RDF parser
|
2012-05-09 18:28:26 +02:00
|
|
|
jsonld_register_rdf_parser(
|
2013-07-15 18:30:28 +02:00
|
|
|
'application/nquads', array('JsonLdProcessor', 'parseNQuads'));
|
2012-05-09 18:28:26 +02:00
|
|
|
|
2011-11-29 04:51:30 +01:00
|
|
|
/**
|
2012-04-23 18:57:06 +02:00
|
|
|
* A JSON-LD Exception.
|
2011-11-29 04:51:30 +01:00
|
|
|
*/
|
2012-04-23 18:57:06 +02:00
|
|
|
class JsonLdException extends Exception {
|
2013-09-13 19:07:18 +02:00
|
|
|
public function __construct(
|
2013-09-13 22:42:10 +02:00
|
|
|
$msg, $type, $code='error', $details=null, $previous=null) {
|
2012-04-23 18:57:06 +02:00
|
|
|
$this->type = $type;
|
2013-09-13 22:42:10 +02:00
|
|
|
$this->code = $code;
|
2012-04-23 18:57:06 +02:00
|
|
|
$this->details = $details;
|
|
|
|
$this->cause = $previous;
|
|
|
|
parent::__construct($msg, 0, $previous);
|
|
|
|
}
|
|
|
|
public function __toString() {
|
|
|
|
$rval = __CLASS__ . ": [{$this->type}]: {$this->message}\n";
|
2013-09-15 21:54:54 +02:00
|
|
|
if($this->code) {
|
|
|
|
$rval .= 'Code: ' . $this->code . "\n";
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
if($this->details) {
|
2012-05-07 23:12:10 +02:00
|
|
|
$rval .= 'Details: ' . print_r($this->details, true) . "\n";
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
if($this->cause) {
|
2012-05-07 23:12:10 +02:00
|
|
|
$rval .= 'Cause: ' . $this->cause;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
$rval .= $this->getTraceAsString() . "\n";
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
};
|
2011-11-29 04:51:30 +01:00
|
|
|
|
2011-10-14 21:56:47 +02:00
|
|
|
/**
|
2012-04-23 18:57:06 +02:00
|
|
|
* A UniqueNamer issues unique names, keeping track of any previously issued
|
|
|
|
* names.
|
2011-10-14 21:56:47 +02:00
|
|
|
*/
|
2012-04-23 18:57:06 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clones this UniqueNamer.
|
|
|
|
*/
|
|
|
|
public function __clone() {
|
|
|
|
$this->existing = clone $this->existing;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the new name for the given old name, where if no old name is given
|
|
|
|
* a new name will be generated.
|
|
|
|
*
|
|
|
|
* @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)) {
|
2012-04-23 20:01:56 +02:00
|
|
|
return $this->existing->{$old_name};
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// get next name
|
|
|
|
$name = $this->prefix . $this->counter;
|
|
|
|
$this->counter += 1;
|
|
|
|
|
|
|
|
// save mapping
|
|
|
|
if($old_name !== null) {
|
2012-04-23 20:01:56 +02:00
|
|
|
$this->existing->{$old_name} = $name;
|
2012-04-23 21:00:13 +02:00
|
|
|
$this->order[] = $old_name;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
*
|
2012-04-23 20:01:56 +02:00
|
|
|
* @return true if the old name has been assigned a new name, false if not.
|
2012-04-23 18:57:06 +02:00
|
|
|
*/
|
|
|
|
public function isNamed($old_name) {
|
|
|
|
return property_exists($this->existing, $old_name);
|
|
|
|
}
|
2011-10-14 21:56:47 +02:00
|
|
|
}
|
|
|
|
|
2011-07-14 06:47:40 +02:00
|
|
|
/**
|
2012-04-23 18:57:06 +02:00
|
|
|
* A Permutator iterates over all possible permutations of the given array
|
|
|
|
* of elements.
|
2011-07-14 06:47:40 +02:00
|
|
|
*/
|
2012-04-23 18:57:06 +02:00
|
|
|
class Permutator {
|
|
|
|
/**
|
|
|
|
* Constructs a new Permutator.
|
|
|
|
*
|
|
|
|
* @param array $list the array of elements to iterate over.
|
|
|
|
*/
|
2012-04-23 20:01:56 +02:00
|
|
|
public function __construct($list) {
|
2012-04-23 18:57:06 +02:00
|
|
|
// 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.
|
|
|
|
*
|
|
|
|
* @return bool true if there is another permutation, false if not.
|
|
|
|
*/
|
|
|
|
public function hasNext() {
|
|
|
|
return !$this->done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the next permutation. Call hasNext() to ensure there is another one
|
|
|
|
* first.
|
|
|
|
*
|
|
|
|
* @return array the next permutation.
|
|
|
|
*/
|
|
|
|
public function next() {
|
|
|
|
// copy current permutation
|
|
|
|
$rval = $this->list;
|
|
|
|
|
|
|
|
/* Calculate the next permutation using the Steinhaus-Johnson-Trotter
|
|
|
|
permutation algorithm. */
|
|
|
|
|
|
|
|
// get largest mobile element k
|
|
|
|
// (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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no more permutations
|
|
|
|
if($k === null) {
|
|
|
|
$this->done = true;
|
2014-04-24 20:39:30 +02:00
|
|
|
} else {
|
2012-04-23 18:57:06 +02:00
|
|
|
// 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;
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
|
2013-02-15 22:25:30 +01:00
|
|
|
/**
|
2013-02-07 23:19:19 +01:00
|
|
|
* An ActiveContextCache caches active contexts so they can be reused without
|
2013-02-15 22:25:30 +01:00
|
|
|
* the overhead of recomputing them.
|
|
|
|
*/
|
|
|
|
class ActiveContextCache {
|
|
|
|
/**
|
|
|
|
* Constructs a new ActiveContextCache.
|
|
|
|
*
|
|
|
|
* @param int size the maximum size of the cache, defaults to 100.
|
|
|
|
*/
|
|
|
|
public function __construct($size=100) {
|
|
|
|
$this->order = array();
|
|
|
|
$this->cache = new stdClass();
|
|
|
|
$this->size = $size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-07 23:19:19 +01:00
|
|
|
* Gets an active context from the cache based on the current active
|
2013-02-15 22:25:30 +01:00
|
|
|
* context and the new local context.
|
|
|
|
*
|
2013-02-07 23:19:19 +01:00
|
|
|
* @param stdClass $active_ctx the current active context.
|
2013-02-15 22:25:30 +01:00
|
|
|
* @param stdClass $local_ctx the new local context.
|
|
|
|
*
|
|
|
|
* @return mixed a shared copy of the cached active context or null.
|
|
|
|
*/
|
2013-02-07 23:19:19 +01:00
|
|
|
public function get($active_ctx, $local_ctx) {
|
2013-02-15 22:25:30 +01:00
|
|
|
$key1 = serialize($active_ctx);
|
2013-02-07 23:19:19 +01:00
|
|
|
$key2 = serialize($local_ctx);
|
|
|
|
if(property_exists($this->cache, $key1)) {
|
2013-02-15 22:25:30 +01:00
|
|
|
$level1 = $this->cache->{$key1};
|
2013-02-08 06:15:45 +01:00
|
|
|
if(property_exists($level1, $key2)) {
|
2013-04-04 04:10:54 +02:00
|
|
|
return $level1->{$key2};
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
|
|
|
return null;
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
|
|
|
|
2013-02-15 22:25:30 +01:00
|
|
|
/**
|
|
|
|
* Sets an active context in the cache based on the previous active
|
|
|
|
* context and the just-processed local context.
|
|
|
|
*
|
|
|
|
* @param stdClass $active_ctx the previous active context.
|
2013-02-07 23:19:19 +01:00
|
|
|
* @param stdClass $local_ctx the just-processed local context.
|
2013-02-15 22:25:30 +01:00
|
|
|
* @param stdClass $result the resulting active context.
|
|
|
|
*/
|
2013-02-07 23:19:19 +01:00
|
|
|
public function set($active_ctx, $local_ctx, $result) {
|
2013-02-15 22:25:30 +01:00
|
|
|
if(count($this->order) === $this->size) {
|
|
|
|
$entry = array_shift($this->order);
|
|
|
|
unset($this->cache->{$entry->activeCtx}->{$entry->localCtx});
|
|
|
|
}
|
|
|
|
$key1 = serialize($active_ctx);
|
|
|
|
$key2 = serialize($local_ctx);
|
2013-02-07 23:19:19 +01:00
|
|
|
$this->order[] = (object)array(
|
|
|
|
'activeCtx' => $key1, 'localCtx' => $key2);
|
2013-02-08 06:15:45 +01:00
|
|
|
if(!property_exists($this->cache, $key1)) {
|
2013-02-07 23:19:19 +01:00
|
|
|
$this->cache->{$key1} = new stdClass();
|
|
|
|
}
|
2013-09-19 22:34:53 +02:00
|
|
|
$this->cache->{$key1}->{$key2} = JsonLdProcessor::copy($result);
|
2013-02-15 22:25:30 +01:00
|
|
|
}
|
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
/* end of file, omit ?> */
|