Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add APA style title helper #49572

Merged
merged 10 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -1238,7 +1238,7 @@ public static function upper($value)
}

/**
* Convert the given string to title case.
* Convert the given string to proper case.
*
* @param string $value
* @return string
Expand All @@ -1249,7 +1249,7 @@ public static function title($value)
}

/**
* Convert the given string to title case for each word.
* Convert the given string to proper case for each word.
*
* @param string $value
* @return string
Expand All @@ -1267,6 +1267,52 @@ public static function headline($value)
return implode(' ', array_filter(explode('_', $collapsed)));
}

/**
* Convert the given string to APA-style title case.
*
* See: https://apastyle.apa.org/style-grammar-guidelines/capitalization/title-case
*
* @param string $value
* @return string
*/
public static function apa($value)
{
$minorWords = [
'and', 'as', 'but', 'for', 'if', 'nor', 'or', 'so', 'yet', 'a', 'an',
'the', 'at', 'by', 'for', 'in', 'of', 'off', 'on', 'per', 'to', 'up', 'via'
];

$endPunctuation = ['.', '!', '?', ':', '—', ','];

$words = preg_split('/\s+/', $value, -1, PREG_SPLIT_NO_EMPTY);

$words[0] = ucfirst(mb_strtolower($words[0]));

for ($i = 0; $i < count($words); $i++) {
$lowercaseWord = mb_strtolower($words[$i]);

if (str_contains($lowercaseWord, '-')) {
$hyphenatedWords = explode('-', $lowercaseWord);

$hyphenatedWords = array_map(function ($part) use ($minorWords) {
return (in_array($part, $minorWords) && mb_strlen($part) <= 3) ? $part : ucfirst($part);
}, $hyphenatedWords);

$words[$i] = implode('-', $hyphenatedWords);
} else {
if (in_array($lowercaseWord, $minorWords) &&
mb_strlen($lowercaseWord) <= 3 &&
! ($i === 0 || in_array(mb_substr($words[$i - 1], -1), $endPunctuation))) {
$words[$i] = $lowercaseWord;
} else {
$words[$i] = ucfirst($lowercaseWord);
}
}
}

return implode(' ', $words);
}

/**
* Get the singular form of an English word.
*
Expand Down
28 changes: 19 additions & 9 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ public function upper()
}

/**
* Convert the given string to title case.
* Convert the given string to proper case.
*
* @return static
*/
Expand All @@ -798,25 +798,35 @@ public function title()
}

/**
* Transliterate a string to its closest ASCII representation.
* Convert the given string to proper case for each word.
*
* @param string|null $unknown
* @param bool|null $strict
* @return static
*/
public function transliterate($unknown = '?', $strict = false)
public function headline()
{
return new static(Str::transliterate($this->value, $unknown, $strict));
return new static(Str::headline($this->value));
}

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

/**
* Transliterate a string to its closest ASCII representation.
*
* @param string|null $unknown
* @param bool|null $strict
* @return static
*/
public function transliterate($unknown = '?', $strict = false)
{
return new static(Str::transliterate($this->value, $unknown, $strict));
}

/**
Expand Down
31 changes: 31 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,37 @@ public function testStringHeadline()
$this->assertSame('Orwell 1984', Str::headline(' orwell_- 1984 '));
}

public function testStringApa()
{
$this->assertSame('Tom and Jerry', Str::apa('tom and jerry'));
$this->assertSame('Tom and Jerry', Str::apa('TOM AND JERRY'));
$this->assertSame('Tom and Jerry', Str::apa('Tom And Jerry'));

$this->assertSame('Back to the Future', Str::apa('back to the future'));
$this->assertSame('Back to the Future', Str::apa('BACK TO THE FUTURE'));
$this->assertSame('Back to the Future', Str::apa('Back To The Future'));

$this->assertSame('This, Then That', Str::apa('this, then that'));
$this->assertSame('This, Then That', Str::apa('THIS, THEN THAT'));
$this->assertSame('This, Then That', Str::apa('This, Then That'));

$this->assertSame('Bond. James Bond.', Str::apa('bond. james bond.'));
$this->assertSame('Bond. James Bond.', Str::apa('BOND. JAMES BOND.'));
$this->assertSame('Bond. James Bond.', Str::apa('Bond. James Bond.'));

$this->assertSame('Self-Report', Str::apa('self-report'));
$this->assertSame('Self-Report', Str::apa('Self-report'));
$this->assertSame('Self-Report', Str::apa('SELF-REPORT'));

$this->assertSame('As the World Turns, So Are the Days of Our Lives', Str::apa('as the world turns, so are the days of our lives'));
$this->assertSame('As the World Turns, So Are the Days of Our Lives', Str::apa('AS THE WORLD TURNS, SO ARE THE DAYS OF OUR LIVES'));
$this->assertSame('As the World Turns, So Are the Days of Our Lives', Str::apa('As The World Turns, So Are The Days Of Our Lives'));

$this->assertSame('To Kill a Mockingbird', Str::apa('to kill a mockingbird'));
$this->assertSame('To Kill a Mockingbird', Str::apa('TO KILL A MOCKINGBIRD'));
$this->assertSame('To Kill a Mockingbird', Str::apa('To Kill A Mockingbird'));
}

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