X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fseekat;a=blobdiff_plain;f=lib%2FAPI%2FLinks.php;fp=lib%2FAPI%2FLinks.php;h=9f4f976570de3d5ed9e39f429ef2751eac7c0d11;hp=20fbc6d846ec3226beb3ee518ade5e6161da69e3;hb=654d736df2c46ec2520f73e9089d06a44f2b9c50;hpb=2121556150be871684b5046af7cf250b8219128d diff --git a/lib/API/Links.php b/lib/API/Links.php index 20fbc6d..9f4f976 100644 --- a/lib/API/Links.php +++ b/lib/API/Links.php @@ -4,25 +4,18 @@ namespace seekat\API; use http\{Header, Params, QueryString, Url}; use seekat\Exception\UnexpectedValueException; -use Serializable; -final class Links implements Serializable { +final class Links { /** * Parsed "Link" relations - * @var Params - */ - private $params; - - /** - * Parsed "Link" relations - * @var array + * @var array */ private $relations = []; /** * Parse the hypermedia link header * - * @param Header $links The Link header + * @param ?Header $links The Link header * @throws UnexpectedValueException */ function __construct(Header $links = null) { @@ -30,33 +23,12 @@ final class Links implements Serializable { if (strcasecmp($links->name, "Link")) { throw new UnexpectedValueException("Expected 'Link' header, got: '{$links->name}'"); } - $this->unserialize($links->value); - } - } - - /** - * @return string - */ - function __toString() : string { - return $this->serialize(); - } - - /** - * @return string - */ - function serialize() { - return (string) $this->params; - } - - /** - * @param string $links - */ - 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); + $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); + } } } } @@ -64,7 +36,7 @@ final class Links implements Serializable { /** * Receive the link header's parsed relations * - * @return array + * @return array */ function getRelations() : array { return $this->relations; @@ -74,10 +46,8 @@ final 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 Url */ - function getNext() { + function getNext() : ?Url { if (isset($this->relations["next"])) { return $this->relations["next"]; } @@ -91,10 +61,8 @@ final 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 Url */ - function getPrev() { + function getPrev() : ?Url { if (isset($this->relations["prev"])) { return $this->relations["prev"]; } @@ -106,10 +74,8 @@ final class Links implements Serializable { /** * Get the URL of the link's "last" relation - * - * @return Url */ - function getLast() { + function getLast() : ?Url { if (isset($this->relations["last"])) { return $this->relations["last"]; } @@ -118,10 +84,8 @@ final class Links implements Serializable { /** * Get the URL of the link's "first" relation - * - * @return Url */ - function getFirst() { + function getFirst() : ?Url { if (isset($this->relations["first"])) { return $this->relations["first"]; } @@ -134,7 +98,7 @@ final 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);