X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fseekat;a=blobdiff_plain;f=lib%2FAPI%2FLinks.php;h=9f4f976570de3d5ed9e39f429ef2751eac7c0d11;hp=5546e65f8f33aa7d0391ed7770e9142c4b1465c4;hb=654d736df2c46ec2520f73e9089d06a44f2b9c50;hpb=8ef054b51c681e7822133b38f7c5ed9dd2a0f29c diff --git a/lib/API/Links.php b/lib/API/Links.php index 5546e65..9f4f976 100644 --- a/lib/API/Links.php +++ b/lib/API/Links.php @@ -2,51 +2,33 @@ namespace seekat\API; -use http\Header; -use http\Params; -use http\QueryString; -use http\Url; +use http\{Header, Params, QueryString, Url}; +use seekat\Exception\UnexpectedValueException; -class Links implements \Serializable -{ +final class Links { /** * Parsed "Link" relations - * @var \http\Params - */ - private $params; - - /** - * Parsed "Link" relations - * @var array + * @var array */ private $relations = []; /** * Parse the hypermedia link header * - * @var string $header_value The value of the "Link" header + * @param ?Header $links The Link header + * @throws UnexpectedValueException */ - function __construct(Header $links) { - if (strcasecmp($links->name, "Link")) { - throw new \UnexpectedValueException("Expected 'Link' header, got: '{$links->name}'"); - } - $this->unserialize($links->value); - } - - function __toString() : string { - return $this->serialize(); - } - - function serialize() { - return (string) $this->params; - } - - function unserialize($links) { - $this->params = new Params($links, ",", ";", "=", - Params::PARSE_RFC5988 | Params::PARSE_ESCAPED); - if ($this->params->params) { - foreach ($this->params->params as $link => $param) { - $this->relations[$param["arguments"]["rel"]] = new Url($link); + function __construct(Header $links = null) { + if ($links) { + if (strcasecmp($links->name, "Link")) { + throw new UnexpectedValueException("Expected 'Link' header, got: '{$links->name}'"); + } + $params = new Params($links->value, ",", ";", "=", + Params::PARSE_RFC5988 | Params::PARSE_ESCAPED); + if ($params->params) { + foreach ($params->params as $link => $param) { + $this->relations[$param["arguments"]["rel"]] = new Url($link); + } } } } @@ -54,7 +36,7 @@ class Links implements \Serializable /** * Receive the link header's parsed relations * - * @return array + * @return array */ function getRelations() : array { return $this->relations; @@ -64,10 +46,8 @@ class Links implements \Serializable * Get the URL of the link's "next" relation * * Returns the link's "last" relation if it exists and "next" is not set. - * - * @return \http\Url */ - function getNext() { + function getNext() : ?Url { if (isset($this->relations["next"])) { return $this->relations["next"]; } @@ -81,10 +61,8 @@ class Links implements \Serializable * Get the URL of the link's "prev" relation * * Returns the link's "first" relation if it exists and "prev" is not set. - * - * @return \http\Url */ - function getPrev() { + function getPrev() : ?Url { if (isset($this->relations["prev"])) { return $this->relations["prev"]; } @@ -96,10 +74,8 @@ class Links implements \Serializable /** * Get the URL of the link's "last" relation - * - * @return \http\Url */ - function getLast() { + function getLast() : ?Url { if (isset($this->relations["last"])) { return $this->relations["last"]; } @@ -108,10 +84,8 @@ class Links implements \Serializable /** * Get the URL of the link's "first" relation - * - * @return \http\Url */ - function getFirst() { + function getFirst() : ?Url { if (isset($this->relations["first"])) { return $this->relations["first"]; } @@ -124,7 +98,7 @@ class Links implements \Serializable * @param string $which The relation of which to extract the page * @return int The current page sequence */ - function getPage($which) { + function getPage($which) : int { if (($link = $this->{"get$which"}())) { $url = new Url($link, null, 0); $qry = new QueryString($url->query);