Skip to content

Commit

Permalink
add proper method to cut the string
Browse files Browse the repository at this point in the history
  • Loading branch information
saeideng committed Mar 22, 2018
1 parent 43df017 commit f405fdf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/Filesystem/File.php
Expand Up @@ -355,21 +355,31 @@ public function name()
}

/**
* Returns the file basename. simulate the php basename().
* Returns the file basename. simulate the php basename() for multibyte (mb_basename).
*
* @param string $path Path to file
* @param string|null $ext The name of the extension
* @return string the file basename.
*/
protected static function _basename($path, $ext = null)
{
//check for multibyte string and use basename() if not found
if (mb_strlen($path) === strlen($path)) {
return ($ext===null)? basename($path) : basename($path, $ext);
}

$splInfo = new SplFileInfo($path);
$name = ltrim($splInfo->getFilename(), DS);
if ($ext === null || rtrim($name, $ext) === '') {

if ($ext === null || $ext === '') {
return $name;
}
$ext = preg_quote($ext);
$new = preg_replace("/({$ext})$/u", "", $name);

// basename of '/etc/.d' is '.d' not ''
return ($new === '')? $name : $new;

return rtrim($name, $ext);
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase/Filesystem/FileTest.php
Expand Up @@ -176,6 +176,7 @@ public function baseNameValueProvider()
['/نام.txt', '.txt'],
['/نام فارسی.txt', '.txt'],
//
['abcde.ab', '.abe'],
['/etc/sudoers.d', null],
['/etc/.d', '.d'],
['/etc/sudoers.d', '.d'],
Expand Down

0 comments on commit f405fdf

Please sign in to comment.