[Miscellaneous] [Regex] Use simpler & stronger pattern to match name of file

This commit is contained in:
Meritoo
2019-12-18 20:25:06 +01:00
parent 872259e63d
commit ddb3f0a544
5 changed files with 16 additions and 9 deletions

View File

@@ -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 1. [BaseCollection] Treat the `null` index as "no index" only while adding new element, iow. do not treat empty
string as "no index" behaviour. string as "no index" behaviour.
2. [Miscellaneous] [Regex] Use simpler & stronger pattern to match name of file
# 1.1.0 # 1.1.0

View File

@@ -200,13 +200,13 @@ class Miscellaneous
* @param string $path A path that contains file name * @param string $path A path that contains file name
* @return string * @return string
*/ */
public static function getFileNameFromPath($path) public static function getFileNameFromPath(string $path): string
{ {
$matches = []; $matches = [];
$pattern = sprintf('|([^\%s.]+\.[A-Za-z0-9.]+)$|', DIRECTORY_SEPARATOR); $pattern = Regex::getFileNamePattern();
if ((bool)preg_match($pattern, $path, $matches)) { if ((bool)preg_match($pattern, $path, $matches)) {
return $matches[1]; return $matches[0];
} }
return ''; return '';
@@ -229,7 +229,7 @@ class Miscellaneous
* Let's clear name of file * Let's clear name of file
* *
* Attention. * 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); $withoutExtension = Urlizer::urlize($withoutExtension);

View File

@@ -33,7 +33,7 @@ class Regex
'letterOrDigit' => '/[a-zA-Z0-9]+/', 'letterOrDigit' => '/[a-zA-Z0-9]+/',
'htmlEntity' => '/&[a-z0-9]+;/', 'htmlEntity' => '/&[a-z0-9]+;/',
'htmlAttribute' => '/([\w-]+)="([\w -]+)"/', 'htmlAttribute' => '/([\w-]+)="([\w -]+)"/',
'fileName' => '/.+\.\w+$/', 'fileName' => '/[\w.\- +=!@$&()?]+\.\w+$/', // e.g. "this-1_2 3 & my! 4+file.jpg"
'isQuoted' => '/^[\'"]{1}.+[\'"]{1}$/', 'isQuoted' => '/^[\'"]{1}.+[\'"]{1}$/',
'windowsBasedPath' => '/^[A-Z]{1}:\\\.*$/', 'windowsBasedPath' => '/^[A-Z]{1}:\\\.*$/',
'money' => '/^[-+]?\d+([\.,]{1}\d*)?$/', 'money' => '/^[-+]?\d+([\.,]{1}\d*)?$/',
@@ -625,7 +625,7 @@ class Regex
* *
* @return string * @return string
*/ */
public static function getFileNamePattern() public static function getFileNamePattern(): string
{ {
return self::$patterns['fileName']; 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. * @param string $fileName Name of file to check. It may be path of file also.
* @return bool * @return bool
*/ */
public static function isFileName($fileName) public static function isFileName(string $fileName): bool
{ {
$pattern = self::getFileNamePattern(); $pattern = self::getFileNamePattern();

View File

@@ -94,11 +94,14 @@ class MiscellaneousTest extends BaseTestCase
self::assertEquals($withoutExtension, Miscellaneous::getFileNameWithoutExtension($fileName)); self::assertEquals($withoutExtension, Miscellaneous::getFileNameWithoutExtension($fileName));
} }
public function testGetFileNameFromPath() public function testGetFileNameFromPath(): void
{ {
// Path with file // Path with file
self::assertEquals('sit.amet.JPG', Miscellaneous::getFileNameFromPath('lorem/ipsum-dolor/sit.amet.JPG')); 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 // Path without file
self::assertEquals('', Miscellaneous::getFileNameFromPath('lorem/ipsum-dolor/sit-amet')); self::assertEquals('', Miscellaneous::getFileNameFromPath('lorem/ipsum-dolor/sit-amet'));

View File

@@ -244,13 +244,16 @@ class RegexTest extends BaseTestCase
self::assertTrue(Regex::contains($this->simpleText, 'l')); self::assertTrue(Regex::contains($this->simpleText, 'l'));
} }
public function testIsFileName() public function testIsFileName(): void
{ {
$filePath = __FILE__; $filePath = __FILE__;
$directoryPath = dirname($filePath); $directoryPath = dirname($filePath);
self::assertTrue(Regex::isFileName($filePath)); self::assertTrue(Regex::isFileName($filePath));
self::assertTrue(Regex::isFileName('this-1_2 3 & my! 4+file.jpg'));
self::assertFalse(Regex::isFileName($directoryPath)); self::assertFalse(Regex::isFileName($directoryPath));
self::assertTrue(Regex::isFileName('directory1/directory2/this-1_2 3 & my! 4+file.jpg'));
} }
public function testIsQuoted() public function testIsQuoted()