mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Remove last references to functional-php
This commit is contained in:
74
module/Core/functions/array-utils.php
Normal file
74
module/Core/functions/array-utils.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\ArrayUtils;
|
||||
|
||||
use function array_filter;
|
||||
use function array_reduce;
|
||||
use function in_array;
|
||||
|
||||
use const ARRAY_FILTER_USE_KEY;
|
||||
|
||||
function contains(mixed $value, array $array): bool
|
||||
{
|
||||
return in_array($value, $array, strict: true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array[] $multiArray
|
||||
* @return array
|
||||
*/
|
||||
function flatten(array $multiArray): array
|
||||
{
|
||||
return array_reduce(
|
||||
$multiArray,
|
||||
static fn (array $carry, array $value) => [...$carry, ...$value],
|
||||
initial: [],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a callback returns true for at least one item in a collection.
|
||||
* @param callable(mixed $value, mixed $key): bool $callback
|
||||
*/
|
||||
function some(iterable $collection, callable $callback): bool
|
||||
{
|
||||
foreach ($collection as $key => $value) {
|
||||
if ($callback($value, $key)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a callback returns true for all item in a collection.
|
||||
* @param callable(mixed $value, string|number $key): bool $callback
|
||||
*/
|
||||
function every(iterable $collection, callable $callback): bool
|
||||
{
|
||||
foreach ($collection as $key => $value) {
|
||||
if (! $callback($value, $key)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing only those entries in the array whose key is in the supplied keys.
|
||||
*/
|
||||
function select_keys(array $array, array $keys): array
|
||||
{
|
||||
return array_filter(
|
||||
$array,
|
||||
static fn (string $key) => contains(
|
||||
$key,
|
||||
$keys,
|
||||
),
|
||||
ARRAY_FILTER_USE_KEY,
|
||||
);
|
||||
}
|
||||
@@ -20,7 +20,6 @@ use function array_keys;
|
||||
use function array_map;
|
||||
use function array_reduce;
|
||||
use function date_default_timezone_get;
|
||||
use function in_array;
|
||||
use function is_array;
|
||||
use function print_r;
|
||||
use function Shlinkio\Shlink\Common\buildDateRange;
|
||||
@@ -183,51 +182,3 @@ function enumValues(string $enum): array
|
||||
$cache[$enum] = array_map(static fn (BackedEnum $type) => (string) $type->value, $enum::cases())
|
||||
);
|
||||
}
|
||||
|
||||
function contains(mixed $value, array $array): bool
|
||||
{
|
||||
return in_array($value, $array, strict: true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array[] $multiArray
|
||||
* @return array
|
||||
*/
|
||||
function flatten(array $multiArray): array
|
||||
{
|
||||
return array_reduce(
|
||||
$multiArray,
|
||||
static fn (array $carry, array $value) => [...$carry, ...$value],
|
||||
initial: [],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a callback returns true for at least one item in a collection.
|
||||
* @param callable(mixed $value, string|number $key): bool $callback
|
||||
*/
|
||||
function some(iterable $collection, callable $callback): bool
|
||||
{
|
||||
foreach ($collection as $key => $value) {
|
||||
if ($callback($value, $key)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a callback returns true for all item in a collection.
|
||||
* @param callable(mixed $value, string|number $key): bool $callback
|
||||
*/
|
||||
function every(iterable $collection, callable $callback): bool
|
||||
{
|
||||
foreach ($collection as $key => $value) {
|
||||
if (! $callback($value, $key)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ use Endroid\QrCode\Writer\WriterInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Shlinkio\Shlink\Core\Options\QrCodeOptions;
|
||||
|
||||
use function Shlinkio\Shlink\Core\contains;
|
||||
use function Shlinkio\Shlink\Core\ArrayUtils\contains;
|
||||
use function strtolower;
|
||||
use function trim;
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@ use Psr\Log\LoggerInterface;
|
||||
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
|
||||
use Shlinkio\Shlink\Core\Util\RedirectResponseHelperInterface;
|
||||
|
||||
use function Functional\compose;
|
||||
use function str_replace;
|
||||
use function urlencode;
|
||||
|
||||
@@ -51,9 +50,6 @@ class NotFoundRedirectResolver implements NotFoundRedirectResolverInterface
|
||||
|
||||
private function resolvePlaceholders(UriInterface $currentUri, string $redirectUrl): string
|
||||
{
|
||||
$domain = $currentUri->getAuthority();
|
||||
$path = $currentUri->getPath();
|
||||
|
||||
try {
|
||||
$redirectUri = Uri::createFromString($redirectUrl);
|
||||
} catch (SyntaxError $e) {
|
||||
@@ -64,18 +60,32 @@ class NotFoundRedirectResolver implements NotFoundRedirectResolverInterface
|
||||
return $redirectUrl;
|
||||
}
|
||||
|
||||
$replacePlaceholderForPattern = static fn (string $pattern, string $replace, callable $modifier) =>
|
||||
static fn (?string $value) =>
|
||||
$value === null ? null : str_replace($modifier($pattern), $modifier($replace), $value);
|
||||
$replacePlaceholders = static fn (callable $modifier) => compose(
|
||||
$replacePlaceholderForPattern(self::DOMAIN_PLACEHOLDER, $domain, $modifier),
|
||||
$replacePlaceholderForPattern(self::ORIGINAL_PATH_PLACEHOLDER, $path, $modifier),
|
||||
$path = $currentUri->getPath();
|
||||
$domain = $currentUri->getAuthority();
|
||||
|
||||
$replacePlaceholderForPattern = static fn (string $pattern, string $replace, ?string $value): string|null =>
|
||||
$value === null ? null : str_replace($pattern, $replace, $value);
|
||||
|
||||
$replacePlaceholders = static function (
|
||||
callable $modifier,
|
||||
?string $value,
|
||||
) use (
|
||||
$replacePlaceholderForPattern,
|
||||
$path,
|
||||
$domain,
|
||||
): string|null {
|
||||
$value = $replacePlaceholderForPattern($modifier(self::DOMAIN_PLACEHOLDER), $modifier($domain), $value);
|
||||
return $replacePlaceholderForPattern($modifier(self::ORIGINAL_PATH_PLACEHOLDER), $modifier($path), $value);
|
||||
};
|
||||
|
||||
$replacePlaceholdersInPath = static function (string $path) use ($replacePlaceholders): string {
|
||||
$result = $replacePlaceholders(static fn (mixed $v) => $v, $path);
|
||||
return str_replace('//', '/', $result ?? '');
|
||||
};
|
||||
$replacePlaceholdersInQuery = static fn (?string $query): string|null => $replacePlaceholders(
|
||||
urlencode(...),
|
||||
$query,
|
||||
);
|
||||
$replacePlaceholdersInPath = compose(
|
||||
$replacePlaceholders(static fn (mixed $v) => $v),
|
||||
static fn (?string $path) => $path === null ? null : str_replace('//', '/', $path),
|
||||
);
|
||||
$replacePlaceholdersInQuery = $replacePlaceholders(urlencode(...));
|
||||
|
||||
return $redirectUri
|
||||
->withPath($replacePlaceholdersInPath($redirectUri->getPath()))
|
||||
|
||||
@@ -24,7 +24,7 @@ final class DeviceLongUrlPair
|
||||
* * The first one is a list of mapped instances for those entries in the map with non-null value
|
||||
* * The second is a list of DeviceTypes which have been provided with value null
|
||||
*
|
||||
* @param array<string, string | null> $map
|
||||
* @param array<string, string|null> $map
|
||||
* @return array{array<string, self>, DeviceType[]}
|
||||
*/
|
||||
public static function fromMapToChangeSet(array $map): array
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Shlinkio\Shlink\Core\ShortUrl\Model;
|
||||
|
||||
use function Shlinkio\Shlink\Core\contains;
|
||||
use function Shlinkio\Shlink\Core\ArrayUtils\contains;
|
||||
|
||||
enum OrderableField: string
|
||||
{
|
||||
|
||||
@@ -11,9 +11,9 @@ use Shlinkio\Shlink\Core\Model\DeviceType;
|
||||
use function array_keys;
|
||||
use function array_values;
|
||||
use function is_array;
|
||||
use function Shlinkio\Shlink\Core\contains;
|
||||
use function Shlinkio\Shlink\Core\ArrayUtils\contains;
|
||||
use function Shlinkio\Shlink\Core\ArrayUtils\every;
|
||||
use function Shlinkio\Shlink\Core\enumValues;
|
||||
use function Shlinkio\Shlink\Core\every;
|
||||
|
||||
class DeviceLongUrlsValidator extends AbstractValidator
|
||||
{
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Util;
|
||||
|
||||
use function Shlinkio\Shlink\Core\contains;
|
||||
use function Shlinkio\Shlink\Core\ArrayUtils\contains;
|
||||
|
||||
enum RedirectStatus: int
|
||||
{
|
||||
|
||||
@@ -20,6 +20,7 @@ use function array_keys;
|
||||
use function array_map;
|
||||
use function explode;
|
||||
use function implode;
|
||||
use function Shlinkio\Shlink\Core\ArrayUtils\some;
|
||||
use function str_contains;
|
||||
|
||||
class RequestTracker implements RequestTrackerInterface, RequestMethodInterface
|
||||
@@ -85,17 +86,13 @@ class RequestTracker implements RequestTrackerInterface, RequestMethodInterface
|
||||
$remoteAddrParts = explode('.', $remoteAddr);
|
||||
$disableTrackingFrom = $this->trackingOptions->disableTrackingFrom;
|
||||
|
||||
foreach ($disableTrackingFrom as $value) {
|
||||
return some($disableTrackingFrom, function (string $value) use ($ip, $remoteAddrParts): bool {
|
||||
$range = str_contains($value, '*')
|
||||
? $this->parseValueWithWildcards($value, $remoteAddrParts)
|
||||
: Factory::parseRangeString($value);
|
||||
|
||||
if ($range !== null && $ip->matches($range)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return $range !== null && $ip->matches($range);
|
||||
});
|
||||
}
|
||||
|
||||
private function parseValueWithWildcards(string $value, array $remoteAddrParts): ?RangeInterface
|
||||
|
||||
@@ -28,7 +28,7 @@ use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
||||
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
|
||||
|
||||
use function count;
|
||||
use function Shlinkio\Shlink\Core\contains;
|
||||
use function Shlinkio\Shlink\Core\ArrayUtils\contains;
|
||||
|
||||
class NotifyVisitToWebHooksTest extends TestCase
|
||||
{
|
||||
|
||||
@@ -32,8 +32,8 @@ use stdClass;
|
||||
use Symfony\Component\Console\Style\StyleInterface;
|
||||
|
||||
use function count;
|
||||
use function Shlinkio\Shlink\Core\contains;
|
||||
use function Shlinkio\Shlink\Core\some;
|
||||
use function Shlinkio\Shlink\Core\ArrayUtils\contains;
|
||||
use function Shlinkio\Shlink\Core\ArrayUtils\some;
|
||||
use function sprintf;
|
||||
use function str_contains;
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ use Shlinkio\Shlink\Importer\Sources\ImportSource;
|
||||
|
||||
use function array_map;
|
||||
use function range;
|
||||
use function Shlinkio\Shlink\Core\every;
|
||||
use function Shlinkio\Shlink\Core\ArrayUtils\every;
|
||||
use function strlen;
|
||||
use function strtolower;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user