| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace League\Flysystem; |
| 6 |
|
| 7 |
use function rtrim; |
| 8 |
use function strlen; |
| 9 |
use function substr; |
| 10 |
|
| 11 |
final class PathPrefixer |
| 12 |
{ |
| 13 |
private string $prefix = ''; |
| 14 |
|
| 15 |
public function __construct(string $prefix, private string $separator = '/') |
| 16 |
{ |
| 17 |
$this->prefix = rtrim($prefix, '\\/'); |
| 18 |
|
| 19 |
if ($this->prefix !== '' || $prefix === $separator) { |
| 20 |
$this->prefix .= $separator; |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
public function prefixPath(string $path): string |
| 25 |
{ |
| 26 |
return $this->prefix . ltrim($path, '\\/'); |
| 27 |
} |
| 28 |
|
| 29 |
public function stripPrefix(string $path): string |
| 30 |
{ |
| 31 |
/* @var string */ |
| 32 |
return substr($path, strlen($this->prefix)); |
| 33 |
} |
| 34 |
|
| 35 |
public function stripDirectoryPrefix(string $path): string |
| 36 |
{ |
| 37 |
return rtrim($this->stripPrefix($path), '\\/'); |
| 38 |
} |
| 39 |
|
| 40 |
public function prefixDirectoryPath(string $path): string |
| 41 |
{ |
| 42 |
$prefixedPath = $this->prefixPath(rtrim($path, '\\/')); |
| 43 |
|
| 44 |
if ($prefixedPath === '' || substr($prefixedPath, -1) === $this->separator) { |
| 45 |
return $prefixedPath; |
| 46 |
} |
| 47 |
|
| 48 |
return $prefixedPath . $this->separator; |
| 49 |
} |
| 50 |
} |
| 51 |
|