1. Go to this page and download the library: Download chrismou/stringmint 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/ */
chrismou / stringmint example snippets
use Chrismou\StringMint\Counter\CounterTableInstaller;
(new CounterTableInstaller($pdo))->install();
// Or output the SQL to paste into your own migration tool:
// $sql = (new CounterTableInstaller($pdo))->installSql();
use Chrismou\StringMint\Generator\PermutationGenerator;
$generator = PermutationGenerator::create(
$pdo,
$_ENV['STRINGMINT_SECRET'], // any secret key, store this as changing it will break the sequence
length: 6,
);
$slug = $generator->generate();
// With length escalation and a named counter:
$generator = PermutationGenerator::create(
$pdo,
$_ENV['STRINGMINT_SECRET'],
length: 4,
maximumLength: 8,
counterName: 'links',
);
$slug = $generator->generateWithAutoLengthIncrement();
use Chrismou\StringMint\Counter\PdoCounterStore;
use Chrismou\StringMint\Existence\PdoColumnExistenceChecker;
use Chrismou\StringMint\Generator\PermutationGenerator;
use Chrismou\StringMint\LengthPolicy;
use Chrismou\StringMint\Permutation\FeistelPermutation;
$generator = new PermutationGenerator(
new LengthPolicy(defaultLength: 6),
new PdoCounterStore($pdo, counterName: 'links'),
new FeistelPermutation($_ENV['STRINGMINT_SECRET']),
// Optional: custom alphabet, existence checker, maximumAttempts
);
use Chrismou\StringMint\Existence\InMemoryExistenceChecker;
use Chrismou\StringMint\Generator\RandomGenerator;
use Chrismou\StringMint\LengthPolicy;
$checker = new InMemoryExistenceChecker(); // or PdoColumnExistenceChecker
$generator = new RandomGenerator(
new LengthPolicy(6),
$checker,
);
$slug = $generator->generate();
$checker->remember($slug);
new LengthPolicy(defaultLength: 6) // fixed default, no ceiling
new LengthPolicy(defaultLength: 4, maximumLength: 8) // default 4, ceiling 8
$generator->generate(2); // always length 2, exception if no unique strings left
$generator->generateWithAutoLengthIncrement(4); // start at 4, increase length as
use Chrismou\StringMint\Alphabet\UrlSafe;
use Chrismou\StringMint\Counter\PdoCounterStore;
use Chrismou\StringMint\Existence\PdoColumnExistenceChecker;
use Chrismou\StringMint\Generator\PermutationGenerator;
use Chrismou\StringMint\LengthPolicy;
use Chrismou\StringMint\Permutation\FeistelPermutation;
use Chrismou\StringMint\UniqueStringGeneratorInterface;
$this->app->singleton(UniqueStringGeneratorInterface::class, function ($app) {
return new PermutationGenerator(
new LengthPolicy(6),
new PdoCounterStore($app['db']->getPdo(), 'links'),
new FeistelPermutation(config('app.stringmint_secret')),
new UrlSafe(),
new PdoColumnExistenceChecker($app['db']->getPdo(), 'links', 'slug'),
);
});
use Chrismou\StringMint\Alphabet\Generic;
$generator = PermutationGenerator::create($pdo, $_ENV['STRINGMINT_SECRET'], length: 8, alphabet: new Generic('0123456789abcdef'));
use Chrismou\StringMint\Alphabet\AbstractAlphabet;
final class Hex extends AbstractAlphabet
{
public function __construct()
{
parent::__construct('0123456789abcdef');
}
}
$generator = PermutationGenerator::create($pdo, $_ENV['STRINGMINT_SECRET'], length: 8, alphabet: new Hex());
public function isReserved(string $candidate): bool
{
return str_starts_with($candidate, '-');
}
use Chrismou\StringMint\Alphabet\AbstractUrlSafeAlphabet;
final class Slug extends AbstractUrlSafeAlphabet
{
public function __construct()
{
parent::__construct(self::RFC3986_UNRESERVED);
}
public function isReserved(string $candidate): bool
{
return parent::isReserved($candidate) || str_starts_with($candidate, '-');
}
}
use Chrismou\StringMint\Alphabet\UrlSafe;
use Chrismou\StringMint\Counter\InMemoryCounterStore;
use Chrismou\StringMint\Existence\InMemoryExistenceChecker;
use Chrismou\StringMint\Generator\RandomGenerator;
// Seed a counter at a specific length to test exhaustion boundaries.
$store = new InMemoryCounterStore([3 => 64]); // length 3 starts at index 64 (capacity = 64 for 4-char alphabet)
// Pre-populate the existence checker for collision tests.
$checker = new InMemoryExistenceChecker(['abc', 'def']);
// Seed a Randomizer for deterministic RandomGenerator tests.
$randomizer = new \Random\Randomizer(new \Random\Engine\Mt19937(42));
$generator = new RandomGenerator($policy, $checker, new UrlSafe(), 10, $randomizer);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.