定制 somework/offset-page-logic 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

somework/offset-page-logic

最新稳定版本:2.0.0

Composer 安装命令:

composer require somework/offset-page-logic

包简介

Utility functions to convert offset/limit requests into page/page-size arguments

README 文档

README

Author: Igor Pinchuk Email: i.pinchuk.work@gmail.com

CI Latest Stable Version License

Utility functions to convert offset/limit requests into page/page-size arguments.

Installation

Install the package via Composer:

composer require somework/offset-page-logic

Usage

Offset::logic() returns a DTO containing the calculated page (1-based) and page size for the given offset and limit. The method also guards against requesting more rows than are available. All inputs are strict integers; negative values are coerced to 0.

Behavior

Offset::logic() branches through several scenarios to normalize offset/limit inputs into page-based pagination:

  • Zeroed inputs – if offset, limit, and nowCount are all 0, the method returns page 0 and size 0, representing a request for “everything” without pagination.
  • Limit-only – with offset at 0, a positive limit sets the page size while the page is 1.
  • Offset-only – with limit at 0, a positive offset yields page 2 and a size of offset + nowCount (the offset is always at least the page size).
  • Limit exceeds current count – when nowCount is positive and smaller than the requested limit, the method recurses by subtracting nowCount from limit and adding it to offset, then resolves the pagination with the remaining values.
  • Standard offset/limit division – when both are positive and nowCount is 0, the page and size are derived from offset divided by limit, using the largest divisor of the offset to maximize page size.
  • AlreadyGetNeededCountException condition – if nowCount is positive and not less than the requested limit, the method throws AlreadyGetNeededCountException to signal that all required rows are already retrieved.
Offset Limit nowCount Outcome Notes
0 0 0 Page 0, Size 0 Zeroed inputs return a sentinel “all rows” response.
0 10 0 Page 1, Size 10 Limit-only scenario with a page starting at 1.
22 0 0 Page 2, Size 22 Offset-only scenario; size grows with the offset.
0 22 10 Page 2, Size 10 Limit exceeds nowCount; recursion reduces the limit.
44 22 0 Page 3, Size 22 Standard offset/limit division (44/22 + 1).
0 5 5 Throws AlreadyGetNeededCountException Requested limit is already satisfied by nowCount.
use SomeWork\OffsetPage\Logic\Offset;

$offset = 0;  // start from the first record
$limit = 10;  // request ten records
$nowCount = 0; // no rows have been processed yet

$result = Offset::logic($offset, $limit, $nowCount);

$result->getPage(); // 1 (first page)
$result->getSize(); // 10 (page size derived from limit)

If the requested limit is already satisfied by nowCount, an exception is thrown:

use SomeWork\OffsetPage\Logic\AlreadyGetNeededCountException;
use SomeWork\OffsetPage\Logic\Offset;

$offset = 0;
$limit = 5;
$nowCount = 5;

$result = Offset::logic($offset, $limit, $nowCount); // throws AlreadyGetNeededCountException

Development

Run the automated checks locally using Composer scripts:

composer install
composer test       # PHPUnit test suite
composer stan       # PHPStan static analysis
composer cs-check   # PHP CS Fixer dry-run for coding standards

License

MIT

统计信息

  • 总下载量: 6.09k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 5
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-06-04