Toda End Point de una API Rest, debe ofrecer posibilidad de paginación para interar con coleciones amplias, usando los los parámetros limit y page se puede controlar la páginación, el parametro limit para especificar el total de datos a mostrar y page la página.
Pagination QueryString
public static function makePageOffset(&$limit, &$page = null) {
if (!isset($limit)) $limit = _LIMIT_MAX_;
if (!isset($page)) $page = _PAGE_MIN_;
if (!is_numeric($limit)) {
throw new InvalidArgumentException('limit param only accepts integers ' . _LIMIT_MIN_ . ' to ' . _LIMIT_MAX_);
}
if (!is_numeric($page)) {
throw new InvalidArgumentException('page param only accepts integers ' . _PAGE_MIN_ . ' to ' . _PAGE_MAX_);
}
if ($limit < 1 || $limit > _LIMIT_MAX_) {
throw new InvalidArgumentException('limit param only accepts integers ' . _LIMIT_MIN_ . ' to ' . _LIMIT_MAX_);
}
if ($page < 0 || $page > _PAGE_MAX_) {
throw new InvalidArgumentException('page param only accepts integers ' . _PAGE_MIN_ . ' to ' . _PAGE_MAX_);
}
return [$limit, $page];
}
Su uso
try {
$offset = QueryStringHelper::makePageOffset($arrparams['limit'], $arrparams['page']);
} catch( \Exception $e ) {
return $response ->withStatus(422)->write("error: " . $e->getMessage());
}
...
[$limit, $page] = $offset;
$stmt->limit(new Limit($limit, $page));