示例#1
0
function parseContextLinkHeaders(array $values, IRI $baseIri)
{
    // Separate multiple links contained in a single header value
    for ($i = 0, $total = count($values); $i < $total; $i++) {
        if (strpos($values[$i], ',') !== false) {
            foreach (preg_split('/,(?=([^"]*"[^"]*")*[^"]*$)/', $values[$i]) as $v) {
                $values[] = trim($v);
            }
            unset($values[$i]);
        }
    }
    $contexts = $matches = array();
    $trimWhitespaceCallback = function ($str) {
        return trim($str, "\"'  \n\t");
    };
    // Split the header in key-value pairs
    foreach ($values as $val) {
        $part = array();
        foreach (preg_split('/;(?=([^"]*"[^"]*")*[^"]*$)/', $val) as $kvp) {
            preg_match_all('/<[^>]+>|[^=]+/', $kvp, $matches);
            $pieces = array_map($trimWhitespaceCallback, $matches[0]);
            $part[$pieces[0]] = isset($pieces[1]) ? $pieces[1] : '';
        }
        if (isset($part['rel']) && in_array('http://www.w3.org/ns/json-ld#context', explode(' ', $part['rel']))) {
            $contexts[] = (string) $baseIri->resolve(trim(key($part), '<> '));
        }
    }
    return array_values(array_unique($contexts));
}
示例#2
0
 /**
  * Converts a JSON-LD element to a RDF Quad object
  *
  * @param Object $element The element to be converted.
  *
  * @return IRI|TypedValue|LanguageTagged|null The converted element to be used as Quad object.
  */
 private function elementToRdf(Object $element)
 {
     if (property_exists($element, '@value')) {
         return Value::fromJsonLd($element);
     }
     $iri = new IRI($element->{'@id'});
     return $iri->isAbsolute() ? $iri : null;
 }