Skip to content

Commit

Permalink
[9.x] Add Str::lcfirst to support non-ascii characters (vs php's co…
Browse files Browse the repository at this point in the history
…re lcfirst fct) and add DX consistency with ucfirst wrapper (#41384)

* Add the lcfirst function to the Str class

* formatting

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
vpratfr and taylorotwell authored Mar 7, 2022
1 parent b7625b3 commit 52b2b26
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,17 @@ public static function swap(array $map, $subject)
return strtr($subject, $map);
}

/**
* Make a string's first character lowercase.
*
* @param string $string
* @return string
*/
public static function lcfirst($string)
{
return static::lower(static::substr($string, 0, 1)).static::substr($string, 1);
}

/**
* Make a string's first character uppercase.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,16 @@ public function rtrim($characters = null)
return new static(rtrim(...array_merge([$this->value], func_get_args())));
}

/**
* Make a string's first character lowercase.
*
* @return static
*/
public function lcfirst()
{
return new static(Str::lcfirst($this->value));
}

/**
* Make a string's first character uppercase.
*
Expand Down
8 changes: 8 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,14 @@ public function testSubstrReplace()
$this->assertSame('Laravel – The PHP Framework for Web Artisans', Str::substrReplace('Laravel Framework', '– The PHP Framework for Web Artisans', 8));
}

public function testLcfirst()
{
$this->assertSame('laravel', Str::lcfirst('Laravel'));
$this->assertSame('laravel framework', Str::lcfirst('Laravel framework'));
$this->assertSame('мама', Str::lcfirst('Мама'));
$this->assertSame('мама мыла раму', Str::lcfirst('Мама мыла раму'));
}

public function testUcfirst()
{
$this->assertSame('Laravel', Str::ucfirst('laravel'));
Expand Down

0 comments on commit 52b2b26

Please sign in to comment.