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