1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Helpers\Providers; |
4
|
|
|
|
5
|
|
|
trait PaginationHelper |
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 function getPaginatedData($callback, $params, $batchesLimit = 0) |
19
|
|
|
{ |
20
|
|
|
$batchesNum = 0; |
21
|
|
|
do { |
22
|
|
|
$response = self::getPaginatedResponse($callback, $params); |
23
|
|
|
$items = self::getDataFromPaginatedResponse($response); |
24
|
|
|
|
25
|
|
|
if (empty($items)) { |
26
|
|
|
return; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$params['bookmarks'] = self::getBookMarks($response); |
30
|
|
|
$batchesNum++; |
31
|
|
|
yield $items; |
32
|
|
|
} while ( ! self::reachBatchesLimit($batchesLimit, $batchesNum)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function getPaginatedResponse(callable $callback, array $params) |
36
|
|
|
{ |
37
|
|
|
$response = call_user_func_array($callback, $params); |
38
|
|
|
if (self::_responseHasData($response)) { |
39
|
|
|
return self::_clearResponseFromMetaData($response); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function getDataFromPaginatedResponse($response) |
44
|
|
|
{ |
45
|
|
|
if (self::_responseHasData($response)) { |
46
|
|
|
$res = self::_clearResponseFromMetaData($response); |
47
|
|
|
|
48
|
|
|
return $res['data']; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return []; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param array $res |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
protected function _responseHasData($res) |
59
|
|
|
{ |
60
|
|
|
return isset($res['data']) && ! empty($res['data']); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Check if we get batches limit in pagination |
65
|
|
|
* @param int $batchesLimit |
66
|
|
|
* @param int $batchesNum |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
protected function reachBatchesLimit($batchesLimit, $batchesNum) |
70
|
|
|
{ |
71
|
|
|
return $batchesLimit && $batchesNum >= $batchesLimit; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Remove 'module' data from response |
76
|
|
|
* @param array $res |
77
|
|
|
* @return array mixed |
78
|
|
|
*/ |
79
|
|
|
protected function _clearResponseFromMetaData($res) |
80
|
|
|
{ |
81
|
|
|
if (isset($res['data'][0]['type']) && $res['data'][0]['type'] == 'module') { |
82
|
|
|
array_shift($res['data']); |
83
|
|
|
|
84
|
|
|
return $res; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $res; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param $response |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
protected function getBookMarks($response) |
95
|
|
|
{ |
96
|
|
|
return isset($response['bookmarks']) ? $response['bookmarks'] : []; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|