Skip to content

Commit f32ee65

Browse files
committed
Added basic Pager in order to support response pagination.
1 parent 30a3e9f commit f32ee65

File tree

4 files changed

+439
-0
lines changed

4 files changed

+439
-0
lines changed

docs/examples/repositories.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ repositories:
1212
### Prepare:
1313
{% include auth.md var_name="repositories" class_ns="Repositories" %}
1414

15+
### Pagination:
16+
{% include pagination.md var_name="repositories" class_ns="Repositories" %}
17+
1518
### Get a list of repositories for an account:
1619

1720
If the caller is properly authenticated and authorized, this method returns a collection containing public and private repositories.
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
/**
3+
* This file is part of the bitbucket-api package.
4+
*
5+
* (c) Alexandru G. <alex@gentle.ro>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace Bitbucket\API\Http\Response;
11+
12+
use Bitbucket\API\Http\ClientInterface;
13+
use Buzz\Message\MessageInterface;
14+
use Buzz\Message\Response;
15+
16+
/**
17+
* @author Alexandru Guzinschi <alex@gentle.ro>
18+
*/
19+
class Pager implements PagerInterface
20+
{
21+
/** @var ClientInterface */
22+
private $httpClient;
23+
24+
/** @var MessageInterface */
25+
private $response;
26+
27+
/**
28+
* @param ClientInterface $httpClient
29+
* @param MessageInterface $response
30+
*
31+
* @throws \UnexpectedValueException
32+
*/
33+
public function __construct(ClientInterface $httpClient, MessageInterface $response)
34+
{
35+
/** @var Response $response */
36+
if (!$response->isOk()) {
37+
throw new \UnexpectedValueException("Can't paginate an unsuccessful response.");
38+
}
39+
40+
$this->httpClient = $httpClient;
41+
$this->response = $response;
42+
}
43+
44+
/**
45+
* {@inheritDoc}
46+
*/
47+
public function hasNext()
48+
{
49+
return array_key_exists('next', $this->getContent());
50+
}
51+
52+
/**
53+
* {@inheritDoc}
54+
*/
55+
public function hasPrevious()
56+
{
57+
return array_key_exists('previous', $this->getContent());
58+
}
59+
60+
/**
61+
* {@inheritDoc}
62+
*/
63+
public function fetchNext()
64+
{
65+
if ($this->hasNext()) {
66+
$content = $this->getContent();
67+
return $this->response = $this->httpClient->get($content['next']);
68+
}
69+
70+
return null;
71+
}
72+
73+
/**
74+
* {@inheritDoc}
75+
*/
76+
public function fetchPrevious()
77+
{
78+
if ($this->hasPrevious()) {
79+
$content = $this->getContent();
80+
return $this->response = $this->httpClient->get($content['previous']);
81+
}
82+
83+
return null;
84+
}
85+
86+
/**
87+
* {@inheritDoc}
88+
*/
89+
public function fetchAll()
90+
{
91+
$content = $this->getContent();
92+
$values = [];
93+
94+
// merge all `values` and replace it inside the most recent response.
95+
while (true) {
96+
if (!array_key_exists('values', $content)) {
97+
break;
98+
}
99+
100+
$values = (0 === count($values)) ? $content['values'] : array_merge($values, $content['values']);
101+
102+
if (null !== ($next = $this->fetchNext())) {
103+
$content = $this->getContent();
104+
continue;
105+
}
106+
107+
break;
108+
}
109+
110+
$content['values'] = $values;
111+
$this->response->setContent(json_encode($content));
112+
113+
return $this->response;
114+
}
115+
116+
/**
117+
* {@inheritDoc}
118+
*/
119+
public function getCurrent()
120+
{
121+
return $this->response;
122+
}
123+
124+
/**
125+
* @access private
126+
* @return array
127+
*/
128+
private function getContent()
129+
{
130+
$content = json_decode($this->response->getContent(), true);
131+
132+
if (is_array($content) && JSON_ERROR_NONE === json_last_error()) {
133+
return $content;
134+
}
135+
136+
return [];
137+
}
138+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* This file is part of the bitbucket-api package.
4+
*
5+
* (c) Alexandru G. <alex@gentle.ro>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace Bitbucket\API\Http\Response;
11+
12+
use Buzz\Message\MessageInterface;
13+
14+
/**
15+
* @author Alexandru Guzinschi <alex@gentle.ro>
16+
*/
17+
interface PagerInterface
18+
{
19+
/**
20+
* @access public
21+
* @return bool
22+
*/
23+
public function hasNext();
24+
25+
/**
26+
* @access public
27+
* @return bool
28+
*/
29+
public function hasPrevious();
30+
31+
/**
32+
* Fetch next page and return http response
33+
*
34+
* @access public
35+
* @return MessageInterface|null
36+
*/
37+
public function fetchNext();
38+
39+
/**
40+
* Fetch previous page and return http response
41+
*
42+
* @access public
43+
* @return MessageInterface|null
44+
*/
45+
public function fetchPrevious();
46+
47+
/**
48+
* Fetch all available pages.
49+
*
50+
* @access public
51+
* @return MessageInterface
52+
*/
53+
public function fetchAll();
54+
55+
/**
56+
* Get current http response.
57+
*
58+
* @access public
59+
* @return MessageInterface
60+
*/
61+
public function getCurrent();
62+
}

0 commit comments

Comments
 (0)