diff --git a/jsonld.php b/jsonld.php index 1d9d0c2..53b18cc 100644 --- a/jsonld.php +++ b/jsonld.php @@ -1,7 +1,7 @@ _compactIri($active_ctx, '@graph'); - $graph = $compacted; - $compacted = new stdClass(); - if($has_context) { - $compacted->{'@context'} = $ctx; - } - $compacted->{$kwgraph} = $graph; - } - else if(is_object($compacted)) { - // reorder keys so @context is first - $graph = $compacted; - $compacted = new stdClass(); + // add context and/or @graph + if(is_array($compacted)) { + // use '@graph' keyword + $kwgraph = $this->_compactIri($active_ctx, '@graph'); + $graph = $compacted; + $compacted = new stdClass(); + if($has_context) { $compacted->{'@context'} = $ctx; - foreach($graph as $k => $v) { - $compacted->{$k} = $v; - } + } + $compacted->{$kwgraph} = $graph; + } + else if(is_object($compacted) && $has_context) { + // reorder keys so @context is first + $graph = $compacted; + $compacted = new stdClass(); + $compacted->{'@context'} = $ctx; + foreach($graph as $k => $v) { + $compacted->{$k} = $v; } }