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