/** * Finds all @context URLs in the given JSON-LD input. * * @param mixed $input the JSON-LD input. * @param stdClass $urls a map of URLs (url => false/@contexts). * @param bool $replace true to replace the URLs in the given input with * the @contexts from the urls map, false not to. * @param string $base the base URL to resolve relative URLs with. */ protected function _findContextUrls($input, $urls, $replace, $base) { if (is_array($input)) { foreach ($input as $e) { $this->_findContextUrls($e, $urls, $replace, $base); } } else { if (is_object($input)) { foreach ($input as $k => &$v) { if ($k !== '@context') { $this->_findContextUrls($v, $urls, $replace, $base); continue; } // array @context if (is_array($v)) { $length = count($v); for ($i = 0; $i < $length; ++$i) { if (is_string($v[$i])) { $url = jsonld_prepend_base($base, $v[$i]); // replace w/@context if requested if ($replace) { $ctx = $urls->{$url}; if (is_array($ctx)) { // add flattened context array_splice($v, $i, 1, $ctx); $i += count($ctx); $length += count($ctx); } else { $v[$i] = $ctx; } } else { if (!property_exists($urls, $url)) { $urls->{$url} = false; } } } } } else { if (is_string($v)) { $v = jsonld_prepend_base($base, $v); // replace w/@context if requested if ($replace) { $input->{$k} = $urls->{$v}; } else { if (!property_exists($urls, $v)) { $urls->{$v} = false; } } } } } } } }
/** * Prepends a base IRI to the given relative IRI. * * @param string $base the base IRI. * @param string $iri the relative IRI. * * @return string the absolute IRI. */ protected function _prependBase($base, $iri) { return jsonld_prepend_base($base, $iri); }