From e34246dcc0077ffe47c5b81986c86a09490a18a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Werner=20M=2E=20Krau=C3=9F?= Date: Wed, 6 Sep 2017 16:17:06 +0200 Subject: [PATCH] Feature: Convert::raw2filename --- src/Core/Convert.php | 16 ++++++++++++++++ tests/php/Core/ConvertTest.php | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/Core/Convert.php b/src/Core/Convert.php index 7db3ad03d26..57b004b0ed0 100644 --- a/src/Core/Convert.php +++ b/src/Core/Convert.php @@ -2,6 +2,7 @@ namespace SilverStripe\Core; +use SilverStripe\Assets\FileNameFilter; use SilverStripe\ORM\DB; use SilverStripe\View\Parsers\URLSegmentFilter; use InvalidArgumentException; @@ -467,6 +468,21 @@ public static function raw2url($title) return $f->filter($title); } + /** + * Convert a string (normally a title) to a string suitable for using + * as a filename. Uses {@link FileNameFilter}. + * + * @param string + * @return string + */ + public static function raw2filename($title) + { + $f = FileNameFilter::create(); + return $f->filter($title); + } + + + /** * Normalises newline sequences to conform to (an) OS specific format. * diff --git a/tests/php/Core/ConvertTest.php b/tests/php/Core/ConvertTest.php index d9dcaf2a3a2..4f047bd5689 100644 --- a/tests/php/Core/ConvertTest.php +++ b/tests/php/Core/ConvertTest.php @@ -276,6 +276,25 @@ public function testRaw2URL() $this->assertEquals('foos-bar-2', Convert::raw2url('foo\'s [bar] (2)')); } + public function filenameProvider() + { + return [ + ['localhost:8080', 'localhost8080'], //remove : froma filename + ['foo.bar', 'foo.bar'], //nothing, is fine + ['/this\-is-a-test', 'this-is-a-test'], //remove trailing slash and backslash inside + ['no whitespace', 'no-whitespace'], + ['österreich.jpg', 'oesterreich.jpg'], //convert umlaut + ]; + } + + /** + * @dataProvider filenameProvider + */ + public function testRaw2Filename($orig, $expected) + { + $this->assertEquals($expected, Convert::raw2filename($orig)); + } + /** * Helper function for comparing characters with significant whitespaces *