PHPackages                             simensen/sequence - 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. simensen/sequence

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

simensen/sequence
=================

Sequences

v0.0.1(10mo ago)01.2kMITPHP

Since Jul 13Pushed 10mo ago1 watchersCompare

[ Source](https://github.com/simensen/simensen-sequence)[ Packagist](https://packagist.org/packages/simensen/sequence)[ RSS](/packages/simensen-sequence/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

Sequence
========

[](#sequence)

Sequence is an opinionated model for generating sequences of unique integers.

Description
-----------

[](#description)

This model was designed to extract and make explicit the requirement that some models need to have auto-incrementing behavior. It originated from a project that required MySQL-style auto-incrementing fields.

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

[](#installation)

Install the project with Composer:

```
composer require simensen/sequence
```

Usage
-----

[](#usage)

### `Sequence`

[](#sequence-1)

#### The `Sequence` Interface

[](#the-sequencet-interface)

The `Sequence` interface represents the state of an incrementing numeric value.

Calling a `Sequence`'s `next(): T` method will return the next value. The core expectations for the next value:

- The next value SHOULD be a number (`int`)
- The next value SHOULD be unique to the sequence

Additional `Sequences` (see below for `Sequences`) may provide additional guarantees.

#### Sequence Configuration

[](#sequence-configuration)

Attributes are provided to facilitate commonly needed Sequence configuration without introducing these concepts through the public interfaces.

##### `Connection`

[](#connection)

Used to configure the connection name used by the sequence.

##### `CurrentValueColumn`

[](#currentvaluecolumn)

Used to configure the column or field name that contains the sequence's current value.

##### `DefaultStartValue`

[](#defaultstartvalue)

Used to configure the start value of a sequence.

##### `Name`

[](#name)

Used to configure the name of the sequence.

##### `NameColumn`

[](#namecolumn)

Used to configure the column or field that contains the sequence's name.

##### `Table`

[](#table)

Used to configure the table or document name that contains the sequence.

#### `NumericSequence`

[](#numericsequence)

The `NumericSequence` abstract class can be used when a native `int` will suffice.

```
use Simensen\Sequence\NumericSequence;

#[Connection('accounting_db')]
#[Table('invoice_number_sequence')]
final readonly class InvoiceNumber extends NumericSequence
{
}
```

#### `CastedSequence`

[](#castedsequence)

The `CastedSequence` abstract class can be used when a model wraps the native `int` with a value object of type `T`. The transformation from `int`to type `T` is facilitated by a `cast(int $next): T` method.

```
use Simensen\Sequence\CastedSequence;

/**
 * @extends CastedSequence
 */
#[Table('part_id_seq')]
final readonly class PartIdSequence extends CastedSequence
{
    protected function cast(int $next): PartId
    {
        return PartId::fromInt($next);
    }
}
```

### `Sequences`

[](#sequences)

The `Sequences` interface provides the bridge between a `Sequence` and its underlying persistence infrastructure.

Calling a `Sequences`' `nextValueForSequence($sequenceClassName): int` method will return the next value for the specified `$sequenceClassName`. The core expectations for the next value:

- The `Sequences` MAY require additional configuration
    - The `Sequences` MAY introduce additional Sequence Configuration attributes
- The `Sequences` SHOULD read and map the Sequence Configuration attributes where possible
    - An in-memory implementation may not care about `Column`, `Connection`, or `Table`
    - A database-backed implementation may care about `Column`, `Connection`, or `Table`
- The next value SHOULD be a number (`int`)
- The next value SHOULD be unique to *at least* the sequence

Additional `Sequences` may provide additional guarantees.

#### `GlobalInMemorySequences`

[](#globalinmemorysequences)

This `Sequences` implementation is useful for testing. It generates unique next values using the same underlying value for every type of `Sequence`.

```
use Simensen\Sequence\Sequences\Adapter\GlobalInMemorySequences;

$sequences = new GlobalInMemorySequences();

$userIdSequence = new UserIdSequence($sequences);
$serviceIdSequence = new ServiceIdSequence($sequences);

$userIdSequence->next(); // 1
$userIdSequence->next(); // 2

$serviceIdSequence->next(); // 3

$userIdSequence->next(); // 4
```

#### `ClassBasedInMemorySequences`

[](#classbasedinmemorysequences)

This `Sequences` implementation is useful for testing. It generates unique next values using a different underlying value for every type of `Sequence`.

```
use Simensen\Sequence\Sequences\Adapter\ClassBasedInMemorySequences;

$sequences = new ClassBasedInMemorySequences();

$userIdSequence = new UserIdSequence($sequences);
$serviceIdSequence = new ServiceIdSequence($sequences);

$userIdSequence->next(); // 1
$userIdSequence->next(); // 2

$serviceIdSequence->next(); // 1

$userIdSequence->next(); // 3
```

#### `CurrentValueManagement`

[](#currentvaluemanagement)

The `CurrentValueManagement` interface exposes additional functionality for `Sequences` to manage aspects of `Sequence`'s current value.

##### `registerPotentialCurrentValueForSequence`

[](#registerpotentialcurrentvalueforsequence)

Useful for backfilling sequence-related data. This method sets the current value to the specified value only if it is a larger value than the existing current value.

This method is called with a `Sequence` class name and a *potential current value*.

If the `Sequence` does not have a current value yet, the default start value is used for the current value.

If the *potential current value* is greater than the current value for the `Sequence`, the *potential current value* becomes the new current value.

Otherwise, there is no change to the current value.

##### `forceSetCurrentValueForSequence`

[](#forcesetcurrentvalueforsequence)

Useful in testing. This method sets the current value to the specified value regardless of the existing current value.

This method is called with a `Sequence` class name and a *value*.

The current value for the `Sequence` is always set to the new value.

License
-------

[](#license)

This project is licensed under the [MIT License](LICENSE). See the `LICENSE`file for more details on terms and conditions.

Feel free to use and contribute to the project under these terms!

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance54

Moderate activity, may be stable

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity26

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

310d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0d6029bd16a0e8a9b97358a75b54affa6baadc1d9b0ca7eab2b6dd6c681ad7a8?d=identicon)[simensen](/maintainers/simensen)

---

Top Contributors

[![simensen](https://avatars.githubusercontent.com/u/191200?v=4)](https://github.com/simensen "simensen (15 commits)")

### Embed Badge

![Health badge](/badges/simensen-sequence/health.svg)

```
[![Health](https://phpackages.com/badges/simensen-sequence/health.svg)](https://phpackages.com/packages/simensen-sequence)
```

PHPackages © 2026

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