PHP code example of sbooker / persistent-sequences
1. Go to this page and download the library: Download sbooker/persistent-sequences 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/ */
sbooker / persistent-sequences example snippets
// Генерируем следующий уникальный ID для нового пользователя
$nextUserId = (int)$sequenceGenerator->next('users_pk', new Algorithm\Increment(1));
$user = new User($nextUserId, /* ... */);
$transactionManager->persist($user);
// Получаем следующий номер для счета
$nextInvoiceNumber = $sequenceGenerator->next('invoices', new InvoiceAlgorithm());
// $nextInvoiceNumber будет "INV-2023-0001"
// bootstrap.php или ваш DI-контейнер
/** @var Sbooker\TransactionManager\TransactionManager $transactionManager */
$sequenceGenerator = new Sbooker\PersistentSequences\SequenceGenerator($transactionManager);
// src/Infrastructure/Persistence/DoctrineSequenceReadStorage.php
use Doctrine\ORM\EntityManagerInterface;
use Sbooker\PersistentSequences\Sequence;
use Sbooker\PersistentSequences\SequenceReadStorage;
final class DoctrineSequenceReadStorage implements SequenceReadStorage { /* ... */ }
// bootstrap.php или ваш DI-контейнер
/** @var DoctrineSequenceReadStorage $readStorage */
$sequenceReader = new Sbooker\PersistentSequences\SequenceReader($readStorage);
// В вашем коде:
$lastUserId = $sequenceReader->last('users_pk');
// src/Sequences/InvoiceAlgorithm.php
use Sbooker\PersistentSequences\Algorithm;
final class InvoiceAlgorithm implements Algorithm
{
public function first(): string
{
return "INV-2023-0000";
}
public function next(string $currentValue): string
{
// Простая (но не идеальная) реализация для примера
[$prefix, $year, $number] = explode('-', $currentValue);
$nextNumber = str_pad((string)((int)$number + 1), 4, '0', STR_PAD_LEFT);
return "$prefix-$year-$nextNumber";
}
}
// Использование:
$nextInvoiceNumber = $sequenceGenerator->next('invoices', new InvoiceAlgorithm());
// вернет "INV-2023-0001"
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.