|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace mpyw\HardBotter\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use mpyw\Co\CoInterface; |
|
6
|
|
|
use Aza\Components\Math\BigMath; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @method mixed get($endpoint, array $params = []) |
|
10
|
|
|
* @method \Generator getAsync($endpoint, array $params = []) |
|
11
|
|
|
*/ |
|
12
|
|
|
trait CollectorTrait |
|
13
|
|
|
{ |
|
14
|
|
|
abstract public function __call($method, array $args); |
|
15
|
|
|
|
|
16
|
7 |
|
public function collect($endpoint, $followable_page_count, array $params = []) |
|
17
|
|
|
{ |
|
18
|
|
|
// カーソルに-1を指定する(不要な場合に余分なパラメータとしてつけても問題ない) |
|
19
|
7 |
|
$params += ['cursor' => '-1']; |
|
20
|
|
|
// 初回の結果を取得 |
|
21
|
7 |
|
if (false === $result = $this->get($endpoint, $params)) { |
|
22
|
2 |
|
return false; |
|
23
|
|
|
} |
|
24
|
|
|
// 整形結果と次のリクエストに必要なパラメータを取得 |
|
25
|
7 |
|
list($formatted, $next_params) = static::getFormattedResultAndNextParams($result, $params); |
|
26
|
|
|
// 次のリクエストが不必要であれば整形結果を返す |
|
27
|
6 |
|
if ($next_params === null || $followable_page_count < 1) { |
|
28
|
3 |
|
return $formatted; |
|
29
|
|
|
} |
|
30
|
|
|
// 次のリクエストを実行してマージした結果を返す |
|
31
|
6 |
|
$children = $this->collect($endpoint, $followable_page_count - 1, $next_params); |
|
32
|
5 |
|
return $children !== false ? array_merge($formatted, $children) : false; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
6 |
|
public function collectAsync($endpoint, $followable_page_count, array $params = []) |
|
36
|
|
|
{ |
|
37
|
|
|
// カーソルに-1を指定する(不要な場合に余分なパラメータとしてつけても問題ない) |
|
38
|
6 |
|
$params += ['cursor' => '-1']; |
|
39
|
|
|
// 初回の結果を取得 |
|
40
|
6 |
|
if (false === $result = (yield $this->getAsync($endpoint, $params))) { |
|
41
|
2 |
|
yield CoInterface::RETURN_WITH => false; |
|
42
|
|
|
} |
|
43
|
|
|
// 整形結果と次のリクエストに必要なパラメータを取得 |
|
44
|
6 |
|
list($formatted, $next_params) = static::getFormattedResultAndNextParams($result, $params); |
|
45
|
|
|
// 次のリクエストが不必要であれば整形結果を返す |
|
46
|
6 |
|
if ($next_params === null || $followable_page_count < 1) { |
|
47
|
3 |
|
yield CoInterface::RETURN_WITH => $formatted; |
|
48
|
|
|
} |
|
49
|
|
|
// 次のリクエストを実行してマージした結果を返す |
|
50
|
6 |
|
$children = (yield $this->collectAsync($endpoint, $followable_page_count - 1, $next_params)); |
|
51
|
5 |
|
yield CoInterface::RETURN_WITH => $children !== false ? array_merge($formatted, $children) : false; |
|
52
|
|
|
// @codeCoverageIgnoreStart |
|
53
|
|
|
} |
|
54
|
|
|
// @codeCoverageIgnoreEnd |
|
55
|
|
|
|
|
56
|
13 |
|
protected static function getFormattedResultAndNextParams($result, array $params) |
|
57
|
|
|
{ |
|
58
|
13 |
|
$is_statuses = is_array($result); |
|
59
|
13 |
|
$is_searches = isset($result->statuses) && is_array($result->statuses); |
|
60
|
13 |
|
$is_cursored = isset($result->next_cursor_str); |
|
61
|
|
|
|
|
62
|
|
|
// GET statuses/home_timeline や GET search/tweets など |
|
63
|
13 |
|
if ($is_statuses || $is_searches) { |
|
64
|
10 |
|
$formatted = $is_statuses ? $result : $result->statuses; |
|
65
|
10 |
|
$math = BigMath::createFromServerConfiguration(); |
|
66
|
|
|
return [ |
|
67
|
10 |
|
$formatted, |
|
68
|
10 |
|
$formatted |
|
69
|
10 |
|
? ['max_id' => $math->subtract(end($formatted)->id_str, 1)] + $params |
|
70
|
|
|
: null |
|
71
|
|
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// GET followers/ids など |
|
75
|
3 |
|
if ($is_cursored) { |
|
76
|
|
|
// 配列であるプロパティの名前を求める |
|
77
|
2 |
|
$prop = key(array_filter((array)$result, 'is_array')); |
|
78
|
2 |
|
$formatted = $result->$prop; |
|
79
|
|
|
return [ |
|
80
|
2 |
|
$formatted, |
|
81
|
2 |
|
$result->next_cursor_str |
|
82
|
2 |
|
? ['cursor' => $result->next_cursor_str] + $params |
|
83
|
|
|
: null |
|
84
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// それ以外の場合はそもそもこのメソッドを使うべきではない |
|
88
|
1 |
|
throw new \BadMethodCallException('Incompatible endpoint.'); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|