Skip to content

Commit

Permalink
class URNParser; basic semantics of RFC 8141 q-component & f-component
Browse files Browse the repository at this point in the history
  • Loading branch information
fititnt committed Oct 1, 2022
1 parent 511f63b commit c34501c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 18 deletions.
81 changes: 64 additions & 17 deletions app/lib/urnresolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,26 @@
$global_conf = new Config();
define("URNRESOLVER_BASE", $global_conf->base_iri);

$extention_to_media_type = [
'.csv' => 'text/csv; charset=utf-8',
'.json' => 'application/json; charset=utf-8',
'.jsonld' => 'application/ld+json; charset=utf-8',
'.tsv' => 'text/tab-separated-values; charset=utf-8',
'.hxl.csv' => 'text/csv; charset=utf-8',
'.hxl.tsv' => 'text/tab-separated-values; charset=utf-8',
];

$processor_tabular = [
'delimiter' => [

class Common
{
public const EXT_TO_MEDIATYPE = [
'.csv' => 'text/csv; charset=utf-8',
'.json' => 'application/json; charset=utf-8',
'.jsonld' => 'application/ld+json; charset=utf-8',
'.tsv' => 'text/tab-separated-values; charset=utf-8',
'.txt' => 'text/plain; charset=utf-8',
'.hxl.csv' => 'text/csv; charset=utf-8',
'.hxl.tsv' => 'text/tab-separated-values; charset=utf-8',
];

public const EXT_TABULAR_DELIMITER = [
'.csv' => ",",
'.hxl.csv' => ",",
'.tsv' => "\t",
'.hxl.tsv' => "\t",
]
];
];
}

// @TODO implement profiles https://www.w3.org/TR/dx-prof-conneg/

Expand Down Expand Up @@ -337,6 +340,11 @@ public function execute_redirect(
/**
* @see https://www.rfc-editor.org/rfc/rfc8141
* @see https://www.php.net/manual/en/function.parse-url.php
*
* @example
* // https://www.rfc-editor.org/rfc/rfc8141#section-2.3.1
* urn:example:weather?=op=map&lat=39.56&lon=-104.85&datetime=1969-07-21T02:56:15Z
*
*/
class URNParser
{
Expand All @@ -360,6 +368,10 @@ class URNParser
public ?string $nid; // Lower case (if applicable)
public ?string $nss;
public ?array $nss_parts;
public ?string $q_component;
public ?string $f_component;

public ?array $q_component_parts;

public function __construct(string $raw_urn)
{
Expand All @@ -384,15 +396,45 @@ public function __construct(string $raw_urn)
$this->nid = strtolower(array_shift($path_parts));
$this->nss = implode(':', $path_parts);
$this->nss_parts = $path_parts;

// https://www.rfc-editor.org/rfc/rfc8141#section-2.3.2
$temp_q = explode('?=', $this->raw_urn);
if (count($temp_q) > 1) {
$q_and_maybe_f = $temp_q[1];
$temp_qf = explode('#', $q_and_maybe_f);
$this->q_component = $temp_qf[0];
parse_str($this->q_component, $this->q_component_parts);
}
// https://www.rfc-editor.org/rfc/rfc8141#section-2.3.3
$temp_f = explode('#', $this->raw_urn);
if (count($temp_f) > 1) {
$this->f_component = array_pop($temp_f);
}
}
}
}

class URNParserResolver extends URNParser
{
public ?string $file_extension;
public ?string $media_type;

public function __construct(string $urn)
{
parent::__construct($urn);
// if ($this->nid === 'resolver') {
// if (!empty($this->query_parts['u2709'])) {
// $this->file_extension = $this->query_parts['u2709'];
// $this->media_type = Common::EXT_TO_MEDIATYPE[$this->file_extension];
// }
// }
// var_dump($this);die;
if (!empty($this->q_component_parts)) {
if (!empty($this->q_component_parts['u2709'])) {
$this->file_extension = $this->q_component_parts['u2709'];
$this->media_type = Common::EXT_TO_MEDIATYPE[$this->file_extension];
}
}
}
}

Expand Down Expand Up @@ -493,11 +535,16 @@ private function _get_urnr_values(array $filters = null)

public function execute()
{
var_dump(parse_url($this->urn));
// var_dump(parse_url($this->urn));

// $test = 'urn:example:weather?=op=map&lat=39.56&lon=-104.85&datetime=1969-07-21T02:56:15Z#lalala#lelele';

$parsed_urn = new URNParser($this->urn);
var_dump($parsed_urn);
die;
// $parsed_urn = new URNParserResolver($test);
// var_dump($parsed_urn);

// $parsed_urn = new URNParserResolver($this->urn);
// var_dump($parsed_urn);
// die;

if ($this->urn === 'urn:resolver:ping') {
return $this->operation_ping();
Expand Down
3 changes: 2 additions & 1 deletion resolvers/urn:resolver:.urnr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ meta:
_cc_mode: internal

examples:
- in.urn: urn:resolver:ping?u2709=.txt
# - in.urn: urn:resolver:ping?u2709=.txt
- in.urn: urn:resolver:ping?=u2709=.txt
- in.urn: urn:resolver:index
# - in.urn: urn:resolver:help
- in.urn: urn:resolver:_explore
Expand Down

0 comments on commit c34501c

Please sign in to comment.