hutulia/pagination

使用 PHP 实现抽象分页

2.2.3 2022-10-10 19:06 UTC

This package is auto-updated.

Last update: 2024-09-10 22:57:03 UTC


README

可视化

目录

可视化

开始使用

实现细节

安装

用法

参考

开始使用

使用 PHP 实现抽象分页。

想法是有一个类(分页),它只负责计算分页数据。这些数据是抽象的,因此可以与任何类型的项一起使用。

开始使用示例
我们有
['a', 'b', 'c', 'd', 'e']

想象一下,我们需要向用户显示它们,但我们一次最多只能显示 3 个。因此,我们使用分页来确定何时显示哪些元素。

我们将生成(每页显示 3 个项目)
Page: 1
a
b
c

Page: 2
d
e
我们将有像这样的对象(伪代码)
//page 1

pagination {
  total              : 5
  perPage            : 3
  totalPages         : 2
  currentPage        : 1
  isStartPage        : true
  isEndPage          : false
  totalOnCurrentPage : 3
  start              : 1
  end                : 3
}
//page 2

pagination {
  total              : 5
  perPage            : 3
  totalPages         : 2
  currentPage        : 2
  isStartPage        : false
  isEndPage          : true
  totalOnCurrentPage : 2
  start              : 4
  end                : 5
}

查看 完整的开始使用示例,其中包含实际可运行的代码。

实现细节

  • 分页可以没有项
  • 分页始终至少有 1 页(即使没有项)
  • 项编号从 1 开始
  • 分页对象的字段存储有关所有设置和当前页的数据。 查看分页属性参考
  • 如果无法构建分页 - 将抛出异常。例如,如果我们尝试使用大于最大可用页码的 currentPage

安装

composer require hutulia/pagination

用法

示例 1:仅通过编程方式使用分页

<?php

use Hutulia\Pagination\Pagination;

require_once 'vendor/autoload.php';

$totalItems  = 11;
$perPage     = 3;
$currentPage = 2;
$pagination  = new Pagination($totalItems, $perPage, $currentPage);

echo $pagination->getStart();
// 4

echo $pagination->getEnd();
// 6

echo $pagination->getTotal();
// 11

echo $pagination->getCurrentPage();
// 2

echo $pagination->getTotalPages();
// 4

示例 2:渲染

<?php

use Hutulia\Pagination\Pagination;
use Hutulia\Pagination\SimpleRenderer;

require_once 'vendor/autoload.php';

$totalItems  = 11;
$perPage     = 3;
$currentPage = 2;
$pagination  = new Pagination($totalItems, $perPage, $currentPage);
$renderer    = new SimpleRenderer($pagination);
$template    = 'Showing {START} - {END} of {TOTAL}. Page {CURRENT_PAGE} of {TOTAL_PAGES}';

echo $renderer->render($template);
// Showing 4 - 6 of 11. Page 2 of 4

//Or without SimpleRenderer - will do exactly same inside

echo $pagination->render($template);
// Showing 4 - 6 of 11. Page 2 of 4

示例 3:导出到普通对象

<?php

use Hutulia\Pagination\Pagination;
use Hutulia\Pagination\ExporterToPlainObject;

require_once 'vendor/autoload.php';

$total                 = 5;
$perPage               = 3;
$currentPage           = 1;
$pagination            = new Pagination($total, $perPage, $currentPage);
$exporterToPlainObject = new ExporterToPlainObject($pagination);

var_dump($exporterToPlainObject->export());

//Or without Exporter - will produce the same output

//var_dump($pagination->toPlainObject());

/*
object(stdClass)#4 (9) {
  ["total"]=>
  int(5)
  ["perPage"]=>
  int(3)
  ["totalPages"]=>
  int(2)
  ["currentPage"]=>
  int(1)
  ["isStartPage"]=>
  bool(true)
  ["isEndPage"]=>
  bool(false)
  ["totalOnCurrentPage"]=>
  int(3)
  ["start"]=>
  int(1)
  ["end"]=>
  int(3)
}
*/

示例 4:完整的入门示例

这是入门示例的完整示例。

<?php

use Hutulia\Pagination\Pagination;

require_once 'vendor/autoload.php';

$items       = ['a', 'b', 'c', 'd', 'e'];
$total       = count($items);
$perPage     = 3;
$currentPage = 1;
$pagination  = new Pagination($total, $perPage, $currentPage);

//var_dump($pagination);

/*
Output is simplified:

object {
  total              : 5
  perPage            : 3
  totalPages         : 2
  currentPage        : 1
  isStartPage        : true
  isEndPage          : false
  totalOnCurrentPage : 3
  start              : 1
  end                : 3
}
*/

// Now $pagination has all needed information and we can display page 1 some like this:

echo "Page: {$pagination->getCurrentPage()}".PHP_EOL;

$i = $pagination->getStart();

while($i <= $pagination->getEnd()){
    $index = $i-1;

    echo $items[$index].PHP_EOL;

    $i++;
}

echo PHP_EOL;

// For now, we will have such output:
/*
Page: 1
a
b
c

*/

// Now we go to next page

$currentPage = 2;
$pagination  = new Pagination($total, $perPage, $currentPage);

echo "Page: {$pagination->getCurrentPage()}".PHP_EOL;

$i = $pagination->getStart();

while($i <= $pagination->getEnd()){
    $index = $i-1;

    echo $items[$index].PHP_EOL;

    $i++;
}

//For now, we will have total output:
/*
Page: 1
a
b
c

Page: 2
d
e

*/

参考

Hutulia\Pagination\Pagination

属性

API(公共方法)

Used during construct but can be used after (they do not change the object)

  • calcTotalPages()
  • calcTotalOnCurrentPage()
  • calcIsStartPage()
  • calcIsEndPage()
  • calcStart()
  • calcEnd()

Hutulia\Pagination\SimpleRenderer

API

render(string $template): string

It uses a template (plain string) and vars. To use var just wrap it with curly braces. Example: some text {START} some other text {TOTAL} ... .

Available vars

Hutulia\Pagination\ExporterToPlainObject

API

export(): stdClass

License

MIT