diff --git a/CHANGELOG.md b/CHANGELOG.md index f3f4780..6dae0a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Common and useful classes, methods, exceptions etc. 1. [BaseCollection] Treat the `null` index as "no index" only while adding new element, iow. do not treat empty string as "no index" behaviour. +2. [Miscellaneous] [Regex] Use simpler & stronger pattern to match name of file # 1.1.0 diff --git a/src/Utilities/Miscellaneous.php b/src/Utilities/Miscellaneous.php index dc679e4..8d56048 100644 --- a/src/Utilities/Miscellaneous.php +++ b/src/Utilities/Miscellaneous.php @@ -200,13 +200,13 @@ class Miscellaneous * @param string $path A path that contains file name * @return string */ - public static function getFileNameFromPath($path) + public static function getFileNameFromPath(string $path): string { $matches = []; - $pattern = sprintf('|([^\%s.]+\.[A-Za-z0-9.]+)$|', DIRECTORY_SEPARATOR); + $pattern = Regex::getFileNamePattern(); if ((bool)preg_match($pattern, $path, $matches)) { - return $matches[1]; + return $matches[0]; } return ''; @@ -229,7 +229,7 @@ class Miscellaneous * Let's clear name of file * * Attention. - * The name without extension may be cleared / urlized only to avoid incorrect name by replacing "." with "-". + * The name without extension should be cleared to avoid incorrect name by replacing "." with "-". */ $withoutExtension = Urlizer::urlize($withoutExtension); diff --git a/src/Utilities/Regex.php b/src/Utilities/Regex.php index 1077bda..e605dd5 100644 --- a/src/Utilities/Regex.php +++ b/src/Utilities/Regex.php @@ -33,7 +33,7 @@ class Regex 'letterOrDigit' => '/[a-zA-Z0-9]+/', 'htmlEntity' => '/&[a-z0-9]+;/', 'htmlAttribute' => '/([\w-]+)="([\w -]+)"/', - 'fileName' => '/.+\.\w+$/', + 'fileName' => '/[\w.\- +=!@$&()?]+\.\w+$/', // e.g. "this-1_2 3 & my! 4+file.jpg" 'isQuoted' => '/^[\'"]{1}.+[\'"]{1}$/', 'windowsBasedPath' => '/^[A-Z]{1}:\\\.*$/', 'money' => '/^[-+]?\d+([\.,]{1}\d*)?$/', @@ -625,7 +625,7 @@ class Regex * * @return string */ - public static function getFileNamePattern() + public static function getFileNamePattern(): string { return self::$patterns['fileName']; } @@ -637,7 +637,7 @@ class Regex * @param string $fileName Name of file to check. It may be path of file also. * @return bool */ - public static function isFileName($fileName) + public static function isFileName(string $fileName): bool { $pattern = self::getFileNamePattern(); diff --git a/tests/Utilities/MiscellaneousTest.php b/tests/Utilities/MiscellaneousTest.php index 25a714f..d3e3dd7 100644 --- a/tests/Utilities/MiscellaneousTest.php +++ b/tests/Utilities/MiscellaneousTest.php @@ -94,11 +94,14 @@ class MiscellaneousTest extends BaseTestCase self::assertEquals($withoutExtension, Miscellaneous::getFileNameWithoutExtension($fileName)); } - public function testGetFileNameFromPath() + public function testGetFileNameFromPath(): void { // Path with file self::assertEquals('sit.amet.JPG', Miscellaneous::getFileNameFromPath('lorem/ipsum-dolor/sit.amet.JPG')); + // Path with complicated name of file + self::assertEquals('this-1_2 3 & my! 4+file.jpg', Miscellaneous::getFileNameFromPath('lorem/ipsum-dolor/this-1_2 3 & my! 4+file.jpg')); + // Path without file self::assertEquals('', Miscellaneous::getFileNameFromPath('lorem/ipsum-dolor/sit-amet')); diff --git a/tests/Utilities/RegexTest.php b/tests/Utilities/RegexTest.php index 3c404ea..cda237c 100644 --- a/tests/Utilities/RegexTest.php +++ b/tests/Utilities/RegexTest.php @@ -244,13 +244,16 @@ class RegexTest extends BaseTestCase self::assertTrue(Regex::contains($this->simpleText, 'l')); } - public function testIsFileName() + public function testIsFileName(): void { $filePath = __FILE__; $directoryPath = dirname($filePath); self::assertTrue(Regex::isFileName($filePath)); + self::assertTrue(Regex::isFileName('this-1_2 3 & my! 4+file.jpg')); + self::assertFalse(Regex::isFileName($directoryPath)); + self::assertTrue(Regex::isFileName('directory1/directory2/this-1_2 3 & my! 4+file.jpg')); } public function testIsQuoted()