Links.php (1497B)
1 <?php 2 3 namespace App\Helpers; 4 5 use Mastodon; 6 use GuzzleHttp\Psr7; 7 8 /** 9 * Container class to hold pagination links. 10 */ 11 class Links 12 { 13 public $next; 14 public $prev; 15 16 /** 17 * Generate pagination links for the given route. 18 * 19 * After receiving a paginated response from the Mastodon API, 20 * parse the "link" header from the Mastodon response and use 21 * it to generate next and previous links for the requested route. 22 * 23 * @param string[] $links_header An array of values of the "link" header, 24 * as might be returned from Guzzle's getHeader() method. 25 * @param string $route The name of the route to generate links for. 26 */ 27 function __construct(array $links_header, string $route) 28 { 29 foreach (Psr7\parse_header($links_header) as $link) 30 { 31 # Find the prev and next links. 32 if ($link['rel'] === 'prev' || $link['rel'] === 'next') 33 { 34 $url = parse_url(trim($link[0], '<>')); 35 foreach (explode('&', $url['query']) as $query_param) 36 { 37 # Grab the ID query parameters from the link. 38 if (strpos($query_param, 'max_id=') === 0 39 || strpos($query_param, 'since_id=') === 0) 40 { 41 # Construct new links with the correct offset. 42 $this->{$link['rel']} = route($route) . '?' . $query_param; 43 } 44 } 45 } 46 } 47 } 48 }