2011-07-14 06:47:40 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PHP implementation of JSON-LD.
|
|
|
|
*
|
|
|
|
* @author Dave Longley
|
|
|
|
*
|
2012-01-12 01:32:30 +01:00
|
|
|
* Copyright (c) 2011-2012 Digital Bazaar, Inc. All rights reserved.
|
2011-07-14 06:47:40 +02:00
|
|
|
*/
|
|
|
|
define('JSONLD_XSD', 'http://www.w3.org/2001/XMLSchema#');
|
|
|
|
define('JSONLD_XSD_BOOLEAN', JSONLD_XSD . 'boolean');
|
|
|
|
define('JSONLD_XSD_DOUBLE', JSONLD_XSD . 'double');
|
|
|
|
define('JSONLD_XSD_INTEGER', JSONLD_XSD . 'integer');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalizes a JSON-LD object.
|
|
|
|
*
|
|
|
|
* @param input the JSON-LD object to normalize.
|
|
|
|
*
|
|
|
|
* @return the normalized JSON-LD object.
|
|
|
|
*/
|
|
|
|
function jsonld_normalize($input)
|
|
|
|
{
|
|
|
|
$p = new JsonLdProcessor();
|
|
|
|
return $p->normalize($input);
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
|
|
|
|
/**
|
2011-08-04 22:33:26 +02:00
|
|
|
* Removes the context from a JSON-LD object, expanding it to full-form.
|
2011-07-14 06:47:40 +02:00
|
|
|
*
|
|
|
|
* @param input the JSON-LD object to remove the context from.
|
|
|
|
*
|
|
|
|
* @return the context-neutral JSON-LD object.
|
|
|
|
*/
|
2011-08-04 22:33:26 +02:00
|
|
|
function jsonld_expand($input)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-08-22 07:53:12 +02:00
|
|
|
$p = new JsonLdProcessor();
|
2011-12-20 03:29:11 +01:00
|
|
|
return $p->expand(new stdClass(), null, $input);
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
|
|
|
|
/**
|
2011-08-04 22:33:26 +02:00
|
|
|
* Expands the given JSON-LD object and then compacts it using the
|
|
|
|
* given context.
|
2011-07-14 06:47:40 +02:00
|
|
|
*
|
|
|
|
* @param ctx the new context to use.
|
2011-08-04 22:33:26 +02:00
|
|
|
* @param input the input JSON-LD object.
|
2011-07-14 06:47:40 +02:00
|
|
|
*
|
2011-08-04 22:33:26 +02:00
|
|
|
* @return the output JSON-LD object.
|
2011-07-14 06:47:40 +02:00
|
|
|
*/
|
2011-08-04 22:33:26 +02:00
|
|
|
function jsonld_compact($ctx, $input)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-08-04 22:33:26 +02:00
|
|
|
$rval = null;
|
2011-07-14 06:47:40 +02:00
|
|
|
|
|
|
|
// TODO: should context simplification be optional? (ie: remove context
|
|
|
|
// entries that are not used in the output)
|
|
|
|
|
2011-08-04 22:33:26 +02:00
|
|
|
if($input !== null)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-08-04 22:33:26 +02:00
|
|
|
// fully expand input
|
|
|
|
$input = jsonld_expand($input);
|
|
|
|
|
2011-10-28 01:00:01 +02:00
|
|
|
// merge context if it is an array
|
|
|
|
if(is_array($ctx))
|
|
|
|
{
|
|
|
|
$ctx = jsonld_merge_contexts(new stdClass, $ctx);
|
|
|
|
}
|
|
|
|
|
2012-01-12 04:56:26 +01:00
|
|
|
// setup output context
|
|
|
|
$ctxOut = new stdClass();
|
2011-08-04 22:33:26 +02:00
|
|
|
|
2012-01-12 04:56:26 +01:00
|
|
|
// compact
|
|
|
|
$p = new JsonLdProcessor();
|
|
|
|
$rval = $out = $p->compact(_clone($ctx), null, $input, $ctxOut);
|
2011-08-04 22:33:26 +02:00
|
|
|
|
2012-01-12 04:56:26 +01:00
|
|
|
// add context if used
|
|
|
|
if(count(array_keys((array)$ctxOut)) > 0)
|
|
|
|
{
|
|
|
|
$rval = new stdClass();
|
|
|
|
$rval->{'@context'} = $ctxOut;
|
|
|
|
if(is_array($out))
|
2011-08-04 22:33:26 +02:00
|
|
|
{
|
2012-01-12 04:56:26 +01:00
|
|
|
$rval->{_getKeywords($ctxOut)->{'@id'}} = $out;
|
2011-08-04 22:33:26 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-12 04:56:26 +01:00
|
|
|
foreach($out as $k => $v)
|
|
|
|
{
|
|
|
|
$rval->{$k} = $v;
|
|
|
|
}
|
2011-08-04 22:33:26 +02:00
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merges one context with another.
|
|
|
|
*
|
|
|
|
* @param ctx1 the context to overwrite/append to.
|
|
|
|
* @param ctx2 the new context to merge onto ctx1.
|
|
|
|
*
|
|
|
|
* @return the merged context.
|
|
|
|
*/
|
|
|
|
function jsonld_merge_contexts($ctx1, $ctx2)
|
|
|
|
{
|
2011-10-28 01:00:01 +02:00
|
|
|
// merge first context if it is an array
|
|
|
|
if(is_array($ctx1))
|
|
|
|
{
|
|
|
|
$ctx1 = jsonld_merge_contexts($ctx1, $ctx2);
|
|
|
|
}
|
|
|
|
|
2011-10-20 22:46:20 +02:00
|
|
|
// copy context to merged output
|
2011-08-02 07:49:14 +02:00
|
|
|
$merged = _clone($ctx1);
|
2011-07-14 06:47:40 +02:00
|
|
|
|
2011-10-28 01:00:01 +02:00
|
|
|
if(is_array($ctx2))
|
|
|
|
{
|
|
|
|
// merge array of contexts in order
|
|
|
|
foreach($ctx2 as $ctx)
|
|
|
|
{
|
|
|
|
$merged = jsonld_merge_contexts($merged, $ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-10-28 01:00:01 +02:00
|
|
|
// if the new context contains any IRIs that are in the merged context,
|
|
|
|
// remove them from the merged context, they will be overwritten
|
|
|
|
foreach($ctx2 as $key => $value)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-10-28 01:00:01 +02:00
|
|
|
// ignore special keys starting with '@'
|
|
|
|
if(strpos($key, '@') !== 0)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-10-28 01:00:01 +02:00
|
|
|
foreach($merged as $mkey => $mvalue)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-10-28 01:00:01 +02:00
|
|
|
if($mvalue === $value)
|
|
|
|
{
|
2011-12-18 20:43:10 +01:00
|
|
|
// FIXME: update related coerce rules
|
2011-10-28 01:00:01 +02:00
|
|
|
unset($merged->$mkey);
|
|
|
|
break;
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-28 01:00:01 +02:00
|
|
|
// merge contexts
|
|
|
|
foreach($ctx2 as $key => $value)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-12-18 20:43:10 +01:00
|
|
|
$merged->$key = _clone($value);
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $merged;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expands a term into an absolute IRI. The term may be a regular term, a
|
2011-12-18 20:43:10 +01:00
|
|
|
* prefix, a relative IRI, or an absolute IRI. In any case, the associated
|
2011-07-14 06:47:40 +02:00
|
|
|
* absolute IRI will be returned.
|
|
|
|
*
|
|
|
|
* @param ctx the context to use.
|
|
|
|
* @param term the term to expand.
|
|
|
|
*
|
|
|
|
* @return the expanded term as an absolute IRI.
|
|
|
|
*/
|
|
|
|
function jsonld_expand_term($ctx, $term)
|
|
|
|
{
|
|
|
|
return _expandTerm($ctx, $term);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-18 20:43:10 +01:00
|
|
|
* Compacts an IRI into a term or prefix if it can be. IRIs will not be
|
2011-07-14 06:47:40 +02:00
|
|
|
* compacted to relative IRIs if they match the given context's default
|
|
|
|
* vocabulary.
|
|
|
|
*
|
|
|
|
* @param ctx the context to use.
|
|
|
|
* @param iri the IRI to compact.
|
|
|
|
*
|
2011-12-18 20:43:10 +01:00
|
|
|
* @return the compacted IRI as a term or prefix or the original IRI.
|
2011-07-14 06:47:40 +02:00
|
|
|
*/
|
|
|
|
function jsonld_compact_iri($ctx, $iri)
|
|
|
|
{
|
|
|
|
return _compactIri($ctx, $iri, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Frames JSON-LD input.
|
|
|
|
*
|
|
|
|
* @param input the JSON-LD input.
|
|
|
|
* @param frame the frame to use.
|
|
|
|
* @param options framing options to use.
|
|
|
|
*
|
|
|
|
* @return the framed output.
|
|
|
|
*/
|
|
|
|
function jsonld_frame($input, $frame, $options=null)
|
|
|
|
{
|
|
|
|
$rval;
|
|
|
|
|
|
|
|
// normalize input
|
|
|
|
$input = jsonld_normalize($input);
|
|
|
|
|
|
|
|
// save frame context
|
|
|
|
$ctx = null;
|
2011-11-24 02:24:38 +01:00
|
|
|
if(is_object($frame) and property_exists($frame, '@context'))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-08-04 22:33:26 +02:00
|
|
|
$ctx = _clone($frame->{'@context'});
|
2011-11-09 18:46:03 +01:00
|
|
|
|
|
|
|
// remove context from frame
|
|
|
|
$frame = jsonld_expand($frame);
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
2011-11-09 18:46:03 +01:00
|
|
|
else if(is_array($frame))
|
|
|
|
{
|
|
|
|
// save first context in the array
|
|
|
|
if(count($frame) > 0 and property_exists($frame[0], '@context'))
|
|
|
|
{
|
|
|
|
$ctx = _clone($frame[0]->{'@context'});
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
|
2011-11-09 18:46:03 +01:00
|
|
|
// expand all elements in the array
|
|
|
|
$tmp = array();
|
|
|
|
foreach($frame as $f)
|
|
|
|
{
|
|
|
|
$tmp[] = jsonld_expand($f);
|
|
|
|
}
|
|
|
|
$frame = $tmp;
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
|
|
|
|
// create framing options
|
|
|
|
// TODO: merge in options from function parameter
|
|
|
|
$options = new stdClass();
|
|
|
|
$options->defaults = new stdClass();
|
|
|
|
$options->defaults->embedOn = true;
|
|
|
|
$options->defaults->explicitOn = false;
|
2011-07-29 20:28:46 +02:00
|
|
|
$options->defaults->omitDefaultOn = false;
|
2011-07-14 06:47:40 +02:00
|
|
|
|
|
|
|
// build map of all subjects
|
|
|
|
$subjects = new stdClass();
|
|
|
|
foreach($input as $i)
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$subjects->{$i->{'@id'}} = $i;
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// frame input
|
2011-10-14 21:56:47 +02:00
|
|
|
$rval = _frame(
|
|
|
|
$subjects, $input, $frame, new stdClass(), false, null, null, $options);
|
2011-07-14 06:47:40 +02:00
|
|
|
|
|
|
|
// apply context
|
|
|
|
if($ctx !== null and $rval !== null)
|
|
|
|
{
|
2012-01-12 04:56:26 +01:00
|
|
|
// preserve top-level array by compacting individual entries
|
|
|
|
if(is_array($rval))
|
|
|
|
{
|
|
|
|
$tmp = $rval;
|
|
|
|
$rval = array();
|
|
|
|
foreach($tmp as $value)
|
|
|
|
{
|
|
|
|
$rval[] = jsonld_compact($ctx, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$rval = jsonld_compact($ctx, $rval);
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
2011-10-28 01:00:01 +02:00
|
|
|
/**
|
|
|
|
* Resolves external @context URLs. Every @context URL in the given JSON-LD
|
|
|
|
* object is resolved using the given URL-resolver function. Once all of
|
|
|
|
* the @contexts have been resolved, the method will return. If an error
|
|
|
|
* is encountered, an exception will be thrown.
|
|
|
|
*
|
|
|
|
* @param input the JSON-LD input object (or array).
|
|
|
|
* @param resolver the resolver method that takes a URL and returns a JSON-LD
|
|
|
|
* serialized @context or throws an exception.
|
|
|
|
*
|
|
|
|
* @return the fully-resolved JSON-LD output (object or array).
|
|
|
|
*/
|
|
|
|
function jsonld_resolve($input, $resolver)
|
|
|
|
{
|
|
|
|
// find all @context URLs
|
|
|
|
$urls = new ArrayObject();
|
|
|
|
_findUrls($input, $urls, false);
|
|
|
|
|
|
|
|
// resolve all URLs
|
|
|
|
foreach($urls as $url => $value)
|
|
|
|
{
|
|
|
|
$result = call_user_func($resolver, $url);
|
|
|
|
if(!is_string($result))
|
|
|
|
{
|
|
|
|
// already deserialized
|
2011-10-28 22:52:05 +02:00
|
|
|
$urls[$url] = $result->{'@context'};
|
2011-10-28 01:00:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// deserialize JSON
|
|
|
|
$tmp = json_decode($result);
|
|
|
|
if($tmp === null)
|
|
|
|
{
|
|
|
|
throw new Exception(
|
|
|
|
'Could not resolve @context URL ("$url"), ' .
|
|
|
|
'malformed JSON detected.');
|
|
|
|
}
|
2011-10-28 22:52:05 +02:00
|
|
|
$urls[$url] = $tmp->{'@context'};
|
2011-10-28 01:00:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace @context URLs in input
|
|
|
|
_findUrls($input, $urls, true);
|
|
|
|
|
|
|
|
return $input;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds all of the @context URLs in the given input and replaces them
|
|
|
|
* if requested by their associated values in the given URL map.
|
|
|
|
*
|
|
|
|
* @param input the JSON-LD input object.
|
|
|
|
* @param urls the URLs ArrayObject.
|
|
|
|
* @param replace true to replace, false not to.
|
|
|
|
*/
|
|
|
|
function _findUrls($input, $urls, $replace)
|
|
|
|
{
|
|
|
|
if(is_array($input))
|
|
|
|
{
|
|
|
|
foreach($input as $v)
|
|
|
|
{
|
|
|
|
_findUrls($v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(is_object($input))
|
|
|
|
{
|
|
|
|
foreach($input as $key => $value)
|
|
|
|
{
|
|
|
|
if($key === '@context')
|
|
|
|
{
|
|
|
|
// @context is an array that might contain URLs
|
|
|
|
if(is_array($value))
|
|
|
|
{
|
|
|
|
foreach($value as $idx => $v)
|
|
|
|
{
|
|
|
|
if(is_string($v))
|
|
|
|
{
|
|
|
|
// replace w/resolved @context if appropriate
|
|
|
|
if($replace)
|
|
|
|
{
|
|
|
|
$input->{$key}[$idx] = $urls[$v];
|
|
|
|
}
|
|
|
|
// unresolved @context found
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$urls[$v] = new stdClass();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(is_string($value))
|
|
|
|
{
|
|
|
|
// replace w/resolved @context if appropriate
|
|
|
|
if($replace)
|
|
|
|
{
|
|
|
|
$input->$key = $urls[$value];
|
|
|
|
}
|
|
|
|
// unresolved @context found
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$urls[$value] = new stdClass();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-14 06:47:40 +02:00
|
|
|
/**
|
2011-08-22 07:53:12 +02:00
|
|
|
* Gets the keywords from a context.
|
2011-07-14 06:47:40 +02:00
|
|
|
*
|
2011-08-22 07:53:12 +02:00
|
|
|
* @param ctx the context.
|
2011-07-14 06:47:40 +02:00
|
|
|
*
|
2011-08-22 07:53:12 +02:00
|
|
|
* @return the keywords.
|
2011-07-14 06:47:40 +02:00
|
|
|
*/
|
2011-08-22 07:53:12 +02:00
|
|
|
function _getKeywords($ctx)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-08-22 07:53:12 +02:00
|
|
|
// TODO: reduce calls to this function by caching keywords in processor
|
|
|
|
// state
|
|
|
|
|
|
|
|
$rval = (object)array(
|
2011-12-20 03:29:11 +01:00
|
|
|
'@id' => '@id',
|
2011-08-22 07:53:12 +02:00
|
|
|
'@language' => '@language',
|
2012-01-12 04:56:26 +01:00
|
|
|
'@value' => '@value',
|
2011-08-22 07:53:12 +02:00
|
|
|
'@type' => '@type'
|
|
|
|
);
|
|
|
|
|
|
|
|
if($ctx)
|
|
|
|
{
|
|
|
|
// gather keyword aliases from context
|
|
|
|
$keywords = new stdClass();
|
2011-07-14 06:47:40 +02:00
|
|
|
foreach($ctx as $key => $value)
|
|
|
|
{
|
2011-08-22 07:53:12 +02:00
|
|
|
if(is_string($value) and property_exists($rval, $value))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-08-22 07:53:12 +02:00
|
|
|
$keywords->{$value} = $key;
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-22 07:53:12 +02:00
|
|
|
// overwrite keywords
|
|
|
|
foreach($keywords as $key => $value)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-08-22 07:53:12 +02:00
|
|
|
$rval->$key = $value;
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a subject's property to the given object value. If a value already
|
|
|
|
* exists, it will be appended to an array.
|
|
|
|
*
|
|
|
|
* @param s the subject.
|
|
|
|
* @param p the property.
|
|
|
|
* @param o the object.
|
|
|
|
*/
|
|
|
|
function _setProperty($s, $p, $o)
|
|
|
|
{
|
2011-07-29 20:28:46 +02:00
|
|
|
if(property_exists($s, $p))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
if(is_array($s->$p))
|
|
|
|
{
|
|
|
|
array_push($s->$p, $o);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$s->$p = array($s->$p, $o);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$s->$p = $o;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-08-02 07:49:14 +02:00
|
|
|
* Clones an object, array, or string/number. If cloning an object, the keys
|
|
|
|
* will be sorted.
|
2011-07-14 06:47:40 +02:00
|
|
|
*
|
|
|
|
* @param value the value to clone.
|
|
|
|
*
|
|
|
|
* @return the cloned value.
|
|
|
|
*/
|
|
|
|
function _clone($value)
|
|
|
|
{
|
|
|
|
$rval;
|
|
|
|
|
|
|
|
if(is_object($value))
|
|
|
|
{
|
|
|
|
$rval = new stdClass();
|
|
|
|
$keys = array_keys((array)$value);
|
|
|
|
sort($keys);
|
|
|
|
foreach($keys as $key)
|
|
|
|
{
|
2011-08-02 07:49:14 +02:00
|
|
|
$rval->$key = _clone($value->$key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(is_array($value))
|
|
|
|
{
|
|
|
|
$rval = array();
|
|
|
|
foreach($value as $v)
|
|
|
|
{
|
|
|
|
$rval[] = _clone($v);
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$rval = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-18 20:43:10 +01:00
|
|
|
* Gets the iri associated with a term.
|
|
|
|
*
|
|
|
|
* @param ctx the context.
|
|
|
|
* @param term the term.
|
|
|
|
*
|
|
|
|
* @return the iri or NULL.
|
|
|
|
*/
|
|
|
|
function _getTermIri($ctx, $term)
|
|
|
|
{
|
|
|
|
$rval = null;
|
|
|
|
if(property_exists($ctx, $term))
|
|
|
|
{
|
|
|
|
if(is_string($ctx->$term))
|
|
|
|
{
|
|
|
|
$rval = $ctx->$term;
|
|
|
|
}
|
2011-12-20 03:29:11 +01:00
|
|
|
else if(is_object($ctx->$term) and property_exists($ctx->$term, '@id'))
|
2011-12-18 20:43:10 +01:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$rval = $ctx->$term->{'@id'};
|
2011-12-18 20:43:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compacts an IRI into a term or prefix if it can be. IRIs will not be
|
2011-08-22 07:53:12 +02:00
|
|
|
* compacted to relative IRIs if they match the given context's default
|
|
|
|
* vocabulary.
|
2011-07-14 06:47:40 +02:00
|
|
|
*
|
|
|
|
* @param ctx the context to use.
|
2011-08-22 07:53:12 +02:00
|
|
|
* @param iri the IRI to compact.
|
2011-07-14 06:47:40 +02:00
|
|
|
* @param usedCtx a context to update if a value was used from "ctx".
|
|
|
|
*
|
2011-12-18 20:43:10 +01:00
|
|
|
* @return the compacted IRI as a term or prefix or the original IRI.
|
2011-07-14 06:47:40 +02:00
|
|
|
*/
|
2011-08-22 07:53:12 +02:00
|
|
|
function _compactIri($ctx, $iri, $usedCtx)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
$rval = null;
|
|
|
|
|
2011-08-22 07:53:12 +02:00
|
|
|
// check the context for a term that could shorten the IRI
|
2011-12-18 20:43:10 +01:00
|
|
|
// (give preference to terms over prefixes)
|
2011-08-22 07:53:12 +02:00
|
|
|
foreach($ctx as $key => $value)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-08-22 07:53:12 +02:00
|
|
|
// skip special context keys (start with '@')
|
|
|
|
if(strlen($key) > 0 and $key[0] !== '@')
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-08-22 07:53:12 +02:00
|
|
|
// compact to a term
|
2011-12-18 20:43:10 +01:00
|
|
|
if($iri === _getTermIri($ctx, $key))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-08-22 07:53:12 +02:00
|
|
|
$rval = $key;
|
|
|
|
if($usedCtx !== null)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-12-18 20:43:10 +01:00
|
|
|
$usedCtx->$key = _clone($ctx->$key);
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
2011-08-22 07:53:12 +02:00
|
|
|
break;
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-12 19:41:23 +01:00
|
|
|
// term not found, if term is keyword, use alias
|
|
|
|
if($rval === null)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2012-01-12 19:41:23 +01:00
|
|
|
$keywords = _getKeywords($ctx);
|
|
|
|
if(property_exists($keywords, $iri))
|
|
|
|
{
|
|
|
|
$rval = $keywords->{$iri};
|
|
|
|
if($rval !== $iri and $usedCtx !== null)
|
|
|
|
{
|
|
|
|
$usedCtx->$rval = $iri;
|
|
|
|
}
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
|
2011-12-18 20:43:10 +01:00
|
|
|
// term not found, check the context for a prefix
|
2011-08-22 07:53:12 +02:00
|
|
|
if($rval === null)
|
|
|
|
{
|
|
|
|
foreach($ctx as $key => $value)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-08-22 07:53:12 +02:00
|
|
|
// skip special context keys (start with '@')
|
|
|
|
if(strlen($key) > 0 and $key[0] !== '@')
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-08-22 07:53:12 +02:00
|
|
|
// see if IRI begins with the next IRI from the context
|
2011-12-18 20:43:10 +01:00
|
|
|
$ctxIri = _getTermIri($ctx, $key);
|
|
|
|
if($ctxIri !== null)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-12-18 20:43:10 +01:00
|
|
|
$idx = strpos($iri, $ctxIri);
|
|
|
|
|
|
|
|
// compact to a prefix
|
|
|
|
if($idx === 0 and strlen($iri) > strlen($ctxIri))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-12-18 20:43:10 +01:00
|
|
|
$rval = $key . ':' . substr($iri, strlen($ctxIri));
|
|
|
|
if($usedCtx !== null)
|
|
|
|
{
|
|
|
|
$usedCtx->$key = _clone($ctx->$key);
|
|
|
|
}
|
|
|
|
break;
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
|
2011-08-22 07:53:12 +02:00
|
|
|
// could not compact IRI
|
|
|
|
if($rval === null)
|
|
|
|
{
|
|
|
|
$rval = $iri;
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-08-22 07:53:12 +02:00
|
|
|
* Expands a term into an absolute IRI. The term may be a regular term, a
|
2011-12-18 20:43:10 +01:00
|
|
|
* prefix, a relative IRI, or an absolute IRI. In any case, the associated
|
2011-08-22 07:53:12 +02:00
|
|
|
* absolute IRI will be returned.
|
2011-07-14 06:47:40 +02:00
|
|
|
*
|
2011-08-22 07:53:12 +02:00
|
|
|
* @param ctx the context to use.
|
|
|
|
* @param term the term to expand.
|
|
|
|
* @param usedCtx a context to update if a value was used from "ctx".
|
2011-07-14 06:47:40 +02:00
|
|
|
*
|
2011-08-22 07:53:12 +02:00
|
|
|
* @return the expanded term as an absolute IRI.
|
2011-07-14 06:47:40 +02:00
|
|
|
*/
|
2011-08-22 07:53:12 +02:00
|
|
|
function _expandTerm($ctx, $term, $usedCtx)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$rval = $term;
|
2011-07-14 06:47:40 +02:00
|
|
|
|
2011-08-22 07:53:12 +02:00
|
|
|
// get JSON-LD keywords
|
|
|
|
$keywords = _getKeywords($ctx);
|
2011-07-14 06:47:40 +02:00
|
|
|
|
2011-12-18 20:43:10 +01:00
|
|
|
// 1. If the property has a colon, it is a prefix or an absolute IRI:
|
2011-08-22 07:53:12 +02:00
|
|
|
$idx = strpos($term, ':');
|
|
|
|
if($idx !== false)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-12-18 20:43:10 +01:00
|
|
|
// get the potential prefix
|
2011-08-22 07:53:12 +02:00
|
|
|
$prefix = substr($term, 0, $idx);
|
2011-07-14 06:47:40 +02:00
|
|
|
|
2011-12-20 03:29:11 +01:00
|
|
|
// expand term if prefix is in context, otherwise leave it be
|
2011-08-22 07:53:12 +02:00
|
|
|
if(property_exists($ctx, $prefix))
|
|
|
|
{
|
|
|
|
// prefix found, expand property to absolute IRI
|
2011-12-18 20:43:10 +01:00
|
|
|
$iri = _getTermIri($ctx, $prefix);
|
|
|
|
$rval = $iri . substr($term, $idx + 1);
|
2011-08-22 07:53:12 +02:00
|
|
|
if($usedCtx !== null)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-12-18 20:43:10 +01:00
|
|
|
$usedCtx->$prefix = _clone($ctx->$prefix);
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
|
|
|
// 2. If the property is in the context, then it's a term.
|
|
|
|
else if(property_exists($ctx, $term))
|
|
|
|
{
|
2011-12-18 20:43:10 +01:00
|
|
|
$rval = _getTermIri($ctx, $term);
|
2011-08-22 07:53:12 +02:00
|
|
|
if($usedCtx !== null)
|
|
|
|
{
|
2011-12-18 20:43:10 +01:00
|
|
|
$usedCtx->$term = _clone($ctx->$term);
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
2011-12-20 03:29:11 +01:00
|
|
|
// 3. The property is a keyword.
|
2011-07-14 06:47:40 +02:00
|
|
|
else
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
foreach($keywords as $key => $value)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
if($term === $value)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$rval = $key;
|
|
|
|
break;
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
2011-12-20 03:29:11 +01:00
|
|
|
/**
|
|
|
|
* Gets whether or not a value is a reference to a subject (or a subject with
|
|
|
|
* no properties).
|
|
|
|
*
|
|
|
|
* @param value the value to check.
|
|
|
|
*
|
|
|
|
* @return true if the value is a reference to a subject, false if not.
|
|
|
|
*/
|
|
|
|
function _isReference($value)
|
|
|
|
{
|
|
|
|
// Note: A value is a reference to a subject if all of these hold true:
|
|
|
|
// 1. It is an Object.
|
|
|
|
// 2. It is has an @id key.
|
|
|
|
// 3. It has only 1 key.
|
|
|
|
return ($value !== null and
|
|
|
|
is_object($value) and
|
|
|
|
property_exists($value, '@id') and
|
|
|
|
count(get_object_vars($value)) === 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets whether or not a value is a subject with properties.
|
|
|
|
*
|
|
|
|
* @param value the value to check.
|
|
|
|
*
|
|
|
|
* @return true if the value is a subject with properties, false if not.
|
|
|
|
*/
|
|
|
|
function _isSubject($value)
|
|
|
|
{
|
|
|
|
$rval = false;
|
|
|
|
|
|
|
|
// Note: A value is a subject if all of these hold true:
|
|
|
|
// 1. It is an Object.
|
2012-01-12 04:56:26 +01:00
|
|
|
// 2. It is not a literal (@value).
|
2011-12-20 03:29:11 +01:00
|
|
|
// 3. It has more than 1 key OR any existing key is not '@id'.
|
|
|
|
if($value !== null and is_object($value) and
|
2012-01-12 04:56:26 +01:00
|
|
|
!property_exists($value, '@value'))
|
2011-12-20 03:29:11 +01:00
|
|
|
{
|
|
|
|
$keyCount = count(get_object_vars($value));
|
|
|
|
$rval = ($keyCount > 1 or !property_exists($value, '@id'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
2011-07-14 06:47:40 +02:00
|
|
|
function _isBlankNodeIri($v)
|
|
|
|
{
|
|
|
|
return strpos($v, '_:') === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _isNamedBlankNode($v)
|
|
|
|
{
|
|
|
|
// look for "_:" at the beginning of the subject
|
|
|
|
return (
|
2011-12-20 03:29:11 +01:00
|
|
|
is_object($v) and property_exists($v, '@id') and
|
|
|
|
_isBlankNodeIri($v->{'@id'}));
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function _isBlankNode($v)
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
// look for a subject with no ID or a blank node ID
|
2011-07-14 06:47:40 +02:00
|
|
|
return (
|
2011-12-20 03:29:11 +01:00
|
|
|
_isSubject($v) and
|
|
|
|
(!property_exists($v, '@id') or _isNamedBlankNode($v)));
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares two values.
|
|
|
|
*
|
|
|
|
* @param v1 the first value.
|
|
|
|
* @param v2 the second value.
|
|
|
|
*
|
|
|
|
* @return -1 if v1 < v2, 0 if v1 == v2, 1 if v1 > v2.
|
|
|
|
*/
|
|
|
|
function _compare($v1, $v2)
|
|
|
|
{
|
|
|
|
$rval = 0;
|
|
|
|
|
|
|
|
if(is_array($v1) and is_array($v2))
|
|
|
|
{
|
|
|
|
$length = count($v1);
|
|
|
|
for($i = 0; $i < $length and $rval === 0; ++$i)
|
|
|
|
{
|
|
|
|
$rval = _compare($v1[$i], $v2[$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$rval = ($v1 < $v2 ? -1 : ($v1 > $v2 ? 1 : 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares two keys in an object. If the key exists in one object
|
2011-07-25 23:40:17 +02:00
|
|
|
* and not the other, the object with the key is less. If the key exists in
|
|
|
|
* both objects, then the one with the lesser value is less.
|
2011-07-14 06:47:40 +02:00
|
|
|
*
|
|
|
|
* @param o1 the first object.
|
|
|
|
* @param o2 the second object.
|
|
|
|
* @param key the key.
|
|
|
|
*
|
|
|
|
* @return -1 if o1 < o2, 0 if o1 == o2, 1 if o1 > o2.
|
|
|
|
*/
|
|
|
|
function _compareObjectKeys($o1, $o2, $key)
|
|
|
|
{
|
|
|
|
$rval = 0;
|
2011-07-29 20:28:46 +02:00
|
|
|
if(property_exists($o1, $key))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-07-29 20:28:46 +02:00
|
|
|
if(property_exists($o2, $key))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
$rval = _compare($o1->$key, $o2->$key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$rval = -1;
|
|
|
|
}
|
|
|
|
}
|
2011-07-29 20:28:46 +02:00
|
|
|
else if(property_exists($o2, $key))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
$rval = 1;
|
|
|
|
}
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares two object values.
|
|
|
|
*
|
|
|
|
* @param o1 the first object.
|
|
|
|
* @param o2 the second object.
|
|
|
|
*
|
|
|
|
* @return -1 if o1 < o2, 0 if o1 == o2, 1 if o1 > o2.
|
|
|
|
*/
|
|
|
|
function _compareObjects($o1, $o2)
|
|
|
|
{
|
|
|
|
$rval = 0;
|
|
|
|
|
|
|
|
if(is_string($o1))
|
|
|
|
{
|
|
|
|
if(!is_string($o2))
|
|
|
|
{
|
|
|
|
$rval = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$rval = _compare($o1, $o2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(is_string($o2))
|
|
|
|
{
|
|
|
|
$rval = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-12 04:56:26 +01:00
|
|
|
$rval = _compareObjectKeys($o1, $o2, '@value');
|
2011-07-14 06:47:40 +02:00
|
|
|
if($rval === 0)
|
|
|
|
{
|
2012-01-12 04:56:26 +01:00
|
|
|
if(property_exists($o1, '@value'))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-12-18 03:08:58 +01:00
|
|
|
$rval = _compareObjectKeys($o1, $o2, '@type');
|
2011-07-14 06:47:40 +02:00
|
|
|
if($rval === 0)
|
|
|
|
{
|
|
|
|
$rval = _compareObjectKeys($o1, $o2, '@language');
|
|
|
|
}
|
|
|
|
}
|
2011-12-20 03:29:11 +01:00
|
|
|
// both are '@id' objects
|
2011-07-14 06:47:40 +02:00
|
|
|
else
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$rval = _compare($o1->{'@id'}, $o2->{'@id'});
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter function for bnodes.
|
|
|
|
*
|
|
|
|
* @param e the array element.
|
|
|
|
*
|
|
|
|
* @return true to return the element in the filter results, false not to.
|
|
|
|
*/
|
|
|
|
function _filterBlankNodes($e)
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
return !_isNamedBlankNode($e);
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares the object values between two bnodes.
|
|
|
|
*
|
|
|
|
* @param a the first bnode.
|
|
|
|
* @param b the second bnode.
|
|
|
|
*
|
|
|
|
* @return -1 if a < b, 0 if a == b, 1 if a > b.
|
|
|
|
*/
|
|
|
|
function _compareBlankNodeObjects($a, $b)
|
|
|
|
{
|
|
|
|
$rval = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
3. For each property, compare sorted object values.
|
|
|
|
3.1. The bnode with fewer objects is first.
|
2012-01-12 04:56:26 +01:00
|
|
|
3.2. For each object value, compare only literals (@values) and non-bnodes.
|
2011-07-14 06:47:40 +02:00
|
|
|
3.2.1. The bnode with fewer non-bnodes is first.
|
|
|
|
3.2.2. The bnode with a string object is first.
|
|
|
|
3.2.3. The bnode with the alphabetically-first string is first.
|
2012-01-12 04:56:26 +01:00
|
|
|
3.2.4. The bnode with a @value is first.
|
|
|
|
3.2.5. The bnode with the alphabetically-first @value is first.
|
2011-12-18 03:08:58 +01:00
|
|
|
3.2.6. The bnode with the alphabetically-first @type is first.
|
2011-07-14 06:47:40 +02:00
|
|
|
3.2.7. The bnode with a @language is first.
|
|
|
|
3.2.8. The bnode with the alphabetically-first @language is first.
|
2011-12-20 03:29:11 +01:00
|
|
|
3.2.9. The bnode with the alphabetically-first @id is first.
|
2011-07-14 06:47:40 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
foreach($a as $p => $value)
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
// skip IDs (IRIs)
|
|
|
|
if($p !== '@id')
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
// step #3.1
|
|
|
|
$lenA = is_array($a->$p) ? count($a->$p) : 1;
|
|
|
|
$lenB = is_array($b->$p) ? count($b->$p) : 1;
|
|
|
|
$rval = _compare($lenA, $lenB);
|
|
|
|
|
|
|
|
// step #3.2.1
|
|
|
|
if($rval === 0)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
// normalize objects to an array
|
|
|
|
$objsA = $a->$p;
|
|
|
|
$objsB = $b->$p;
|
|
|
|
if(!is_array($objsA))
|
|
|
|
{
|
|
|
|
$objsA = array($objsA);
|
|
|
|
$objsB = array($objsB);
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
|
2011-12-20 03:29:11 +01:00
|
|
|
// compare non-bnodes (remove bnodes from comparison)
|
|
|
|
$objsA = array_filter($objsA, '_filterBlankNodes');
|
|
|
|
$objsB = array_filter($objsB, '_filterBlankNodes');
|
|
|
|
$objsALen = count($objsA);
|
|
|
|
$rval = _compare($objsALen, count($objsB));
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
|
2011-12-20 03:29:11 +01:00
|
|
|
// steps #3.2.2-3.2.9
|
|
|
|
if($rval === 0)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
usort($objsA, '_compareObjects');
|
|
|
|
usort($objsB, '_compareObjects');
|
|
|
|
for($i = 0; $i < $objsALen and $rval === 0; ++$i)
|
|
|
|
{
|
|
|
|
$rval = _compareObjects($objsA[$i], $objsB[$i]);
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
|
2011-12-20 03:29:11 +01:00
|
|
|
if($rval !== 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A blank node name generator that uses a prefix and counter.
|
|
|
|
*/
|
|
|
|
class NameGenerator
|
|
|
|
{
|
|
|
|
public function __construct($prefix)
|
|
|
|
{
|
|
|
|
$this->count = -1;
|
|
|
|
$this->base = '_:' . $prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function next()
|
|
|
|
{
|
|
|
|
$this->count += 1;
|
|
|
|
return $this->current();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function current()
|
|
|
|
{
|
|
|
|
return $this->base . $this->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function inNamespace($iri)
|
|
|
|
{
|
|
|
|
return strpos($iri, $this->base) === 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populates a map of all named subjects from the given input and an array
|
|
|
|
* of all unnamed bnodes (includes embedded ones).
|
|
|
|
*
|
|
|
|
* @param input the input (must be expanded, no context).
|
|
|
|
* @param subjects the subjects map to populate.
|
|
|
|
* @param bnodes the bnodes array to populate.
|
|
|
|
*/
|
|
|
|
function _collectSubjects($input, $subjects, $bnodes)
|
|
|
|
{
|
2011-07-29 17:46:23 +02:00
|
|
|
if($input === null)
|
|
|
|
{
|
|
|
|
// nothing to collect
|
|
|
|
}
|
|
|
|
else if(is_array($input))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
foreach($input as $value)
|
|
|
|
{
|
|
|
|
_collectSubjects($value, $subjects, $bnodes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(is_object($input))
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
if(property_exists($input, '@id'))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
// graph literal/disjoint graph
|
|
|
|
if(is_array($input->{'@id'}))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
_collectSubjects($input->{'@id'}, $subjects, $bnodes);
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
// named subject
|
2011-12-20 03:29:11 +01:00
|
|
|
else if(_isSubject($input))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$subjects->{$input->{'@id'}} = $input;
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// unnamed blank node
|
|
|
|
else if(_isBlankNode($input))
|
|
|
|
{
|
|
|
|
$bnodes[] = $input;
|
|
|
|
}
|
|
|
|
|
|
|
|
// recurse through subject properties
|
|
|
|
foreach($input as $value)
|
|
|
|
{
|
|
|
|
_collectSubjects($value, $subjects, $bnodes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters duplicate IRIs.
|
|
|
|
*/
|
|
|
|
class DuplicateIriFilter
|
|
|
|
{
|
|
|
|
public function __construct($iri)
|
|
|
|
{
|
|
|
|
$this->iri = $iri;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function filter($e)
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
return (is_object($e) and property_exists($e, '@id') and
|
|
|
|
$e->{'@id'} === $this->iri);
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flattens the given value into a map of unique subjects. It is assumed that
|
|
|
|
* all blank nodes have been uniquely named before this call. Array values for
|
|
|
|
* properties will be sorted.
|
|
|
|
*
|
|
|
|
* @param parent the value's parent, NULL for none.
|
|
|
|
* @param parentProperty the property relating the value to the parent.
|
|
|
|
* @param value the value to flatten.
|
|
|
|
* @param subjects the map of subjects to write to.
|
|
|
|
*/
|
|
|
|
function _flatten($parent, $parentProperty, $value, $subjects)
|
|
|
|
{
|
|
|
|
$flattened = null;
|
|
|
|
|
2011-07-29 22:17:51 +02:00
|
|
|
if($value === null)
|
|
|
|
{
|
|
|
|
// drop null values
|
|
|
|
}
|
|
|
|
else if(is_array($value))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
// list of objects or a disjoint graph
|
|
|
|
foreach($value as $v)
|
|
|
|
{
|
|
|
|
_flatten($parent, $parentProperty, $v, $subjects);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(is_object($value))
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
// already-expanded value or special-case reference-only @type
|
2012-01-12 04:56:26 +01:00
|
|
|
if(property_exists($value, '@value') or $parentProperty === '@type')
|
2011-12-20 03:29:11 +01:00
|
|
|
{
|
|
|
|
$flattened = _clone($value);
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
// graph literal/disjoint graph
|
2011-12-20 03:29:11 +01:00
|
|
|
else if(is_array($value->{'@id'}))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
// cannot flatten embedded graph literals
|
|
|
|
if($parent !== null)
|
|
|
|
{
|
|
|
|
throw new Exception('Embedded graph literals cannot be flattened.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// top-level graph literal
|
2011-12-20 03:29:11 +01:00
|
|
|
foreach($value->{'@id'} as $k => $v)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
_flatten($parent, $parentProperty, $v, $subjects);
|
|
|
|
}
|
|
|
|
}
|
2011-12-20 03:29:11 +01:00
|
|
|
// regular subject
|
2011-07-14 06:47:40 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// create or fetch existing subject
|
2011-12-20 03:29:11 +01:00
|
|
|
if(property_exists($subjects, $value->{'@id'}))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
// FIXME: '@id' might be a graph literal (as {})
|
|
|
|
$subject = $subjects->{$value->{'@id'}};
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
// FIXME: '@id' might be a graph literal (as {})
|
2011-07-14 06:47:40 +02:00
|
|
|
$subject = new stdClass();
|
2011-12-20 03:29:11 +01:00
|
|
|
$subject->{'@id'} = $value->{'@id'};
|
|
|
|
$subjects->{$value->{'@id'}} = $subject;
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
2011-12-20 03:29:11 +01:00
|
|
|
$flattened = new stdClass();
|
|
|
|
$flattened->{'@id'} = $subject->{'@id'};
|
2011-07-14 06:47:40 +02:00
|
|
|
|
|
|
|
// flatten embeds
|
|
|
|
foreach($value as $key => $v)
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
// drop null values, skip @id (it is already set above)
|
|
|
|
if($v !== null and $key !== '@id')
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-08-07 05:24:17 +02:00
|
|
|
if(property_exists($subject, $key))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-08-07 05:24:17 +02:00
|
|
|
if(!is_array($subject->$key))
|
2011-07-29 17:46:23 +02:00
|
|
|
{
|
2011-08-07 05:24:17 +02:00
|
|
|
$subject->$key = new ArrayObject(array($subject->$key));
|
2011-07-29 17:46:23 +02:00
|
|
|
}
|
2011-08-07 05:56:43 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$subject->$key = new ArrayObject($subject->$key);
|
|
|
|
}
|
2011-07-29 17:46:23 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-08-07 05:24:17 +02:00
|
|
|
$subject->$key = new ArrayObject();
|
|
|
|
}
|
|
|
|
|
2011-12-20 03:29:11 +01:00
|
|
|
_flatten($subject->$key, $key, $value->$key, $subjects);
|
2011-08-07 05:24:17 +02:00
|
|
|
$subject->$key = (array)$subject->$key;
|
|
|
|
if(count($subject->$key) === 1)
|
|
|
|
{
|
|
|
|
// convert subject[key] to object if it has only 1
|
|
|
|
$arr = $subject->$key;
|
|
|
|
$subject->$key = $arr[0];
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// string value
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$flattened = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add flattened value to parent
|
|
|
|
if($flattened !== null and $parent !== null)
|
|
|
|
{
|
|
|
|
if($parent instanceof ArrayObject)
|
|
|
|
{
|
|
|
|
// do not add duplicate IRIs for the same property
|
|
|
|
$duplicate = false;
|
2011-12-20 03:29:11 +01:00
|
|
|
if(is_object($flattened) and property_exists($flattened, '@id'))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
$duplicate = count(array_filter(
|
|
|
|
(array)$parent, array(
|
2011-12-20 03:29:11 +01:00
|
|
|
new DuplicateIriFilter($flattened->{'@id'}), 'filter'))) > 0;
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
if(!$duplicate)
|
|
|
|
{
|
|
|
|
$parent[] = $flattened;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$parent->$parentProperty = $flattened;
|
|
|
|
}
|
|
|
|
}
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A MappingBuilder is used to build a mapping of existing blank node names
|
|
|
|
* to a form for serialization. The serialization is used to compare blank
|
|
|
|
* nodes against one another to determine a sort order.
|
|
|
|
*/
|
|
|
|
class MappingBuilder
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Constructs a new MappingBuilder.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->count = 1;
|
2011-08-02 07:49:14 +02:00
|
|
|
$this->processed = new stdClass();
|
2011-07-14 06:47:40 +02:00
|
|
|
$this->mapping = new stdClass();
|
2011-08-02 07:49:14 +02:00
|
|
|
$this->adj = new stdClass();
|
|
|
|
$this->keyStack = array();
|
|
|
|
$entry = new stdClass();
|
|
|
|
$entry->keys = array('s1');
|
|
|
|
$entry->idx = 0;
|
|
|
|
$this->keyStack[] = $entry;
|
|
|
|
$this->done = new stdClass();
|
|
|
|
$this->s = '';
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies this MappingBuilder.
|
|
|
|
*
|
|
|
|
* @return the MappingBuilder copy.
|
|
|
|
*/
|
|
|
|
public function copy()
|
|
|
|
{
|
|
|
|
$rval = new MappingBuilder();
|
|
|
|
$rval->count = $this->count;
|
2011-08-02 07:49:14 +02:00
|
|
|
$rval->processed = _clone($this->processed);
|
2011-07-14 06:47:40 +02:00
|
|
|
$rval->mapping = _clone($this->mapping);
|
2011-08-02 07:49:14 +02:00
|
|
|
$rval->adj = _clone($this->adj);
|
|
|
|
$rval->keyStack = _clone($this->keyStack);
|
|
|
|
$rval->done = _clone($this->done);
|
|
|
|
$rval->s = $this->s;
|
2011-07-14 06:47:40 +02:00
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps the next name to the given bnode IRI if the bnode IRI isn't already
|
|
|
|
* in the mapping. If the given bnode IRI is canonical, then it will be
|
|
|
|
* given a shortened form of the same name.
|
|
|
|
*
|
|
|
|
* @param iri the blank node IRI to map the next name to.
|
|
|
|
*
|
|
|
|
* @return the mapped name.
|
|
|
|
*/
|
|
|
|
public function mapNode($iri)
|
|
|
|
{
|
2011-07-29 20:28:46 +02:00
|
|
|
if(!property_exists($this->mapping, $iri))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
if(strpos($iri, '_:c14n') === 0)
|
|
|
|
{
|
|
|
|
$this->mapping->$iri = 'c' . substr($iri, 6);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->mapping->$iri = 's' . $this->count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->mapping->$iri;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rotates the elements in an array one position.
|
|
|
|
*
|
|
|
|
* @param a the array.
|
|
|
|
*/
|
|
|
|
function _rotate(&$a)
|
|
|
|
{
|
|
|
|
$e = array_shift($a);
|
|
|
|
array_push($a, $e);
|
|
|
|
return $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares two serializations for the same blank node. If the two
|
|
|
|
* serializations aren't complete enough to determine if they are equal (or if
|
|
|
|
* they are actually equal), 0 is returned.
|
|
|
|
*
|
|
|
|
* @param s1 the first serialization.
|
|
|
|
* @param s2 the second serialization.
|
|
|
|
*
|
|
|
|
* @return -1 if s1 < s2, 0 if s1 == s2 (or indeterminate), 1 if s1 > v2.
|
|
|
|
*/
|
|
|
|
function _compareSerializations($s1, $s2)
|
|
|
|
{
|
|
|
|
$rval = 0;
|
|
|
|
|
|
|
|
$s1Len = strlen($s1);
|
|
|
|
$s2Len = strlen($s2);
|
|
|
|
if($s1Len == $s2Len)
|
|
|
|
{
|
|
|
|
$rval = strcmp($s1, $s2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$rval = strncmp($s1, $s2, ($s1Len > $s2Len) ? $s2Len : $s1Len);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares two nodes based on their iris.
|
|
|
|
*
|
|
|
|
* @param a the first node.
|
|
|
|
* @param b the second node.
|
|
|
|
*
|
|
|
|
* @return -1 if iriA < iriB, 0 if iriA == iriB, 1 if iriA > iriB.
|
|
|
|
*/
|
|
|
|
function _compareIris($a, $b)
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
return _compare($a->{'@id'}, $b->{'@id'});
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters blank node edges.
|
|
|
|
*
|
|
|
|
* @param e the edge to check.
|
|
|
|
*
|
|
|
|
* @return true if the edge is a blank node edge.
|
|
|
|
*/
|
|
|
|
function _filterBlankNodeEdges($e)
|
|
|
|
{
|
|
|
|
return _isBlankNodeIri($e->s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sorts mapping keys based on their associated mapping values.
|
|
|
|
*/
|
|
|
|
class MappingKeySorter
|
|
|
|
{
|
|
|
|
public function __construct($mapping)
|
|
|
|
{
|
|
|
|
$this->mapping = $mapping;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function compare($a, $b)
|
|
|
|
{
|
|
|
|
return _compare($this->mapping->$a, $this->mapping->$b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-08-22 07:53:12 +02:00
|
|
|
* A JSON-LD processor.
|
2011-07-14 06:47:40 +02:00
|
|
|
*/
|
|
|
|
class JsonLdProcessor
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Constructs a JSON-LD processor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-18 20:43:10 +01:00
|
|
|
* Recursively compacts a value. This method will compact IRIs to prefixes
|
|
|
|
* or terms and do reverse type coercion to compact a value.
|
2011-08-22 07:53:12 +02:00
|
|
|
*
|
|
|
|
* @param ctx the context to use.
|
|
|
|
* @param property the property that points to the value, NULL for none.
|
|
|
|
* @param value the value to compact.
|
|
|
|
* @param usedCtx a context to update if a value was used from "ctx".
|
|
|
|
*
|
|
|
|
* @return the compacted value.
|
|
|
|
*/
|
|
|
|
public function compact($ctx, $property, $value, $usedCtx)
|
|
|
|
{
|
|
|
|
$rval;
|
|
|
|
|
|
|
|
// get JSON-LD keywords
|
|
|
|
$keywords = _getKeywords($ctx);
|
|
|
|
|
|
|
|
if($value === null)
|
|
|
|
{
|
|
|
|
// return null, but check coerce type to add to usedCtx
|
|
|
|
$rval = null;
|
|
|
|
$this->getCoerceType($ctx, $property, $usedCtx);
|
|
|
|
}
|
|
|
|
else if(is_array($value))
|
|
|
|
{
|
|
|
|
// recursively add compacted values to array
|
|
|
|
$rval = array();
|
|
|
|
foreach($value as $v)
|
|
|
|
{
|
|
|
|
$rval[] = $this->compact($ctx, $property, $v, $usedCtx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// graph literal/disjoint graph
|
|
|
|
else if(
|
|
|
|
is_object($value) and
|
2011-12-20 03:29:11 +01:00
|
|
|
property_exists($value, '@id') and
|
|
|
|
is_array($value->{'@id'}))
|
2011-08-22 07:53:12 +02:00
|
|
|
{
|
|
|
|
$rval = new stdClass();
|
2011-12-20 03:29:11 +01:00
|
|
|
$rval->{$keywords->{'@id'}} = $this->compact(
|
|
|
|
$ctx, $property, $value->{'@id'}, $usedCtx);
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
2011-12-20 03:29:11 +01:00
|
|
|
// recurse if value is a subject
|
|
|
|
else if(_isSubject($value))
|
2011-08-22 07:53:12 +02:00
|
|
|
{
|
|
|
|
// recursively handle sub-properties that aren't a sub-context
|
|
|
|
$rval = new stdClass();
|
|
|
|
foreach($value as $key => $v)
|
|
|
|
{
|
|
|
|
if($v !== '@context')
|
|
|
|
{
|
|
|
|
// set object to compacted property, only overwrite existing
|
|
|
|
// properties if the property actually compacted
|
|
|
|
$p = _compactIri($ctx, $key, $usedCtx);
|
|
|
|
if($p !== $key or !property_exists($rval, $p))
|
|
|
|
{
|
|
|
|
$rval->$p = $this->compact($ctx, $key, $v, $usedCtx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// get coerce type
|
|
|
|
$coerce = $this->getCoerceType($ctx, $property, $usedCtx);
|
|
|
|
|
|
|
|
// get type from value, to ensure coercion is valid
|
|
|
|
$type = null;
|
|
|
|
if(is_object($value))
|
|
|
|
{
|
|
|
|
// type coercion can only occur if language is not specified
|
|
|
|
if(!property_exists($value, '@language'))
|
|
|
|
{
|
2011-12-18 03:08:58 +01:00
|
|
|
// type must match coerce type if specified
|
|
|
|
if(property_exists($value, '@type'))
|
2011-08-22 07:53:12 +02:00
|
|
|
{
|
2011-12-18 03:08:58 +01:00
|
|
|
$type = $value->{'@type'};
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
2011-12-20 03:29:11 +01:00
|
|
|
// type is ID (IRI)
|
|
|
|
else if(property_exists($value, '@id'))
|
2011-08-22 07:53:12 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$type = '@id';
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
|
|
|
// can be coerced to any type
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$type = $coerce;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// type can be coerced to anything
|
|
|
|
else if(is_string($value))
|
|
|
|
{
|
|
|
|
$type = $coerce;
|
|
|
|
}
|
|
|
|
|
|
|
|
// types that can be auto-coerced from a JSON-builtin
|
|
|
|
if($coerce === null and
|
|
|
|
($type === JSONLD_XSD_BOOLEAN or $type === JSONLD_XSD_INTEGER or
|
|
|
|
$type === JSONLD_XSD_DOUBLE))
|
|
|
|
{
|
|
|
|
$coerce = $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
// do reverse type-coercion
|
|
|
|
if($coerce !== null)
|
|
|
|
{
|
|
|
|
// type is only null if a language was specified, which is an error
|
|
|
|
// if type coercion is specified
|
|
|
|
if($type === null)
|
|
|
|
{
|
|
|
|
throw new Exception(
|
|
|
|
'Cannot coerce type when a language is specified. ' .
|
|
|
|
'The language information would be lost.');
|
|
|
|
}
|
|
|
|
// if the value type does not match the coerce type, it is an error
|
|
|
|
else if($type !== $coerce)
|
|
|
|
{
|
|
|
|
throw new Exception(
|
2011-12-18 03:08:58 +01:00
|
|
|
'Cannot coerce type because the type does not match.');
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
|
|
|
// do reverse type-coercion
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(is_object($value))
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
if(property_exists($value, '@id'))
|
2011-08-22 07:53:12 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$rval = $value->{'@id'};
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
2012-01-12 04:56:26 +01:00
|
|
|
else if(property_exists($value, '@value'))
|
2011-08-22 07:53:12 +02:00
|
|
|
{
|
2012-01-12 04:56:26 +01:00
|
|
|
$rval = $value->{'@value'};
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$rval = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// do basic JSON types conversion
|
|
|
|
if($coerce === JSONLD_XSD_BOOLEAN)
|
|
|
|
{
|
|
|
|
$rval = ($rval === 'true' or $rval != 0);
|
|
|
|
}
|
|
|
|
else if($coerce === JSONLD_XSD_DOUBLE)
|
|
|
|
{
|
|
|
|
$rval = floatval($rval);
|
|
|
|
}
|
|
|
|
else if($coerce === JSONLD_XSD_INTEGER)
|
|
|
|
{
|
|
|
|
$rval = intval($rval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// no type-coercion, just change keywords/copy value
|
|
|
|
else if(is_object($value))
|
|
|
|
{
|
|
|
|
$rval = new stdClass();
|
|
|
|
foreach($value as $key => $v)
|
|
|
|
{
|
|
|
|
$rval->{$keywords->$key} = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$rval = _clone($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// compact IRI
|
2011-12-20 03:29:11 +01:00
|
|
|
if($type === '@id')
|
2011-08-22 07:53:12 +02:00
|
|
|
{
|
|
|
|
if(is_object($rval))
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$rval->{$keywords->{'@id'}} = _compactIri(
|
|
|
|
$ctx, $rval->{$keywords->{'@id'}}, $usedCtx);
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$rval = _compactIri($ctx, $rval, $usedCtx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursively expands a value using the given context. Any context in
|
|
|
|
* the value will be removed.
|
|
|
|
*
|
|
|
|
* @param ctx the context.
|
|
|
|
* @param property the property that points to the value, NULL for none.
|
|
|
|
* @param value the value to expand.
|
|
|
|
*
|
|
|
|
* @return the expanded value.
|
|
|
|
*/
|
2011-12-18 21:14:25 +01:00
|
|
|
public function expand($ctx, $property, $value)
|
2011-08-22 07:53:12 +02:00
|
|
|
{
|
|
|
|
$rval;
|
|
|
|
|
|
|
|
// TODO: add data format error detection?
|
|
|
|
|
|
|
|
// value is null, nothing to expand
|
|
|
|
if($value === null)
|
|
|
|
{
|
|
|
|
$rval = null;
|
|
|
|
}
|
|
|
|
// if no property is specified and the value is a string (this means the
|
|
|
|
// value is a property itself), expand to an IRI
|
|
|
|
else if($property === null and is_string($value))
|
|
|
|
{
|
|
|
|
$rval = _expandTerm($ctx, $value, null);
|
|
|
|
}
|
|
|
|
else if(is_array($value))
|
|
|
|
{
|
|
|
|
// recursively add expanded values to array
|
|
|
|
$rval = array();
|
|
|
|
foreach($value as $v)
|
|
|
|
{
|
2011-12-18 21:14:25 +01:00
|
|
|
$rval[] = $this->expand($ctx, $property, $v);
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(is_object($value))
|
|
|
|
{
|
|
|
|
// if value has a context, use it
|
|
|
|
if(property_exists($value, '@context'))
|
|
|
|
{
|
|
|
|
$ctx = jsonld_merge_contexts($ctx, $value->{'@context'});
|
|
|
|
}
|
|
|
|
|
2011-12-20 03:29:11 +01:00
|
|
|
// recursively handle sub-properties that aren't a sub-context
|
|
|
|
$rval = new stdClass();
|
|
|
|
foreach($value as $key => $v)
|
2011-08-22 07:53:12 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
// preserve frame keywords
|
|
|
|
if($key === '@embed' or $key === '@explicit' or
|
|
|
|
$key === '@default' or $key === '@omitDefault')
|
2011-08-22 07:53:12 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
_setProperty($rval, $key, _clone($v));
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
2011-12-20 03:29:11 +01:00
|
|
|
else if($key !== '@context')
|
2011-08-22 07:53:12 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
// set object to expanded property
|
|
|
|
_setProperty(
|
|
|
|
$rval, _expandTerm($ctx, $key, null),
|
|
|
|
$this->expand($ctx, $key, $v));
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// do type coercion
|
|
|
|
$coerce = $this->getCoerceType($ctx, $property, null);
|
|
|
|
|
|
|
|
// get JSON-LD keywords
|
|
|
|
$keywords = _getKeywords($ctx);
|
|
|
|
|
|
|
|
// automatic coercion for basic JSON types
|
|
|
|
if($coerce === null and !is_string($value) and
|
|
|
|
(is_numeric($value) or is_bool($value)))
|
|
|
|
{
|
|
|
|
if(is_bool($value))
|
|
|
|
{
|
|
|
|
$coerce = JSONLD_XSD_BOOLEAN;
|
|
|
|
}
|
|
|
|
else if(is_int($value))
|
|
|
|
{
|
|
|
|
$coerce = JSONLD_XSD_INTEGER;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$coerce = JSONLD_XSD_DOUBLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 03:29:11 +01:00
|
|
|
// special-case expand @id and @type (skips '@id' expansion)
|
2012-01-12 01:32:30 +01:00
|
|
|
if($property === '@id' or $property === $keywords->{'@id'} or
|
|
|
|
$property === '@type' or $property === $keywords->{'@type'})
|
2011-12-20 03:29:11 +01:00
|
|
|
{
|
|
|
|
$rval = _expandTerm($ctx, $value, null);
|
|
|
|
}
|
|
|
|
// coerce to appropriate type
|
|
|
|
else if($coerce !== null)
|
2011-08-22 07:53:12 +02:00
|
|
|
{
|
|
|
|
$rval = new stdClass();
|
|
|
|
|
2011-12-20 03:29:11 +01:00
|
|
|
// expand ID (IRI)
|
|
|
|
if($coerce === '@id')
|
2011-08-22 07:53:12 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$rval->{'@id'} = _expandTerm($ctx, $value, null);
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
2011-12-18 03:08:58 +01:00
|
|
|
// other type
|
2011-08-22 07:53:12 +02:00
|
|
|
else
|
|
|
|
{
|
2011-12-18 03:08:58 +01:00
|
|
|
$rval->{'@type'} = $coerce;
|
2011-08-22 07:53:12 +02:00
|
|
|
if($coerce === JSONLD_XSD_DOUBLE)
|
|
|
|
{
|
|
|
|
// do special JSON-LD double format
|
|
|
|
$value = preg_replace(
|
|
|
|
'/(e(?:\+|-))([0-9])$/', '${1}0${2}',
|
|
|
|
sprintf('%1.6e', $value));
|
|
|
|
}
|
|
|
|
else if($coerce === JSONLD_XSD_BOOLEAN)
|
|
|
|
{
|
|
|
|
$value = $value ? 'true' : 'false';
|
|
|
|
}
|
2012-01-12 04:56:26 +01:00
|
|
|
$rval->{'@value'} = '' . $value;
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// nothing to coerce
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$rval = '' . $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalizes a JSON-LD object.
|
|
|
|
*
|
|
|
|
* @param input the JSON-LD object to normalize.
|
|
|
|
*
|
|
|
|
* @return the normalized JSON-LD object.
|
|
|
|
*/
|
|
|
|
public function normalize($input)
|
|
|
|
{
|
|
|
|
$rval = array();
|
|
|
|
|
|
|
|
// TODO: validate context
|
|
|
|
|
|
|
|
if($input !== null)
|
|
|
|
{
|
2011-08-22 07:53:12 +02:00
|
|
|
// create name generator state
|
|
|
|
$this->ng = new stdClass();
|
|
|
|
$this->ng->tmp = null;
|
|
|
|
$this->ng->c14n = null;
|
|
|
|
|
2011-07-14 06:47:40 +02:00
|
|
|
// expand input
|
2011-12-20 03:29:11 +01:00
|
|
|
$expanded = $this->expand(new stdClass(), null, $input);
|
2011-07-14 06:47:40 +02:00
|
|
|
|
|
|
|
// assign names to unnamed bnodes
|
|
|
|
$this->nameBlankNodes($expanded);
|
|
|
|
|
|
|
|
// flatten
|
|
|
|
$subjects = new stdClass();
|
|
|
|
_flatten(null, null, $expanded, $subjects);
|
|
|
|
|
|
|
|
// append subjects with sorted properties to array
|
|
|
|
foreach($subjects as $key => $s)
|
|
|
|
{
|
|
|
|
$sorted = new stdClass();
|
|
|
|
$keys = array_keys((array)$s);
|
|
|
|
sort($keys);
|
|
|
|
foreach($keys as $i => $k)
|
|
|
|
{
|
|
|
|
$sorted->$k = $s->$k;
|
|
|
|
}
|
|
|
|
$rval[] = $sorted;
|
|
|
|
}
|
|
|
|
|
|
|
|
// canonicalize blank nodes
|
|
|
|
$this->canonicalizeBlankNodes($rval);
|
|
|
|
|
|
|
|
// sort output
|
|
|
|
usort($rval, '_compareIris');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
2011-08-22 07:53:12 +02:00
|
|
|
/**
|
|
|
|
* Gets the coerce type for the given property.
|
|
|
|
*
|
|
|
|
* @param ctx the context to use.
|
|
|
|
* @param property the property to get the coerced type for.
|
|
|
|
* @param usedCtx a context to update if a value was used from "ctx".
|
|
|
|
*
|
|
|
|
* @return the coerce type, null for none.
|
|
|
|
*/
|
|
|
|
public function getCoerceType($ctx, $property, $usedCtx)
|
|
|
|
{
|
|
|
|
$rval = null;
|
|
|
|
|
|
|
|
// get expanded property
|
|
|
|
$p = _expandTerm($ctx, $property, null);
|
|
|
|
|
|
|
|
// built-in type coercion JSON-LD-isms
|
2011-12-20 03:29:11 +01:00
|
|
|
if($p === '@id' or $p === '@type')
|
2011-08-22 07:53:12 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$rval = '@id';
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
2011-12-18 20:43:10 +01:00
|
|
|
else
|
2011-08-22 07:53:12 +02:00
|
|
|
{
|
2011-10-20 22:46:20 +02:00
|
|
|
// look up compacted property in coercion map
|
2011-08-22 07:53:12 +02:00
|
|
|
$p = _compactIri($ctx, $p, null);
|
2011-12-18 20:43:10 +01:00
|
|
|
if(property_exists($ctx, $p) and is_object($ctx->$p) and
|
|
|
|
property_exists($ctx->$p, '@type'))
|
2011-08-22 07:53:12 +02:00
|
|
|
{
|
2011-10-20 22:46:20 +02:00
|
|
|
// property found, return expanded type
|
2011-12-18 20:43:10 +01:00
|
|
|
$type = $ctx->$p->{'@type'};
|
2011-10-20 22:46:20 +02:00
|
|
|
$rval = _expandTerm($ctx, $type, $usedCtx);
|
|
|
|
if($usedCtx !== null)
|
2011-08-22 07:53:12 +02:00
|
|
|
{
|
2011-12-18 20:43:10 +01:00
|
|
|
$usedCtx->$p = _clone($ctx->$p);
|
2011-08-22 07:53:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
2011-07-14 06:47:40 +02:00
|
|
|
/**
|
|
|
|
* Assigns unique names to blank nodes that are unnamed in the given input.
|
|
|
|
*
|
|
|
|
* @param input the input to assign names to.
|
|
|
|
*/
|
|
|
|
public function nameBlankNodes($input)
|
|
|
|
{
|
|
|
|
// create temporary blank node name generator
|
|
|
|
$ng = $this->ng->tmp = new NameGenerator('tmp');
|
|
|
|
|
|
|
|
// collect subjects and unnamed bnodes
|
|
|
|
$subjects = new stdClass();
|
|
|
|
$bnodes = new ArrayObject();
|
|
|
|
_collectSubjects($input, $subjects, $bnodes);
|
|
|
|
|
|
|
|
// uniquely name all unnamed bnodes
|
|
|
|
foreach($bnodes as $i => $bnode)
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
if(!property_exists($bnode, '@id'))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
// generate names until one is unique
|
2011-07-29 20:28:46 +02:00
|
|
|
while(property_exists($subjects, $ng->next()));
|
2011-12-20 03:29:11 +01:00
|
|
|
$bnode->{'@id'} = $ng->current();
|
2011-07-14 06:47:40 +02:00
|
|
|
$subjects->{$ng->current()} = $bnode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renames a blank node, changing its references, etc. The method assumes
|
|
|
|
* that the given name is unique.
|
|
|
|
*
|
|
|
|
* @param b the blank node to rename.
|
|
|
|
* @param id the new name to use.
|
|
|
|
*/
|
|
|
|
public function renameBlankNode($b, $id)
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$old = $b->{'@id'};
|
2011-07-14 06:47:40 +02:00
|
|
|
|
|
|
|
// update bnode IRI
|
2011-12-20 03:29:11 +01:00
|
|
|
$b->{'@id'} = $id;
|
2011-07-14 06:47:40 +02:00
|
|
|
|
|
|
|
// update subjects map
|
|
|
|
$subjects = $this->subjects;
|
|
|
|
$subjects->$id = $subjects->$old;
|
|
|
|
unset($subjects->$old);
|
|
|
|
|
|
|
|
// update reference and property lists
|
|
|
|
$this->edges->refs->$id = $this->edges->refs->$old;
|
|
|
|
$this->edges->props->$id = $this->edges->props->$old;
|
|
|
|
unset($this->edges->refs->$old);
|
|
|
|
unset($this->edges->props->$old);
|
|
|
|
|
|
|
|
// update references to this bnode
|
|
|
|
$refs = $this->edges->refs->$id->all;
|
|
|
|
foreach($refs as $i => $r)
|
|
|
|
{
|
|
|
|
$iri = $r->s;
|
|
|
|
if($iri === $old)
|
|
|
|
{
|
|
|
|
$iri = $id;
|
|
|
|
}
|
|
|
|
$ref = $subjects->$iri;
|
|
|
|
$props = $this->edges->props->$iri->all;
|
|
|
|
foreach($props as $prop)
|
|
|
|
{
|
|
|
|
if($prop->s === $old)
|
|
|
|
{
|
|
|
|
$prop->s = $id;
|
|
|
|
|
|
|
|
// normalize property to array for single code-path
|
|
|
|
$p = $prop->p;
|
|
|
|
$tmp = is_object($ref->$p) ? array($ref->$p) :
|
|
|
|
(is_array($ref->$p) ? $ref->$p : array());
|
|
|
|
$length = count($tmp);
|
|
|
|
for($n = 0; $n < $length; ++$n)
|
|
|
|
{
|
|
|
|
if(is_object($tmp[$n]) and
|
2011-12-20 03:29:11 +01:00
|
|
|
property_exists($tmp[$n], '@id') and
|
|
|
|
$tmp[$n]->{'@id'} === $old)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$tmp[$n]->{'@id'} = $id;
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update references from this bnode
|
|
|
|
$props = $this->edges->props->$id->all;
|
|
|
|
foreach($props as $prop)
|
|
|
|
{
|
|
|
|
$iri = $prop->s;
|
|
|
|
$refs = $this->edges->refs->$iri->all;
|
|
|
|
foreach($refs as $r)
|
|
|
|
{
|
|
|
|
if($r->s === $old)
|
|
|
|
{
|
|
|
|
$r->s = $id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Canonically names blank nodes in the given input.
|
|
|
|
*
|
|
|
|
* @param input the flat input graph to assign names to.
|
|
|
|
*/
|
|
|
|
public function canonicalizeBlankNodes($input)
|
|
|
|
{
|
|
|
|
// create serialization state
|
|
|
|
$this->renamed = new stdClass();
|
|
|
|
$this->mappings = new stdClass();
|
|
|
|
$this->serializations = new stdClass();
|
|
|
|
|
|
|
|
// collect subjects and bnodes from flat input graph
|
|
|
|
$edges = $this->edges = new stdClass();
|
|
|
|
$edges->refs = new stdClass();
|
|
|
|
$edges->props = new stdClass();
|
|
|
|
$subjects = $this->subjects = new stdClass();
|
|
|
|
$bnodes = array();
|
|
|
|
foreach($input as $v)
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$iri = $v->{'@id'};
|
2011-07-14 06:47:40 +02:00
|
|
|
$subjects->$iri = $v;
|
|
|
|
$edges->refs->$iri = new stdClass();
|
|
|
|
$edges->refs->$iri->all = array();
|
|
|
|
$edges->refs->$iri->bnodes = array();
|
|
|
|
$edges->props->$iri = new stdClass();
|
|
|
|
$edges->props->$iri->all = array();
|
|
|
|
$edges->props->$iri->bnodes = array();
|
|
|
|
if(_isBlankNodeIri($iri))
|
|
|
|
{
|
|
|
|
$bnodes[] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect edges in the graph
|
|
|
|
$this->collectEdges();
|
|
|
|
|
|
|
|
// create canonical blank node name generator
|
|
|
|
$c14n = $this->ng->c14n = new NameGenerator('c14n');
|
|
|
|
$ngTmp = $this->ng->tmp;
|
|
|
|
|
|
|
|
// rename all bnodes that happen to be in the c14n namespace
|
|
|
|
// and initialize serializations
|
|
|
|
foreach($bnodes as $i => $bnode)
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$iri = $bnode->{'@id'};
|
2011-07-14 06:47:40 +02:00
|
|
|
if($c14n->inNamespace($iri))
|
|
|
|
{
|
|
|
|
// generate names until one is unique
|
2011-07-29 20:28:46 +02:00
|
|
|
while(property_exists($subjects, $ngTmp->next()));
|
2011-07-14 06:47:40 +02:00
|
|
|
$this->renameBlankNode($bnode, $ngTmp->current());
|
2011-12-20 03:29:11 +01:00
|
|
|
$iri = $bnode->{'@id'};
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
$this->serializations->$iri = new stdClass();
|
|
|
|
$this->serializations->$iri->props = null;
|
|
|
|
$this->serializations->$iri->refs = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// keep sorting and naming blank nodes until they are all named
|
2011-11-09 19:02:18 +01:00
|
|
|
$resort = true;
|
2011-07-14 06:47:40 +02:00
|
|
|
while(count($bnodes) > 0)
|
|
|
|
{
|
2011-11-09 19:02:18 +01:00
|
|
|
if($resort)
|
|
|
|
{
|
2011-11-09 19:58:47 +01:00
|
|
|
$resort = false;
|
2011-11-09 19:02:18 +01:00
|
|
|
usort($bnodes, array($this, 'deepCompareBlankNodes'));
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
|
|
|
|
// name all bnodes according to the first bnode's relation mappings
|
2011-11-09 19:02:18 +01:00
|
|
|
// (if it has mappings then a resort will be necessary)
|
2011-07-14 06:47:40 +02:00
|
|
|
$bnode = array_shift($bnodes);
|
2011-12-20 03:29:11 +01:00
|
|
|
$iri = $bnode->{'@id'};
|
2011-11-09 19:02:18 +01:00
|
|
|
$resort = ($this->serializations->$iri->{'props'} !== null);
|
2011-07-14 06:47:40 +02:00
|
|
|
$dirs = array('props', 'refs');
|
|
|
|
foreach($dirs as $dir)
|
|
|
|
{
|
|
|
|
// if no serialization has been computed, name only the first node
|
|
|
|
if($this->serializations->$iri->$dir === null)
|
|
|
|
{
|
|
|
|
$mapping = new stdClass();
|
|
|
|
$mapping->$iri = 's1';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$mapping = $this->serializations->$iri->$dir->m;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort keys by value to name them in order
|
|
|
|
$keys = array_keys((array)$mapping);
|
|
|
|
usort($keys, array(new MappingKeySorter($mapping), 'compare'));
|
|
|
|
|
|
|
|
// name bnodes in mapping
|
|
|
|
$renamed = array();
|
|
|
|
foreach($keys as $i => $iriK)
|
|
|
|
{
|
2011-07-29 20:28:46 +02:00
|
|
|
if(!$c14n->inNamespace($iri) and
|
|
|
|
property_exists($subjects, $iriK))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
$this->renameBlankNode($subjects->$iriK, $c14n->next());
|
|
|
|
$renamed[] = $iriK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-09 19:58:47 +01:00
|
|
|
// only keep non-canonically named bnodes
|
|
|
|
$tmp = $bnodes;
|
|
|
|
$bnodes = array();
|
|
|
|
foreach($tmp as $i => $b)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$iriB = $b->{'@id'};
|
2011-11-09 19:58:47 +01:00
|
|
|
if(!$c14n->inNamespace($iriB))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-11-09 19:58:47 +01:00
|
|
|
// mark serializations related to the named bnodes as dirty
|
|
|
|
foreach($renamed as $r)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-11-09 19:58:47 +01:00
|
|
|
if($this->markSerializationDirty($iriB, $r, $dir))
|
2011-11-09 19:02:18 +01:00
|
|
|
{
|
2011-11-09 19:58:47 +01:00
|
|
|
// resort if a serialization was marked dirty
|
|
|
|
$resort = true;
|
2011-11-09 19:02:18 +01:00
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
2011-11-09 19:58:47 +01:00
|
|
|
$bnodes[] = $b;
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort property lists that now have canonically-named bnodes
|
|
|
|
foreach($edges->props as $key => $value)
|
|
|
|
{
|
|
|
|
if(count($value->bnodes) > 0)
|
|
|
|
{
|
|
|
|
$bnode = $subjects->$key;
|
|
|
|
foreach($bnode as $p => $v)
|
|
|
|
{
|
|
|
|
if(strpos($p, '@') !== 0 and is_array($v))
|
|
|
|
{
|
|
|
|
usort($v, '_compareObjects');
|
|
|
|
$bnode->$p = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Marks a relation serialization as dirty if necessary.
|
|
|
|
*
|
|
|
|
* @param iri the IRI of the bnode to check.
|
|
|
|
* @param changed the old IRI of the bnode that changed.
|
|
|
|
* @param dir the direction to check ('props' or 'refs').
|
2011-11-09 19:58:47 +01:00
|
|
|
*
|
|
|
|
* @return true if the serialization was marked dirty, false if not.
|
2011-07-14 06:47:40 +02:00
|
|
|
*/
|
|
|
|
public function markSerializationDirty($iri, $changed, $dir)
|
|
|
|
{
|
2011-11-09 19:58:47 +01:00
|
|
|
$rval = false;
|
2011-07-14 06:47:40 +02:00
|
|
|
$s = $this->serializations->$iri;
|
2011-07-29 20:28:46 +02:00
|
|
|
if($s->$dir !== null and property_exists($s->$dir->m, $changed))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
$s->$dir = null;
|
2011-11-09 19:58:47 +01:00
|
|
|
$rval = true;
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
2011-11-09 19:58:47 +01:00
|
|
|
return $rval;
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serializes the properties of the given bnode for its relation
|
|
|
|
* serialization.
|
2011-07-18 22:22:57 +02:00
|
|
|
*
|
2011-07-14 06:47:40 +02:00
|
|
|
* @param b the blank node.
|
2011-07-18 22:22:57 +02:00
|
|
|
*
|
2011-07-14 06:47:40 +02:00
|
|
|
* @return the serialized properties.
|
|
|
|
*/
|
|
|
|
public function serializeProperties($b)
|
|
|
|
{
|
|
|
|
$rval = '';
|
2011-07-18 22:22:57 +02:00
|
|
|
|
2011-08-03 05:54:19 +02:00
|
|
|
$first = true;
|
2011-07-14 06:47:40 +02:00
|
|
|
foreach($b as $p => $o)
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
if($p !== '@id')
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-08-03 05:54:19 +02:00
|
|
|
if($first)
|
|
|
|
{
|
|
|
|
$first = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$rval .= '|';
|
|
|
|
}
|
|
|
|
|
|
|
|
// property
|
|
|
|
$rval .= '<' . $p . '>';
|
|
|
|
|
|
|
|
// object(s)
|
2011-07-14 06:47:40 +02:00
|
|
|
$objs = is_array($o) ? $o : array($o);
|
|
|
|
foreach($objs as $obj)
|
|
|
|
{
|
2011-08-03 05:54:19 +02:00
|
|
|
if(is_object($obj))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
// ID (IRI)
|
|
|
|
if(property_exists($obj, '@id'))
|
2011-08-03 05:54:19 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
if(_isBlankNodeIri($obj->{'@id'}))
|
2011-08-03 05:54:19 +02:00
|
|
|
{
|
|
|
|
$rval .= '_:';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$rval .= '<' . $obj->{'@id'} . '>';
|
2011-08-03 05:54:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// literal
|
|
|
|
else
|
|
|
|
{
|
2012-01-12 04:56:26 +01:00
|
|
|
$rval .= '"' . $obj->{'@value'} . '"';
|
2011-08-03 05:54:19 +02:00
|
|
|
|
2011-12-18 03:08:58 +01:00
|
|
|
// type literal
|
|
|
|
if(property_exists($obj, '@type'))
|
2011-08-03 05:54:19 +02:00
|
|
|
{
|
2011-12-18 03:08:58 +01:00
|
|
|
$rval .= '^^<' . $obj->{'@type'} . '>';
|
2011-08-03 05:54:19 +02:00
|
|
|
}
|
|
|
|
// language literal
|
|
|
|
else if(property_exists($obj, '@language'))
|
|
|
|
{
|
|
|
|
$rval .= '@' . $obj->{'@language'};
|
|
|
|
}
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
2011-08-03 05:54:19 +02:00
|
|
|
// plain literal
|
2011-07-14 06:47:40 +02:00
|
|
|
else
|
|
|
|
{
|
2011-08-03 05:54:19 +02:00
|
|
|
$rval .= '"' . $obj . '"';
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-07-18 22:22:57 +02:00
|
|
|
|
2011-07-14 06:47:40 +02:00
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-08-02 07:49:14 +02:00
|
|
|
* Recursively increments the relation serialization for a mapping.
|
2011-07-14 06:47:40 +02:00
|
|
|
*
|
2011-08-02 07:49:14 +02:00
|
|
|
* @param mb the mapping builder to update.
|
2011-07-14 06:47:40 +02:00
|
|
|
*/
|
2011-08-02 07:49:14 +02:00
|
|
|
public function serializeMapping($mb)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-08-02 07:49:14 +02:00
|
|
|
if(count($mb->keyStack) > 0)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-08-02 07:49:14 +02:00
|
|
|
// continue from top of key stack
|
|
|
|
$next = array_pop($mb->keyStack);
|
|
|
|
$len = count($next->keys);
|
|
|
|
for(; $next->idx < $len; ++$next->idx)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-08-02 07:49:14 +02:00
|
|
|
$k = $next->keys[$next->idx];
|
|
|
|
if(!property_exists($mb->adj, $k))
|
|
|
|
{
|
|
|
|
$mb->keyStack[] = $next;
|
|
|
|
break;
|
|
|
|
}
|
2011-07-18 22:22:57 +02:00
|
|
|
|
2011-08-02 07:49:14 +02:00
|
|
|
if(property_exists($mb->done, $k))
|
|
|
|
{
|
|
|
|
// mark cycle
|
|
|
|
$mb->s .= '_' . $k;
|
|
|
|
}
|
|
|
|
else
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-08-02 07:49:14 +02:00
|
|
|
// mark key as serialized
|
|
|
|
$mb->done->$k = true;
|
|
|
|
|
|
|
|
// serialize top-level key and its details
|
|
|
|
$s = $k;
|
|
|
|
$adj = $mb->adj->$k;
|
|
|
|
$iri = $adj->i;
|
2011-07-29 20:28:46 +02:00
|
|
|
if(property_exists($this->subjects, $iri))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
$b = $this->subjects->$iri;
|
2011-07-18 22:22:57 +02:00
|
|
|
|
2011-07-14 06:47:40 +02:00
|
|
|
// serialize properties
|
2011-08-03 05:54:19 +02:00
|
|
|
$s .= '[' . $this->serializeProperties($b) . ']';
|
2011-07-18 22:22:57 +02:00
|
|
|
|
2011-07-14 06:47:40 +02:00
|
|
|
// serialize references
|
2011-08-03 05:54:19 +02:00
|
|
|
$s .= '[';
|
2011-07-14 06:47:40 +02:00
|
|
|
$first = true;
|
|
|
|
$refs = $this->edges->refs->$iri->all;
|
|
|
|
foreach($refs as $r)
|
|
|
|
{
|
|
|
|
if($first)
|
|
|
|
{
|
|
|
|
$first = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-08-02 07:49:14 +02:00
|
|
|
$s .= '|';
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
2011-08-03 05:54:19 +02:00
|
|
|
$s .= '<' . $r->p . '>';
|
|
|
|
$s .= _isBlankNodeIri($r->s) ?
|
|
|
|
'_:' : ('<' . $refs->s . '>');
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
2011-08-03 05:54:19 +02:00
|
|
|
$s .= ']';
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
2011-07-18 22:22:57 +02:00
|
|
|
|
2011-08-02 07:49:14 +02:00
|
|
|
// serialize adjacent node keys
|
|
|
|
$s .= implode($adj->k);
|
|
|
|
$mb->s .= $s;
|
|
|
|
$entry = new stdClass();
|
|
|
|
$entry->keys = $adj->k;
|
|
|
|
$entry->idx = 0;
|
|
|
|
$mb->keyStack[] = $entry;
|
|
|
|
$this->serializeMapping($mb);
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursively serializes adjacent bnode combinations.
|
|
|
|
*
|
|
|
|
* @param s the serialization to update.
|
2011-08-02 07:49:14 +02:00
|
|
|
* @param iri the IRI of the bnode being serialized.
|
|
|
|
* @param siri the serialization name for the bnode IRI.
|
2011-07-14 06:47:40 +02:00
|
|
|
* @param mb the MappingBuilder to use.
|
|
|
|
* @param dir the edge direction to use ('props' or 'refs').
|
|
|
|
* @param mapped all of the already-mapped adjacent bnodes.
|
|
|
|
* @param notMapped all of the not-yet mapped adjacent bnodes.
|
|
|
|
*/
|
|
|
|
public function serializeCombos(
|
2011-08-02 07:49:14 +02:00
|
|
|
$s, $iri, $siri, $mb, $dir, $mapped, $notMapped)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
// handle recursion
|
|
|
|
if(count($notMapped) > 0)
|
|
|
|
{
|
2011-08-02 07:49:14 +02:00
|
|
|
// copy mapped nodes
|
|
|
|
$mapped = _clone($mapped);
|
|
|
|
|
2011-07-14 06:47:40 +02:00
|
|
|
// map first bnode in list
|
|
|
|
$mapped->{$mb->mapNode($notMapped[0]->s)} = $notMapped[0]->s;
|
|
|
|
|
|
|
|
// recurse into remaining possible combinations
|
|
|
|
$original = $mb->copy();
|
|
|
|
$notMapped = array_slice($notMapped, 1);
|
|
|
|
$rotations = max(1, count($notMapped));
|
|
|
|
for($r = 0; $r < $rotations; ++$r)
|
|
|
|
{
|
|
|
|
$m = ($r === 0) ? $mb : $original->copy();
|
2011-08-02 07:49:14 +02:00
|
|
|
$this->serializeCombos(
|
|
|
|
$s, $iri, $siri, $m, $dir, $mapped, $notMapped);
|
2011-07-14 06:47:40 +02:00
|
|
|
|
|
|
|
// rotate not-mapped for next combination
|
|
|
|
_rotate($notMapped);
|
|
|
|
}
|
|
|
|
}
|
2011-08-02 07:49:14 +02:00
|
|
|
// no more adjacent bnodes to map, update serialization
|
2011-07-14 06:47:40 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$keys = array_keys((array)$mapped);
|
|
|
|
sort($keys);
|
2011-08-02 07:49:14 +02:00
|
|
|
$entry = new stdClass();
|
|
|
|
$entry->i = $iri;
|
|
|
|
$entry->k = $keys;
|
|
|
|
$entry->m = $mapped;
|
|
|
|
$mb->adj->$siri = $entry;
|
|
|
|
$this->serializeMapping($mb);
|
2011-07-14 06:47:40 +02:00
|
|
|
|
|
|
|
// optimize away mappings that are already too large
|
2011-08-02 07:49:14 +02:00
|
|
|
if($s->$dir === null or
|
|
|
|
_compareSerializations($mb->s, $s->$dir->s) <= 0)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
// recurse into adjacent values
|
|
|
|
foreach($keys as $i => $k)
|
|
|
|
{
|
|
|
|
$this->serializeBlankNode($s, $mapped->$k, $mb, $dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
// update least serialization if new one has been found
|
2011-08-02 07:49:14 +02:00
|
|
|
$this->serializeMapping($mb);
|
2011-07-14 06:47:40 +02:00
|
|
|
if($s->$dir === null or
|
2011-08-02 07:49:14 +02:00
|
|
|
(_compareSerializations($mb->s, $s->$dir->s) <= 0 and
|
|
|
|
strlen($mb->s) >= strlen($s->$dir->s)))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
$s->$dir = new stdClass();
|
2011-08-02 07:49:14 +02:00
|
|
|
$s->$dir->s = $mb->s;
|
2011-07-14 06:47:40 +02:00
|
|
|
$s->$dir->m = $mb->mapping;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the relation serialization for the given blank node IRI.
|
|
|
|
*
|
|
|
|
* @param s the serialization to update.
|
|
|
|
* @param iri the current bnode IRI to be mapped.
|
|
|
|
* @param mb the MappingBuilder to use.
|
|
|
|
* @param dir the edge direction to use ('props' or 'refs').
|
|
|
|
*/
|
|
|
|
public function serializeBlankNode($s, $iri, $mb, $dir)
|
|
|
|
{
|
2011-08-02 07:49:14 +02:00
|
|
|
// only do mapping if iri not already processed
|
|
|
|
if(!property_exists($mb->processed, $iri))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-08-02 07:49:14 +02:00
|
|
|
// iri now processed
|
|
|
|
$mb->processed->$iri = true;
|
|
|
|
$siri = $mb->mapNode($iri);
|
2011-07-14 06:47:40 +02:00
|
|
|
|
|
|
|
// copy original mapping builder
|
|
|
|
$original = $mb->copy();
|
|
|
|
|
|
|
|
// split adjacent bnodes on mapped and not-mapped
|
|
|
|
$adjs = $this->edges->$dir->$iri->bnodes;
|
|
|
|
$mapped = new stdClass();
|
|
|
|
$notMapped = array();
|
|
|
|
foreach($adjs as $adj)
|
|
|
|
{
|
2011-07-29 20:28:46 +02:00
|
|
|
if(property_exists($mb->mapping, $adj->s))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
$mapped->{$mb->mapping->{$adj->s}} = $adj->s;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$notMapped[] = $adj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: ensure this optimization does not alter canonical order
|
|
|
|
|
|
|
|
// if the current bnode already has a serialization, reuse it
|
2011-07-29 20:28:46 +02:00
|
|
|
/*$hint = property_exists($this->serializations, $iri) ?
|
2011-07-14 06:47:40 +02:00
|
|
|
$this->serializations->$iri->$dir : null;
|
|
|
|
if($hint !== null)
|
|
|
|
{
|
|
|
|
$hm = $hint->m;
|
|
|
|
usort(notMapped,
|
|
|
|
{
|
|
|
|
return _compare(hm[a.s], hm[b.s]);
|
|
|
|
});
|
|
|
|
for($i in notMapped)
|
|
|
|
{
|
|
|
|
mapped[mb.mapNode(notMapped[i].s)] = notMapped[i].s;
|
|
|
|
}
|
|
|
|
notMapped = array();
|
|
|
|
}*/
|
|
|
|
|
|
|
|
// loop over possible combinations
|
|
|
|
$combos = max(1, count($notMapped));
|
|
|
|
for($i = 0; $i < $combos; ++$i)
|
|
|
|
{
|
|
|
|
$m = ($i === 0) ? $mb : $original->copy();
|
2011-08-02 07:49:14 +02:00
|
|
|
$this->serializeCombos(
|
|
|
|
$s, $iri, $siri, $mb, $dir, $mapped, $notMapped);
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares two blank nodes for equivalence.
|
|
|
|
*
|
|
|
|
* @param a the first blank node.
|
|
|
|
* @param b the second blank node.
|
|
|
|
*
|
|
|
|
* @return -1 if a < b, 0 if a == b, 1 if a > b.
|
|
|
|
*/
|
|
|
|
public function deepCompareBlankNodes($a, $b)
|
|
|
|
{
|
|
|
|
$rval = 0;
|
|
|
|
|
|
|
|
// compare IRIs
|
2011-12-20 03:29:11 +01:00
|
|
|
$iriA = $a->{'@id'};
|
|
|
|
$iriB = $b->{'@id'};
|
2011-07-14 06:47:40 +02:00
|
|
|
if($iriA === $iriB)
|
|
|
|
{
|
|
|
|
$rval = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// do shallow compare first
|
|
|
|
$rval = $this->shallowCompareBlankNodes($a, $b);
|
|
|
|
|
|
|
|
// deep comparison is necessary
|
|
|
|
if($rval === 0)
|
|
|
|
{
|
|
|
|
// compare property edges and then reference edges
|
|
|
|
$dirs = array('props', 'refs');
|
|
|
|
for($i = 0; $rval === 0 and $i < 2; ++$i)
|
|
|
|
{
|
|
|
|
// recompute 'a' and 'b' serializations as necessary
|
|
|
|
$dir = $dirs[$i];
|
|
|
|
$sA = $this->serializations->$iriA;
|
|
|
|
$sB = $this->serializations->$iriB;
|
|
|
|
if($sA->$dir === null)
|
|
|
|
{
|
|
|
|
$mb = new MappingBuilder();
|
|
|
|
if($dir === 'refs')
|
|
|
|
{
|
|
|
|
// keep same mapping and count from 'props' serialization
|
|
|
|
$mb->mapping = _clone($sA->props->m);
|
|
|
|
$mb->count = count(array_keys((array)$mb->mapping)) + 1;
|
|
|
|
}
|
|
|
|
$this->serializeBlankNode($sA, $iriA, $mb, $dir);
|
|
|
|
}
|
|
|
|
if($sB->$dir === null)
|
|
|
|
{
|
|
|
|
$mb = new MappingBuilder();
|
|
|
|
if($dir === 'refs')
|
|
|
|
{
|
|
|
|
// keep same mapping and count from 'props' serialization
|
|
|
|
$mb->mapping = _clone($sB->props->m);
|
|
|
|
$mb->count = count(array_keys((array)$mb->mapping)) + 1;
|
|
|
|
}
|
|
|
|
$this->serializeBlankNode($sB, $iriB, $mb, $dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
// compare serializations
|
|
|
|
$rval = _compare($sA->$dir->s, $sB->$dir->s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs a shallow sort comparison on the given bnodes.
|
|
|
|
*
|
|
|
|
* @param a the first bnode.
|
|
|
|
* @param b the second bnode.
|
|
|
|
*
|
|
|
|
* @return -1 if a < b, 0 if a == b, 1 if a > b.
|
|
|
|
*/
|
|
|
|
public function shallowCompareBlankNodes($a, $b)
|
|
|
|
{
|
|
|
|
$rval = 0;
|
|
|
|
|
|
|
|
/* ShallowSort Algorithm (when comparing two bnodes):
|
|
|
|
1. Compare the number of properties.
|
|
|
|
1.1. The bnode with fewer properties is first.
|
|
|
|
2. Compare alphabetically sorted-properties.
|
|
|
|
2.1. The bnode with the alphabetically-first property is first.
|
|
|
|
3. For each property, compare object values.
|
|
|
|
4. Compare the number of references.
|
|
|
|
4.1. The bnode with fewer references is first.
|
|
|
|
5. Compare sorted references.
|
|
|
|
5.1. The bnode with the reference iri (vs. bnode) is first.
|
|
|
|
5.2. The bnode with the alphabetically-first reference iri is first.
|
|
|
|
5.3. The bnode with the alphabetically-first reference property is
|
|
|
|
first.
|
|
|
|
*/
|
|
|
|
$pA = array_keys((array)$a);
|
|
|
|
$pB = array_keys((array)$b);
|
|
|
|
|
|
|
|
// step #1
|
|
|
|
$rval = _compare(count($pA), count($pB));
|
|
|
|
|
|
|
|
// step #2
|
|
|
|
if($rval === 0)
|
|
|
|
{
|
|
|
|
sort($pA);
|
|
|
|
sort($pB);
|
|
|
|
$rval = _compare($pA, $pB);
|
|
|
|
}
|
|
|
|
|
|
|
|
// step #3
|
|
|
|
if($rval === 0)
|
|
|
|
{
|
|
|
|
$rval = _compareBlankNodeObjects($a, $b);
|
|
|
|
}
|
|
|
|
|
|
|
|
// step #4
|
|
|
|
if($rval === 0)
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$edgesA = $this->edges->refs->{$a->{'@id'}}->all;
|
|
|
|
$edgesB = $this->edges->refs->{$b->{'@id'}}->all;
|
2011-07-14 06:47:40 +02:00
|
|
|
$edgesALen = count($edgesA);
|
|
|
|
$rval = _compare($edgesALen, count($edgesB));
|
|
|
|
}
|
|
|
|
|
|
|
|
// step #5
|
|
|
|
if($rval === 0)
|
|
|
|
{
|
|
|
|
for($i = 0; $i < $edgesALen and $rval === 0; ++$i)
|
|
|
|
{
|
|
|
|
$rval = $this->compareEdges($edgesA[$i], $edgesB[$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares two edges. Edges with an IRI (vs. a bnode ID) come first, then
|
|
|
|
* alphabetically-first IRIs, then alphabetically-first properties. If a
|
|
|
|
* blank node has been canonically named, then blank nodes will be compared
|
|
|
|
* after properties (with a preference for canonically named over
|
|
|
|
* non-canonically named), otherwise they won't be.
|
|
|
|
*
|
|
|
|
* @param a the first edge.
|
|
|
|
* @param b the second edge.
|
|
|
|
*
|
|
|
|
* @return -1 if a < b, 0 if a == b, 1 if a > b.
|
|
|
|
*/
|
|
|
|
public function compareEdges($a, $b)
|
|
|
|
{
|
|
|
|
$rval = 0;
|
|
|
|
|
|
|
|
$bnodeA = _isBlankNodeIri($a->s);
|
|
|
|
$bnodeB = _isBlankNodeIri($b->s);
|
|
|
|
$c14n = $this->ng->c14n;
|
|
|
|
|
|
|
|
// if not both bnodes, one that is a bnode is greater
|
|
|
|
if($bnodeA != $bnodeB)
|
|
|
|
{
|
|
|
|
$rval = $bnodeA ? 1 : -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!$bnodeA)
|
|
|
|
{
|
|
|
|
$rval = _compare($a->s, $b->s);
|
|
|
|
}
|
|
|
|
if($rval === 0)
|
|
|
|
{
|
|
|
|
$rval = _compare($a->p, $b->p);
|
|
|
|
}
|
|
|
|
|
|
|
|
// do bnode IRI comparison if canonical naming has begun
|
|
|
|
if($rval === 0 and $c14n !== null)
|
|
|
|
{
|
|
|
|
$c14nA = $c14n->inNamespace($a->s);
|
|
|
|
$c14nB = $c14n->inNamespace($b->s);
|
|
|
|
if($c14nA != $c14nB)
|
|
|
|
{
|
|
|
|
$rval = $c14nA ? 1 : -1;
|
|
|
|
}
|
|
|
|
else if($c14nA)
|
|
|
|
{
|
|
|
|
$rval = _compare($a->s, $b->s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populates the given reference map with all of the subject edges in the
|
|
|
|
* graph. The references will be categorized by the direction of the edges,
|
|
|
|
* where 'props' is for properties and 'refs' is for references to a subject
|
|
|
|
* as an object. The edge direction categories for each IRI will be sorted
|
|
|
|
* into groups 'all' and 'bnodes'.
|
|
|
|
*/
|
|
|
|
public function collectEdges()
|
|
|
|
{
|
|
|
|
$refs = $this->edges->refs;
|
|
|
|
$props = $this->edges->props;
|
|
|
|
|
|
|
|
// collect all references and properties
|
|
|
|
foreach($this->subjects as $iri => $subject)
|
|
|
|
{
|
|
|
|
foreach($subject as $key => $object)
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
if($key !== '@id')
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
// normalize to array for single codepath
|
|
|
|
$tmp = !is_array($object) ? array($object) : $object;
|
|
|
|
foreach($tmp as $o)
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
if(is_object($o) and property_exists($o, '@id') and
|
|
|
|
property_exists($this->subjects, $o->{'@id'}))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$objIri = $o->{'@id'};
|
2011-07-14 06:47:40 +02:00
|
|
|
|
|
|
|
// map object to this subject
|
|
|
|
$e = new stdClass();
|
|
|
|
$e->s = $iri;
|
|
|
|
$e->p = $key;
|
|
|
|
$refs->$objIri->all[] = $e;
|
|
|
|
|
|
|
|
// map this subject to object
|
|
|
|
$e = new stdClass();
|
|
|
|
$e->s = $objIri;
|
|
|
|
$e->p = $key;
|
|
|
|
$props->$iri->all[] = $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create sorted categories
|
|
|
|
foreach($refs as $iri => $ref)
|
|
|
|
{
|
|
|
|
usort($ref->all, array($this, 'compareEdges'));
|
|
|
|
$ref->bnodes = array_filter($ref->all, '_filterBlankNodeEdges');
|
|
|
|
}
|
|
|
|
foreach($props as $iri => $prop)
|
|
|
|
{
|
|
|
|
usort($prop->all, array($this, 'compareEdges'));
|
|
|
|
$prop->bnodes = array_filter($prop->all, '_filterBlankNodeEdges');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the given input is a subject and has one of the given types
|
|
|
|
* in the given frame.
|
|
|
|
*
|
|
|
|
* @param input the input.
|
|
|
|
* @param frame the frame with types to look for.
|
|
|
|
*
|
|
|
|
* @return true if the input has one of the given types.
|
|
|
|
*/
|
|
|
|
function _isType($input, $frame)
|
|
|
|
{
|
|
|
|
$rval = false;
|
|
|
|
|
|
|
|
// check if type(s) are specified in frame and input
|
2011-11-29 05:12:11 +01:00
|
|
|
$type = '@type';
|
|
|
|
if(property_exists($frame, $type) and
|
2011-12-20 03:29:11 +01:00
|
|
|
is_object($input) and
|
2011-11-29 05:12:11 +01:00
|
|
|
property_exists($input, $type))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-11-29 05:12:11 +01:00
|
|
|
$tmp = is_array($input->$type) ? $input->$type : array($input->$type);
|
|
|
|
$types = is_array($frame->$type) ? $frame->$type : array($frame->$type);
|
2011-07-14 06:47:40 +02:00
|
|
|
$length = count($types);
|
|
|
|
for($t = 0; $t < $length and !$rval; ++$t)
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$type = $types[$t];
|
2011-07-14 06:47:40 +02:00
|
|
|
foreach($tmp as $e)
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
if($e === $type)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
$rval = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
2011-07-25 23:40:17 +02:00
|
|
|
/**
|
|
|
|
* Filters non-keywords.
|
|
|
|
*
|
|
|
|
* @param e the element to check.
|
|
|
|
*
|
|
|
|
* @return true if the element is a non-keyword.
|
|
|
|
*/
|
|
|
|
function _filterNonKeyWords($e)
|
|
|
|
{
|
|
|
|
return strpos($e, '@') !== 0;
|
|
|
|
}
|
|
|
|
|
2011-07-14 06:47:40 +02:00
|
|
|
/**
|
|
|
|
* Returns true if the given input matches the given frame via duck-typing.
|
|
|
|
*
|
|
|
|
* @param input the input.
|
|
|
|
* @param frame the frame to check against.
|
|
|
|
*
|
|
|
|
* @return true if the input matches the frame.
|
|
|
|
*/
|
|
|
|
function _isDuckType($input, $frame)
|
|
|
|
{
|
|
|
|
$rval = false;
|
|
|
|
|
|
|
|
// frame must not have a specific type
|
2011-11-29 05:12:11 +01:00
|
|
|
if(!property_exists($frame, '@type'))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
// get frame properties that must exist on input
|
2011-07-25 23:40:17 +02:00
|
|
|
$props = array_filter(array_keys((array)$frame), '_filterNonKeywords');
|
2011-07-14 06:47:40 +02:00
|
|
|
if(count($props) === 0)
|
|
|
|
{
|
|
|
|
// input always matches if there are no properties
|
|
|
|
$rval = true;
|
|
|
|
}
|
|
|
|
// input must be a subject with all the given properties
|
2011-12-20 03:29:11 +01:00
|
|
|
else if(is_object($input) and property_exists($input, '@id'))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
$rval = true;
|
|
|
|
foreach($props as $prop)
|
|
|
|
{
|
2011-07-29 20:28:46 +02:00
|
|
|
if(!property_exists($input, $prop))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
$rval = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
2011-11-29 04:51:30 +01:00
|
|
|
/**
|
|
|
|
* Recursively removes dependent dangling embeds.
|
|
|
|
*
|
|
|
|
* @param iri the iri of the parent to remove embeds for.
|
|
|
|
* @param embeds the embeds map.
|
|
|
|
*/
|
|
|
|
function removeDependentEmbeds($iri, $embeds)
|
|
|
|
{
|
|
|
|
$iris = get_object_vars($embeds);
|
|
|
|
foreach($iris as $i => $embed)
|
|
|
|
{
|
|
|
|
if($embed->parent !== null and
|
2011-12-20 03:29:11 +01:00
|
|
|
$embed->parent->{'@id'} === $iri)
|
2011-11-29 04:51:30 +01:00
|
|
|
{
|
|
|
|
unset($embeds->$i);
|
|
|
|
removeDependentEmbeds($i, $embeds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-14 21:56:47 +02:00
|
|
|
/**
|
|
|
|
* Subframes a value.
|
|
|
|
*
|
|
|
|
* @param subjects a map of subjects in the graph.
|
|
|
|
* @param value the value to subframe.
|
|
|
|
* @param frame the frame to use.
|
|
|
|
* @param embeds a map of previously embedded subjects, used to prevent cycles.
|
|
|
|
* @param autoembed true if auto-embed is on, false if not.
|
|
|
|
* @param parent the parent object.
|
|
|
|
* @param parentKey the parent object.
|
|
|
|
* @param options the framing options.
|
|
|
|
*
|
|
|
|
* @return the framed input.
|
|
|
|
*/
|
|
|
|
function _subframe(
|
|
|
|
$subjects, $value, $frame, $embeds, $autoembed,
|
|
|
|
$parent, $parentKey, $options)
|
|
|
|
{
|
|
|
|
// get existing embed entry
|
2011-12-20 03:29:11 +01:00
|
|
|
$iri = $value->{'@id'};
|
2011-10-14 21:56:47 +02:00
|
|
|
$embed = property_exists($embeds, $iri) ? $embeds->{$iri} : null;
|
|
|
|
|
|
|
|
// determine if value should be embedded or referenced,
|
|
|
|
// embed is ON if:
|
|
|
|
// 1. The frame OR default option specifies @embed as ON, AND
|
|
|
|
// 2. There is no existing embed OR it is an autoembed, AND
|
|
|
|
// autoembed mode is off.
|
2011-11-24 02:24:38 +01:00
|
|
|
$embedOn = (
|
2011-10-14 21:56:47 +02:00
|
|
|
((property_exists($frame, '@embed') and $frame->{'@embed'}) or
|
2011-10-21 02:31:38 +02:00
|
|
|
(!property_exists($frame, '@embed') and $options->defaults->embedOn)) and
|
2011-11-24 02:24:38 +01:00
|
|
|
($embed === null or ($embed->autoembed and !$autoembed)));
|
2011-10-14 21:56:47 +02:00
|
|
|
|
|
|
|
if(!$embedOn)
|
|
|
|
{
|
|
|
|
// not embedding, so only use subject IRI as reference
|
2011-12-20 03:29:11 +01:00
|
|
|
$tmp = new stdClass();
|
|
|
|
$tmp->{'@id'} = $value->{'@id'};
|
|
|
|
$value = $tmp;
|
2011-10-14 21:56:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// create new embed entry
|
|
|
|
if($embed === null)
|
|
|
|
{
|
|
|
|
$embed = new stdClass();
|
|
|
|
$embeds->{$iri} = $embed;
|
|
|
|
}
|
2011-10-14 22:24:07 +02:00
|
|
|
// replace the existing embed with a reference
|
2011-11-24 02:24:38 +01:00
|
|
|
else if($embed->parent !== null)
|
2011-10-14 21:56:47 +02:00
|
|
|
{
|
2011-11-24 02:24:38 +01:00
|
|
|
if(is_array($embed->parent->{$embed->key}))
|
|
|
|
{
|
2011-11-29 04:51:30 +01:00
|
|
|
// find and replace embed in array
|
2011-11-24 02:24:38 +01:00
|
|
|
$arrLen = count($embed->parent->{$embed->key});
|
|
|
|
for($i = 0; $i < $arrLen; ++$i)
|
|
|
|
{
|
|
|
|
$obj = $embed->parent->{$embed->key}[$i];
|
2011-12-20 03:29:11 +01:00
|
|
|
if(is_object($obj) and property_exists($obj, '@id') and
|
|
|
|
$obj->{'@id'} === $iri)
|
2011-11-24 02:24:38 +01:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$tmp = new stdClass();
|
|
|
|
$tmp->{'@id'} = $value->{'@id'};
|
|
|
|
$embed->parent->{$embed->key}[$i] = $tmp;
|
2011-11-24 02:24:38 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$tmp = new stdClass();
|
|
|
|
$tmp->{'@id'} = $value->{'@id'};
|
|
|
|
$embed->parent->{$embed->key} = $tmp;
|
2011-11-24 02:24:38 +01:00
|
|
|
}
|
2011-11-29 04:51:30 +01:00
|
|
|
|
|
|
|
// recursively remove any dependent dangling embeds
|
|
|
|
removeDependentEmbeds($iri, $embeds);
|
2011-10-14 21:56:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// update embed entry
|
|
|
|
$embed->autoembed = $autoembed;
|
|
|
|
$embed->parent = $parent;
|
|
|
|
$embed->key = $parentKey;
|
|
|
|
|
|
|
|
// check explicit flag
|
|
|
|
$explicitOn = property_exists($frame, '@explicit') ?
|
|
|
|
$frame->{'@explicit'} : $options->defaults->explicitOn;
|
|
|
|
if($explicitOn)
|
|
|
|
{
|
|
|
|
// remove keys from the value that aren't in the frame
|
|
|
|
foreach($value as $key => $v)
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
// do not remove @id or any frame key
|
|
|
|
if($key !== '@id' and !property_exists($frame, $key))
|
2011-10-14 21:56:47 +02:00
|
|
|
{
|
|
|
|
unset($value->$key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate over keys in value
|
2011-11-24 02:24:38 +01:00
|
|
|
$vars = get_object_vars($value);
|
|
|
|
foreach($vars as $key => $v)
|
2011-10-14 21:56:47 +02:00
|
|
|
{
|
2011-12-18 03:01:13 +01:00
|
|
|
// skip keywords
|
|
|
|
if(strpos($key, '@') !== 0)
|
2011-10-14 21:56:47 +02:00
|
|
|
{
|
|
|
|
// get the subframe if available
|
|
|
|
if(property_exists($frame, $key))
|
|
|
|
{
|
|
|
|
$f = $frame->{$key};
|
|
|
|
$_autoembed = false;
|
|
|
|
}
|
|
|
|
// use a catch-all subframe to preserve data from graph
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$f = is_array($v) ? array() : new stdClass();
|
|
|
|
$_autoembed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// build input and do recursion
|
|
|
|
$input = is_array($v) ? $v : array($v);
|
|
|
|
$length = count($input);
|
|
|
|
for($n = 0; $n < $length; ++$n)
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
// replace reference to subject w/embedded subject
|
2011-10-14 21:56:47 +02:00
|
|
|
if(is_object($input[$n]) and
|
2011-12-20 03:29:11 +01:00
|
|
|
property_exists($input[$n], '@id') and
|
|
|
|
property_exists($subjects, $input[$n]->{'@id'}))
|
2011-10-14 21:56:47 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$input[$n] = $subjects->{$input[$n]->{'@id'}};
|
2011-10-14 21:56:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$value->$key = _frame(
|
|
|
|
$subjects, $input, $f, $embeds, $_autoembed,
|
|
|
|
$value, $key, $options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate over frame keys to add any missing values
|
|
|
|
foreach($frame as $key => $f)
|
|
|
|
{
|
2011-12-18 03:01:13 +01:00
|
|
|
// skip keywords and non-null keys in value
|
|
|
|
if(strpos($key, '@') !== 0 and
|
2011-10-20 22:46:20 +02:00
|
|
|
(!property_exists($value, $key) || $value->{$key} === null))
|
2011-10-14 21:56:47 +02:00
|
|
|
{
|
|
|
|
// add empty array to value
|
|
|
|
if(is_array($f))
|
|
|
|
{
|
|
|
|
// add empty array/null property to value
|
|
|
|
$value->$key = array();
|
|
|
|
}
|
|
|
|
// add default value to value
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// use first subframe if frame is an array
|
|
|
|
if(is_array($f))
|
|
|
|
{
|
|
|
|
$f = (count($f) > 0) ? $f[0] : new stdClass();
|
|
|
|
}
|
|
|
|
|
|
|
|
// determine if omit default is on
|
|
|
|
$omitOn = property_exists($f, '@omitDefault') ?
|
|
|
|
$f->{'@omitDefault'} :
|
|
|
|
$options->defaults->omitDefaultOn;
|
|
|
|
if(!$omitOn)
|
|
|
|
{
|
|
|
|
if(property_exists($f, '@default'))
|
|
|
|
{
|
|
|
|
// use specified default value
|
|
|
|
$value->{$key} = $f->{'@default'};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// build-in default value is: null
|
|
|
|
$value->{$key} = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2011-07-14 06:47:40 +02:00
|
|
|
/**
|
|
|
|
* Recursively frames the given input according to the given frame.
|
|
|
|
*
|
|
|
|
* @param subjects a map of subjects in the graph.
|
|
|
|
* @param input the input to frame.
|
|
|
|
* @param frame the frame to use.
|
|
|
|
* @param embeds a map of previously embedded subjects, used to prevent cycles.
|
2011-10-14 21:56:47 +02:00
|
|
|
* @param autoembed true if auto-embed is on, false if not.
|
|
|
|
* @param parent the parent object (for subframing), null for none.
|
|
|
|
* @param parentKey the parent key (for subframing), null for none.
|
2011-07-14 06:47:40 +02:00
|
|
|
* @param options the framing options.
|
|
|
|
*
|
|
|
|
* @return the framed input.
|
|
|
|
*/
|
2011-10-14 21:56:47 +02:00
|
|
|
function _frame(
|
|
|
|
$subjects, $input, $frame, $embeds, $autoembed,
|
|
|
|
$parent, $parentKey, $options)
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
|
|
|
$rval = null;
|
|
|
|
|
|
|
|
// prepare output, set limit, get array of frames
|
|
|
|
$limit = -1;
|
|
|
|
if(is_array($frame))
|
|
|
|
{
|
|
|
|
$rval = array();
|
|
|
|
$frames = $frame;
|
2011-07-25 23:40:17 +02:00
|
|
|
if(count($frames) == 0)
|
|
|
|
{
|
|
|
|
$frames[] = new stdClass();
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$frames = array($frame);
|
|
|
|
$limit = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate over frames adding input matches to list
|
|
|
|
$frameLen = count($frames);
|
|
|
|
$values = array();
|
|
|
|
for($i = 0; $i < $frameLen and $limit !== 0; ++$i)
|
|
|
|
{
|
|
|
|
// get next frame
|
|
|
|
$frame = $frames[$i];
|
|
|
|
if(!is_object($frame))
|
|
|
|
{
|
|
|
|
throw new Exception(
|
|
|
|
'Invalid JSON-LD frame. Frame type is not a map or array.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// create array of values for each frame
|
|
|
|
$inLen = count($input);
|
|
|
|
$v = array();
|
|
|
|
for($n = 0; $n < $inLen and $limit !== 0; ++$n)
|
|
|
|
{
|
2011-07-25 23:40:17 +02:00
|
|
|
// dereference input if it refers to a subject
|
|
|
|
$next = $input[$n];
|
2011-12-20 03:29:11 +01:00
|
|
|
if(is_object($next) and property_exists($next, '@id') and
|
|
|
|
property_exists($subjects, $next->{'@id'}))
|
2011-07-25 23:40:17 +02:00
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
$next = $subjects->{$next->{'@id'}};
|
2011-07-25 23:40:17 +02:00
|
|
|
}
|
|
|
|
|
2011-07-14 06:47:40 +02:00
|
|
|
// add input to list if it matches frame specific type or duck-type
|
2011-07-25 23:40:17 +02:00
|
|
|
if(_isType($next, $frame) or _isDuckType($next, $frame))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-07-25 23:40:17 +02:00
|
|
|
$v[] = $next;
|
2011-07-14 06:47:40 +02:00
|
|
|
--$limit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$values[$i] = $v;
|
|
|
|
}
|
|
|
|
|
|
|
|
// for each matching value, add it to the output
|
|
|
|
$vaLen = count($values);
|
|
|
|
for($i1 = 0; $i1 < $vaLen; ++$i1)
|
|
|
|
{
|
|
|
|
foreach($values[$i1] as $value)
|
|
|
|
{
|
|
|
|
$frame = $frames[$i1];
|
|
|
|
|
2011-10-14 21:56:47 +02:00
|
|
|
// if value is a subject, do subframing
|
2011-12-20 03:29:11 +01:00
|
|
|
if(_isSubject($value))
|
2011-07-14 06:47:40 +02:00
|
|
|
{
|
2011-10-14 21:56:47 +02:00
|
|
|
$value = _subframe(
|
|
|
|
$subjects, $value, $frame, $embeds, $autoembed,
|
|
|
|
$parent, $parentKey, $options);
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// add value to output
|
|
|
|
if($rval === null)
|
|
|
|
{
|
|
|
|
$rval = $value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-20 03:29:11 +01:00
|
|
|
// determine if value is a reference to an embed
|
|
|
|
$isRef = (_isReference($value) and
|
|
|
|
property_exists($embeds, $value->{'@id'}));
|
2011-11-24 02:24:38 +01:00
|
|
|
|
|
|
|
// push any value that isn't a parentless reference
|
|
|
|
if(!($parent === null and $isRef))
|
|
|
|
{
|
|
|
|
$rval[] = $value;
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|