diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f9c65d..5f4dbc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ CHANGELOG for 1.x =================== +## v1.14.4 - (2025-06-24) +### Added +- `FileUtils::slugifyFilename` Slugify and normalize filename + tests + ## v1.14.3 - (2025-03-06) ### Fixed - `ProcessMonitor::logException` switch $e param type from `\Exception` to `\Throwable` to better handle lower level error log (such as undefined method, ...) diff --git a/src/Utils/FileUtils.php b/src/Utils/FileUtils.php new file mode 100644 index 0000000..077ec73 --- /dev/null +++ b/src/Utils/FileUtils.php @@ -0,0 +1,28 @@ +slug($baseName)->lower()->toString(); + + if (!empty($extension)) { + $downloadFileName = $slugifiedBase . '.' . strtolower($extension); + } else { + $downloadFileName = $slugifiedBase; + } + + return $downloadFileName; + } +} diff --git a/tests/Utils/FileUtilsTest.php b/tests/Utils/FileUtilsTest.php new file mode 100644 index 0000000..d2f62c0 --- /dev/null +++ b/tests/Utils/FileUtilsTest.php @@ -0,0 +1,50 @@ +assertSame($expected, FileUtils::slugifyFilename($filename)); + } + + public function slugifyFilenameProvider(): array + { + return [ + 'basic' => [ + // expected + 'smartcore.pdf', + // filename + 'smartcore.pdf', + ], + 'with_ascii_issue' => [ + // expected + 'smartcore.pdf', + // filename + 'smàrtcôre.pdf', + ], + 'with_space' => [ + // expected + 'smart-core.pdf', + // filename + 'smart core.pdf', + ], + 'without_extension' => [ + // expected + 'smartcore', + // filename + 'smartcôre', + ], + ]; + } +}