PHP code example of kumwe / sequence

1. Go to this page and download the library: Download kumwe/sequence library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kumwe / sequence example snippets




declare(strict_types=1);

use DateTimeImmutable;
use Kumwe\Sequence\Contract\NumberSequenceAllocator;
use Kumwe\Sequence\Exception\NumberSequenceUnavailable;
use Kumwe\Sequence\Value\NumberSequenceFormat;

final readonly class IssueInvoice
{
    public function __construct(private NumberSequenceAllocator $numbers)
    {
    }

    public function issue(DateTimeImmutable $now, ?string $organization): string
    {
        $format = NumberSequenceFormat::fromConfiguration([
            'reset' => 'yearly',
            'prefix' => 'INV-',
            'padding' => 6,
            'timezone' => 'Africa/Windhoek',
        ]);
        $counter = $format->counter($organization, $now);
        try {
            $value = $this->numbers->allocate(
                'default',                 // site: the legal entity
                'invoice',                 // definition: the document type
                'invoice_number',          // field handle
                $counter['scope'],         // '-' for a site-wide run
                $counter['period'],        // '2026', judged in Windhoek
                $now,
            );
        } catch (NumberSequenceUnavailable $refusal) {
            throw $refusal;                // nothing was reserved: replay the whole command, never guess
        }

        return $format->render($value, $counter['period']);   // INV-2026-000001
    }
}

$container->share(NumberSequenceAllocator::class, static fn (Container $c): NumberSequenceAllocator =>
    new DoctrineBusinessNumberSequenceAllocator($c->get(Connection::class), $c->get(TableNames::class)));
bash
composer install
composer check                          # the complete lane, ending in the built-archive clean-consumer gate
php tests/run.php                       # the dependency-free suite alone; no composer install needed
php examples/allocate-and-render.php    # the shipped example; no composer install needed