Skip to content

Commit 30a8894

Browse files
authored
Merge pull request #8 from LibreSign/fix/empty-path-return-empty
fix: empty path return empty
2 parents 36ac8b6 + 5c2e5ac commit 30a8894

3 files changed

Lines changed: 14 additions & 11 deletions

File tree

src/helpers.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,16 @@ function translate_path($page, ?string $target_locale = null): string
5151

5252
$current_locale = current_path_locale($page);
5353

54-
$partial_path = match (true) {
54+
$partial_path = (string) match (true) {
5555
$current_locale === packageDefaultLocale($page) => $page->getPath(),
5656
default => substr($page->getPath(), strlen($current_locale) + 1),
5757
};
58+
if ($partial_path === '/') {
59+
$partial_path = '';
60+
}
5861

5962
return match (true) {
60-
$target_locale === packageDefaultLocale($page) => $partial_path ?: '/',
63+
$target_locale === packageDefaultLocale($page) => $partial_path,
6164
default => "/{$target_locale}".($partial_path === '/' ? '' : $partial_path),
6265
};
6366
}
@@ -89,7 +92,7 @@ function locale_path($page, string $partial_path, ?string $target_locale = null)
8992
}
9093

9194
return match (true) {
92-
$target_locale === packageDefaultLocale($page) => $partial_path ?: '/',
95+
$target_locale === packageDefaultLocale($page) => $partial_path,
9396
default => "/{$target_locale}".($partial_path === '/' ? '' : $partial_path),
9497
};
9598
}

tests/unit/LocalePathTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ public static function providerLocalePath(): array
1616
{
1717
return [
1818
// Handling empty paths
19-
['', null, '/'],
19+
['', null, ''],
2020
['', 'ar', '/ar'],
2121

2222
// Root path handling
23-
['/', null, '/'],
23+
['/', null, ''],
2424
['/', 'ar', '/ar'],
25-
['/', packageDefaultLocale(), '/'],
25+
['/', packageDefaultLocale(), ''],
2626

2727
// Basic path translation
2828
['blog', null, '/blog'],
@@ -36,7 +36,7 @@ public static function providerLocalePath(): array
3636

3737
// Paths when `packageDefaultLocale()` should not add a locale prefix
3838
['blog', packageDefaultLocale(), '/blog'],
39-
['/', packageDefaultLocale(), '/'],
39+
['/blog', packageDefaultLocale(), '/blog'],
4040

4141
// Special characters handling
4242
['café', 'fr', '/fr/café'],

tests/unit/TranslatePathTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public static function providerTranslatePath(): array
6161

6262
// Testing `/` as a root path (edge case)
6363
['/', 'ar', '/ar'],
64-
['/', null, '/'],
65-
['/', packageDefaultLocale(), '/'],
64+
['/', null, ''],
65+
['/', packageDefaultLocale(), ''],
6666

6767
// Testing paths with unrecognized prefixes (should keep them)
6868
['/custom-path/blog', 'fr-CA', '/fr-CA/custom-path/blog'],
@@ -74,7 +74,7 @@ public static function providerTranslatePath(): array
7474

7575
// Handling an empty path (should default to `/`)
7676
['', 'ar', '/ar'],
77-
['', null, '/'],
77+
['', null, ''],
7878

7979
// Deeply nested paths
8080
['/es/a/b/c/d/e', 'ar', '/ar/a/b/c/d/e'],
@@ -91,7 +91,7 @@ public static function providerTranslatePath(): array
9191

9292
// Handling trailing slashes
9393
['/es/', 'ar', '/ar'],
94-
['/fr-CA/', null, '/'],
94+
['/fr-CA/', null, ''],
9595
['/blog/', 'ar', '/ar/blog'],
9696

9797
/**

0 commit comments

Comments
 (0)