|
| 1 | +<?php namespace Gitlab; |
| 2 | + |
| 3 | +use Gitlab\Api\ApiInterface; |
| 4 | + |
| 5 | +/** |
| 6 | + * Pager class for supporting pagination in Gitlab classes |
| 7 | + */ |
| 8 | +class ResultPager implements ResultPagerInterface |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @var \Gitlab\Client client |
| 12 | + */ |
| 13 | + protected $client; |
| 14 | + |
| 15 | + /** |
| 16 | + * The Gitlab client to use for pagination. This must be the same |
| 17 | + * instance that you got the Api instance from, i.e.: |
| 18 | + * |
| 19 | + * $client = new \Gitlab\Client(); |
| 20 | + * $api = $client->api('someApi'); |
| 21 | + * $pager = new \Gitlab\ResultPager($client); |
| 22 | + * |
| 23 | + * @param \Gitlab\Client $client |
| 24 | + * |
| 25 | + */ |
| 26 | + public function __construct(Client $client) |
| 27 | + { |
| 28 | + $this->client = $client; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * {@inheritdoc} |
| 33 | + */ |
| 34 | + public function fetch(ApiInterface $api, $method, array $parameters = array()) |
| 35 | + { |
| 36 | + $result = call_user_func_array(array($api, $method), $parameters); |
| 37 | + |
| 38 | + return $result; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * {@inheritdoc} |
| 43 | + */ |
| 44 | + public function fetchAll(ApiInterface $api, $method, array $parameters = array()) |
| 45 | + { |
| 46 | + $result = call_user_func_array(array($api, $method), $parameters); |
| 47 | + |
| 48 | + while ($this->hasNext()) { |
| 49 | + $result = array_merge($result, $this->fetchNext()); |
| 50 | + } |
| 51 | + |
| 52 | + return $result; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * {@inheritdoc} |
| 57 | + */ |
| 58 | + public function hasNext() |
| 59 | + { |
| 60 | + return $this->has('next'); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * {@inheritdoc} |
| 65 | + */ |
| 66 | + public function fetchNext() |
| 67 | + { |
| 68 | + return $this->get('next'); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * {@inheritdoc} |
| 73 | + */ |
| 74 | + public function hasPrevious() |
| 75 | + { |
| 76 | + return $this->has('prev'); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * {@inheritdoc} |
| 81 | + */ |
| 82 | + public function fetchPrevious() |
| 83 | + { |
| 84 | + return $this->get('prev'); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * {@inheritdoc} |
| 89 | + */ |
| 90 | + public function fetchFirst() |
| 91 | + { |
| 92 | + return $this->get('first'); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * {@inheritdoc} |
| 97 | + */ |
| 98 | + public function fetchLast() |
| 99 | + { |
| 100 | + return $this->get('last'); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * {@inheritdoc} |
| 105 | + */ |
| 106 | + protected function has($key) |
| 107 | + { |
| 108 | + return !empty($this->client->getHttpClient()->getLastResponse()->getPagination()) && isset($this->client->getHttpClient()->getLastResponse()->getPagination()[$key]); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * {@inheritdoc} |
| 113 | + */ |
| 114 | + protected function get($key) |
| 115 | + { |
| 116 | + if ($this->has($key)) { |
| 117 | + $result = $this->client->getHttpClient()->get(strtr($this->client->getHttpClient()->getLastResponse()->getPagination()[$key], array($this->client->getBaseUrl() => '')))->getContent(); |
| 118 | + return $result; |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments