Various syntax/logic fixes.

This commit is contained in:
Dave Longley 2012-04-23 14:01:56 -04:00
parent cdce63a99c
commit a1a5a4cb34
2 changed files with 47 additions and 52 deletions

View file

@ -56,6 +56,7 @@ function deep_compare($expect, $result) {
return false; return false;
} }
} }
return true;
} }
return $expect === $result; return $expect === $result;
@ -130,7 +131,7 @@ class TestRunner {
public function check($test, $expect, $result) { public function check($test, $expect, $result) {
global $eol; global $eol;
if(strpos($test->{'@type'}, 'NormalizeTest') !== false) { if(in_array('jld:NormalizeTest', $test->{'@type'}) !== false) {
$pass = JsonLdProcessor::compareNormalized($expect, $result); $pass = JsonLdProcessor::compareNormalized($expect, $result);
} }
else { else {
@ -154,9 +155,6 @@ class TestRunner {
echo 'JSON Result: ' . echo 'JSON Result: ' .
json_encode(json_decode(result, $flags)) . $eol; json_encode(json_decode(result, $flags)) . $eol;
*/ */
// FIXME: remove me
throw new Exception('FAIL');
} }
} }
@ -173,12 +171,11 @@ class TestRunner {
$handle = opendir($filepath); $handle = opendir($filepath);
if($handle) { if($handle) {
while(($file = readdir($handle)) !== false) { while(($file = readdir($handle)) !== false) {
if($file !== '..' and $file !== '.') if($file !== '..' and $file !== '.') {
{ $files[] = $filepath . '/' . $file;
$files[] = $filepath . '/' . $file; }
} }
} closedir($handle);
closedir($handle);
} }
else { else {
throw new Exception('Could not open directory.'); throw new Exception('Could not open directory.');
@ -243,6 +240,7 @@ class TestRunner {
$result = jsonld_expand($input, $options); $result = jsonld_expand($input, $options);
} }
else if(in_array('jld:CompactTest', $type)) { else if(in_array('jld:CompactTest', $type)) {
continue;
$this->test($test->name); $this->test($test->name);
$input = read_test_json($test->input, $filepath); $input = read_test_json($test->input, $filepath);
$test->context = read_test_json($test->context, $filepath); $test->context = read_test_json($test->context, $filepath);
@ -250,6 +248,7 @@ class TestRunner {
$result = jsonld_compact($input, $test->context, $options); $result = jsonld_compact($input, $test->context, $options);
} }
else if(in_array('jld:FrameTest', $type)) { else if(in_array('jld:FrameTest', $type)) {
continue;
$this->test($test->name); $this->test($test->name);
$input = read_test_json($test->input, $filepath); $input = read_test_json($test->input, $filepath);
$test->frame = read_test_json($test->frame, $filepath); $test->frame = read_test_json($test->frame, $filepath);

View file

@ -236,7 +236,7 @@ class JsonLdProcessor {
if($has_context || $options['graph']) { if($has_context || $options['graph']) {
if(is_array($compacted)) { if(is_array($compacted)) {
// use '@graph' keyword // use '@graph' keyword
$kwgraph =$this->_compactIri($active_ctx, '@graph'); $kwgraph = $this->_compactIri($active_ctx, '@graph');
$graph = $compacted; $graph = $compacted;
$compacted = new stdClass(); $compacted = new stdClass();
if($hasContext) { if($hasContext) {
@ -398,7 +398,7 @@ class JsonLdProcessor {
} }
// do normalization // do normalization
$this->_normalize($expanded); return $this->_normalize($expanded);
} }
/** /**
@ -885,7 +885,7 @@ class JsonLdProcessor {
// preserve empty arrays // preserve empty arrays
if(count($value) === 0) { if(count($value) === 0) {
$prop =$this->_compactIri($ctx, $key); $prop = $this->_compactIri($ctx, $key);
self::addValue($rval, $prop, array(), true); self::addValue($rval, $prop, array(), true);
} }
@ -1063,8 +1063,7 @@ class JsonLdProcessor {
$container = self::getContextValue($ctx, $property, '@container'); $container = self::getContextValue($ctx, $property, '@container');
if($container === '@list') { if($container === '@list') {
// ensure value is an array // ensure value is an array
$value = self::arrayify(); $value = (object)array('@list' => self::arrayify($value));
$value = (object)array('@list' => $value);
} }
} }
@ -1155,7 +1154,8 @@ class JsonLdProcessor {
// frame the subjects // frame the subjects
$framed = new ArrayObject(); $framed = new ArrayObject();
$this->_match_frame($state, $state->subjects, $frame, $framed, null); $this->_match_frame(
$state, array_keys((array)$state->subjects), $frame, $framed, null);
return (array)$framed; return (array)$framed;
} }
@ -1178,10 +1178,10 @@ class JsonLdProcessor {
// continue to hash bnode statements while bnodes are assigned names // continue to hash bnode statements while bnodes are assigned names
$unnamed = null; $unnamed = null;
$nextUnnamed = get_object_vars($bnodes); $nextUnnamed = array_keys((array)$bnodes);
$duplicates = null; $duplicates = null;
do { do {
$unnamed = nextUnnamed; $unnamed = $nextUnnamed;
$nextUnnamed = array(); $nextUnnamed = array();
$duplicates = new stdClass(); $duplicates = new stdClass();
$unique = new stdClass(); $unique = new stdClass();
@ -1191,7 +1191,7 @@ class JsonLdProcessor {
$hash = $this->_hashStatements($statements, $namer); $hash = $this->_hashStatements($statements, $namer);
// store hash as unique or a duplicate // store hash as unique or a duplicate
if(property_exists($duplicate, $hash)) { if(property_exists($duplicates, $hash)) {
$duplicates->{$hash}[] = $bnode; $duplicates->{$hash}[] = $bnode;
$nextUnnamed[] = $bnode; $nextUnnamed[] = $bnode;
} }
@ -1207,7 +1207,7 @@ class JsonLdProcessor {
} }
// name unique bnodes in sorted hash order // name unique bnodes in sorted hash order
$hashes = get_object_vars($unique); $hashes = array_keys((array)$unique);
sort($hashes); sort($hashes);
foreach($hashes as $hash) { foreach($hashes as $hash) {
$bnode = $unique->{$hash}; $bnode = $unique->{$hash};
@ -1217,7 +1217,7 @@ class JsonLdProcessor {
while(count($unnamed) > count($nextUnnamed)); while(count($unnamed) > count($nextUnnamed));
// enumerate duplicate hash groups in sorted order // enumerate duplicate hash groups in sorted order
$hashes = get_object_vars($duplicates); $hashes = array_keys((array)$duplicates);
sort($hashes); sort($hashes);
foreach($hashes as $hash) { foreach($hashes as $hash) {
// process group // process group
@ -1261,7 +1261,7 @@ class JsonLdProcessor {
foreach($statements as $statement) { foreach($statements as $statement) {
if($statement->s === '_:a') { if($statement->s === '_:a') {
$z = $this->_getBlankNodeName($statement->o); $z = $this->_getBlankNodeName($statement->o);
$o = $z ? (object)array('@id' => $namer.getName($z)) : $statement->o; $o = $z ? (object)array('@id' => $namer->getName($z)) : $statement->o;
self::addValue($bnode, $statement->p, $o, true); self::addValue($bnode, $statement->p, $o, true);
} }
} }
@ -1274,7 +1274,7 @@ class JsonLdProcessor {
$subject = (object)array('@id' => $id); $subject = (object)array('@id' => $id);
foreach($statements as $statement) { foreach($statements as $statement) {
$z = $this->_getBlankNodeName($statement->o); $z = $this->_getBlankNodeName($statement->o);
$o = $z ? (object)array('@id' => $namer.getName($z)) : $statement->o; $o = $z ? (object)array('@id' => $namer->getName($z)) : $statement->o;
self::addValue($subject, $statement->p, $o, true); self::addValue($subject, $statement->p, $o, true);
} }
$output[] = $subject; $output[] = $subject;
@ -1389,7 +1389,7 @@ class JsonLdProcessor {
$language = self::getContextValue($ctx, $property, '@language'); $language = self::getContextValue($ctx, $property, '@language');
if($language !== null) { if($language !== null) {
$rval = (object)array( $rval = (object)array(
'@value' => strval(value), '@language' => $language); '@value' => strval($value), '@language' => $language);
} }
} }
} }
@ -1466,7 +1466,8 @@ class JsonLdProcessor {
// convert boolean to @value // convert boolean to @value
if(is_bool($o)) { if(is_bool($o)) {
$o = (object)array( $o = (object)array(
'@value' => strval($o), '@type' => self::XSD_BOOLEAN); '@value' => ($o ? 'true' : 'false'),
'@type' => self::XSD_BOOLEAN);
} }
// convert double to @value // convert double to @value
else if(is_double($o)) { else if(is_double($o)) {
@ -1484,14 +1485,15 @@ class JsonLdProcessor {
// object is a blank node // object is a blank node
if(self::_isBlankNode($o)) { if(self::_isBlankNode($o)) {
// name object position blank node // name object position blank node
$o_name = $namer->getName($o->{'@id'}); $o_name = property_exists($o, '@id') ? $o->{'@id'} : null;
$o_name = $namer->getName($o_name);
// add property statement // add property statement
$this->_addStatement($entries, (object)array( $this->_addStatement($entries, (object)array(
's' => $s, 'p' => $p, 'o' => (object)array('@id' => $o_name))); 's' => $s, 'p' => $p, 'o' => (object)array('@id' => $o_name)));
// add reference statement // add reference statement
if(!property_exists($bnodes, $name)) { if(!property_exists($bnodes, $o_name)) {
$o_entries = $bnodes->{$o_name} = new ArrayObject(); $o_entries = $bnodes->{$o_name} = new ArrayObject();
} }
else { else {
@ -1513,8 +1515,8 @@ class JsonLdProcessor {
// ensure a subject entry exists for subject reference // ensure a subject entry exists for subject reference
if(self::_isSubjectReference($o) && if(self::_isSubjectReference($o) &&
!property_exists($subjects, $name)) { !property_exists($subjects, $o->{'@id'})) {
$subjects->{$name} = new ArrayObject(); $subjects->{$o->{'@id'}} = new ArrayObject();
} }
} }
// object must be an embedded subject // object must be an embedded subject
@ -1628,7 +1630,7 @@ class JsonLdProcessor {
$triple .= '"' . $statement->o->{'@value'} . '"'; $triple .= '"' . $statement->o->{'@value'} . '"';
if(property_exists($statement->o, '@type')) { if(property_exists($statement->o, '@type')) {
$triple .= '^^<' . $statement->o['@type'] . '>'; $triple .= '^^<' . $statement->o->{'@type'} . '>';
} }
else if(property_exists($statement->o, '@language')) { else if(property_exists($statement->o, '@language')) {
$triple .= '@' . $statement->o{'@language'}; $triple .= '@' . $statement->o{'@language'};
@ -1667,6 +1669,7 @@ class JsonLdProcessor {
// group adjacent bnodes by hash, keep properties and references separate // group adjacent bnodes by hash, keep properties and references separate
$groups = new stdClass(); $groups = new stdClass();
$cache = new stdClass();
foreach($statements as $statement) { foreach($statements as $statement) {
$bnode = null; $bnode = null;
$direction = null; $direction = null;
@ -1714,7 +1717,7 @@ class JsonLdProcessor {
} }
// iterate over groups in sorted hash order // iterate over groups in sorted hash order
$group_hashes = get_object_vars($groups); $group_hashes = array_keys((array)$groups);
sort($group_hashes); sort($group_hashes);
foreach($group_hashes as $group_hash) { foreach($group_hashes as $group_hash) {
// digest group hash // digest group hash
@ -1872,7 +1875,7 @@ class JsonLdProcessor {
} }
// add reference and recurse // add reference and recurse
self::addValue($subject, $prop, (object)array('@id' => id), true); self::addValue($subject, $prop, (object)array('@id' => $id), true);
$this->_flatten($subjects, $o, $namer, $id, null); $this->_flatten($subjects, $o, $namer, $id, null);
} }
else { else {
@ -1972,7 +1975,7 @@ class JsonLdProcessor {
$state->embeds->{$id} = $embed; $state->embeds->{$id} = $embed;
// iterate over subject properties // iterate over subject properties
$props = get_object_vars($subject); $props = array_keys((array)$subject);
sort($props); sort($props);
foreach($props as $prop) { foreach($props as $prop) {
// copy keywords to output // copy keywords to output
@ -2030,7 +2033,7 @@ class JsonLdProcessor {
} }
// handle defaults // handle defaults
$props = get_object_vars($frame); $props = array_keys((array)$frame);
sort($props); sort($props);
foreach($props as $prop) { foreach($props as $prop) {
// skip keywords // skip keywords
@ -2069,7 +2072,7 @@ class JsonLdProcessor {
protected function _getFrameFlag($frame, $options, $name) { protected function _getFrameFlag($frame, $options, $name) {
$flag = "'@'$name"; $flag = "'@'$name";
return (property_exists($frame, $flag) ? return (property_exists($frame, $flag) ?
$frame->{$flag}[0] : $options->{$name}); $frame->{$flag}[0] : $options[$name]);
} }
/** /**
@ -2228,7 +2231,7 @@ class JsonLdProcessor {
// recursively remove dependent dangling embeds // recursively remove dependent dangling embeds
$removeDependents = function($id) { $removeDependents = function($id) {
// get embed keys as a separate array to enable deleting keys in map // get embed keys as a separate array to enable deleting keys in map
$ids = get_object_vars($embeds); $ids = array_keys((array)$embeds);
foreach($ids as $next) { foreach($ids as $next) {
if(property_exists($embeds, $next) && if(property_exists($embeds, $next) &&
is_object($embeds->{$next}->parent) && is_object($embeds->{$next}->parent) &&
@ -2765,7 +2768,8 @@ class JsonLdProcessor {
protected function _expandContextIri( protected function _expandContextIri(
$active_ctx, $ctx, $value, $base, $defined) { $active_ctx, $ctx, $value, $base, $defined) {
// dependency not defined, define it // dependency not defined, define it
if(property_exists($ctx, $value) && $defined->{$value} !== true) { if(property_exists($ctx, $value) &&
(!property_exists($defined, $value) || !$defined->{$value})) {
$this->_defineContextMapping($active_ctx, $ctx, $value, $base, $defined); $this->_defineContextMapping($active_ctx, $ctx, $value, $base, $defined);
} }
@ -2776,7 +2780,7 @@ class JsonLdProcessor {
if($value === $id) { if($value === $id) {
return $value; return $value;
} }
return _expandContextIri($active_ctx, $ctx, $id, $base, $defined); return $this->_expandContextIri($active_ctx, $ctx, $id, $base, $defined);
} }
// split value into prefix:suffix // split value into prefix:suffix
@ -2794,7 +2798,8 @@ class JsonLdProcessor {
} }
// dependency not defined, define it // dependency not defined, define it
if(property_exists($ctx, $prefix) && $defined->{$prefix} !== true) { if(property_exists($ctx, $prefix) &&
(!property_exists($defined, $prefix) || !$defined->{$prefix})) {
$this->_defineContextMapping( $this->_defineContextMapping(
$active_ctx, $ctx, $prefix, $base, $defined); $active_ctx, $ctx, $prefix, $base, $defined);
} }
@ -3250,11 +3255,6 @@ class JsonLdException extends Exception {
* names. * names.
*/ */
class UniqueNamer { class UniqueNamer {
protected $prefix;
protected $counter;
protected $existing;
protected $order;
/** /**
* Constructs a new UniqueNamer. * Constructs a new UniqueNamer.
* *
@ -3285,7 +3285,7 @@ class UniqueNamer {
public function getName($old_name=null) { public function getName($old_name=null) {
// return existing old name // return existing old name
if($old_name && property_exists($this->existing, $old_name)) { if($old_name && property_exists($this->existing, $old_name)) {
return $this->existing->{$oldName}; return $this->existing->{$old_name};
} }
// get next name // get next name
@ -3294,7 +3294,7 @@ class UniqueNamer {
// save mapping // save mapping
if($old_name !== null) { if($old_name !== null) {
$this->existing->{$oldName} = $name; $this->existing->{$old_name} = $name;
} }
return $name; return $name;
@ -3305,7 +3305,7 @@ class UniqueNamer {
* *
* @param string $old_name the old name to check. * @param string $old_name the old name to check.
* *
* @return true if the oldName has been assigned a new name, false if not. * @return true if the old name has been assigned a new name, false if not.
*/ */
public function isNamed($old_name) { public function isNamed($old_name) {
return property_exists($this->existing, $old_name); return property_exists($this->existing, $old_name);
@ -3317,16 +3317,12 @@ class UniqueNamer {
* of elements. * of elements.
*/ */
class Permutator { class Permutator {
protected $list;
protected $done;
protected $left;
/** /**
* Constructs a new Permutator. * Constructs a new Permutator.
* *
* @param array $list the array of elements to iterate over. * @param array $list the array of elements to iterate over.
*/ */
public function __constructor($list) { public function __construct($list) {
// original array // original array
$this->list = $list; $this->list = $list;
sort($this->list); sort($this->list);