refactor guthub api
[pharext/pharext.org] / app / Github / Links.php
1 <?php
2
3 namespace app\Github;
4
5 use http\Params;
6 use http\QueryString;
7 use http\Url;
8
9 class Links
10 {
11 /**
12 * @var \http\Params
13 */
14 private $params;
15
16 /**
17 * @var array
18 */
19 private $relations = [];
20
21 function __construct($header_value) {
22 $this->params = new Params($header_value, ",", ";", "=",
23 Params::PARSE_RFC5988 | Params::PARSE_ESCAPED);
24 if ($this->params->params) {
25 foreach ($this->params->params as $link => $param) {
26 $this->relations[$param["arguments"]["rel"]] = $link;
27 }
28 }
29 }
30
31 function getRelations() {
32 return $this->relations;
33 }
34
35 function getNext() {
36 if (isset($this->relations["next"])) {
37 return $this->relations["next"];
38 }
39 if (isset($this->relations["last"])) {
40 return $this->relations["last"];
41 }
42 return null;
43 }
44
45 function getPrev() {
46 if (isset($this->relations["prev"])) {
47 return $this->relations["prev"];
48 }
49 if (isset($this->relations["first"])) {
50 return $this->relations["first"];
51 }
52 return null;
53 }
54
55 function getLast() {
56 if (isset($this->relations["last"])) {
57 return $this->relations["last"];
58 }
59 return null;
60 }
61
62 function getFirst() {
63 if (isset($this->relations["first"])) {
64 return $this->relations["first"];
65 }
66 return null;
67 }
68
69 function getPage($which) {
70 if (($link = $this->{"get$which"}())) {
71 $url = new Url($link, null, 0);
72 $qry = new QueryString($url->query);
73 return $qry->getInt("page", 1);
74 }
75 return 1;
76 }
77 }