5 use http\
{Header
, Params
, QueryString
, Url
};
6 use seekat\Exception\UnexpectedValueException
;
10 * Parsed "Link" relations
11 * @var array<string, Url>
13 private $relations = [];
16 * Parse the hypermedia link header
18 * @param ?Header $links The Link header
19 * @throws UnexpectedValueException
21 function __construct(Header
$links = null) {
23 if (strcasecmp($links->name
, "Link")) {
24 throw new UnexpectedValueException("Expected 'Link' header, got: '{$links->name}'");
26 $params = new Params($links->value
, ",", ";", "=",
27 Params
::PARSE_RFC5988 | Params
::PARSE_ESCAPED
);
28 if ($params->params
) {
29 foreach ($params->params
as $link => $param) {
30 $this->relations
[$param["arguments"]["rel"]] = new Url($link);
37 * Receive the link header's parsed relations
39 * @return array<string, Url>
41 function getRelations() : array {
42 return $this->relations
;
46 * Get the URL of the link's "next" relation
48 * Returns the link's "last" relation if it exists and "next" is not set.
50 function getNext() : ?Url
{
51 if (isset($this->relations
["next"])) {
52 return $this->relations
["next"];
54 if (isset($this->relations
["last"])) {
55 return $this->relations
["last"];
61 * Get the URL of the link's "prev" relation
63 * Returns the link's "first" relation if it exists and "prev" is not set.
65 function getPrev() : ?Url
{
66 if (isset($this->relations
["prev"])) {
67 return $this->relations
["prev"];
69 if (isset($this->relations
["first"])) {
70 return $this->relations
["first"];
76 * Get the URL of the link's "last" relation
78 function getLast() : ?Url
{
79 if (isset($this->relations
["last"])) {
80 return $this->relations
["last"];
86 * Get the URL of the link's "first" relation
88 function getFirst() : ?Url
{
89 if (isset($this->relations
["first"])) {
90 return $this->relations
["first"];
96 * Get the page sequence of the current link's relation
98 * @param string $which The relation of which to extract the page
99 * @return int The current page sequence
101 function getPage($which) : int {
102 if (($link = $this->{"get$which"}())) {
103 $url = new Url($link, null, 0);
104 $qry = new QueryString($url->query
);
105 return $qry->getInt("page", 1);