Skip to content

Commit

Permalink
Ability to replace invalid characters with a chosen one
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakshan-Madushanka committed Aug 19, 2024
1 parent 1f6d45a commit 6471df4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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(
Expand Down
7 changes: 3 additions & 4 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
6 changes: 5 additions & 1 deletion tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/\<<z//'));
$this->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
Expand Down
7 changes: 6 additions & 1 deletion tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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/\<<z//')->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()
Expand Down

0 comments on commit 6471df4

Please sign in to comment.