2019-10-11 02:35:09 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Shlinkio\Shlink\Core;
|
|
|
|
|
2023-01-03 06:45:39 -06:00
|
|
|
use BackedEnum;
|
2020-01-28 02:41:48 -06:00
|
|
|
use Cake\Chronos\Chronos;
|
2022-12-04 13:35:38 -06:00
|
|
|
use Cake\Chronos\ChronosInterface;
|
2020-01-28 02:41:48 -06:00
|
|
|
use DateTimeInterface;
|
2022-01-10 07:37:44 -06:00
|
|
|
use Doctrine\ORM\Mapping\Builder\FieldBuilder;
|
2021-05-22 08:09:14 -05:00
|
|
|
use Jaybizzle\CrawlerDetect\CrawlerDetect;
|
2022-05-23 14:19:59 -05:00
|
|
|
use Laminas\Filter\Word\CamelCaseToSeparator;
|
2023-01-02 12:28:32 -06:00
|
|
|
use Laminas\Filter\Word\CamelCaseToUnderscore;
|
2020-09-23 12:19:17 -05:00
|
|
|
use Laminas\InputFilter\InputFilter;
|
2019-10-11 02:35:09 -05:00
|
|
|
use PUGX\Shortid\Factory as ShortIdFactory;
|
2021-02-09 15:11:09 -06:00
|
|
|
use Shlinkio\Shlink\Common\Util\DateRange;
|
2023-01-25 13:33:07 -06:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlMode;
|
2019-10-11 02:35:09 -05:00
|
|
|
|
2023-11-29 05:34:13 -06:00
|
|
|
use function array_map;
|
2022-04-15 12:57:27 -05:00
|
|
|
use function date_default_timezone_get;
|
2021-01-10 13:28:52 -06:00
|
|
|
use function Functional\reduce_left;
|
|
|
|
use function is_array;
|
|
|
|
use function print_r;
|
2021-08-04 11:46:19 -05:00
|
|
|
use function Shlinkio\Shlink\Common\buildDateRange;
|
2020-01-29 03:53:06 -06:00
|
|
|
use function sprintf;
|
2021-01-11 08:20:26 -06:00
|
|
|
use function str_repeat;
|
2023-01-02 12:28:32 -06:00
|
|
|
use function strtolower;
|
2022-05-23 14:19:59 -05:00
|
|
|
use function ucfirst;
|
2020-01-29 03:53:06 -06:00
|
|
|
|
2023-01-25 13:33:07 -06:00
|
|
|
function generateRandomShortCode(int $length, ShortUrlMode $mode = ShortUrlMode::STRICT): string
|
2019-10-11 02:35:09 -05:00
|
|
|
{
|
|
|
|
static $shortIdFactory;
|
|
|
|
if ($shortIdFactory === null) {
|
|
|
|
$shortIdFactory = new ShortIdFactory();
|
|
|
|
}
|
|
|
|
|
2023-01-25 13:33:07 -06:00
|
|
|
$alphabet = $mode === ShortUrlMode::STRICT
|
|
|
|
? '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
|
|
: '0123456789abcdefghijklmnopqrstuvwxyz';
|
2019-10-11 02:35:09 -05:00
|
|
|
return $shortIdFactory->generate($length, $alphabet)->serialize();
|
|
|
|
}
|
2020-01-28 02:41:48 -06:00
|
|
|
|
|
|
|
function parseDateFromQuery(array $query, string $dateName): ?Chronos
|
|
|
|
{
|
2022-12-04 13:35:38 -06:00
|
|
|
return normalizeOptionalDate(empty($query[$dateName] ?? null) ? null : Chronos::parse($query[$dateName]));
|
2020-01-28 02:41:48 -06:00
|
|
|
}
|
|
|
|
|
2021-02-09 15:11:09 -06:00
|
|
|
function parseDateRangeFromQuery(array $query, string $startDateName, string $endDateName): DateRange
|
|
|
|
{
|
|
|
|
$startDate = parseDateFromQuery($query, $startDateName);
|
|
|
|
$endDate = parseDateFromQuery($query, $endDateName);
|
|
|
|
|
2021-08-04 11:46:19 -05:00
|
|
|
return buildDateRange($startDate, $endDate);
|
2021-02-09 15:11:09 -06:00
|
|
|
}
|
|
|
|
|
2022-12-04 13:35:38 -06:00
|
|
|
/**
|
|
|
|
* @return ($date is null ? null : Chronos)
|
|
|
|
*/
|
|
|
|
function normalizeOptionalDate(string|DateTimeInterface|ChronosInterface|null $date): ?Chronos
|
2020-01-28 02:41:48 -06:00
|
|
|
{
|
2022-04-15 12:57:27 -05:00
|
|
|
$parsedDate = match (true) {
|
|
|
|
$date === null || $date instanceof Chronos => $date,
|
|
|
|
$date instanceof DateTimeInterface => Chronos::instance($date),
|
|
|
|
default => Chronos::parse($date),
|
|
|
|
};
|
2020-01-29 03:53:06 -06:00
|
|
|
|
2022-04-15 12:57:27 -05:00
|
|
|
return $parsedDate?->setTimezone(date_default_timezone_get());
|
2020-01-29 03:53:06 -06:00
|
|
|
}
|
2020-09-23 12:19:17 -05:00
|
|
|
|
2022-12-04 13:35:38 -06:00
|
|
|
function normalizeDate(string|DateTimeInterface|ChronosInterface $date): Chronos
|
|
|
|
{
|
|
|
|
return normalizeOptionalDate($date);
|
|
|
|
}
|
|
|
|
|
2020-09-23 12:19:17 -05:00
|
|
|
function getOptionalIntFromInputFilter(InputFilter $inputFilter, string $fieldName): ?int
|
|
|
|
{
|
|
|
|
$value = $inputFilter->getValue($fieldName);
|
|
|
|
return $value !== null ? (int) $value : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getOptionalBoolFromInputFilter(InputFilter $inputFilter, string $fieldName): ?bool
|
|
|
|
{
|
|
|
|
$value = $inputFilter->getValue($fieldName);
|
|
|
|
return $value !== null ? (bool) $value : null;
|
|
|
|
}
|
2021-01-10 13:28:52 -06:00
|
|
|
|
2022-12-07 12:06:05 -06:00
|
|
|
function getNonEmptyOptionalValueFromInputFilter(InputFilter $inputFilter, string $fieldName): mixed
|
|
|
|
{
|
|
|
|
$value = $inputFilter->getValue($fieldName);
|
|
|
|
return empty($value) ? null : $value;
|
|
|
|
}
|
|
|
|
|
2021-01-11 08:20:26 -06:00
|
|
|
function arrayToString(array $array, int $indentSize = 4): string
|
2021-01-10 13:28:52 -06:00
|
|
|
{
|
2021-01-11 08:20:26 -06:00
|
|
|
$indent = str_repeat(' ', $indentSize);
|
|
|
|
$index = 0;
|
|
|
|
|
|
|
|
return reduce_left($array, static function ($messages, string $name, $_, string $acc) use (&$index, $indent) {
|
|
|
|
$index++;
|
|
|
|
|
|
|
|
return $acc . sprintf(
|
|
|
|
"%s%s'%s' => %s",
|
|
|
|
$index === 1 ? '' : "\n",
|
|
|
|
$indent,
|
|
|
|
$name,
|
|
|
|
is_array($messages) ? print_r($messages, true) : $messages,
|
|
|
|
);
|
|
|
|
}, '');
|
2021-01-10 13:28:52 -06:00
|
|
|
}
|
2021-01-30 03:54:04 -06:00
|
|
|
|
2021-05-22 08:09:14 -05:00
|
|
|
function isCrawler(string $userAgent): bool
|
|
|
|
{
|
|
|
|
static $detector;
|
|
|
|
if ($detector === null) {
|
|
|
|
$detector = new CrawlerDetect();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $detector->isCrawler($userAgent);
|
|
|
|
}
|
2022-01-10 07:37:44 -06:00
|
|
|
|
2022-04-15 12:57:27 -05: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);
|
|
|
|
}
|
|
|
|
|
2022-01-10 07:37:44 -06:00
|
|
|
function fieldWithUtf8Charset(FieldBuilder $field, array $emConfig, string $collation = 'unicode_ci'): FieldBuilder
|
|
|
|
{
|
|
|
|
return match ($emConfig['connection']['driver'] ?? null) {
|
|
|
|
'pdo_mysql' => $field->option('charset', 'utf8mb4')
|
|
|
|
->option('collation', 'utf8mb4_' . $collation),
|
|
|
|
default => $field,
|
|
|
|
};
|
|
|
|
}
|
2022-05-23 14:19:59 -05:00
|
|
|
|
|
|
|
function camelCaseToHumanFriendly(string $value): string
|
|
|
|
{
|
|
|
|
static $filter;
|
|
|
|
if ($filter === null) {
|
|
|
|
$filter = new CamelCaseToSeparator(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
return ucfirst($filter->filter($value));
|
|
|
|
}
|
2022-08-14 06:12:10 -05:00
|
|
|
|
2023-01-02 12:28:32 -06:00
|
|
|
function camelCaseToSnakeCase(string $value): string
|
|
|
|
{
|
|
|
|
static $filter;
|
|
|
|
if ($filter === null) {
|
|
|
|
$filter = new CamelCaseToUnderscore();
|
|
|
|
}
|
|
|
|
|
|
|
|
return strtolower($filter->filter($value));
|
|
|
|
}
|
|
|
|
|
2022-08-14 06:12:10 -05:00
|
|
|
function toProblemDetailsType(string $errorCode): string
|
|
|
|
{
|
|
|
|
return sprintf('https://shlink.io/api/error/%s', $errorCode);
|
|
|
|
}
|
2023-01-03 06:45:39 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param class-string<BackedEnum> $enum
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
function enumValues(string $enum): array
|
|
|
|
{
|
|
|
|
static $cache;
|
|
|
|
if ($cache === null) {
|
|
|
|
$cache = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $cache[$enum] ?? (
|
2023-11-29 05:34:13 -06:00
|
|
|
$cache[$enum] = array_map(static fn (BackedEnum $type) => (string) $type->value, $enum::cases())
|
2023-01-03 06:45:39 -06:00
|
|
|
);
|
|
|
|
}
|