5 use seekat\Exception\UnexpectedValueException
;
14 class Links
implements Serializable
17 * Parsed "Link" relations
23 * Parsed "Link" relations
26 private $relations = [];
29 * Parse the hypermedia link header
31 * @param Header $links The Link header
32 * @throws UnexpectedValueException
34 function __construct(Header
$links) {
35 if (strcasecmp($links->name
, "Link")) {
36 throw new UnexpectedValueException("Expected 'Link' header, got: '{$links->name}'");
38 $this->unserialize($links->value
);
44 function __toString() : string {
45 return $this->serialize();
51 function serialize() {
52 return (string) $this->params
;
56 * @param string $links
58 function unserialize($links) {
59 $this->params
= new Params($links, ",", ";", "=",
60 Params
::PARSE_RFC5988 | Params
::PARSE_ESCAPED
);
61 if ($this->params
->params
) {
62 foreach ($this->params
->params
as $link => $param) {
63 $this->relations
[$param["arguments"]["rel"]] = new Url($link);
69 * Receive the link header's parsed relations
73 function getRelations() : array {
74 return $this->relations
;
78 * Get the URL of the link's "next" relation
80 * Returns the link's "last" relation if it exists and "next" is not set.
85 if (isset($this->relations
["next"])) {
86 return $this->relations
["next"];
88 if (isset($this->relations
["last"])) {
89 return $this->relations
["last"];
95 * Get the URL of the link's "prev" relation
97 * Returns the link's "first" relation if it exists and "prev" is not set.
102 if (isset($this->relations
["prev"])) {
103 return $this->relations
["prev"];
105 if (isset($this->relations
["first"])) {
106 return $this->relations
["first"];
112 * Get the URL of the link's "last" relation
117 if (isset($this->relations
["last"])) {
118 return $this->relations
["last"];
124 * Get the URL of the link's "first" relation
128 function getFirst() {
129 if (isset($this->relations
["first"])) {
130 return $this->relations
["first"];
136 * Get the page sequence of the current link's relation
138 * @param string $which The relation of which to extract the page
139 * @return int The current page sequence
141 function getPage($which) {
142 if (($link = $this->{"get$which"}())) {
143 $url = new Url($link, null, 0);
144 $qry = new QueryString($url->query
);
145 return $qry->getInt("page", 1);