|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Api\Contracts\PaginatedResponse; |
|
6
|
|
|
|
|
7
|
|
|
class Pagination |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var int |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $limit; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $bookmarks = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param int $limit |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct($limit = 0) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->limit = $limit; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Iterate through results of Api function call. By |
|
29
|
|
|
* default generator will return all pagination results. |
|
30
|
|
|
* To limit result batches, set $limit. Call function |
|
31
|
|
|
* of object to get data. |
|
32
|
|
|
* |
|
33
|
|
|
* @param callable $callback |
|
34
|
|
|
* @return \Generator |
|
35
|
|
|
*/ |
|
36
|
|
|
public function paginateOver(callable $callback) |
|
37
|
|
|
{ |
|
38
|
|
|
$resultsNum = 0; |
|
39
|
|
|
while (true) { |
|
40
|
|
|
|
|
41
|
|
|
$response = $callback($this->bookmarks); |
|
42
|
|
|
$results = $this->processResponse($response); |
|
43
|
|
|
|
|
44
|
|
|
if (empty($results)) return; |
|
45
|
|
|
|
|
46
|
|
|
foreach ($results as $result) { |
|
|
|
|
|
|
47
|
|
|
$resultsNum++; |
|
48
|
|
|
yield $result; |
|
49
|
|
|
|
|
50
|
|
|
if ($this->paginationFinished($resultsNum)) { |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param PaginatedResponse $response |
|
61
|
|
|
* @return array |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function processResponse(PaginatedResponse $response) |
|
64
|
|
|
{ |
|
65
|
|
|
if ($response->hasResponseData()) { |
|
66
|
|
|
$this->bookmarks = $response->getBookmarks(); |
|
67
|
|
|
|
|
68
|
|
|
return $response->getResponseData(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return []; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param int $resultsNum |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function paginationFinished($resultsNum) |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->reachesLimit($this->limit, $resultsNum) || $this->checkEndBookMarks(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Check if we get batches limit in pagination. |
|
85
|
|
|
* |
|
86
|
|
|
* @param int $limit |
|
87
|
|
|
* @param int $resultsNum |
|
88
|
|
|
* |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function reachesLimit($limit, $resultsNum) |
|
92
|
|
|
{ |
|
93
|
|
|
return $limit && $resultsNum >= $limit; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Checks for -end- substring in bookmarks |
|
99
|
|
|
* |
|
100
|
|
|
* @return bool |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function checkEndBookMarks() |
|
103
|
|
|
{ |
|
104
|
|
|
return !empty($this->bookmarks) && $this->bookmarks[0] == '-end-'; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.