diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index e5a06ff00dbc..0a4e1bfb6738 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -928,9 +928,10 @@ public static function parseCallback($callback, $default = null) * Convert to a universal file path * * @param string $value + * @param string $replace Special characters are replaced with this param default empty * @return string */ - public static function path(string $value) + public static function path(string $value, string $replace = '') { $value = trim($value); @@ -941,7 +942,7 @@ public static function path(string $value) $value = trim($value, DIRECTORY_SEPARATOR); // Remove special characters (:,*,?,",<,>,|) that aren't allowed in some os - $value = preg_replace('/[:\*\?\"\<\>\|]/', '', $value); + $value = preg_replace('/[:\*\?\"\<\>\|]/', $replace, $value); // Trim paths $value = implode( diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 2f379f94b189..0b3e1fd594bb 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -602,13 +602,12 @@ public function parseCallback($default = null) /** * Convert to a universal file path - * - * @param string $value + * @param string $replace Special characters are replaced with this param default empty * @return static */ - public function path() + public function path($replace = '') { - return new static(Str::path($this->value)); + return new static(Str::path($this->value, $replace)); } /** diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index 88724e5e364c..7524ffb614f3 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -1152,13 +1152,17 @@ public function testPadRight() public function testPath() { - $path = sprintf('x%sy%sz', DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR); + $ds = DIRECTORY_SEPARATOR; + $path = sprintf('x%sy%sz', $ds, $ds); $this->assertSame("$path", Str::path(' x/ y/z/ ')); $this->assertSame("$path", Str::path('///x/y/z//')); $this->assertSame("$path", Str::path(' | " ?:x/\>>**y/\<assertSame("$path", Str::path('x////y///z')); $this->assertSame("$path", Str::path('x\\\\y\\z')); + + $this->assertSame("users{$ds}davejohn", Str::path('users/dave:john')); + $this->assertSame("users{$ds}dave_john", Str::path('users/dave_john', '_')); } public function testSwapKeywords(): void diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index 8b1f6ef2d9cd..089811e60264 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -5,6 +5,7 @@ use Illuminate\Support\Carbon; use Illuminate\Support\Collection; use Illuminate\Support\HtmlString; +use Illuminate\Support\Str; use Illuminate\Support\Stringable; use League\CommonMark\Environment\EnvironmentBuilderInterface; use League\CommonMark\Extension\ExtensionInterface; @@ -1080,13 +1081,17 @@ public function testPadLeft() public function testPath() { - $path = sprintf('x%sy%sz', DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR); + $ds = DIRECTORY_SEPARATOR; + $path = sprintf('x%sy%sz', $ds, $ds); $this->assertSame("$path", (string) $this->stringable(' x/ y/z/ ')->path()); $this->assertSame("$path", (string) $this->stringable('///x/y/z//')->path()); $this->assertSame("$path", (string) $this->stringable(' | " ?:x/\>>**y/\<path()); $this->assertSame("$path", (string) $this->stringable('x////y///z')->path()); $this->assertSame("$path", (string) $this->stringable('x\\\\y\\z')->path()); + + $this->assertSame("users{$ds}davejohn", (string) $this->stringable('users/dave:john')->path()); + $this->assertSame("users{$ds}dave_john", (string) $this->stringable('users/dave_john')->path('_')); } public function testPadRight()