|
| 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 <ramon@future500.nl> |
| 14 | + * @author Mitchel Verschoof <mitchel@future500.nl> |
| 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 = null; |
| 28 | + |
| 29 | + public function __construct( Client $client ) |
| 30 | + { |
| 31 | + $this->client = $client; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * {@inheritdoc} |
| 36 | + */ |
| 37 | + public function fetch( ApiInterface $api, $method ) |
| 38 | + { |
| 39 | + $parameters = array_slice(func_get_args(),2); |
| 40 | + |
| 41 | + $result = call_user_func_array(array($api, $method), $parameters); |
| 42 | + $this->postFetch(); |
| 43 | + |
| 44 | + return $result; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * {@inheritdoc} |
| 49 | + */ |
| 50 | + public function fetchAll( ApiInterface $api, $method ) |
| 51 | + { |
| 52 | + $parameters = array_slice(func_get_args(),2); |
| 53 | + |
| 54 | + // Set parameters per_page to GitHub max to minimize number of requests |
| 55 | + $api->setPerPage(100); |
| 56 | + |
| 57 | + $result = array(); |
| 58 | + $result = call_user_func_array(array($api, $method), $parameters); |
| 59 | + $this->postFetch(); |
| 60 | + |
| 61 | + while ($this->hasNext()) { |
| 62 | + $result = array_merge($result, $this->fetchNext()); |
| 63 | + } |
| 64 | + |
| 65 | + return $result; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * {@inheritdoc} |
| 70 | + */ |
| 71 | + public function postFetch() |
| 72 | + { |
| 73 | + $this->pagination = $this->client->getHttpClient()->getLastResponse()->getPagination(); |
| 74 | + var_dump( $this->pagination ); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * {@inheritdoc} |
| 79 | + */ |
| 80 | + public function hasNext() |
| 81 | + { |
| 82 | + return $this->has('next'); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * {@inheritdoc} |
| 87 | + */ |
| 88 | + public function fetchNext() |
| 89 | + { |
| 90 | + return $this->get('next'); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * {@inheritdoc} |
| 95 | + */ |
| 96 | + public function hasPrevious() |
| 97 | + { |
| 98 | + return $this->has('prev'); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * {@inheritdoc} |
| 103 | + */ |
| 104 | + public function fetchPrevious() |
| 105 | + { |
| 106 | + return $this->get('prev'); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * {@inheritdoc} |
| 111 | + */ |
| 112 | + public function fetchFirst() |
| 113 | + { |
| 114 | + return $this->get('first'); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * {@inheritdoc} |
| 119 | + */ |
| 120 | + public function fetchLast() |
| 121 | + { |
| 122 | + return $this->get('last'); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * {@inheritdoc} |
| 127 | + */ |
| 128 | + protected function has($key) |
| 129 | + { |
| 130 | + if (!empty($this->pagination) and isset($this->pagination[$key])) { |
| 131 | + return true; |
| 132 | + } |
| 133 | + |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * {@inheritdoc} |
| 139 | + */ |
| 140 | + protected function get($key) |
| 141 | + { |
| 142 | + if ( $this->has($key) ) { |
| 143 | + $result = $this->client->getHttpClient()->get($this->pagination[$key]); |
| 144 | + $this->postFetch(); |
| 145 | + |
| 146 | + return $result->getContent(); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments