From 5b6bce85d0d95a5ab0d1381f594e49f4325a52ca Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Wed, 3 Apr 2024 12:32:18 -0300 Subject: [PATCH] fix: usage of defaultLocale property Need to use the function packageDefaultLocale to prevent problems when defaultLocale is not defined Signed-off-by: Vitor Mattos --- src/helpers.php | 10 ++++------ tests/_Mocks/PageMock.php | 5 ----- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/helpers.php b/src/helpers.php index 0fe97f3..be52c68 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -29,8 +29,6 @@ function current_path_locale($page): string { $path = trim($page->getPath(), '/'); - $default_locale = $page->defaultLocale ?? packageDefaultLocale(); - /** * - [a-z]{2,3} language code * - [A-Z]{2} region code @@ -41,7 +39,7 @@ function current_path_locale($page): string preg_match($locale_regex, $path, $matches); - return $matches['locale'] ?? $default_locale; + return $matches['locale'] ?? packageDefaultLocale(); } /** @@ -56,12 +54,12 @@ function translate_path($page, ?string $target_locale = null): string $current_locale = current_path_locale($page); $partial_path = match (true) { - $current_locale === $page->defaultLocale => $page->getPath(), + $current_locale === packageDefaultLocale($page) => $page->getPath(), default => substr($page->getPath(), strlen($current_locale) + 1), }; return match (true) { - $target_locale === $page->defaultLocale => "{$partial_path}", + $target_locale === packageDefaultLocale($page) => "{$partial_path}", default => "/{$target_locale}{$partial_path}", }; } @@ -89,7 +87,7 @@ function locale_path($page, string $partial_path, ?string $target_locale = null) $partial_path = '/'.trim($partial_path, '/'); return match (true) { - $target_locale === $page->defaultLocale => $partial_path, + $target_locale === packageDefaultLocale($page) => $partial_path, default => "/{$target_locale}{$partial_path}" }; } diff --git a/tests/_Mocks/PageMock.php b/tests/_Mocks/PageMock.php index 8070728..55de948 100644 --- a/tests/_Mocks/PageMock.php +++ b/tests/_Mocks/PageMock.php @@ -12,11 +12,6 @@ class PageMock public array $localization = []; - public function __construct() - { - $this->defaultLocale = packageDefaultLocale(); - } - public function setPath(string $path): static { $this->path = '/'.trim($path, '/');