shlink/module/Core/functions/functions.php

292 lines
8.2 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core;
use BackedEnum;
use Cake\Chronos\Chronos;
use DateTimeInterface;
use Doctrine\ORM\Mapping\Builder\FieldBuilder;
use GuzzleHttp\Psr7\Query;
2024-05-21 11:09:45 -05:00
use Hidehalo\Nanoid\Client as NanoidClient;
use Jaybizzle\CrawlerDetect\CrawlerDetect;
use Laminas\Filter\Word\CamelCaseToSeparator;
use Laminas\Filter\Word\CamelCaseToUnderscore;
use Laminas\InputFilter\InputFilter;
use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Common\Middleware\IpAddressMiddlewareFactory;
use Shlinkio\Shlink\Common\Util\DateRange;
2023-01-25 13:33:07 -06:00
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlMode;
2023-11-30 07:34:21 -06:00
use function array_keys;
use function array_map;
use function array_pad;
2023-11-30 02:13:29 -06:00
use function array_reduce;
use function date_default_timezone_get;
use function explode;
use function implode;
use function is_array;
use function print_r;
use function Shlinkio\Shlink\Common\buildDateRange;
use function Shlinkio\Shlink\Core\ArrayUtils\map;
use function sprintf;
use function str_repeat;
use function str_replace;
use function strtolower;
use function trim;
use function ucfirst;
2023-01-25 13:33:07 -06:00
function generateRandomShortCode(int $length, ShortUrlMode $mode = ShortUrlMode::STRICT): string
{
2024-05-21 11:09:45 -05:00
static $nanoIdClient;
if ($nanoIdClient === null) {
$nanoIdClient = new NanoidClient();
}
2023-01-25 13:33:07 -06:00
$alphabet = $mode === ShortUrlMode::STRICT
? '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
: '0123456789abcdefghijklmnopqrstuvwxyz';
2024-05-21 11:09:45 -05:00
return $nanoIdClient->formattedId($alphabet, $length);
}
2024-10-28 16:27:30 -05:00
function parseDateFromQuery(array $query, string $dateName): Chronos|null
{
2022-12-04 13:35:38 -06:00
return normalizeOptionalDate(empty($query[$dateName] ?? null) ? null : Chronos::parse($query[$dateName]));
}
function parseDateRangeFromQuery(array $query, string $startDateName, string $endDateName): DateRange
{
$startDate = parseDateFromQuery($query, $startDateName);
$endDate = parseDateFromQuery($query, $endDateName);
return buildDateRange($startDate, $endDate);
}
2024-10-28 16:27:30 -05:00
function dateRangeToHumanFriendly(DateRange|null $dateRange): string
{
$startDate = $dateRange?->startDate;
$endDate = $dateRange?->endDate;
return match (true) {
$startDate !== null && $endDate !== null => sprintf(
'Between %s and %s',
$startDate->toDateTimeString(),
$endDate->toDateTimeString(),
),
$startDate !== null => sprintf('Since %s', $startDate->toDateTimeString()),
$endDate !== null => sprintf('Until %s', $endDate->toDateTimeString()),
default => 'All time',
};
}
2022-12-04 13:35:38 -06:00
/**
* @return ($date is null ? null : Chronos)
*/
2024-10-28 16:27:30 -05:00
function normalizeOptionalDate(string|DateTimeInterface|Chronos|null $date): Chronos|null
{
$parsedDate = match (true) {
$date === null || $date instanceof Chronos => $date,
$date instanceof DateTimeInterface => Chronos::instance($date),
default => Chronos::parse($date),
};
return $parsedDate?->setTimezone(date_default_timezone_get());
}
2023-11-30 02:13:29 -06:00
function normalizeDate(string|DateTimeInterface|Chronos $date): Chronos
2022-12-04 13:35:38 -06:00
{
return normalizeOptionalDate($date);
}
function normalizeLocale(string $locale): string
{
return trim(strtolower(str_replace('_', '-', $locale)));
}
/**
* Parse an accept-language-like pattern into a list of locales, optionally filtering out those which do not match a
* minimum quality
*
* @param non-empty-string $acceptLanguage
* @return iterable<string>;
*/
function acceptLanguageToLocales(string $acceptLanguage, float $minQuality = 0): iterable
{
/** @var array{string, float|null}[] $acceptLanguagesList */
$acceptLanguagesList = map(explode(',', $acceptLanguage), static function (string $lang): array {
// Split locale/language and quality (en-US;q=0.7) -> [en-US, q=0.7]
[$lang, $qualityString] = array_pad(explode(';', $lang), length: 2, value: '');
$normalizedLang = normalizeLocale($lang);
$quality = Query::parse(trim($qualityString))['q'] ?? 1;
return [$normalizedLang, (float) $quality];
});
foreach ($acceptLanguagesList as [$lang, $quality]) {
if ($lang !== '*' && $quality >= $minQuality) {
yield $lang;
}
}
}
/**
* Splits a locale into its corresponding language and country codes.
* The country code will be null if not present
* 'es-AR' -> ['es', 'AR']
* 'fr-FR' -> ['fr', 'FR']
* 'en' -> ['en', null]
*
* @return array{string, string|null}
*/
function splitLocale(string $locale): array
{
2024-07-31 12:53:05 -05:00
[$lang, $countryCode] = array_pad(explode('-', $locale), length: 2, value: null);
return [$lang, $countryCode];
}
2022-12-04 13:35:38 -06:00
2024-07-31 12:53:05 -05:00
/**
* @param InputFilter<mixed> $inputFilter
*/
2024-10-28 16:27:30 -05:00
function getOptionalIntFromInputFilter(InputFilter $inputFilter, string $fieldName): int|null
{
$value = $inputFilter->getValue($fieldName);
return $value !== null ? (int) $value : null;
}
2024-07-31 12:53:05 -05:00
/**
* @param InputFilter<mixed> $inputFilter
*/
2024-10-28 16:27:30 -05:00
function getOptionalBoolFromInputFilter(InputFilter $inputFilter, string $fieldName): bool|null
{
$value = $inputFilter->getValue($fieldName);
return $value !== null ? (bool) $value : null;
}
2024-07-31 12:53:05 -05:00
/**
* @param InputFilter<mixed> $inputFilter
*/
function getNonEmptyOptionalValueFromInputFilter(InputFilter $inputFilter, string $fieldName): mixed
{
$value = $inputFilter->getValue($fieldName);
return empty($value) ? null : $value;
}
function arrayToString(array $array, int $indentSize = 4): string
{
$indent = str_repeat(' ', $indentSize);
2023-11-30 07:34:21 -06:00
$names = array_keys($array);
$index = 0;
2023-11-30 07:34:21 -06:00
return array_reduce($names, static function (string $acc, string $name) use (&$index, $indent, $array) {
$index++;
2023-11-30 07:34:21 -06:00
$messages = $array[$name];
return $acc . sprintf(
"%s%s'%s' => %s",
$index === 1 ? '' : "\n",
$indent,
$name,
is_array($messages) ? print_r($messages, true) : $messages,
);
}, '');
}
function isCrawler(string $userAgent): bool
{
static $detector;
if ($detector === null) {
$detector = new CrawlerDetect();
}
return $detector->isCrawler($userAgent);
}
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);
}
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,
};
}
function camelCaseToHumanFriendly(string $value): string
{
static $filter;
if ($filter === null) {
$filter = new CamelCaseToSeparator(' ');
}
return ucfirst($filter->filter($value));
}
function camelCaseToSnakeCase(string $value): string
{
static $filter;
if ($filter === null) {
$filter = new CamelCaseToUnderscore();
}
return strtolower($filter->filter($value));
}
function toProblemDetailsType(string $errorCode): string
{
return sprintf('https://shlink.io/api/error/%s', $errorCode);
}
/**
* @param class-string<BackedEnum> $enum
* @return string[]
*/
function enumValues(string $enum): array
{
static $cache;
if ($cache === null) {
$cache = [];
}
return $cache[$enum] ?? (
$cache[$enum] = array_map(static fn (BackedEnum $type) => (string) $type->value, $enum::cases())
);
}
/**
* @param class-string<BackedEnum> $enum
*/
function enumToString(string $enum): string
{
return sprintf('["%s"]', implode('", "', enumValues($enum)));
}
/**
* Split provided string by comma and return a list of the results.
* An empty array is returned if provided value is empty
*/
2024-10-28 16:27:30 -05:00
function splitByComma(string|null $value): array
{
if ($value === null || trim($value) === '') {
return [];
}
return array_map(trim(...), explode(',', $value));
}
2024-10-28 16:27:30 -05:00
function ipAddressFromRequest(ServerRequestInterface $request): string|null
{
return $request->getAttribute(IpAddressMiddlewareFactory::REQUEST_ATTR);
}