Switch style to same-line bracket w/else and catch.

This commit is contained in:
Dave Longley 2014-04-24 14:39:30 -04:00
commit 1aac75aab7
2 changed files with 214 additions and 370 deletions

View file

@ -1,12 +1,12 @@
<?php <?php
/** /**
* PHP implementation of the JSON-LD API. * PHP implementation of the JSON-LD API.
* Version: 0.3.1 * Version: 0.3.2
* *
* @author Dave Longley * @author Dave Longley
* *
* BSD 3-Clause License * BSD 3-Clause License
* Copyright (c) 2011-2013 Digital Bazaar, Inc. * Copyright (c) 2011-2014 Digital Bazaar, Inc.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -234,11 +234,9 @@ function jsonld_parse_link_header($header) {
$rel = property_exists($result, 'rel') ? $result->rel : ''; $rel = property_exists($result, 'rel') ? $result->rel : '';
if(!isset($rval[$rel])) { if(!isset($rval[$rel])) {
$rval[$rel] = $result; $rval[$rel] = $result;
} } else if(is_array($rval[$rel])) {
else if(is_array($rval[$rel])) {
$rval[$rel][] = $result; $rval[$rel][] = $result;
} } else {
else {
$rval[$rel] = array($rval[$rel], $result); $rval[$rel] = array($rval[$rel], $result);
} }
} }
@ -287,8 +285,7 @@ function jsonld_get_url($url) {
global $jsonld_default_load_document; global $jsonld_default_load_document;
if($jsonld_default_load_document !== null) { if($jsonld_default_load_document !== null) {
$document_loader = $jsonld_default_load_document; $document_loader = $jsonld_default_load_document;
} } else {
else {
$document_loader = $jsonld_default_document_loader; $document_loader = $jsonld_default_document_loader;
} }
@ -349,8 +346,7 @@ function jsonld_default_document_loader($url) {
$link_header = jsonld_parse_link_header(join(',', $link_header)); $link_header = jsonld_parse_link_header(join(',', $link_header));
if(isset($link_header['http://www.w3.org/ns/json-ld#context'])) { if(isset($link_header['http://www.w3.org/ns/json-ld#context'])) {
$link_header = $link_header['http://www.w3.org/ns/json-ld#context']; $link_header = $link_header['http://www.w3.org/ns/json-ld#context'];
} } else {
else {
$link_header = null; $link_header = null;
} }
if($link_header && $content_type !== 'application/ld+json') { if($link_header && $content_type !== 'application/ld+json') {
@ -431,8 +427,7 @@ function jsonld_default_secure_document_loader($url) {
$link_header = jsonld_parse_link_header(join(',', $link_header)); $link_header = jsonld_parse_link_header(join(',', $link_header));
if(isset($link_header['http://www.w3.org/ns/json-ld#context'])) { if(isset($link_header['http://www.w3.org/ns/json-ld#context'])) {
$link_header = $link_header['http://www.w3.org/ns/json-ld#context']; $link_header = $link_header['http://www.w3.org/ns/json-ld#context'];
} } else {
else {
$link_header = null; $link_header = null;
} }
if($link_header && $content_type !== 'application/ld+json') { if($link_header && $content_type !== 'application/ld+json') {
@ -513,8 +508,7 @@ function jsonld_parse_url($url) {
if(!isset($rval['scheme'])) { if(!isset($rval['scheme'])) {
$rval['scheme'] = ''; $rval['scheme'] = '';
$rval['protocol'] = ''; $rval['protocol'] = '';
} } else {
else {
$rval['protocol'] = $rval['scheme'] . ':'; $rval['protocol'] = $rval['scheme'] . ':';
} }
if(!isset($rval['host'])) { if(!isset($rval['host'])) {
@ -541,13 +535,11 @@ function jsonld_parse_url($url) {
if($idx === false) { if($idx === false) {
$rval['authority'] = $rval['path']; $rval['authority'] = $rval['path'];
$rval['path'] = ''; $rval['path'] = '';
} } else {
else {
$rval['authority'] = substr($rval['path'], 0, $idx); $rval['authority'] = substr($rval['path'], 0, $idx);
$rval['path'] = substr($rval['path'], $idx); $rval['path'] = substr($rval['path'], $idx);
} }
} } else {
else {
$rval['authority'] = $rval['host']; $rval['authority'] = $rval['host'];
if(isset($rval['port'])) { if(isset($rval['port'])) {
$rval['authority'] .= ":{$rval['port']}"; $rval['authority'] .= ":{$rval['port']}";
@ -588,9 +580,8 @@ function jsonld_remove_dot_segments($path, $has_authority) {
if($has_authority || if($has_authority ||
(count($output) > 0 && $output[count($output) - 1] !== '..')) { (count($output) > 0 && $output[count($output) - 1] !== '..')) {
array_pop($output); array_pop($output);
} } else {
// leading relative URL '..' // leading relative URL '..'
else {
$output[] = '..'; $output[] = '..';
} }
continue; continue;
@ -632,8 +623,7 @@ function jsonld_prepend_base($base, $iri) {
$hierPart = $base['protocol']; $hierPart = $base['protocol'];
if($rel['authority']) { if($rel['authority']) {
$hierPart .= "//{$rel['authority']}"; $hierPart .= "//{$rel['authority']}";
} } else if($base['href'] !== '') {
else if($base['href'] !== '') {
$hierPart .= "//{$base['authority']}"; $hierPart .= "//{$base['authority']}";
} }
@ -642,8 +632,7 @@ function jsonld_prepend_base($base, $iri) {
// IRI represents an absolute path // IRI represents an absolute path
if(strpos($rel['path'], '/') === 0) { if(strpos($rel['path'], '/') === 0) {
$path = $rel['path']; $path = $rel['path'];
} } else {
else {
$path = $base['path']; $path = $base['path'];
// append relative path to the end of the last directory from base // append relative path to the end of the last directory from base
@ -701,9 +690,8 @@ function jsonld_remove_base($base, $iri) {
$root = ''; $root = '';
if($base['href'] !== '') { if($base['href'] !== '') {
$root .= "{$base['protocol']}//{$base['authority']}"; $root .= "{$base['protocol']}//{$base['authority']}";
} } else if(strpos($iri, '//') === false) {
// support network-path reference with empty base // support network-path reference with empty base
else if(strpos($iri, '//') === false) {
$root .= '//'; $root .= '//';
} }
@ -827,13 +815,11 @@ class JsonLdProcessor {
if($options['skipExpansion'] === true) { if($options['skipExpansion'] === true) {
$expanded = $input; $expanded = $input;
} } else {
else {
// expand input // expand input
try { try {
$expanded = $this->expand($input, $options); $expanded = $this->expand($input, $options);
} } catch(JsonLdException $e) {
catch(JsonLdException $e) {
throw new JsonLdException( throw new JsonLdException(
'Could not expand input before compaction.', 'Could not expand input before compaction.',
'jsonld.CompactError', null, null, $e); 'jsonld.CompactError', null, null, $e);
@ -844,8 +830,7 @@ class JsonLdProcessor {
$active_ctx = $this->_getInitialContext($options); $active_ctx = $this->_getInitialContext($options);
try { try {
$active_ctx = $this->processContext($active_ctx, $ctx, $options); $active_ctx = $this->processContext($active_ctx, $ctx, $options);
} } catch(JsonLdException $e) {
catch(JsonLdException $e) {
throw new JsonLdException( throw new JsonLdException(
'Could not process context before compaction.', 'Could not process context before compaction.',
'jsonld.CompactError', null, null, $e); 'jsonld.CompactError', null, null, $e);
@ -856,17 +841,15 @@ class JsonLdProcessor {
if($options['compactArrays'] && if($options['compactArrays'] &&
!$options['graph'] && is_array($compacted)) { !$options['graph'] && is_array($compacted)) {
// simplify to a single item
if(count($compacted) === 1) { if(count($compacted) === 1) {
// simplify to a single item
$compacted = $compacted[0]; $compacted = $compacted[0];
} } else if(count($compacted) === 0) {
// simplify to an empty object // simplify to an empty object
else if(count($compacted) === 0) {
$compacted = new stdClass(); $compacted = new stdClass();
} }
} } else if($options['graph']) {
// always use array if graph option is on // always use array if graph option is on
else if($options['graph']) {
$compacted = self::arrayify($compacted); $compacted = self::arrayify($compacted);
} }
@ -905,8 +888,7 @@ class JsonLdProcessor {
$compacted->{'@context'} = $ctx; $compacted->{'@context'} = $ctx;
} }
$compacted->{$kwgraph} = $graph; $compacted->{$kwgraph} = $graph;
} } else if(is_object($compacted) && $has_context) {
else if(is_object($compacted) && $has_context) {
// reorder keys so @context is first // reorder keys so @context is first
$graph = $compacted; $graph = $compacted;
$compacted = new stdClass(); $compacted = new stdClass();
@ -944,8 +926,7 @@ class JsonLdProcessor {
// if input is a string, attempt to dereference remote document // if input is a string, attempt to dereference remote document
if(is_string($input)) { if(is_string($input)) {
$remote_doc = call_user_func($options['documentLoader'], $input); $remote_doc = call_user_func($options['documentLoader'], $input);
} } else {
else {
$remote_doc = (object)array( $remote_doc = (object)array(
'contextUrl' => null, 'contextUrl' => null,
'documentUrl' => null, 'documentUrl' => null,
@ -961,8 +942,7 @@ class JsonLdProcessor {
if(is_string($remote_doc->document)) { if(is_string($remote_doc->document)) {
$remote_doc->document = self::_parse_json($remote_doc->document); $remote_doc->document = self::_parse_json($remote_doc->document);
} }
} } catch(Exception $e) {
catch(Exception $e) {
throw new JsonLdException( throw new JsonLdException(
'Could not retrieve a JSON-LD document from the URL.', 'Could not retrieve a JSON-LD document from the URL.',
'jsonld.LoadDocumentError', 'loading document failed', 'jsonld.LoadDocumentError', 'loading document failed',
@ -982,8 +962,7 @@ class JsonLdProcessor {
if(is_object($expand_context) && if(is_object($expand_context) &&
property_exists($expand_context, '@context')) { property_exists($expand_context, '@context')) {
$input->expandContext = $expand_context; $input->expandContext = $expand_context;
} } else {
else {
$input->expandContext = (object)array('@context' => $expand_context); $input->expandContext = (object)array('@context' => $expand_context);
} }
} }
@ -992,8 +971,7 @@ class JsonLdProcessor {
try { try {
$this->_retrieveContextUrls( $this->_retrieveContextUrls(
$input, new stdClass(), $options['documentLoader'], $options['base']); $input, new stdClass(), $options['documentLoader'], $options['base']);
} } catch(Exception $e) {
catch(Exception $e) {
throw new JsonLdException( throw new JsonLdException(
'Could not perform JSON-LD expansion.', 'Could not perform JSON-LD expansion.',
'jsonld.ExpandError', null, null, $e); 'jsonld.ExpandError', null, null, $e);
@ -1022,8 +1000,7 @@ class JsonLdProcessor {
if(is_object($expanded) && property_exists($expanded, '@graph') && if(is_object($expanded) && property_exists($expanded, '@graph') &&
count(array_keys((array)$expanded)) === 1) { count(array_keys((array)$expanded)) === 1) {
$expanded = $expanded->{'@graph'}; $expanded = $expanded->{'@graph'};
} } else if($expanded === null) {
else if($expanded === null) {
$expanded = array(); $expanded = array();
} }
// normalize to an array // normalize to an array
@ -1050,8 +1027,7 @@ class JsonLdProcessor {
try { try {
// expand input // expand input
$expanded = $this->expand($input, $options); $expanded = $this->expand($input, $options);
} } catch(Exception $e) {
catch(Exception $e) {
throw new JsonLdException( throw new JsonLdException(
'Could not expand input before flattening.', 'Could not expand input before flattening.',
'jsonld.FlattenError', null, null, $e); 'jsonld.FlattenError', null, null, $e);
@ -1069,8 +1045,7 @@ class JsonLdProcessor {
$options['skipExpansion'] = true; $options['skipExpansion'] = true;
try { try {
$compacted = $this->compact($flattened, $ctx, $options); $compacted = $this->compact($flattened, $ctx, $options);
} } catch(Exception $e) {
catch(Exception $e) {
throw new JsonLdException( throw new JsonLdException(
'Could not compact flattened output.', 'Could not compact flattened output.',
'jsonld.FlattenError', null, null, $e); 'jsonld.FlattenError', null, null, $e);
@ -1106,8 +1081,7 @@ class JsonLdProcessor {
// if frame is a string, attempt to dereference remote document // if frame is a string, attempt to dereference remote document
if(is_string($frame)) { if(is_string($frame)) {
$remote_frame = call_user_func($options['documentLoader'], $frame); $remote_frame = call_user_func($options['documentLoader'], $frame);
} } else {
else {
$remote_frame = (object)array( $remote_frame = (object)array(
'contextUrl' => null, 'contextUrl' => null,
'documentUrl' => null, 'documentUrl' => null,
@ -1123,8 +1097,7 @@ class JsonLdProcessor {
if(is_string($remote_frame->document)) { if(is_string($remote_frame->document)) {
$remote_frame->document = self::_parse_json($remote_frame->document); $remote_frame->document = self::_parse_json($remote_frame->document);
} }
} } catch(Exception $e) {
catch(Exception $e) {
throw new JsonLdException( throw new JsonLdException(
'Could not retrieve a JSON-LD document from the URL.', 'Could not retrieve a JSON-LD document from the URL.',
'jsonld.LoadDocumentError', 'loading document failed', 'jsonld.LoadDocumentError', 'loading document failed',
@ -1139,8 +1112,7 @@ class JsonLdProcessor {
if($remote_frame->contextUrl !== null) { if($remote_frame->contextUrl !== null) {
if($ctx !== null) { if($ctx !== null) {
$ctx = $remote_frame->contextUrl; $ctx = $remote_frame->contextUrl;
} } else {
else {
$ctx = self::arrayify($ctx); $ctx = self::arrayify($ctx);
$ctx[] = $remote_frame->contextUrl; $ctx[] = $remote_frame->contextUrl;
} }
@ -1151,8 +1123,7 @@ class JsonLdProcessor {
try { try {
// expand input // expand input
$expanded = $this->expand($input, $options); $expanded = $this->expand($input, $options);
} } catch(Exception $e) {
catch(Exception $e) {
throw new JsonLdException( throw new JsonLdException(
'Could not expand input before framing.', 'Could not expand input before framing.',
'jsonld.FrameError', null, null, $e); 'jsonld.FrameError', null, null, $e);
@ -1163,8 +1134,7 @@ class JsonLdProcessor {
$opts = $options; $opts = $options;
$opts['keepFreeFloatingNodes'] = true; $opts['keepFreeFloatingNodes'] = true;
$expanded_frame = $this->expand($frame, $opts); $expanded_frame = $this->expand($frame, $opts);
} } catch(Exception $e) {
catch(Exception $e) {
throw new JsonLdException( throw new JsonLdException(
'Could not expand frame before framing.', 'Could not expand frame before framing.',
'jsonld.FrameError', null, null, $e); 'jsonld.FrameError', null, null, $e);
@ -1179,8 +1149,7 @@ class JsonLdProcessor {
$options['skipExpansion'] = true; $options['skipExpansion'] = true;
$options['activeCtx'] = true; $options['activeCtx'] = true;
$result = $this->compact($framed, $ctx, $options); $result = $this->compact($framed, $ctx, $options);
} } catch(Exception $e) {
catch(Exception $e) {
throw new JsonLdException( throw new JsonLdException(
'Could not compact framed output.', 'Could not compact framed output.',
'jsonld.FrameError', null, null, $e); 'jsonld.FrameError', null, null, $e);
@ -1223,8 +1192,7 @@ class JsonLdProcessor {
} }
$opts['produceGeneralizedRdf'] = false; $opts['produceGeneralizedRdf'] = false;
$dataset = $this->toRDF($input, $opts); $dataset = $this->toRDF($input, $opts);
} } catch(Exception $e) {
catch(Exception $e) {
throw new JsonLdException( throw new JsonLdException(
'Could not convert input to RDF dataset before normalization.', 'Could not convert input to RDF dataset before normalization.',
'jsonld.NormalizeError', null, null, $e); 'jsonld.NormalizeError', null, null, $e);
@ -1274,8 +1242,7 @@ class JsonLdProcessor {
} }
if($this->rdfParsers !== null) { if($this->rdfParsers !== null) {
$callable = $this->rdfParsers->{$options['format']}; $callable = $this->rdfParsers->{$options['format']};
} } else {
else {
$callable = $jsonld_rdf_parsers->{$options['format']}; $callable = $jsonld_rdf_parsers->{$options['format']};
} }
$dataset = call_user_func($callable, $dataset); $dataset = call_user_func($callable, $dataset);
@ -1309,8 +1276,7 @@ class JsonLdProcessor {
try { try {
// expand input // expand input
$expanded = $this->expand($input, $options); $expanded = $this->expand($input, $options);
} } catch(JsonLdException $e) {
catch(JsonLdException $e) {
throw new JsonLdException( throw new JsonLdException(
'Could not expand input before serialization to RDF.', 'Could not expand input before serialization to RDF.',
'jsonld.RdfError', null, null, $e); 'jsonld.RdfError', null, null, $e);
@ -1340,8 +1306,7 @@ class JsonLdProcessor {
// supported formats // supported formats
if($options['format'] === 'application/nquads') { if($options['format'] === 'application/nquads') {
$rval = self::toNQuads($dataset); $rval = self::toNQuads($dataset);
} } else {
else {
throw new JsonLdException( throw new JsonLdException(
'Unknown output format.', 'jsonld.UnknownFormat', 'Unknown output format.', 'jsonld.UnknownFormat',
null, array('format' => $options['format'])); null, array('format' => $options['format']));
@ -1382,8 +1347,7 @@ class JsonLdProcessor {
$this->_retrieveContextUrls( $this->_retrieveContextUrls(
$local_ctx, new stdClass(), $local_ctx, new stdClass(),
$options['documentLoader'], $options['base']); $options['documentLoader'], $options['base']);
} } catch(Exception $e) {
catch(Exception $e) {
throw new JsonLdException( throw new JsonLdException(
'Could not process JSON-LD context.', 'Could not process JSON-LD context.',
'jsonld.ContextError', null, null, $e); 'jsonld.ContextError', null, null, $e);
@ -1434,9 +1398,8 @@ class JsonLdProcessor {
break; break;
} }
} }
} } else if(!is_array($value)) {
// avoid matching the set of values with an array value parameter // avoid matching the set of values with an array value parameter
else if(!is_array($value)) {
$rval = self::compareValues($value, $val); $rval = self::compareValues($value, $val);
} }
} }
@ -1475,8 +1438,7 @@ class JsonLdProcessor {
foreach($value as $v) { foreach($value as $v) {
self::addValue($subject, $property, $v, $options); self::addValue($subject, $property, $v, $options);
} }
} } else if(property_exists($subject, $property)) {
else if(property_exists($subject, $property)) {
// check if subject already has value if duplicates not allowed // check if subject already has value if duplicates not allowed
$has_value = (!$options['allowDuplicate'] && $has_value = (!$options['allowDuplicate'] &&
self::hasValue($subject, $property, $value)); self::hasValue($subject, $property, $value));
@ -1491,8 +1453,7 @@ class JsonLdProcessor {
if(!$has_value) { if(!$has_value) {
$subject->{$property}[] = $value; $subject->{$property}[] = $value;
} }
} } else {
else {
// add new value as set or single value // add new value as set or single value
$subject->{$property} = ($options['propertyIsArray'] ? $subject->{$property} = ($options['propertyIsArray'] ?
array($value) : $value); array($value) : $value);
@ -1547,11 +1508,9 @@ class JsonLdProcessor {
if(count($values) === 0) { if(count($values) === 0) {
self::removeProperty($subject, $property); self::removeProperty($subject, $property);
} } else if(count($values) === 1 && !$options['propertyIsArray']) {
else if(count($values) === 1 && !$options['propertyIsArray']) {
$subject->{$property} = $values[0]; $subject->{$property} = $values[0];
} } else {
else {
$subject->{$property} = $values; $subject->{$property} = $values;
} }
} }
@ -1625,12 +1584,11 @@ class JsonLdProcessor {
return null; return null;
} }
// return whole entry
if($type === null) { if($type === null) {
// return whole entry
$rval = $entry; $rval = $entry;
} } else if(property_exists($entry, $type)) {
// return entry value for type // return entry value for type
else if(property_exists($entry, $type)) {
$rval = $entry->{$type}; $rval = $entry->{$type};
} }
} }
@ -1697,8 +1655,7 @@ class JsonLdProcessor {
if($match[1] !== '') { if($match[1] !== '') {
$triple->subject->type = 'IRI'; $triple->subject->type = 'IRI';
$triple->subject->value = $match[1]; $triple->subject->value = $match[1];
} } else {
else {
$triple->subject->type = 'blank node'; $triple->subject->type = 'blank node';
$triple->subject->value = $match[2]; $triple->subject->value = $match[2];
} }
@ -1711,12 +1668,10 @@ class JsonLdProcessor {
if($match[4] !== '') { if($match[4] !== '') {
$triple->object->type = 'IRI'; $triple->object->type = 'IRI';
$triple->object->value = $match[4]; $triple->object->value = $match[4];
} } else if($match[5] !== '') {
else if($match[5] !== '') {
$triple->object->type = 'blank node'; $triple->object->type = 'blank node';
$triple->object->value = $match[5]; $triple->object->value = $match[5];
} } else {
else {
$triple->object->type = 'literal'; $triple->object->type = 'literal';
$unescaped = str_replace( $unescaped = str_replace(
array('\"', '\t', '\n', '\r', '\\\\'), array('\"', '\t', '\n', '\r', '\\\\'),
@ -1724,12 +1679,10 @@ class JsonLdProcessor {
$match[6]); $match[6]);
if(isset($match[7]) && $match[7] !== '') { if(isset($match[7]) && $match[7] !== '') {
$triple->object->datatype = $match[7]; $triple->object->datatype = $match[7];
} } else if(isset($match[8]) && $match[8] !== '') {
else if(isset($match[8]) && $match[8] !== '') {
$triple->object->datatype = self::RDF_LANGSTRING; $triple->object->datatype = self::RDF_LANGSTRING;
$triple->object->language = $match[8]; $triple->object->language = $match[8];
} } else {
else {
$triple->object->datatype = self::XSD_STRING; $triple->object->datatype = self::XSD_STRING;
} }
$triple->object->value = $unescaped; $triple->object->value = $unescaped;
@ -1739,17 +1692,15 @@ class JsonLdProcessor {
$name = '@default'; $name = '@default';
if(isset($match[9]) && $match[9] !== '') { if(isset($match[9]) && $match[9] !== '') {
$name = $match[9]; $name = $match[9];
} } else if(isset($match[10]) && $match[10] !== '') {
else if(isset($match[10]) && $match[10] !== '') {
$name = $match[10]; $name = $match[10];
} }
// initialize graph in dataset // initialize graph in dataset
if(!property_exists($dataset, $name)) { if(!property_exists($dataset, $name)) {
$dataset->{$name} = array($triple); $dataset->{$name} = array($triple);
} } else {
// add triple if unique to its graph // add triple if unique to its graph
else {
$unique = true; $unique = true;
$triples = &$dataset->{$name}; $triples = &$dataset->{$name};
foreach($triples as $t) { foreach($triples as $t) {
@ -1810,13 +1761,11 @@ class JsonLdProcessor {
// subject is an IRI // subject is an IRI
if($s->type === 'IRI') { if($s->type === 'IRI') {
$quad .= "<{$s->value}>"; $quad .= "<{$s->value}>";
} } else if($bnode !== null) {
// bnode normalization mode // bnode normalization mode
else if($bnode !== null) {
$quad .= ($s->value === $bnode) ? '_:a' : '_:z'; $quad .= ($s->value === $bnode) ? '_:a' : '_:z';
} } else {
// bnode normal mode // bnode normal mode
else {
$quad .= $s->value; $quad .= $s->value;
} }
$quad .= ' '; $quad .= ' ';
@ -1824,14 +1773,12 @@ class JsonLdProcessor {
// predicate is an IRI // predicate is an IRI
if($p->type === 'IRI') { if($p->type === 'IRI') {
$quad .= "<{$p->value}>"; $quad .= "<{$p->value}>";
} } else if($bnode !== null) {
// FIXME: TBD what to do with bnode predicates during normalization // FIXME: TBD what to do with bnode predicates during normalization
// bnode normalization mode // bnode normalization mode
else if($bnode !== null) {
$quad .= '_:p'; $quad .= '_:p';
} } else {
// bnode normal mode // bnode normal mode
else {
$quad .= $p->value; $quad .= $p->value;
} }
$quad .= ' '; $quad .= ' ';
@ -1839,18 +1786,15 @@ class JsonLdProcessor {
// object is IRI, bnode, or literal // object is IRI, bnode, or literal
if($o->type === 'IRI') { if($o->type === 'IRI') {
$quad .= "<{$o->value}>"; $quad .= "<{$o->value}>";
} } else if($o->type === 'blank node') {
else if($o->type === 'blank node') {
// normalization mode
if($bnode !== null) { if($bnode !== null) {
// normalization mode
$quad .= ($o->value === $bnode) ? '_:a' : '_:z'; $quad .= ($o->value === $bnode) ? '_:a' : '_:z';
} } else {
// normal mode // normal mode
else {
$quad .= $o->value; $quad .= $o->value;
} }
} } else {
else {
$escaped = str_replace( $escaped = str_replace(
array('\\', "\t", "\n", "\r", '"'), array('\\', "\t", "\n", "\r", '"'),
array('\\\\', '\t', '\n', '\r', '\"'), array('\\\\', '\t', '\n', '\r', '\"'),
@ -1860,8 +1804,7 @@ class JsonLdProcessor {
if($o->language) { if($o->language) {
$quad .= "@{$o->language}"; $quad .= "@{$o->language}";
} }
} } else if($o->datatype !== self::XSD_STRING) {
else if($o->datatype !== self::XSD_STRING) {
$quad .= "^^<{$o->datatype}>"; $quad .= "^^<{$o->datatype}>";
} }
} }
@ -1870,11 +1813,9 @@ class JsonLdProcessor {
if($g !== null) { if($g !== null) {
if(strpos($g, '_:') !== 0) { if(strpos($g, '_:') !== 0) {
$quad .= " <$g>"; $quad .= " <$g>";
} } else if($bnode) {
else if($bnode) {
$quad .= ' _:g'; $quad .= ' _:g';
} } else {
else {
$quad .= " $g"; $quad .= " $g";
} }
} }
@ -2020,14 +1961,13 @@ class JsonLdProcessor {
// compact @id and @type(s) // compact @id and @type(s)
if($expanded_property === '@id' || $expanded_property === '@type') { if($expanded_property === '@id' || $expanded_property === '@type') {
// compact single @id
if(is_string($expanded_value)) { if(is_string($expanded_value)) {
// compact single @id
$compacted_value = $this->_compactIri( $compacted_value = $this->_compactIri(
$active_ctx, $expanded_value, null, $active_ctx, $expanded_value, null,
array('vocab' => ($expanded_property === '@type'))); array('vocab' => ($expanded_property === '@type')));
} } else {
// expanded value must be a @type array // expanded value must be a @type array
else {
$compacted_value = array(); $compacted_value = array();
foreach($expanded_value as $ev) { foreach($expanded_value as $ev) {
$compacted_value[] = $this->_compactIri( $compacted_value[] = $this->_compactIri(
@ -2139,9 +2079,8 @@ class JsonLdProcessor {
$compacted_item->{$this->_compactIri($active_ctx, '@index')} = $compacted_item->{$this->_compactIri($active_ctx, '@index')} =
$expanded_item->{'@index'}; $expanded_item->{'@index'};
} }
} } else if(property_exists($rval, $item_active_property)) {
// can't use @list container for more than 1 list // can't use @list container for more than 1 list
else if(property_exists($rval, $item_active_property)) {
throw new JsonLdException( throw new JsonLdException(
'JSON-LD compact error; property has a "@list" @container ' . 'JSON-LD compact error; property has a "@list" @container ' .
'rule but there is more than a single @list that matches ' . 'rule but there is more than a single @list that matches ' .
@ -2156,8 +2095,7 @@ class JsonLdProcessor {
// get or create the map object // get or create the map object
if(property_exists($rval, $item_active_property)) { if(property_exists($rval, $item_active_property)) {
$map_object = $rval->{$item_active_property}; $map_object = $rval->{$item_active_property};
} } else {
else {
$rval->{$item_active_property} = $map_object = new stdClass(); $rval->{$item_active_property} = $map_object = new stdClass();
} }
@ -2171,8 +2109,7 @@ class JsonLdProcessor {
// based on the container type // based on the container type
self::addValue( self::addValue(
$map_object, $expanded_item->{$container}, $compacted_item); $map_object, $expanded_item->{$container}, $compacted_item);
} } else {
else {
// use an array if: compactArrays flag is false, // use an array if: compactArrays flag is false,
// @container is @set or @list, value is an empty // @container is @set or @list, value is an empty
// array, or key is @graph // array, or key is @graph
@ -2237,8 +2174,7 @@ class JsonLdProcessor {
if($e !== null) { if($e !== null) {
if(is_array($e)) { if(is_array($e)) {
$rval = array_merge($rval, $e); $rval = array_merge($rval, $e);
} } else {
else {
$rval[] = $e; $rval[] = $e;
} }
} }
@ -2392,8 +2328,7 @@ class JsonLdProcessor {
// merge in all reversed properties // merge in all reversed properties
if(property_exists($rval, '@reverse')) { if(property_exists($rval, '@reverse')) {
$reverse_map = $rval->{'@reverse'}; $reverse_map = $rval->{'@reverse'};
} } else {
else {
$reverse_map = null; $reverse_map = null;
} }
foreach($expanded_value as $property => $items) { foreach($expanded_value as $property => $items) {
@ -2425,12 +2360,11 @@ class JsonLdProcessor {
$container = self::getContextValue($active_ctx, $key, '@container'); $container = self::getContextValue($active_ctx, $key, '@container');
// handle language map container (skip if value is not an object)
if($container === '@language' && is_object($value)) { if($container === '@language' && is_object($value)) {
// handle language map container (skip if value is not an object)
$expanded_value = $this->_expandLanguageMap($value); $expanded_value = $this->_expandLanguageMap($value);
} } else if($container === '@index' && is_object($value)) {
// handle index container (skip if value is not an object) // handle index container (skip if value is not an object)
else if($container === '@index' && is_object($value)) {
$expanded_value = array(); $expanded_value = array();
$value_keys = array_keys((array)$value); $value_keys = array_keys((array)$value);
sort($value_keys); sort($value_keys);
@ -2445,8 +2379,7 @@ class JsonLdProcessor {
$expanded_value[] = $item; $expanded_value[] = $item;
} }
} }
} } else {
else {
// recurse into @list or @set // recurse into @list or @set
$is_list = ($expanded_property === '@list'); $is_list = ($expanded_property === '@list');
if($is_list || $expanded_property === '@set') { if($is_list || $expanded_property === '@set') {
@ -2461,8 +2394,7 @@ class JsonLdProcessor {
'Invalid JSON-LD syntax; lists of lists are not permitted.', 'Invalid JSON-LD syntax; lists of lists are not permitted.',
'jsonld.SyntaxError', 'list of lists'); 'jsonld.SyntaxError', 'list of lists');
} }
} } else {
else {
// recursively expand value with key as new active property // recursively expand value with key as new active property
$expanded_value = $this->_expand( $expanded_value = $this->_expand(
$active_ctx, $key, $value, $options, false); $active_ctx, $key, $value, $options, false);
@ -2550,16 +2482,14 @@ class JsonLdProcessor {
// drop null @values // drop null @values
if($rval->{'@value'} === null) { if($rval->{'@value'} === null) {
$rval = null; $rval = null;
} } else if(property_exists($rval, '@language') &&
// if @language is present, @value must be a string
else if(property_exists($rval, '@language') &&
!is_string($rval->{'@value'})) { !is_string($rval->{'@value'})) {
// if @language is present, @value must be a string
throw new JsonLdException( throw new JsonLdException(
'Invalid JSON-LD syntax; only strings may be language-tagged.', 'Invalid JSON-LD syntax; only strings may be language-tagged.',
'jsonld.SyntaxError', 'invalid language-tagged value', 'jsonld.SyntaxError', 'invalid language-tagged value',
array('element' => $rval)); array('element' => $rval));
} } else if(property_exists($rval, '@type') &&
else if(property_exists($rval, '@type') &&
(!self::_isAbsoluteIri($rval->{'@type'}) || (!self::_isAbsoluteIri($rval->{'@type'}) ||
strpos($rval->{'@type'}, '_:') === 0)) { strpos($rval->{'@type'}, '_:') === 0)) {
throw new JsonLdException( throw new JsonLdException(
@ -2568,14 +2498,12 @@ class JsonLdProcessor {
'of "@type".', 'jsonld.SyntaxError', 'invalid typed value', 'of "@type".', 'jsonld.SyntaxError', 'invalid typed value',
array('element' => $rval)); array('element' => $rval));
} }
} } else if(property_exists($rval, '@type') && !is_array($rval->{'@type'})) {
// convert @type to an array // convert @type to an array
else if(property_exists($rval, '@type') && !is_array($rval->{'@type'})) {
$rval->{'@type'} = array($rval->{'@type'}); $rval->{'@type'} = array($rval->{'@type'});
} } else if(property_exists($rval, '@set') ||
// handle @set and @list
else if(property_exists($rval, '@set') ||
property_exists($rval, '@list')) { property_exists($rval, '@list')) {
// handle @set and @list
if($count > 1 && !($count === 2 && property_exists($rval, '@index'))) { if($count > 1 && !($count === 2 && property_exists($rval, '@index'))) {
throw new JsonLdException( throw new JsonLdException(
'Invalid JSON-LD syntax; if an element has the property "@set" ' . 'Invalid JSON-LD syntax; if an element has the property "@set" ' .
@ -2589,9 +2517,8 @@ class JsonLdProcessor {
$keys = array_keys((array)$rval); $keys = array_keys((array)$rval);
$count = count($keys); $count = count($keys);
} }
} } else if($count === 1 && property_exists($rval, '@language')) {
// drop objects with only @language // drop objects with only @language
else if($count === 1 && property_exists($rval, '@language')) {
$rval = null; $rval = null;
} }
@ -2717,8 +2644,7 @@ class JsonLdProcessor {
if(strpos($graph_name, '_:') === 0) { if(strpos($graph_name, '_:') === 0) {
$quad->name = (object)array( $quad->name = (object)array(
'type' => 'blank node', 'value' => $graph_name); 'type' => 'blank node', 'value' => $graph_name);
} } else {
else {
$quad->name = (object)array( $quad->name = (object)array(
'type' => 'IRI', 'value' => $graph_name); 'type' => 'IRI', 'value' => $graph_name);
} }
@ -2731,8 +2657,7 @@ class JsonLdProcessor {
$id = $quad->{$attr}->value; $id = $quad->{$attr}->value;
if(property_exists($bnodes, $id)) { if(property_exists($bnodes, $id)) {
$bnodes->{$id}->quads[] = $quad; $bnodes->{$id}->quads[] = $quad;
} } else {
else {
$bnodes->{$id} = (object)array('quads' => array($quad)); $bnodes->{$id} = (object)array('quads' => array($quad));
} }
} }
@ -2760,14 +2685,12 @@ class JsonLdProcessor {
if(property_exists($duplicates, $hash)) { if(property_exists($duplicates, $hash)) {
$duplicates->{$hash}[] = $bnode; $duplicates->{$hash}[] = $bnode;
$nextUnnamed[] = $bnode; $nextUnnamed[] = $bnode;
} } else if(property_exists($unique, $hash)) {
else if(property_exists($unique, $hash)) {
$duplicates->{$hash} = array($unique->{$hash}, $bnode); $duplicates->{$hash} = array($unique->{$hash}, $bnode);
$nextUnnamed[] = $unique->{$hash}; $nextUnnamed[] = $unique->{$hash};
$nextUnnamed[] = $bnode; $nextUnnamed[] = $bnode;
unset($unique->{$hash}); unset($unique->{$hash});
} } else {
else {
$unique->{$hash} = $bnode; $unique->{$hash} = $bnode;
} }
} }
@ -3087,14 +3010,12 @@ class JsonLdProcessor {
$base = $ctx->{'@base'}; $base = $ctx->{'@base'};
if($base === null) { if($base === null) {
$base = null; $base = null;
} } else if(!is_string($base)) {
else if(!is_string($base)) {
throw new JsonLdException( throw new JsonLdException(
'Invalid JSON-LD syntax; the value of "@base" in a ' . 'Invalid JSON-LD syntax; the value of "@base" in a ' .
'@context must be a string or null.', '@context must be a string or null.',
'jsonld.SyntaxError', 'invalid base IRI', array('context' => $ctx)); 'jsonld.SyntaxError', 'invalid base IRI', array('context' => $ctx));
} } else if($base !== '' && !self::_isAbsoluteIri($base)) {
else if($base !== '' && !self::_isAbsoluteIri($base)) {
throw new JsonLdException( throw new JsonLdException(
'Invalid JSON-LD syntax; the value of "@base" in a ' . 'Invalid JSON-LD syntax; the value of "@base" in a ' .
'@context must be an absolute IRI or the empty string.', '@context must be an absolute IRI or the empty string.',
@ -3112,22 +3033,19 @@ class JsonLdProcessor {
$value = $ctx->{'@vocab'}; $value = $ctx->{'@vocab'};
if($value === null) { if($value === null) {
unset($rval->{'@vocab'}); unset($rval->{'@vocab'});
} } else if(!is_string($value)) {
else if(!is_string($value)) {
throw new JsonLdException( throw new JsonLdException(
'Invalid JSON-LD syntax; the value of "@vocab" in a ' . 'Invalid JSON-LD syntax; the value of "@vocab" in a ' .
'@context must be a string or null.', '@context must be a string or null.',
'jsonld.SyntaxError', 'invalid vocab mapping', 'jsonld.SyntaxError', 'invalid vocab mapping',
array('context' => $ctx)); array('context' => $ctx));
} } else if(!self::_isAbsoluteIri($value)) {
else if(!self::_isAbsoluteIri($value)) {
throw new JsonLdException( throw new JsonLdException(
'Invalid JSON-LD syntax; the value of "@vocab" in a ' . 'Invalid JSON-LD syntax; the value of "@vocab" in a ' .
'@context must be an absolute IRI.', '@context must be an absolute IRI.',
'jsonld.SyntaxError', 'invalid vocab mapping', 'jsonld.SyntaxError', 'invalid vocab mapping',
array('context' => $ctx)); array('context' => $ctx));
} } else {
else {
$rval->{'@vocab'} = $value; $rval->{'@vocab'} = $value;
} }
$defined->{'@vocab'} = true; $defined->{'@vocab'} = true;
@ -3138,15 +3056,13 @@ class JsonLdProcessor {
$value = $ctx->{'@language'}; $value = $ctx->{'@language'};
if($value === null) { if($value === null) {
unset($rval->{'@language'}); unset($rval->{'@language'});
} } else if(!is_string($value)) {
else if(!is_string($value)) {
throw new JsonLdException( throw new JsonLdException(
'Invalid JSON-LD syntax; the value of "@language" in a ' . 'Invalid JSON-LD syntax; the value of "@language" in a ' .
'@context must be a string or null.', '@context must be a string or null.',
'jsonld.SyntaxError', 'invalid default language', 'jsonld.SyntaxError', 'invalid default language',
array('context' => $ctx)); array('context' => $ctx));
} } else {
else {
$rval->{'@language'} = strtolower($value); $rval->{'@language'} = strtolower($value);
} }
$defined->{'@language'} = true; $defined->{'@language'} = true;
@ -3209,12 +3125,10 @@ class JsonLdProcessor {
for($i = 0; $i < $length; ++$i) { for($i = 0; $i < $length; ++$i) {
$element[$i] = $this->_labelBlankNodes($namer, $element[$i]); $element[$i] = $this->_labelBlankNodes($namer, $element[$i]);
} }
} } else if(self::_isList($element)) {
else if(self::_isList($element)) {
$element->{'@list'} = $this->_labelBlankNodes( $element->{'@list'} = $this->_labelBlankNodes(
$namer, $element->{'@list'}); $namer, $element->{'@list'});
} } else if(is_object($element)) {
else if(is_object($element)) {
// rename blank node // rename blank node
if(self::_isBlankNode($element)) { if(self::_isBlankNode($element)) {
$name = null; $name = null;
@ -3258,8 +3172,7 @@ class JsonLdProcessor {
$active_ctx, $active_property, array('vocab' => true)); $active_ctx, $active_property, array('vocab' => true));
if($expanded_property === '@id') { if($expanded_property === '@id') {
return $this->_expandIri($active_ctx, $value, array('base' => true)); return $this->_expandIri($active_ctx, $value, array('base' => true));
} } else if($expanded_property === '@type') {
else if($expanded_property === '@type') {
return $this->_expandIri( return $this->_expandIri(
$active_ctx, $value, array('vocab' => true, 'base' => true)); $active_ctx, $value, array('vocab' => true, 'base' => true));
} }
@ -3289,9 +3202,8 @@ class JsonLdProcessor {
// other type // other type
if($type !== null) { if($type !== null) {
$rval->{'@type'} = $type; $rval->{'@type'} = $type;
} } else if(is_string($value)) {
// check for language tagging for strings // check for language tagging for strings
else if(is_string($value)) {
$language = self::getContextValue( $language = self::getContextValue(
$active_ctx, $active_property, '@language'); $active_ctx, $active_property, '@language');
if($language !== null) { if($language !== null) {
@ -3328,8 +3240,7 @@ class JsonLdProcessor {
$items = $node->{$property}; $items = $node->{$property};
if($property === '@type') { if($property === '@type') {
$property = self::RDF_TYPE; $property = self::RDF_TYPE;
} } else if(self::_isKeyword($property)) {
else if(self::_isKeyword($property)) {
continue; continue;
} }
@ -3356,13 +3267,12 @@ class JsonLdProcessor {
continue; continue;
} }
// convert @list to triples
if(self::_isList($item)) { if(self::_isList($item)) {
// convert @list to triples
$this->_listToRDF( $this->_listToRDF(
$item->{'@list'}, $namer, $subject, $predicate, $rval); $item->{'@list'}, $namer, $subject, $predicate, $rval);
} } else {
// convert value or node object to triple // convert value or node object to triple
else {
$object = $this->_objectToRDF($item); $object = $this->_objectToRDF($item);
// skip null objects (they are relative IRIs) // skip null objects (they are relative IRIs)
if($object) { if($object) {
@ -3441,29 +3351,24 @@ class JsonLdProcessor {
if(is_bool($value)) { if(is_bool($value)) {
$object->value = ($value ? 'true' : 'false'); $object->value = ($value ? 'true' : 'false');
$object->datatype = $datatype ? $datatype : self::XSD_BOOLEAN; $object->datatype = $datatype ? $datatype : self::XSD_BOOLEAN;
} } else if(is_double($value) || $datatype == self::XSD_DOUBLE) {
else if(is_double($value) || $datatype == self::XSD_DOUBLE) {
// canonical double representation // canonical double representation
$object->value = preg_replace( $object->value = preg_replace(
'/(\d)0*E\+?/', '$1E', sprintf('%1.15E', $value)); '/(\d)0*E\+?/', '$1E', sprintf('%1.15E', $value));
$object->datatype = $datatype ? $datatype : self::XSD_DOUBLE; $object->datatype = $datatype ? $datatype : self::XSD_DOUBLE;
} } else if(is_integer($value)) {
else if(is_integer($value)) {
$object->value = strval($value); $object->value = strval($value);
$object->datatype = $datatype ? $datatype : self::XSD_INTEGER; $object->datatype = $datatype ? $datatype : self::XSD_INTEGER;
} } else if(property_exists($item, '@language')) {
else if(property_exists($item, '@language')) {
$object->value = $value; $object->value = $value;
$object->datatype = $datatype ? $datatype : self::RDF_LANGSTRING; $object->datatype = $datatype ? $datatype : self::RDF_LANGSTRING;
$object->language = $item->{'@language'}; $object->language = $item->{'@language'};
} } else {
else {
$object->value = $value; $object->value = $value;
$object->datatype = $datatype ? $datatype : self::XSD_STRING; $object->datatype = $datatype ? $datatype : self::XSD_STRING;
} }
} } else {
// convert string/node object to RDF // convert string/node object to RDF
else {
$id = is_object($item) ? $item->{'@id'} : $item; $id = is_object($item) ? $item->{'@id'} : $item;
$object->type = (strpos($id, '_:') === 0) ? 'blank node' : 'IRI'; $object->type = (strpos($id, '_:') === 0) ? 'blank node' : 'IRI';
$object->value = $id; $object->value = $id;
@ -3494,31 +3399,27 @@ class JsonLdProcessor {
// convert literal object to JSON-LD // convert literal object to JSON-LD
$rval = (object)array('@value' => $o->value); $rval = (object)array('@value' => $o->value);
// add language
if(property_exists($o, 'language')) { if(property_exists($o, 'language')) {
// add language
$rval->{'@language'} = $o->language; $rval->{'@language'} = $o->language;
} } else {
// add datatype // add datatype
else {
$type = $o->datatype; $type = $o->datatype;
// use native types for certain xsd types // use native types for certain xsd types
if($use_native_types) { if($use_native_types) {
if($type === self::XSD_BOOLEAN) { if($type === self::XSD_BOOLEAN) {
if($rval->{'@value'} === 'true') { if($rval->{'@value'} === 'true') {
$rval->{'@value'} = true; $rval->{'@value'} = true;
} } else if($rval->{'@value'} === 'false') {
else if($rval->{'@value'} === 'false') {
$rval->{'@value'} = false; $rval->{'@value'} = false;
} }
} } else if(is_numeric($rval->{'@value'})) {
else if(is_numeric($rval->{'@value'})) {
if($type === self::XSD_INTEGER) { if($type === self::XSD_INTEGER) {
$i = intval($rval->{'@value'}); $i = intval($rval->{'@value'});
if(strval($i) === $rval->{'@value'}) { if(strval($i) === $rval->{'@value'}) {
$rval->{'@value'} = $i; $rval->{'@value'} = $i;
} }
} } else if($type === self::XSD_DOUBLE) {
else if($type === self::XSD_DOUBLE) {
$rval->{'@value'} = doubleval($rval->{'@value'}); $rval->{'@value'} = doubleval($rval->{'@value'});
} }
} }
@ -3528,8 +3429,7 @@ class JsonLdProcessor {
self::XSD_STRING))) { self::XSD_STRING))) {
$rval->{'@type'} = $type; $rval->{'@type'} = $type;
} }
} } else if($type !== self::XSD_STRING) {
else if($type !== self::XSD_STRING) {
$rval->{'@type'} = $type; $rval->{'@type'} = $type;
} }
} }
@ -3615,15 +3515,13 @@ class JsonLdProcessor {
if(!property_exists($subjects, $name)) { if(!property_exists($subjects, $name)) {
if($name === '') { if($name === '') {
$subjects->{'"'} = new stdClass(); $subjects->{'"'} = new stdClass();
} } else {
else {
$subjects->{$name} = new stdClass(); $subjects->{$name} = new stdClass();
} }
} }
if($name === '') { if($name === '') {
$subject = $subjects->{'"'}; $subject = $subjects->{'"'};
} } else {
else {
$subject = $subjects->{$name}; $subject = $subjects->{$name};
} }
$subject->{'@id'} = $name; $subject->{'@id'} = $name;
@ -3717,9 +3615,8 @@ class JsonLdProcessor {
$subject, $property, (object)array('@id' => $id), $subject, $property, (object)array('@id' => $id),
array('propertyIsArray' => true, 'allowDuplicate' => false)); array('propertyIsArray' => true, 'allowDuplicate' => false));
$this->_createNodeMap($o, $graphs, $graph, $namer, $id, null); $this->_createNodeMap($o, $graphs, $graph, $namer, $id, null);
} } else if(self::_isList($o)) {
// handle @list // handle @list
else if(self::_isList($o)) {
$_list = new ArrayObject(); $_list = new ArrayObject();
$this->_createNodeMap( $this->_createNodeMap(
$o->{'@list'}, $graphs, $graph, $namer, $name, $_list); $o->{'@list'}, $graphs, $graph, $namer, $name, $_list);
@ -3727,9 +3624,8 @@ class JsonLdProcessor {
self::addValue( self::addValue(
$subject, $property, $o, $subject, $property, $o,
array('propertyIsArray' => true, 'allowDuplicate' => false)); array('propertyIsArray' => true, 'allowDuplicate' => false));
} } else {
// handle @value // handle @value
else {
$this->_createNodeMap($o, $graphs, $graph, $namer, $name, null); $this->_createNodeMap($o, $graphs, $graph, $namer, $name, null);
self::addValue( self::addValue(
$subject, $property, $o, $subject, $property, $o,
@ -3794,10 +3690,9 @@ class JsonLdProcessor {
break; break;
} }
} }
} } else if(self::hasValue(
// existing embed's parent is an object
else if(self::hasValue(
$existing->parent, $existing->property, $output)) { $existing->parent, $existing->property, $output)) {
// existing embed's parent is an object
$embed_on = true; $embed_on = true;
} }
@ -3810,8 +3705,7 @@ class JsonLdProcessor {
// not embedding, add output without any other properties // not embedding, add output without any other properties
if(!$embed_on) { if(!$embed_on) {
$this->_addFrameOutput($state, $parent, $property, $output); $this->_addFrameOutput($state, $parent, $property, $output);
} } else {
else {
// add embed meta info // add embed meta info
$state->embeds->{$id} = $embed; $state->embeds->{$id} = $embed;
@ -3846,14 +3740,13 @@ class JsonLdProcessor {
// add list objects // add list objects
$src = $o->{'@list'}; $src = $o->{'@list'};
foreach($src as $o) { foreach($src as $o) {
// recurse into subject reference
if(self::_isSubjectReference($o)) { if(self::_isSubjectReference($o)) {
// recurse into subject reference
$this->_matchFrame( $this->_matchFrame(
$state, array($o->{'@id'}), $frame->{$prop}[0]->{'@list'}, $state, array($o->{'@id'}), $frame->{$prop}[0]->{'@list'},
$list, '@list'); $list, '@list');
} } else {
// include other values automatically // include other values automatically
else {
$this->_addFrameOutput( $this->_addFrameOutput(
$state, $list, '@list', self::copy($o)); $state, $list, '@list', self::copy($o));
} }
@ -3861,13 +3754,12 @@ class JsonLdProcessor {
continue; continue;
} }
// recurse into subject reference
if(self::_isSubjectReference($o)) { if(self::_isSubjectReference($o)) {
// recurse into subject reference
$this->_matchFrame( $this->_matchFrame(
$state, array($o->{'@id'}), $frame->{$prop}, $output, $prop); $state, array($o->{'@id'}), $frame->{$prop}, $output, $prop);
} } else {
// include other values automatically // include other values automatically
else {
$this->_addFrameOutput($state, $output, $prop, self::copy($o)); $this->_addFrameOutput($state, $output, $prop, self::copy($o));
} }
} }
@ -4031,9 +3923,8 @@ class JsonLdProcessor {
} }
} }
$this->_addFrameOutput($state, $output, $property, $o); $this->_addFrameOutput($state, $output, $property, $o);
} } else {
// copy non-subject value // copy non-subject value
else {
$this->_addFrameOutput($state, $output, $property, self::copy($o)); $this->_addFrameOutput($state, $output, $property, self::copy($o));
} }
} }
@ -4063,8 +3954,7 @@ class JsonLdProcessor {
break; break;
} }
} }
} } else {
else {
// replace subject with reference // replace subject with reference
$use_array = is_array($embed->parent->{$property}); $use_array = is_array($embed->parent->{$property});
self::removeValue($embed->parent, $property, $subject, self::removeValue($embed->parent, $property, $subject,
@ -4101,8 +3991,7 @@ class JsonLdProcessor {
if(is_object($parent) && !($parent instanceof ArrayObject)) { if(is_object($parent) && !($parent instanceof ArrayObject)) {
self::addValue( self::addValue(
$parent, $property, $output, array('propertyIsArray' => true)); $parent, $property, $output, array('propertyIsArray' => true));
} } else {
else {
$parent[] = $output; $parent[] = $output;
} }
} }
@ -4128,8 +4017,7 @@ class JsonLdProcessor {
} }
} }
$input = $output; $input = $output;
} } else if(is_object($input)) {
else if(is_object($input)) {
// remove @preserve // remove @preserve
if(property_exists($input, '@preserve')) { if(property_exists($input, '@preserve')) {
if($input->{'@preserve'} === '@null') { if($input->{'@preserve'} === '@null') {
@ -4253,8 +4141,7 @@ class JsonLdProcessor {
if($bnode !== null) { if($bnode !== null) {
// normal property // normal property
$direction = 'p'; $direction = 'p';
} } else {
else {
$bnode = $this->_getAdjacentBlankNodeName($quad->object, $id); $bnode = $this->_getAdjacentBlankNodeName($quad->object, $id);
if($bnode !== null) { if($bnode !== null) {
// reverse property // reverse property
@ -4265,11 +4152,9 @@ class JsonLdProcessor {
// get bnode name (try canonical, path, then hash) // get bnode name (try canonical, path, then hash)
if($namer->isNamed($bnode)) { if($namer->isNamed($bnode)) {
$name = $namer->getName($bnode); $name = $namer->getName($bnode);
} } else if($path_namer->isNamed($bnode)) {
else if($path_namer->isNamed($bnode)) {
$name = $path_namer->getName($bnode); $name = $path_namer->getName($bnode);
} } else {
else {
$name = $this->_hashQuads($bnode, $bnodes, $namer); $name = $this->_hashQuads($bnode, $bnodes, $namer);
} }
@ -4283,8 +4168,7 @@ class JsonLdProcessor {
// add bnode to hash group // add bnode to hash group
if(property_exists($groups, $group_hash)) { if(property_exists($groups, $group_hash)) {
$groups->{$group_hash}[] = $bnode; $groups->{$group_hash}[] = $bnode;
} } else {
else {
$groups->{$group_hash} = array($bnode); $groups->{$group_hash} = array($bnode);
} }
} }
@ -4313,8 +4197,7 @@ class JsonLdProcessor {
// use canonical name if available // use canonical name if available
if($namer->isNamed($bnode)) { if($namer->isNamed($bnode)) {
$path .= $namer->getName($bnode); $path .= $namer->getName($bnode);
} } else {
else {
// recurse if bnode isn't named in the path yet // recurse if bnode isn't named in the path yet
if(!$path_namer_copy->isNamed($bnode)) { if(!$path_namer_copy->isNamed($bnode)) {
$recurse[] = $bnode; $recurse[] = $bnode;
@ -4395,10 +4278,10 @@ class JsonLdProcessor {
if($len_a < $len_b) { if($len_a < $len_b) {
return -1; return -1;
} }
else if($len_b < $len_a) { if($len_b < $len_a) {
return 1; return 1;
} }
else if($a === $b) { if($a === $b) {
return 0; return 0;
} }
return ($a < $b) ? -1 : 1; return ($a < $b) ? -1 : 1;
@ -4443,13 +4326,11 @@ class JsonLdProcessor {
$active_ctx->mappings->{$term}->{'@id'} === $value->{'@id'}) { $active_ctx->mappings->{$term}->{'@id'} === $value->{'@id'}) {
// prefer @vocab // prefer @vocab
array_push($prefs, '@vocab', '@id'); array_push($prefs, '@vocab', '@id');
} } else {
else {
// prefer @id // prefer @id
array_push($prefs, '@id', '@vocab'); array_push($prefs, '@id', '@vocab');
} }
} } else {
else {
$prefs[] = $type_or_language_value; $prefs[] = $type_or_language_value;
} }
$prefs[] = '@none'; $prefs[] = '@none';
@ -4500,8 +4381,7 @@ class JsonLdProcessor {
// term is a keyword, default vocab to true // term is a keyword, default vocab to true
if(self::_isKeyword($iri)) { if(self::_isKeyword($iri)) {
$relative_to['vocab'] = true; $relative_to['vocab'] = true;
} } else if(!isset($relative_to['vocab'])) {
else if(!isset($relative_to['vocab'])) {
$relative_to['vocab'] = false; $relative_to['vocab'] = false;
} }
@ -4527,9 +4407,8 @@ class JsonLdProcessor {
$type_or_language = '@type'; $type_or_language = '@type';
$type_or_language_value = '@reverse'; $type_or_language_value = '@reverse';
$containers[] = '@set'; $containers[] = '@set';
} } else if(self::_isList($value)) {
// choose the most specific term that works for all elements in @list // choose the most specific term that works for all elements in @list
else if(self::_isList($value)) {
// only select @list containers if @index is NOT in value // only select @list containers if @index is NOT in value
if(!property_exists($value, '@index')) { if(!property_exists($value, '@index')) {
$containers[] = '@list'; $containers[] = '@list';
@ -4543,29 +4422,24 @@ class JsonLdProcessor {
if(self::_isValue($item)) { if(self::_isValue($item)) {
if(property_exists($item, '@language')) { if(property_exists($item, '@language')) {
$item_language = $item->{'@language'}; $item_language = $item->{'@language'};
} } else if(property_exists($item, '@type')) {
else if(property_exists($item, '@type')) {
$item_type = $item->{'@type'}; $item_type = $item->{'@type'};
} } else {
// plain literal // plain literal
else {
$item_language = '@null'; $item_language = '@null';
} }
} } else {
else {
$item_type = '@id'; $item_type = '@id';
} }
if($common_language === null) { if($common_language === null) {
$common_language = $item_language; $common_language = $item_language;
} } else if($item_language !== $common_language &&
else if($item_language !== $common_language &&
self::_isValue($item)) { self::_isValue($item)) {
$common_language = '@none'; $common_language = '@none';
} }
if($common_type === null) { if($common_type === null) {
$common_type = $item_type; $common_type = $item_type;
} } else if($item_type !== $common_type) {
else if($item_type !== $common_type) {
$common_type = '@none'; $common_type = '@none';
} }
// there are different languages and types in the list, so choose // there are different languages and types in the list, so choose
@ -4583,24 +4457,20 @@ class JsonLdProcessor {
if($common_type !== '@none') { if($common_type !== '@none') {
$type_or_language = '@type'; $type_or_language = '@type';
$type_or_language_value = $common_type; $type_or_language_value = $common_type;
} } else {
else {
$type_or_language_value = $common_language; $type_or_language_value = $common_language;
} }
} } else {
else {
if(self::_isValue($value)) { if(self::_isValue($value)) {
if(property_exists($value, '@language') && if(property_exists($value, '@language') &&
!property_exists($value, '@index')) { !property_exists($value, '@index')) {
$containers[] = '@language'; $containers[] = '@language';
$type_or_language_value = $value->{'@language'}; $type_or_language_value = $value->{'@language'};
} } else if(property_exists($value, '@type')) {
else if(property_exists($value, '@type')) {
$type_or_language = '@type'; $type_or_language = '@type';
$type_or_language_value = $value->{'@type'}; $type_or_language_value = $value->{'@type'};
} }
} } else {
else {
$type_or_language = '@type'; $type_or_language = '@type';
$type_or_language_value = '@id'; $type_or_language_value = '@id';
} }
@ -4743,9 +4613,8 @@ class JsonLdProcessor {
if(property_exists($value, '@type')) { if(property_exists($value, '@type')) {
$rval->{$this->_compactIri($active_ctx, '@type')} = $this->_compactIri( $rval->{$this->_compactIri($active_ctx, '@type')} = $this->_compactIri(
$active_ctx, $value->{'@type'}, null, array('vocab' => true)); $active_ctx, $value->{'@type'}, null, array('vocab' => true));
} } else if(property_exists($value, '@language')) {
// alias @language // alias @language
else if(property_exists($value, '@language')) {
$rval->{$this->_compactIri($active_ctx, '@language')} = $rval->{$this->_compactIri($active_ctx, '@language')} =
$value->{'@language'}; $value->{'@language'};
} }
@ -4868,8 +4737,7 @@ class JsonLdProcessor {
} }
$mapping->{'@id'} = $id; $mapping->{'@id'} = $id;
$mapping->reverse = true; $mapping->reverse = true;
} } else if(property_exists($value, '@id')) {
else if(property_exists($value, '@id')) {
$id = $value->{'@id'}; $id = $value->{'@id'};
if(!is_string($id)) { if(!is_string($id)) {
throw new JsonLdException( throw new JsonLdException(
@ -4904,19 +4772,17 @@ class JsonLdProcessor {
$active_ctx, $local_ctx, $prefix, $defined); $active_ctx, $local_ctx, $prefix, $defined);
} }
// set @id based on prefix parent
if(property_exists($active_ctx->mappings, $prefix) && if(property_exists($active_ctx->mappings, $prefix) &&
$active_ctx->mappings->{$prefix}) { $active_ctx->mappings->{$prefix}) {
// set @id based on prefix parent
$suffix = substr($term, $colon + 1); $suffix = substr($term, $colon + 1);
$mapping->{'@id'} = $active_ctx->mappings->{$prefix}->{'@id'} . $mapping->{'@id'} = $active_ctx->mappings->{$prefix}->{'@id'} .
$suffix; $suffix;
} } else {
// term is an absolute IRI // term is an absolute IRI
else {
$mapping->{'@id'} = $term; $mapping->{'@id'} = $term;
} }
} } else {
else {
// non-IRIs *must* define @ids if @vocab is not available // non-IRIs *must* define @ids if @vocab is not available
if(!property_exists($active_ctx, '@vocab')) { if(!property_exists($active_ctx, '@vocab')) {
throw new JsonLdException( throw new JsonLdException(
@ -5117,8 +4983,7 @@ class JsonLdProcessor {
foreach($input as $e) { foreach($input as $e) {
$this->_findContextUrls($e, $urls, $replace, $base); $this->_findContextUrls($e, $urls, $replace, $base);
} }
} } else if(is_object($input)) {
else if(is_object($input)) {
foreach($input as $k => &$v) { foreach($input as $k => &$v) {
if($k !== '@context') { if($k !== '@context') {
$this->_findContextUrls($v, $urls, $replace, $base); $this->_findContextUrls($v, $urls, $replace, $base);
@ -5139,27 +5004,23 @@ class JsonLdProcessor {
array_splice($v, $i, 1, $ctx); array_splice($v, $i, 1, $ctx);
$i += count($ctx); $i += count($ctx);
$length += count($ctx); $length += count($ctx);
} } else {
else {
$v[$i] = $ctx; $v[$i] = $ctx;
} }
} } else if(!property_exists($urls, $url)) {
// @context URL found // @context URL found
else if(!property_exists($urls, $url)) {
$urls->{$url} = false; $urls->{$url} = false;
} }
} }
} }
} } else if(is_string($v)) {
// string @context // string @context
else if(is_string($v)) {
$v = jsonld_prepend_base($base, $v); $v = jsonld_prepend_base($base, $v);
// replace w/@context if requested // replace w/@context if requested
if($replace) { if($replace) {
$input->{$k} = $urls->{$v}; $input->{$k} = $urls->{$v};
} } else if(!property_exists($urls, $v)) {
// @context URL found // @context URL found
else if(!property_exists($urls, $v)) {
$urls->{$v} = false; $urls->{$v} = false;
} }
} }
@ -5231,8 +5092,7 @@ class JsonLdProcessor {
if(is_string($ctx)) { if(is_string($ctx)) {
try { try {
$ctx = self::_parse_json($ctx); $ctx = self::_parse_json($ctx);
} } catch(Exception $e) {
catch(Exception $e) {
throw new JsonLdException( throw new JsonLdException(
'Could not parse JSON from URL.', 'Could not parse JSON from URL.',
'jsonld.ParseError', 'loading remote context failed', 'jsonld.ParseError', 'loading remote context failed',
@ -5250,8 +5110,7 @@ class JsonLdProcessor {
// use empty context if no @context key is present // use empty context if no @context key is present
if(!property_exists($ctx, '@context')) { if(!property_exists($ctx, '@context')) {
$ctx = (object)array('@context' => new stdClass()); $ctx = (object)array('@context' => new stdClass());
} } else {
else {
$ctx = (object)array('@context' => $ctx->{'@context'}); $ctx = (object)array('@context' => $ctx->{'@context'});
} }
@ -5321,8 +5180,7 @@ class JsonLdProcessor {
// add term selection where it applies // add term selection where it applies
if(property_exists($mapping, '@container')) { if(property_exists($mapping, '@container')) {
$container = $mapping->{'@container'}; $container = $mapping->{'@container'};
} } else {
else {
$container = '@none'; $container = '@none';
} }
@ -5344,28 +5202,25 @@ class JsonLdProcessor {
} }
$entry = $container_map->{$container}; $entry = $container_map->{$container};
// term is preferred for values using @reverse
if($mapping->reverse) { if($mapping->reverse) {
// term is preferred for values using @reverse
$this->_addPreferredTerm( $this->_addPreferredTerm(
$mapping, $term, $entry->{'@type'}, '@reverse'); $mapping, $term, $entry->{'@type'}, '@reverse');
} } else if(property_exists($mapping, '@type')) {
// term is preferred for values using specific type // term is preferred for values using specific type
else if(property_exists($mapping, '@type')) {
$this->_addPreferredTerm( $this->_addPreferredTerm(
$mapping, $term, $entry->{'@type'}, $mapping->{'@type'}); $mapping, $term, $entry->{'@type'}, $mapping->{'@type'});
} } else if(property_exists($mapping, '@language')) {
// term is preferred for values using specific language // term is preferred for values using specific language
else if(property_exists($mapping, '@language')) {
$language = $mapping->{'@language'}; $language = $mapping->{'@language'};
if($language === null) { if($language === null) {
$language = '@null'; $language = '@null';
} }
$this->_addPreferredTerm( $this->_addPreferredTerm(
$mapping, $term, $entry->{'@language'}, $language); $mapping, $term, $entry->{'@language'}, $language);
} } else {
// term is preferred for values w/default language or no type and // term is preferred for values w/default language or no type and
// no language // no language
else {
// add an entry for the default language // add an entry for the default language
$this->_addPreferredTerm( $this->_addPreferredTerm(
$mapping, $term, $entry->{'@language'}, $default_language); $mapping, $term, $entry->{'@language'}, $default_language);
@ -5576,8 +5431,7 @@ class JsonLdProcessor {
if(is_object($v)) { if(is_object($v)) {
if(property_exists($v, '@id')) { if(property_exists($v, '@id')) {
$rval = (strpos($v->{'@id'}, '_:') === 0); $rval = (strpos($v->{'@id'}, '_:') === 0);
} } else {
else {
$rval = (count(get_object_vars($v)) === 0 || $rval = (count(get_object_vars($v)) === 0 ||
!(property_exists($v, '@value') || !(property_exists($v, '@value') ||
property_exists($v, '@set') || property_exists($v, '@set') ||
@ -5831,8 +5685,7 @@ class Permutator {
// no more permutations // no more permutations
if($k === null) { if($k === null) {
$this->done = true; $this->done = true;
} } else {
else {
// swap k and the element it is looking at // swap k and the element it is looking at
$swap = $this->left->{$k} ? $pos - 1 : $pos + 1; $swap = $this->left->{$k} ? $pos - 1 : $pos + 1;
$this->list[$pos] = $this->list[$swap]; $this->list[$pos] = $this->list[$swap];

View file

@ -173,18 +173,16 @@ class JsonLdManifest {
$filename = join( $filename = join(
DIRECTORY_SEPARATOR, array($this->dirname, $entry)); DIRECTORY_SEPARATOR, array($this->dirname, $entry));
$entry = Util::readJson($filename); $entry = Util::readJson($filename);
} } else {
else {
$filename = $this->filename; $filename = $this->filename;
} }
// entry is another manifest
if(JsonLdProcessor::hasValue($entry, '@type', 'mf:Manifest')) { if(JsonLdProcessor::hasValue($entry, '@type', 'mf:Manifest')) {
// entry is another manifest
$manifest = new JsonLdManifest($entry, $filename); $manifest = new JsonLdManifest($entry, $filename);
$manifest->load($tests); $manifest->load($tests);
} } else {
// assume entry is a test // assume entry is a test
else {
$test = new JsonLdTest($this, $entry, $filename); $test = new JsonLdTest($this, $entry, $filename);
$types = JsonLdProcessor::getValues($test->data, '@type'); $types = JsonLdProcessor::getValues($test->data, '@type');
foreach($types as $type) { foreach($types as $type) {
@ -223,8 +221,7 @@ class JsonLdTest {
// read expected data // read expected data
if($this->isNegative) { if($this->isNegative) {
$this->expected = $this->data->expect; $this->expected = $this->data->expect;
} } else {
else {
$this->expected = $this->readProperty('expect'); $this->expected = $this->readProperty('expect');
} }
@ -234,8 +231,7 @@ class JsonLdTest {
throw new Exception('Expected an error; one was not raised.'); throw new Exception('Expected an error; one was not raised.');
} }
PHPUnit_Framework_TestCase::assertEquals($this->expected, $this->actual); PHPUnit_Framework_TestCase::assertEquals($this->expected, $this->actual);
} } catch(Exception $e) {
catch(Exception $e) {
if($this->isPositive) { if($this->isPositive) {
throw $e; throw $e;
} }
@ -301,8 +297,7 @@ class JsonLdTest {
$options->httpStatus >= '300') { $options->httpStatus >= '300') {
$doc->documentUrl = ($test->manifest->data->{'baseIri'} . $doc->documentUrl = ($test->manifest->data->{'baseIri'} .
$options->redirectTo); $options->redirectTo);
} } else if(property_exists($options, 'httpLink')) {
else if(property_exists($options, 'httpLink')) {
$content_type = (property_exists($options, 'contentType') ? $content_type = (property_exists($options, 'contentType') ?
$options->contentType : null); $options->contentType : null);
$extension = pathinfo($url, PATHINFO_EXTENSION); $extension = pathinfo($url, PATHINFO_EXTENSION);
@ -316,8 +311,7 @@ class JsonLdTest {
$link_header = jsonld_parse_link_header($link_header); $link_header = jsonld_parse_link_header($link_header);
if(isset($link_header['http://www.w3.org/ns/json-ld#context'])) { if(isset($link_header['http://www.w3.org/ns/json-ld#context'])) {
$link_header = $link_header['http://www.w3.org/ns/json-ld#context']; $link_header = $link_header['http://www.w3.org/ns/json-ld#context'];
} } else {
else {
$link_header = null; $link_header = null;
} }
if($link_header && $content_type !== 'application/ld+json') { if($link_header && $content_type !== 'application/ld+json') {
@ -333,8 +327,7 @@ class JsonLdTest {
substr($doc->{'documentUrl'}, strlen($base)); substr($doc->{'documentUrl'}, strlen($base));
try { try {
$doc->{'document'} = Util::readJson($filename); $doc->{'document'} = Util::readJson($filename);
} } catch(Exception $e) {
catch(Exception $e) {
throw new Exception('loading document failed'); throw new Exception('loading document failed');
} }
return $doc; return $doc;
@ -387,8 +380,7 @@ class JsonLdTestIterator implements Iterator {
global $TESTS; global $TESTS;
if(isset($TESTS[$type])) { if(isset($TESTS[$type])) {
$this->tests = $TESTS[$type]; $this->tests = $TESTS[$type];
} } else {
else {
$this->tests = array(); $this->tests = array();
} }
$this->count = count($this->tests); $this->count = count($this->tests);
@ -590,8 +582,7 @@ class Util {
$options |= JSON_PRETTY_PRINT; $options |= JSON_PRETTY_PRINT;
} }
$json = json_encode($input, $options); $json = json_encode($input, $options);
} } else {
else {
// use a simple string replacement of '\/' to '/'. // use a simple string replacement of '\/' to '/'.
$json = str_replace('\\/', '/', json_encode($input)); $json = str_replace('\\/', '/', json_encode($input));
} }