pimbay-php/sequence-contracts
最新稳定版本:v1.0.0
Composer 安装命令:
composer require pimbay-php/sequence-contracts
包简介
Contracts, DTOs and exceptions for the PimBay Sequence Stack. Shared foundation for number and random sequence implementations.
README 文档
README
The pimbay-php/sequence-contracts package provides the foundational interfaces, Data Transfer Objects (DTOs), and exceptions for the entire PimBay Sequence Stack.
It defines the core contracts for atomic sequence generation and ensures deterministic testing through PSR-20 clock abstraction.
This package is designed for maximum modularity, acting as the bedrock for all other components in the Sequence Stack.
Key Features
- Atomic Consistency: Focused on preventing duplicates in high-concurrency environments.
- PSR-20 Clock: Uses
psr/clockas the only dependency — compatible with any PSR-20 clock implementation (Symfony, Laravel, or custom). - Testing Friendly: Includes an
InMemoryNumberSequencefor rapid unit testing. - Global Standard: Built for PHP 8.3+ with strict typing and PSR-4 compliance.
- PHPStan Ready: Fully type-hinted for maximum static analysis compatibility (Level Max).
Core Features
NumberSequenceInterface(PimBay\SequenceContracts\Number\NumberSequenceInterface): Defines the contract for generating and retrieving number sequences, ensuring atomic integrity.nextNumber(string $group, string $name, int $initialValue = 1, array $metadata = []): int: Performs an atomic increment and returns the new value.getCurrent(string $group, string $name): SequenceResult: Retrieves the last generated value and its metadata without incrementing. ThrowsSequenceNotFoundExceptionif the sequence does not exist.
SequenceResultDTO: Afinal readonlyDTO that encapsulates the state of a sequence, including its last value, metadata, creation, and update timestamps.Psr\Clock\ClockInterface(PSR-20): Used directly as the clock abstraction — no custom wrapper. Any PSR-20 compliant clock implementation can be injected, includingMockClockprovided by this package.InMemoryNumberSequence: A simple, array-based implementation ofNumberSequenceInterfaceincluded for easy testing and mocking in consumer projects.MockClock: A test support class that implementsClockInterface, allowing developers to "freeze" or advance time in tests.
Installation
You can install the package via Composer:
composer require pimbay-php/sequence-contracts
Usage
NumberSequenceInterface
This interface defines how to interact with any number sequence implementation:
<?php
declare(strict_types=1);
namespace App;
use PimBay\SequenceContracts\Number\NumberSequenceInterface;
use PimBay\SequenceContracts\Dto\SequenceResult;
use PimBay\SequenceContracts\Exception\SequenceNotFoundException;
class MyService
{
public function __construct(private readonly NumberSequenceInterface $numberSequence)
{
}
public function generateOrderNumber(string $platform): string
{
$group = 'order';
$name = date('Y');
$initialValue = 1000;
$metadata = ['platform' => $platform];
$nextNumber = $this->numberSequence->nextNumber($group, $name, $initialValue, $metadata);
return "ORD-{$nextNumber}";
}
public function getCurrentInvoiceNumber(): ?SequenceResult
{
try {
return $this->numberSequence->getCurrent('invoice', date('Y'));
} catch (SequenceNotFoundException $e) {
return null; // Invoice sequence not yet created
}
}
}
MockClock — testing with controlled time
MockClock implements Psr\Clock\ClockInterface and allows freezing or advancing time in tests:
<?php
declare(strict_types=1);
namespace App\Tests;
use PHPUnit\Framework\TestCase;
use PimBay\SequenceContracts\InMemory\InMemoryNumberSequence;
use PimBay\SequenceContracts\Test\Support\MockClock;
final class MyNumberSequenceTest extends TestCase
{
public function testSequenceCreationTimestamp(): void
{
$mockClock = new MockClock(new \DateTimeImmutable('2026-04-25 10:00:00'));
$sequence = new InMemoryNumberSequence($mockClock);
$result = $sequence->nextNumber('test', 'first');
self::assertSame(1, $result);
self::assertEquals($mockClock->now(), $sequence->getCurrent('test', 'first')->createdAt);
// Advance time and verify updatedAt changes
$mockClock->advance('+1 hour');
$sequence->nextNumber('test', 'first');
self::assertNotEquals(
$sequence->getCurrent('test', 'first')->createdAt,
$sequence->getCurrent('test', 'first')->updatedAt,
);
}
}
Test Matrix
This package is rigorously tested across multiple PHP versions to ensure compatibility and reliability.
| PHP Version |
|---|
| 8.3 |
| 8.4 |
| 8.5 |
Contribution
Contributions are welcome. Please ensure that your code adheres to the existing coding standards and all tests pass. Open an issue or submit a pull request for any new features or bug fixes.
License
Public domain — Unlicense
Created by Jan Sarmir · No conditions · No copyright
pimbay-php/sequence-contracts 适用场景与选型建议
pimbay-php/sequence-contracts 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 05 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「interface」 「sequence」 「dto」 「contracts」 「number-sequence」 「random-sequence」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 pimbay-php/sequence-contracts 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pimbay-php/sequence-contracts 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 pimbay-php/sequence-contracts 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP library for the MUMSYS project
A simple library that allows transform any kind of data to native php data or whatever
Data transfer objects
Convert doctrine dbal query result to dto
Git manager and platform provider for Laravel — facades, typed DTOs, test fakes, and scoped repositories.
Reusable utilities library for Lacus Solutions' packages (type description, HTML escaping, random sequences)
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 39
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Unlicense
- 更新时间: 2026-05-11