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<string, Url>
*/
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) {
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);
+ }
}
}
}
/**
* Receive the link header's parsed relations
*
- * @return array
+ * @return array<string, Url>
*/
function getRelations() : array {
return $this->relations;
* 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"];
}
* 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"];
}
/**
* 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"];
}
/**
* 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"];
}
* @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);