10 class Links
implements \Serializable
13 * Parsed "Link" relations
19 * Parsed "Link" relations
22 private $relations = [];
25 * Parse the hypermedia link header
27 * @var string $header_value The value of the "Link" header
29 function __construct(Header
$links) {
30 if (strcasecmp($links->name
, "Link")) {
31 throw new \
UnexpectedValueException("Expected 'Link' header, got: '{$links->name}'");
33 $this->unserialize($links->value
);
36 function __toString() : string {
37 return $this->serialize();
40 function serialize() {
41 return (string) $this->params
;
44 function unserialize($links) {
45 $this->params
= new Params($links, ",", ";", "=",
46 Params
::PARSE_RFC5988 | Params
::PARSE_ESCAPED
);
47 if ($this->params
->params
) {
48 foreach ($this->params
->params
as $link => $param) {
49 $this->relations
[$param["arguments"]["rel"]] = new Url($link);
55 * Receive the link header's parsed relations
59 function getRelations() : array {
60 return $this->relations
;
64 * Get the URL of the link's "next" relation
66 * Returns the link's "last" relation if it exists and "next" is not set.
71 if (isset($this->relations
["next"])) {
72 return $this->relations
["next"];
74 if (isset($this->relations
["last"])) {
75 return $this->relations
["last"];
81 * Get the URL of the link's "prev" relation
83 * Returns the link's "first" relation if it exists and "prev" is not set.
88 if (isset($this->relations
["prev"])) {
89 return $this->relations
["prev"];
91 if (isset($this->relations
["first"])) {
92 return $this->relations
["first"];
98 * Get the URL of the link's "last" relation
103 if (isset($this->relations
["last"])) {
104 return $this->relations
["last"];
110 * Get the URL of the link's "first" relation
114 function getFirst() {
115 if (isset($this->relations
["first"])) {
116 return $this->relations
["first"];
122 * Get the page sequence of the current link's relation
124 * @param string $which The relation of which to extract the page
125 * @return int The current page sequence
127 function getPage($which) {
128 if (($link = $this->{"get$which"}())) {
129 $url = new Url($link, null, 0);
130 $qry = new QueryString($url->query
);
131 return $qry->getInt("page", 1);