Some fixes for bnodes for properties and xsd:double.

This commit is contained in:
Dave Longley 2013-06-06 22:00:52 -04:00
parent 4d4334ea43
commit 5be8e20433

View file

@ -1,7 +1,7 @@
<?php <?php
/** /**
* PHP implementation of the JSON-LD API. * PHP implementation of the JSON-LD API.
* Version: 0.0.33 * Version: 0.0.34
* *
* @author Dave Longley * @author Dave Longley
* *
@ -1030,7 +1030,10 @@ class JsonLdProcessor {
// output RDF dataset // output RDF dataset
$namer = new UniqueNamer('_:b'); $namer = new UniqueNamer('_:b');
$dataset = new stdClass(); $dataset = new stdClass();
foreach($node_map as $graph_name => $graph) { $graph_names = array_keys((array)$node_map);
sort($graph_names);
foreach($graph_names as $graph_name) {
$graph = $node_map->{$graph_name};
if(strpos($graph_name, '_:') === 0) { if(strpos($graph_name, '_:') === 0) {
$graph_name = $namer->getName($graph_name); $graph_name = $namer->getName($graph_name);
} }
@ -1509,21 +1512,34 @@ class JsonLdProcessor {
$quad = ''; $quad = '';
// subject is an IRI or bnode // subject is an IRI
if($s->type === 'IRI') { if($s->type === 'IRI') {
$quad .= "<{$s->value}>"; $quad .= "<{$s->value}>";
} }
// normalization mode // bnode normalization mode
else if($bnode !== null) { else if($bnode !== null) {
$quad .= ($s->value === $bnode) ? '_:a' : '_:z'; $quad .= ($s->value === $bnode) ? '_:a' : '_:z';
} }
// normal mode // bnode normal mode
else { else {
$quad .= $s->value; $quad .= $s->value;
} }
$quad .= ' ';
// predicate is always an IRI // predicate is an IRI
$quad .= " <{$p->value}> "; if($p->type === 'IRI') {
$quad .= "<{$p->value}>";
}
// FIXME: TBD what to do with bnode predicates during normalization
// bnode normalization mode
else if($bnode !== null) {
$quad .= '_:p';
}
// bnode normal mode
else {
$quad .= $p->value;
}
$quad .= ' ';
// object is IRI, bnode, or literal // object is IRI, bnode, or literal
if($o->type === 'IRI') { if($o->type === 'IRI') {
@ -2591,6 +2607,8 @@ class JsonLdProcessor {
if(!(is_object($node) && strpos($node->{'@id'}, '_:') === 0 && if(!(is_object($node) && strpos($node->{'@id'}, '_:') === 0 &&
($node_key_count === 3 || ($node_key_count === 4 && ($node_key_count === 3 || ($node_key_count === 4 &&
property_exists($node, 'listHeadFor'))) && property_exists($node, 'listHeadFor'))) &&
property_exists($node, self::RDF_FIRST) &&
property_exists($node, self::RDF_REST) &&
is_array($node->{self::RDF_FIRST}) && is_array($node->{self::RDF_FIRST}) &&
count($node->{self::RDF_FIRST}) === 1 && count($node->{self::RDF_FIRST}) === 1 &&
is_array($node->{self::RDF_REST}) && is_array($node->{self::RDF_REST}) &&
@ -2704,7 +2722,7 @@ class JsonLdProcessor {
if(property_exists($ctx, '@base')) { if(property_exists($ctx, '@base')) {
$base = $ctx->{'@base'}; $base = $ctx->{'@base'};
if($base === null) { if($base === null) {
$base = $options['base']; $base = null;
} }
else if(!is_string($base)) { else if(!is_string($base)) {
throw new JsonLdException( throw new JsonLdException(
@ -2948,8 +2966,14 @@ class JsonLdProcessor {
// RDF predicate // RDF predicate
$predicate = new stdClass(); $predicate = new stdClass();
if(strpos($property, '_:') === 0) {
$predicate->type = 'blank node';
$predicate->value = $namer->getName($property);
}
else {
$predicate->type = 'IRI'; $predicate->type = 'IRI';
$predicate->value = $property; $predicate->value = $property;
}
// convert @list to triples // convert @list to triples
if(self::_isList($item)) { if(self::_isList($item)) {
@ -3030,7 +3054,7 @@ class JsonLdProcessor {
$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)) { 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));