|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Github; |
| 4 | + |
| 5 | +use Github\Api\ApiInterface; |
| 6 | +use Github\Exception\InvalidArgumentException; |
| 7 | +use Github\HttpClient\HttpClient; |
| 8 | +use Github\HttpClient\HttpClientInterface; |
| 9 | + |
| 10 | +/** |
| 11 | + * Pager class for supporting pagination in github classes |
| 12 | + * |
| 13 | + * @author Ramon de la Fuente <[email protected]> |
| 14 | + * @author Mitchel Verschoof <[email protected]> |
| 15 | + */ |
| 16 | +class ResultPager implements ResultPagerInterface |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var Github\Client client |
| 20 | + */ |
| 21 | + protected $client; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var array pagination |
| 25 | + * Comes from pagination headers in Github API results |
| 26 | + */ |
| 27 | + protected $pagination; |
| 28 | + |
| 29 | + |
| 30 | + /** |
| 31 | + * The Github client to use for pagination. This must be the same |
| 32 | + * instance that you got the Api instance from, i.e.: |
| 33 | + * |
| 34 | + * $client = new \Github\Client(); |
| 35 | + * $api = $client->api('someApi'); |
| 36 | + * $pager = new \Github\ResultPager($client); |
| 37 | + * |
| 38 | + * @param \Github\Client $client |
| 39 | + * |
| 40 | + */ |
| 41 | + public function __construct(Client $client) |
| 42 | + { |
| 43 | + $this->client = $client; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * {@inheritdoc} |
| 48 | + */ |
| 49 | + public function getPagination() |
| 50 | + { |
| 51 | + return $this->pagination; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * {@inheritdoc} |
| 56 | + */ |
| 57 | + public function fetch(ApiInterface $api, $method, array $parameters = array()) |
| 58 | + { |
| 59 | + $result = call_user_func_array(array($api, $method), $parameters); |
| 60 | + $this->postFetch(); |
| 61 | + |
| 62 | + return $result; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * {@inheritdoc} |
| 67 | + */ |
| 68 | + public function fetchAll(ApiInterface $api, $method, array $parameters = array()) |
| 69 | + { |
| 70 | + // get the perPage from the api |
| 71 | + $perPage = $api->getPerPage(); |
| 72 | + |
| 73 | + // Set parameters per_page to GitHub max to minimize number of requests |
| 74 | + $api->setPerPage(100); |
| 75 | + |
| 76 | + $result = array(); |
| 77 | + $result = call_user_func_array(array($api, $method), $parameters); |
| 78 | + $this->postFetch(); |
| 79 | + |
| 80 | + while ($this->hasNext()) { |
| 81 | + $result = array_merge($result, $this->fetchNext()); |
| 82 | + } |
| 83 | + |
| 84 | + // restore the perPage |
| 85 | + $api->setPerPage($perPage); |
| 86 | + |
| 87 | + return $result; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * {@inheritdoc} |
| 92 | + */ |
| 93 | + public function postFetch() |
| 94 | + { |
| 95 | + $this->pagination = $this->client->getHttpClient()->getLastResponse()->getPagination(); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * {@inheritdoc} |
| 100 | + */ |
| 101 | + public function hasNext() |
| 102 | + { |
| 103 | + return $this->has('next'); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * {@inheritdoc} |
| 108 | + */ |
| 109 | + public function fetchNext() |
| 110 | + { |
| 111 | + return $this->get('next'); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * {@inheritdoc} |
| 116 | + */ |
| 117 | + public function hasPrevious() |
| 118 | + { |
| 119 | + return $this->has('prev'); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * {@inheritdoc} |
| 124 | + */ |
| 125 | + public function fetchPrevious() |
| 126 | + { |
| 127 | + return $this->get('prev'); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * {@inheritdoc} |
| 132 | + */ |
| 133 | + public function fetchFirst() |
| 134 | + { |
| 135 | + return $this->get('first'); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * {@inheritdoc} |
| 140 | + */ |
| 141 | + public function fetchLast() |
| 142 | + { |
| 143 | + return $this->get('last'); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * {@inheritdoc} |
| 148 | + */ |
| 149 | + protected function has($key) |
| 150 | + { |
| 151 | + return !empty($this->pagination) && isset($this->pagination[$key]); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * {@inheritdoc} |
| 156 | + */ |
| 157 | + protected function get($key) |
| 158 | + { |
| 159 | + if ($this->has($key)) { |
| 160 | + $result = $this->client->getHttpClient()->get($this->pagination[$key]); |
| 161 | + $this->postFetch(); |
| 162 | + |
| 163 | + return $result->getContent(); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments