Pantheon\Terminus\Request\Request::pagedRequest PHP Method

pagedRequest() public method

Make a request to the Dashbord's internal API
public pagedRequest ( string $path, array $options = [] ) : array
$path string API path (URL)
$options array Options for the request [string] method GET is default [mixed] data Native PHP data structure (e.g. int, string array, or simple object) to be sent along with the request. Will be encoded as JSON for you.
return array
    public function pagedRequest($path, array $options = [])
    {
        $limit = 100;
        if (isset($options['limit'])) {
            $limit = $options['limit'];
        }
        //$results is an associative array so we don't refetch
        $results = [];
        $finished = false;
        $start = null;
        while (!$finished) {
            $paged_path = $path . '?limit=' . $limit;
            if ($start) {
                $paged_path .= '&start=' . $start;
            }
            $resp = $this->request($paged_path);
            $data = $resp['data'];
            if (count($data) > 0) {
                if (count($data) < $limit) {
                    $finished = true;
                }
                $start = end($data)->id;
                //If the last item of the results has previously been received,
                //that means there are no more pages to fetch
                if (isset($results[$start])) {
                    $finished = true;
                    continue;
                }
                foreach ($data as $item) {
                    $results[$item->id] = $item;
                }
            } else {
                $finished = true;
            }
        }
        $return = ['data' => array_values($results)];
        return $return;
    }