somework / offset-page-logic
Utility functions to convert offset/limit requests into page/page-size arguments
Installs: 5 533
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 1
Open Issues: 0
pkg:composer/somework/offset-page-logic
Requires
- php: ^8.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.91
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.5
- dev-master
- 2.0.0
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0
- dev-feature/fix_style_issue
- dev-analysis-BAwVLA
- dev-codex/update-changelog.md-for-version-2.0.0
- dev-codex/update-phpstan-configuration-and-fix-issues
- dev-codex/revise-behavior-section-in-readme.md
- dev-codex/expand-readme-with-behavior-section
- dev-codex/add-changelog.md-for-release-summaries
- dev-codex/extend-phpunit-data-providers-for-offsets
- dev-codex/document-composer-installation-and-usage
- dev-codex/add-phpunit-tests-for-offset-and-limit
- dev-codex/refactor-ci.yml-for-separate-test-and-quality-jobs
- dev-codex/join-multiline-string-in-exception-message
- dev-analysis-k2Jg2A
- dev-codex/raise-phpstan-level-and-adjust-code
- dev-codex/add-strict-types-and-type-hints-to-tests
- dev-codex/delete-.travis.yml-and-update-documentation
- dev-codex/update-.php-cs-fixer.php-for-psr12
- dev-codex/replace-multiline-string-in-logic
- dev-dependabot/composer/phpstan/phpstan-tw-2.1
- dev-dependabot/github_actions/actions/checkout-6
- dev-codex/add-dependabot-and-minimal-configs
- dev-codex/create-ci-workflow-for-php-8.2/8.3
- dev-codex/update-phpunit.xml.dist-for-phpunit-10
- dev-codex/add-strict-types-and-typed-properties
- dev-codex/add-strict-types-and-type-hints-to-logic
- dev-codex/add-strict-types-and-type-hints-to-logic-6b9g1s
- dev-codex/update-composer.json-for-php-8.2-and-tools
- dev-release/1.0.x
- dev-analysis-zOAr20
This package is auto-updated.
Last update: 2025-12-04 10:47:55 UTC
README
Author: Igor Pinchuk Email: i.pinchuk.work@gmail.com
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, andnowCountare all0, the method returns page0and size0, representing a request for “everything” without pagination. - Limit-only – with
offsetat0, a positivelimitsets the page size while the page is1. - Offset-only – with
limitat0, a positiveoffsetyields page2and a size ofoffset + nowCount(the offset is always at least the page size). - Limit exceeds current count – when
nowCountis positive and smaller than the requestedlimit, the method recurses by subtractingnowCountfromlimitand adding it tooffset, then resolves the pagination with the remaining values. - Standard offset/limit division – when both are positive and
nowCountis0, the page and size are derived fromoffsetdivided bylimit, using the largest divisor of the offset to maximize page size. AlreadyGetNeededCountExceptioncondition – ifnowCountis positive and not less than the requestedlimit, the method throwsAlreadyGetNeededCountExceptionto 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