Moved logic to generate random short codes to external function

This commit is contained in:
Alejandro Celaya
2019-10-11 09:35:09 +02:00
parent 2f09ff456c
commit 8f2e78c946
3 changed files with 24 additions and 10 deletions

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core;
use PUGX\Shortid\Factory as ShortIdFactory;
function generateRandomShortCode(int $length = 5): string
{
static $shortIdFactory;
if ($shortIdFactory === null) {
$shortIdFactory = new ShortIdFactory();
}
$alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
return $shortIdFactory->generate($length, $alphabet)->serialize();
}