PaginationParameters.php (1247B)
1 <?php 2 3 namespace App\Helpers; 4 5 use Illuminate\Http\Request; 6 7 /** 8 * Parse and store pagination query parameters. 9 */ 10 class PaginationParameters 11 { 12 public $max_id; 13 public $since_id; 14 15 /** 16 * Parse a Request and populate the pagination parameters from it. 17 * 18 * @param Request $request The request received with pagination query params. 19 */ 20 function __construct(Request $request) 21 { 22 if ($request->has('max_id') && $request->has('since_id')) 23 { 24 # This scenario makes no sense. 25 # Someone's trying to dicker with the URL. 26 abort(400); 27 } 28 elseif ($request->has('max_id')) 29 { 30 $this->max_id = $request->max_id; 31 } 32 elseif ($request->has('since_id')) 33 { 34 $this->since_id = $request->since_id; 35 } 36 } 37 38 /** 39 * Figure out which property is set and return it in an array. 40 * 41 * @return string[] The max_id or the since_id in an associative array. 42 */ 43 public function to_array() 44 { 45 if (isset($this->max_id)) 46 { 47 return ['max_id' => $this->max_id]; 48 } 49 else 50 { 51 return ['since_id' => $this->since_id]; 52 } 53 } 54 }