Download the PHP package somework/offset-page-logic without Composer
On this page you can find all versions of the php package somework/offset-page-logic. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download somework/offset-page-logic
More information about somework/offset-page-logic
Files in somework/offset-page-logic
Package offset-page-logic
Short Description Utility functions to convert offset/limit requests into page/page-size arguments
License MIT
Homepage https://github.com/somework/offset-page-logic
Informations about the package offset-page-logic
Offset Page Logic
Author: Igor Pinchuk Email: [email protected]
Utility functions to convert offset/limit requests into page/page-size arguments.
Installation
Install the package via Composer:
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. |
If the requested limit is already satisfied by nowCount, an exception is thrown:
Development
Run the automated checks locally using Composer scripts:
License
MIT
Maintained by
Igor Pinchuk — I work on legacy PHP and Symfony systems: audits, rescue and refactoring, performance. My open-source packages have been installed over 70,000 times on Packagist.
Inherited a codebase nobody wants to touch? Written questions get written answers — no discovery call required. [email protected] · LinkedIn