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