From 944e9ee886b122176c235d1e528372992602e62c Mon Sep 17 00:00:00 2001 From: Dave Longley Date: Fri, 13 Sep 2013 13:07:18 -0400 Subject: [PATCH] Add link header parse method. --- jsonld.php | 61 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 5 deletions(-) diff --git a/jsonld.php b/jsonld.php index 5fcebac..4cc84c6 100644 --- a/jsonld.php +++ b/jsonld.php @@ -1,7 +1,7 @@ toRDF($input, $options); } +/** + * Parses a link header. The results will be key'd by the value of "rel". + * + * Link: ; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json" + * + * Parses as: { + * 'http://www.w3.org/ns/json-ld#context': { + * target: http://json-ld.org/contexts/person.jsonld, + * type: 'application/ld+json' + * } + * } + * + * If there is more than one "rel" with the same IRI, then entries in the + * resulting map for that "rel" will be arrays. + * + * @param string $header the link header to parse. + * + * @return assoc the parsed result. + */ +function jsonld_parse_link_header($header) { + $rval = array(); + // split on unbracketed/unquoted commas + if(!preg_match_all('/(?:<[^>]*?>|"[^"]*?"|[^,])+/', $header, $entries)) { + return $rval; + } + $r_link_header = '/\s*<([^>]*?)>\s*(?:;\s*(.*))?/'; + foreach($entries as $entry) { + $match = preg_match($r_link_header, $entry[0]); + if(!$match) { + continue; + } + $result = array('target' => $match[1]); + $params = $match[2]; + $r_params = '/(.*?)=(?:(?:"([^"]*?)")|([^"]*?))\s*(?:(?:;\s*)|$)/'; + $matches = preg_match_all($r_params, $params); + foreach($matches as $match) { + $result[$match[1]] = $match[2] ?: $match[3]; + } + $rel = $result['rel'] ?: ''; + if(!isset($rval, $rel)) { + $rval[$rel] = $result; + } + else if(is_array($rval, $rel)) { + $rval[$rel][] = $result; + } + else { + $rval[$rel] = array($rval[$rel], $result); + } + } + return $rval; +} + /** * Relabels all blank nodes in the given JSON-LD input. * @@ -5355,11 +5407,10 @@ jsonld_register_rdf_parser( * A JSON-LD Exception. */ class JsonLdException extends Exception { - protected $type; - protected $details; - protected $cause; - public function __construct($msg, $type, $details=null, $previous=null) { + public function __construct( + $msg, $type, /*$code='error',*/ $details=null, $previous=null) { $this->type = $type; + //$this->code = $code; $this->details = $details; $this->cause = $previous; parent::__construct($msg, 0, $previous);