1 | <?php |
||
12 | trait CollectorTrait |
||
13 | { |
||
14 | abstract public function __call($method, array $args); |
||
15 | |||
16 | 7 | public function collect($endpoint, $followable_page_count, array $params = []) |
|
34 | |||
35 | 2 | public function collectAsync($endpoint, $followable_page_count, array $params = []) |
|
36 | { |
||
37 | // カーソルに-1を指定する(不要な場合に余分なパラメータとしてつけても問題ない) |
||
38 | 2 | $params += ['cursor' => '-1']; |
|
39 | // 初回の結果を取得 |
||
40 | 2 | if (false === $result = (yield $this->getAsync($endpoint, $params))) { |
|
41 | yield CoInterface::RETURN_WITH => false; |
||
42 | } |
||
43 | // 整形結果と次のリクエストに必要なパラメータを取得 |
||
44 | 2 | list($formatted, $next_params) = static::getFormattedResultAndNextParams($result, $params); |
|
45 | // 次のリクエストが不必要であれば整形結果を返す |
||
46 | 2 | if ($next_params === null || $followable_page_count < 1) { |
|
47 | 2 | yield CoInterface::RETURN_WITH => $formatted; |
|
48 | } |
||
49 | // 次のリクエストを実行してマージした結果を返す |
||
50 | 2 | $children = (yield $this->collectAsync($endpoint, $followable_page_count - 1, $next_params)); |
|
51 | 2 | yield CoInterface::RETURN_WITH => $children !== false ? array_merge($formatted, $children) : false; |
|
52 | // @codeCoverageIgnoreStart |
||
53 | } |
||
54 | // @codeCoverageIgnoreEnd |
||
55 | |||
56 | 9 | protected static function getFormattedResultAndNextParams($result, array $params) |
|
90 | } |
||
91 |