mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-22 08:56:42 -06:00
Extract logic to match every type of redirect condition to its own private method
This commit is contained in:
parent
4e87affb0b
commit
202d0b86b3
@ -25,22 +25,38 @@ class RedirectCondition extends AbstractEntity
|
|||||||
*/
|
*/
|
||||||
public function matchesRequest(ServerRequestInterface $request): bool
|
public function matchesRequest(ServerRequestInterface $request): bool
|
||||||
{
|
{
|
||||||
if ($this->type === RedirectConditionType::QUERY_PARAM && $this->matchKey !== null) {
|
return match ($this->type) {
|
||||||
$query = $request->getQueryParams();
|
RedirectConditionType::QUERY_PARAM => $this->matchesQueryParam($request),
|
||||||
$queryValue = $query[$this->matchKey] ?? null;
|
RedirectConditionType::LANGUAGE => $this->matchesLanguage($request),
|
||||||
return $queryValue === $this->matchValue;
|
default => false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function matchesQueryParam(ServerRequestInterface $request): bool
|
||||||
|
{
|
||||||
|
if ($this->matchKey !== null) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->type === RedirectConditionType::LANGUAGE && $request->hasHeader('Accept-Language')) {
|
$query = $request->getQueryParams();
|
||||||
$acceptedLanguages = explode(',', $request->getHeaderLine('Accept-Language'));
|
$queryValue = $query[$this->matchKey] ?? null;
|
||||||
$normalizedLanguage = normalizeLocale($this->matchValue);
|
|
||||||
|
|
||||||
return some(
|
return $queryValue === $this->matchValue;
|
||||||
$acceptedLanguages,
|
}
|
||||||
static fn (string $lang) => normalizeLocale($lang) === $normalizedLanguage,
|
|
||||||
);
|
public function matchesLanguage(ServerRequestInterface $request): bool
|
||||||
|
{
|
||||||
|
$acceptLanguage = $request->getHeaderLine('Accept-Language');
|
||||||
|
if ($acceptLanguage === '' || $acceptLanguage === '*') {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
$acceptedLanguages = explode(',', $acceptLanguage);
|
||||||
|
$normalizedLanguage = normalizeLocale($this->matchValue);
|
||||||
|
|
||||||
|
return some(
|
||||||
|
$acceptedLanguages,
|
||||||
|
static fn (string $lang) => normalizeLocale($lang) === $normalizedLanguage,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user