2019-10-11 02:35:09 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Shlinkio\Shlink\Core;
|
|
|
|
|
2020-01-28 02:41:48 -06:00
|
|
|
use Cake\Chronos\Chronos;
|
|
|
|
use DateTimeInterface;
|
2019-10-11 02:35:09 -05:00
|
|
|
use PUGX\Shortid\Factory as ShortIdFactory;
|
|
|
|
|
2020-01-29 03:53:06 -06:00
|
|
|
use function sprintf;
|
|
|
|
|
2020-02-18 11:54:40 -06:00
|
|
|
const DEFAULT_SHORT_CODES_LENGTH = 5;
|
2020-02-18 12:34:01 -06:00
|
|
|
const MIN_SHORT_CODES_LENGTH = 4;
|
2020-03-22 11:42:56 -05:00
|
|
|
const LOCAL_LOCK_FACTORY = 'Shlinkio\Shlink\LocalLockFactory';
|
2020-02-18 11:54:40 -06:00
|
|
|
|
|
|
|
function generateRandomShortCode(int $length): string
|
2019-10-11 02:35:09 -05:00
|
|
|
{
|
|
|
|
static $shortIdFactory;
|
|
|
|
if ($shortIdFactory === null) {
|
|
|
|
$shortIdFactory = new ShortIdFactory();
|
|
|
|
}
|
|
|
|
|
|
|
|
$alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
|
|
return $shortIdFactory->generate($length, $alphabet)->serialize();
|
|
|
|
}
|
2020-01-28 02:41:48 -06:00
|
|
|
|
|
|
|
function parseDateFromQuery(array $query, string $dateName): ?Chronos
|
|
|
|
{
|
|
|
|
return ! isset($query[$dateName]) || empty($query[$dateName]) ? null : Chronos::parse($query[$dateName]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string|DateTimeInterface|Chronos|null $date
|
|
|
|
*/
|
|
|
|
function parseDateField($date): ?Chronos
|
|
|
|
{
|
|
|
|
if ($date === null || $date instanceof Chronos) {
|
|
|
|
return $date;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($date instanceof DateTimeInterface) {
|
|
|
|
return Chronos::instance($date);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Chronos::parse($date);
|
|
|
|
}
|
2020-01-29 03:53:06 -06:00
|
|
|
|
|
|
|
function determineTableName(string $tableName, array $emConfig = []): string
|
|
|
|
{
|
|
|
|
$schema = $emConfig['connection']['schema'] ?? null;
|
|
|
|
// $tablePrefix = $emConfig['connection']['table_prefix'] ?? null; // TODO
|
|
|
|
|
|
|
|
if ($schema === null) {
|
|
|
|
return $tableName;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf('%s.%s', $schema, $tableName);
|
|
|
|
}
|