1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the bitbucket-api package. |
4
|
|
|
* |
5
|
|
|
* (c) Alexandru G. <[email protected]> |
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 <[email protected]> |
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
|
10 |
|
public function __construct(ClientInterface $httpClient, MessageInterface $response) |
34
|
|
|
{ |
35
|
|
|
/** @var Response $response */ |
36
|
10 |
|
if (!$response->isOk()) { |
37
|
2 |
|
throw new \UnexpectedValueException("Can't paginate an unsuccessful response."); |
38
|
|
|
} |
39
|
|
|
|
40
|
8 |
|
$this->httpClient = $httpClient; |
41
|
8 |
|
$this->response = $response; |
42
|
8 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritDoc} |
46
|
|
|
*/ |
47
|
3 |
|
public function hasNext() |
48
|
|
|
{ |
49
|
3 |
|
return array_key_exists('next', $this->getContent()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritDoc} |
54
|
|
|
*/ |
55
|
2 |
|
public function hasPrevious() |
56
|
|
|
{ |
57
|
2 |
|
return array_key_exists('previous', $this->getContent()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritDoc} |
62
|
|
|
*/ |
63
|
3 |
|
public function fetchNext() |
64
|
|
|
{ |
65
|
3 |
|
if ($this->hasNext()) { |
66
|
2 |
|
$content = $this->getContent(); |
67
|
2 |
|
return $this->response = $this->httpClient->get($content['next']); |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritDoc} |
75
|
|
|
*/ |
76
|
2 |
|
public function fetchPrevious() |
77
|
|
|
{ |
78
|
2 |
|
if ($this->hasPrevious()) { |
79
|
1 |
|
$content = $this->getContent(); |
80
|
1 |
|
return $this->response = $this->httpClient->get($content['previous']); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
return null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritDoc} |
88
|
|
|
*/ |
89
|
3 |
|
public function fetchAll() |
90
|
|
|
{ |
91
|
3 |
|
$content = $this->getContent(); |
92
|
3 |
|
$values = []; |
93
|
|
|
|
94
|
|
|
// merge all `values` and replace it inside the most recent response. |
95
|
3 |
|
while (true) { |
96
|
3 |
|
if (!array_key_exists('values', $content)) { |
97
|
2 |
|
break; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
$values = (0 === count($values)) ? $content['values'] : array_merge($values, $content['values']); |
101
|
|
|
|
102
|
1 |
|
if (null !== ($next = $this->fetchNext())) { |
103
|
1 |
|
$content = $this->getContent(); |
104
|
1 |
|
continue; |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
break; |
108
|
|
|
} |
109
|
|
|
|
110
|
3 |
|
$content['values'] = $values; |
111
|
3 |
|
$this->response->setContent(json_encode($content)); |
112
|
|
|
|
113
|
3 |
|
return $this->response; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritDoc} |
118
|
|
|
*/ |
119
|
1 |
|
public function getCurrent() |
120
|
|
|
{ |
121
|
1 |
|
return $this->response; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @access private |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
7 |
|
private function getContent() |
129
|
|
|
{ |
130
|
7 |
|
$content = json_decode($this->response->getContent(), true); |
131
|
|
|
|
132
|
7 |
|
if (is_array($content) && JSON_ERROR_NONE === json_last_error()) { |
133
|
|
|
// replace reference inserted by `LegacyCollectionListener` with actual data. |
134
|
6 |
|
if (array_key_exists('values', $content) && |
135
|
6 |
|
is_string($content['values']) && |
136
|
6 |
|
strpos($content['values'], '.') !== false) { |
137
|
|
|
$content['values'] = $content[str_replace('.', '', $content['values'])]; |
138
|
|
|
} |
139
|
6 |
|
return $content; |
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
return []; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|