Skip to content

Commit

Permalink
[8.x] Add Str::headline() (#39174)
Browse files Browse the repository at this point in the history
* Add `Str::studlyWords()`

* Rename method to `headline` and alter logic to properly handle title test cases

* Add more assertions

Co-authored-by: Steve Bauman <6421846+stevebauman@users.noreply.github.com>
  • Loading branch information
stevebauman and stevebauman authored Oct 18, 2021
1 parent fea2e54 commit 6791576
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,27 @@ public static function title($value)
return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
}

/**
* Convert the given string to title case for each word.
*
* @param string $value
* @return string
*/
public static function headline($value)
{
$parts = explode('_', static::replace(' ', '_', $value));

if (count($parts) > 1) {
$parts = array_map([static::class, 'title'], $parts);
}

$studly = static::studly(implode($parts));

$words = preg_split('/(?=[A-Z])/', $studly, -1, PREG_SPLIT_NO_EMPTY);

return implode(' ', $words);
}

/**
* Get the singular form of an English word.
*
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 @@ -596,6 +596,16 @@ public function title()
return new static(Str::title($this->value));
}

/**
* Convert the given string to title case for each word.
*
* @return static
*/
public function headline()
{
return new static(Str::headline($this->value));
}

/**
* Get the singular form of an English word.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ public function testStringTitle()
$this->assertSame('Jefferson Costella', Str::title('jefFErson coSTella'));
}

public function testStringHeadline()
{
$this->assertSame('Jefferson Costella', Str::headline('jefferson costella'));
$this->assertSame('Jefferson Costella', Str::headline('jefFErson coSTella'));
$this->assertSame('Jefferson Costella Uses Laravel', Str::headline('jefferson_costella uses-_Laravel'));
$this->assertSame('Jefferson Costella Uses Laravel', Str::headline('jefferson_costella uses__Laravel'));

$this->assertSame('Laravel P H P Framework', Str::headline('laravel_p_h_p_framework'));
$this->assertSame('Laravel P H P Framework', Str::headline('laravel _p _h _p _framework'));
$this->assertSame('Laravel Php Framework', Str::headline('laravel_php_framework'));
$this->assertSame('Laravel Ph P Framework', Str::headline('laravel-phP-framework'));
$this->assertSame('Laravel Php Framework', Str::headline('laravel -_- php -_- framework '));

$this->assertSame('Foo Bar', Str::headline('fooBar'));
$this->assertSame('Foo Bar', Str::headline('foo_bar'));
$this->assertSame('Foo Bar Baz', Str::headline('foo-barBaz'));
$this->assertSame('Foo Bar Baz', Str::headline('foo-bar_baz'));
}

public function testStringWithoutWordsDoesntProduceError()
{
$nbsp = chr(0xC2).chr(0xA0);
Expand Down

0 comments on commit 6791576

Please sign in to comment.