Skip to content

Commit

Permalink
Path improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Apr 18, 2019
1 parent c876801 commit b16f9c8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,28 @@ public static function url(...$parts)
/**
* Concatenate a path with a custom separator
*
* @param string $directorySeparator
* @param string $separator
* @param string[] $pathComponents
*
* @return string
*/
public static function custom($directorySeparator, array $pathComponents)
public static function custom($separator, array $pathComponents)
{
$fullPath = [];
$charList = '/\\' . $directorySeparator;
$charList = '/\\' . $separator;
foreach($pathComponents as $section)
{
if($section !== '')
if(isset($section[1]))
{
if(empty($fullPath) && $section[0] === $directorySeparator && $section !== $directorySeparator)
{
$fullPath[] = '';
}
$fullPath[] = trim($section, $charList);
$fullPath[] = empty($fullPath) ? rtrim($section, $charList) : trim($section, $charList);
}
else if(isset($section[0]))
{
$fullPath[] = $section == $separator ? '' : $section;
}
}

return implode($directorySeparator, $fullPath);
return ($fullPath[0] == '' && count($fullPath) === 1 ? $separator : implode($separator, $fullPath));
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/PathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ public function testUrl()
{
$this->assertEquals('/', Path::url('/', ''));
$this->assertEquals('/test', Path::url('/', '/test'));
$this->assertEquals('/test', Path::url('/', '', '/test', ''));
$this->assertEquals('/test/subdir/test', Path::url('/test', '/subdir/test/'));
$this->assertEquals('/test/subdir/test/', Path::url('/test', '/subdir/test', '/'));
$this->assertEquals('test/subdir/test', Path::url('test', '/subdir/test/'));
}
}

0 comments on commit b16f9c8

Please sign in to comment.