Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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, ...)
Expand Down
28 changes: 28 additions & 0 deletions src/Utils/FileUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Smart\CoreBundle\Utils;

use Symfony\Component\String\Slugger\AsciiSlugger;

class FileUtils
{
/**
* Slugify and normalize filename
*/
public static function slugifyFilename(string $fileName): string
{
$slugger = new AsciiSlugger('fr');

$extension = pathinfo($fileName, PATHINFO_EXTENSION);
$baseName = pathinfo($fileName, PATHINFO_FILENAME);
$slugifiedBase = $slugger->slug($baseName)->lower()->toString();

if (!empty($extension)) {
$downloadFileName = $slugifiedBase . '.' . strtolower($extension);
} else {
$downloadFileName = $slugifiedBase;
}

return $downloadFileName;
}
}
50 changes: 50 additions & 0 deletions tests/Utils/FileUtilsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Smart\CoreBundle\Tests\Utils;

use Smart\CoreBundle\Utils\FileUtils;
use PHPUnit\Framework\TestCase;

/**
* vendor/bin/simple-phpunit tests/Utils/FileUtilsTest.php
*/
class FileUtilsTest extends TestCase
{
/**
* @dataProvider slugifyFilenameProvider
*/
public function testSlugifyFilename(string $expected, string $filename): void
{
$this->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',
],
];
}
}