|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Provider; |
|
6
|
|
|
|
|
7
|
|
|
class Pagination |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var Provider |
|
11
|
|
|
*/ |
|
12
|
|
|
private $provider; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $bookmarks = []; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(Provider $provider) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->provider = $provider; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Iterate through results of Api function call. By |
|
26
|
|
|
* default generator will return all pagination results. |
|
27
|
|
|
* To limit result batches, set $batchesLimit. Call function |
|
28
|
|
|
* of object to get data. |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $method |
|
31
|
|
|
* @param array $params |
|
32
|
|
|
* @param int $batchesLimit |
|
33
|
|
|
* @return \Iterator |
|
34
|
|
|
*/ |
|
35
|
|
|
public function run($method, $params, $batchesLimit = 0) |
|
36
|
|
|
{ |
|
37
|
|
|
$batchesNum = 0; |
|
38
|
|
|
do { |
|
39
|
|
|
$results = $this->callProviderRequest($method, $params); |
|
40
|
|
|
if (empty($results)) { |
|
41
|
|
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$batchesNum++; |
|
45
|
|
|
yield $results; |
|
46
|
|
|
|
|
47
|
|
|
if ($this->checkEndBookMarks()) { |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
} while (!$this->reachBatchesLimit($batchesLimit, $batchesNum)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function callProviderRequest($method, array $params) |
|
54
|
|
|
{ |
|
55
|
|
|
$params['bookmarks'] = $this->bookmarks; |
|
56
|
|
|
$response = call_user_func_array([$this->provider, $method], $params); |
|
57
|
|
|
|
|
58
|
|
|
if ($this->responseHasData($response)) { |
|
59
|
|
|
$this->getBookMarks($response); |
|
60
|
|
|
|
|
61
|
|
|
return $this->getDataFromPaginatedResponse($response); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return []; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
protected function getDataFromPaginatedResponse($response) |
|
68
|
|
|
{ |
|
69
|
|
|
if ($this->responseHasData($response)) { |
|
70
|
|
|
$res = $this->clearResponseFromMetaData($response); |
|
71
|
|
|
|
|
72
|
|
|
return $res['data']; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return []; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param array $response |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function responseHasData($response) |
|
84
|
|
|
{ |
|
85
|
|
|
return isset($response['data']) && !empty($response['data']); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Check if we get batches limit in pagination. |
|
90
|
|
|
* |
|
91
|
|
|
* @param int $batchesLimit |
|
92
|
|
|
* @param int $batchesNum |
|
93
|
|
|
* |
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function reachBatchesLimit($batchesLimit, $batchesNum) |
|
97
|
|
|
{ |
|
98
|
|
|
return $batchesLimit && $batchesNum >= $batchesLimit; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Remove 'module' data from response. |
|
103
|
|
|
* |
|
104
|
|
|
* @param array $response |
|
105
|
|
|
* |
|
106
|
|
|
* @return array mixed |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function clearResponseFromMetaData($response) |
|
109
|
|
|
{ |
|
110
|
|
|
if (isset($response['data'][0]['type']) && $response['data'][0]['type'] == 'module') { |
|
111
|
|
|
array_shift($response['data']); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $response; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param $response |
|
119
|
|
|
* |
|
120
|
|
|
* @return array |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function getBookMarks($response) |
|
123
|
|
|
{ |
|
124
|
|
|
$this->bookmarks = isset($response['bookmarks']) ? $response['bookmarks'] : []; |
|
125
|
|
|
|
|
126
|
|
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
protected function checkEndBookMarks() |
|
130
|
|
|
{ |
|
131
|
|
|
return !empty($this->bookmarks) && $this->bookmarks[0] == '-end-'; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|