hyperf-chat/app/Traits/PagingTrait.php

46 lines
1.1 KiB
PHP
Raw Normal View History

2020-11-04 22:58:49 +08:00
<?php
2021-08-01 00:45:35 +08:00
declare(strict_types=1);
2020-11-04 22:58:49 +08:00
namespace App\Traits;
/**
* Trait PagingTrait 分页处理
*
* @package App\Traits
*/
trait PagingTrait
{
/**
* 计算分页总数
*
2021-04-20 16:30:57 +08:00
* @param int $total 总记录数
2020-11-04 22:58:49 +08:00
* @param int $page_size 分页大小
* @return int 分页总数
*/
2021-08-10 21:35:41 +08:00
protected function getPagingTotal(int $total, int $page_size): int
2020-11-04 22:58:49 +08:00
{
2021-08-10 21:35:41 +08:00
return ($total === 0) ? 0 : (int)ceil($total / $page_size);
2020-11-04 22:58:49 +08:00
}
/**
* 获取分页数据
*
2021-04-20 16:30:57 +08:00
* @param array $rows 列表数据
* @param int $total 数据总记录数
* @param int $page 当前分页
* @param int $page_size 分页大小
* @param array $params 额外参数
2020-11-04 22:58:49 +08:00
* @return array
*/
2021-08-10 21:35:41 +08:00
protected function getPagingRows(array $rows, int $total, int $page, int $page_size, array $params = []): array
2020-11-04 22:58:49 +08:00
{
return array_merge([
2021-04-20 16:30:57 +08:00
'rows' => $rows,
'page' => $page,
'page_size' => $page_size,
2020-11-04 22:58:49 +08:00
'page_total' => ($page_size == 0) ? 1 : $this->getPagingTotal($total, $page_size),
2021-04-20 16:30:57 +08:00
'total' => $total,
2020-11-04 22:58:49 +08:00
], $params);
}
}