From 8bad8e2d240b33fd477e7bf81d8abfc74d032886 Mon Sep 17 00:00:00 2001 From: Dave Longley Date: Thu, 21 Feb 2013 16:06:33 -0500 Subject: [PATCH] Remove optimize option; add compactArrays option to compaction. --- jsonld.php | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/jsonld.php b/jsonld.php index 75f8db3..2cec124 100644 --- a/jsonld.php +++ b/jsonld.php @@ -1,7 +1,7 @@ _compact($active_ctx, null, $expanded, $options); - if(!$options['graph'] && is_array($compacted)) { + if($options['compactArrays'] && + !$options['graph'] && is_array($compacted)) { // simplify to a single item if(count($compacted) === 1) { $compacted = $compacted[0]; @@ -781,7 +784,6 @@ class JsonLdProcessor { * [embed] default @embed flag (default: true). * [explicit] default @explicit flag (default: false). * [omitDefault] default @omitDefault flag (default: false). - * [optimize] optimize when compacting (default: false). * [loadContext(url)] the context loader. * * @return stdClass the framed JSON-LD output. @@ -789,10 +791,10 @@ class JsonLdProcessor { public function frame($input, $frame, $options) { // set default options isset($options['base']) or $options['base'] = ''; + isset($options['compactArrays']) or $options['compactArrays'] = true; isset($options['embed']) or $options['embed'] = true; isset($options['explicit']) or $options['explicit'] = false; isset($options['omitDefault']) or $options['omitDefault'] = false; - isset($options['optimize']) or $options['optimize'] = false; isset($options['loadContext']) or $options['loadContext'] = 'jsonld_get_url'; @@ -845,7 +847,7 @@ class JsonLdProcessor { $graph = $this->_compactIri($active_ctx, '@graph'); // remove @preserve from results $compacted->{$graph} = $this->_removePreserve( - $active_ctx, $compacted->{$graph}); + $active_ctx, $compacted->{$graph}, $options); return $compacted; } @@ -1622,7 +1624,7 @@ class JsonLdProcessor { $rval[] = $compacted; } } - if(count($rval) === 1) { + if($options['compactArrays'] && count($rval) === 1) { // use single element if no container is specified $container = self::getContextValue( $active_ctx, $active_property, '@container'); @@ -1800,9 +1802,11 @@ class JsonLdProcessor { $map_object, $expanded_item->{$container}, $compacted_item); } else { - // use an array if: @container is @set or @list , value is an empty + // use an array if: compactArrays flag is false, + // @container is @set or @list , value is an empty // array, or key is @graph - $is_array = ($container === '@set' || $container === '@list' || + $is_array = (!$options['compactArrays'] || + $container === '@set' || $container === '@list' || (is_array($compacted_item) && count($compacted_item) === 0) || $expanded_property === '@list' || $expanded_property === '@graph'); @@ -3538,15 +3542,16 @@ class JsonLdProcessor { * * @param stdClass $ctx the active context used to compact the input. * @param mixed $input the framed, compacted output. + * @param assoc $options the compaction options used. * * @return mixed the resulting output. */ - protected function _removePreserve($ctx, $input) { + protected function _removePreserve($ctx, $input, $options) { // recurse through arrays if(is_array($input)) { $output = array(); foreach($input as $e) { - $result = $this->_removePreserve($ctx, $e); + $result = $this->_removePreserve($ctx, $e, $options); // drop nulls from arrays if($result !== null) { $output[] = $result; @@ -3570,15 +3575,17 @@ class JsonLdProcessor { // recurse through @lists if(self::_isList($input)) { - $input->{'@list'} = $this->_removePreserve($ctx, $input->{'@list'}); + $input->{'@list'} = $this->_removePreserve( + $ctx, $input->{'@list'}, $options); return $input; } // recurse through properties foreach($input as $prop => $v) { - $result = $this->_removePreserve($ctx, $v); + $result = $this->_removePreserve($ctx, $v, $options); $container = self::getContextValue($ctx, $prop, '@container'); - if(is_array($result) && count($result) === 1 && + if($options['compactArrays'] && + is_array($result) && count($result) === 1 && $container !== '@set' && $container !== '@list') { $result = $result[0]; }