PHPackages                             somework/offset-page-logic - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. somework/offset-page-logic

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

somework/offset-page-logic
==========================

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

2.0.0(5mo ago)05.9k↓45%11MITPHPPHP ^8.2CI passing

Since Jun 4Pushed 5mo ago2 watchersCompare

[ Source](https://github.com/somework/offset-page-logic)[ Packagist](https://packagist.org/packages/somework/offset-page-logic)[ Docs](https://github.com/somework/offset-page-logic)[ RSS](/packages/somework-offset-page-logic/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (3)Versions (14)Used By (1)

Offset Page Logic
=================

[](#offset-page-logic)

Author: [Igor Pinchuk](https://github.com/somework "GitHub") Email:

[![CI](https://camo.githubusercontent.com/672c08e6159dc419dd5a162a0f81580d45afd08faf31b5317a2704cf17de29ef/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f736f6d65776f726b2f6f66667365742d706167652d6c6f6769632f63692e796d6c3f6272616e63683d6d6173746572266c6162656c3d4349)](https://github.com/somework/offset-page-logic/actions/workflows/ci.yml?query=branch%3Amaster)[![Latest Stable Version](https://camo.githubusercontent.com/cedcb6955b705e27652922aceeb8672cef32d0107cb1f384195cb23c3af26e95/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736f6d65776f726b2f6f66667365742d706167652d6c6f6769632e737667)](https://packagist.org/packages/somework/offset-page-logic)[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](LICENSE)

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

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require somework/offset-page-logic
```

Usage
-----

[](#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
--------

[](#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.

OffsetLimitnowCountOutcomeNotes`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
-----------

[](#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
-------

[](#license)

MIT

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance71

Regular maintenance activity

Popularity23

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity83

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 93.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~388 days

Recently: every ~776 days

Total

9

Last Release

166d ago

Major Versions

1.0.7 → 2.0.02025-12-04

PHP version history (2 changes)1.0PHP &gt;=5.5

2.0.0PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/19801428?v=4)[Igor Pinchuk](/maintainers/somework)[@somework](https://github.com/somework)

---

Top Contributors

[![somework](https://avatars.githubusercontent.com/u/19801428?v=4)](https://github.com/somework "somework (30 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")

---

Tags

pagepaginationconverterlimitoffset

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/somework-offset-page-logic/health.svg)

```
[![Health](https://phpackages.com/badges/somework-offset-page-logic/health.svg)](https://phpackages.com/packages/somework-offset-page-logic)
```

###  Alternatives

[beberlei/porpaginas

Library that generically solves several pagination issues with DAO/repository abstractions.

163612.6k11](/packages/beberlei-porpaginas)[lampager/lampager-laravel

Rapid pagination for Laravel

7641.4k](/packages/lampager-lampager-laravel)[lampager/lampager

Rapid pagination without using OFFSET

3778.3k5](/packages/lampager-lampager)[paysera/lib-pagination

Paginates Doctrine QueryBuilder using cursor based or offset based pagination

13191.8k1](/packages/paysera-lib-pagination)[usmanhalalit/strana

Pagination library for PHP, framework agnostic, with built-in adapters for Doctrine, Eloquent, Pixie and PHP Array.

947.4k3](/packages/usmanhalalit-strana)[soup/paginator

Paginator is a simple class that allows you to create pagination. It doesn't require any database connection. It is compatible with Twitter's Bootstrap Framework, by using the CSS class pagination that is also attached.

351.5k](/packages/soup-paginator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
