Skip to content

Commit

Permalink
[10.x] Fix apa on non ASCII characters (#51428)
Browse files Browse the repository at this point in the history
* fix apa on accented words

* fix code style

* add more tests to apa with more characters

* fix code style

* remove empty line
  • Loading branch information
faissaloux authored May 16, 2024
1 parent ee3a1aa commit 263f389
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -1305,22 +1305,23 @@ 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',
'et', 'ou', 'un', 'une', 'la', 'le', 'les', 'de', 'du', 'des', 'par', 'à',
];

$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);
return (in_array($part, $minorWords) && mb_strlen($part) <= 3)
? $part
: mb_strtoupper(mb_substr($part, 0, 1)).mb_substr($part, 1);
}, $hyphenatedWords);

$words[$i] = implode('-', $hyphenatedWords);
Expand All @@ -1330,7 +1331,7 @@ public static function apa($value)
! ($i === 0 || in_array(mb_substr($words[$i - 1], -1), $endPunctuation))) {
$words[$i] = $lowercaseWord;
} else {
$words[$i] = ucfirst($lowercaseWord);
$words[$i] = mb_strtoupper(mb_substr($lowercaseWord, 0, 1)).mb_substr($lowercaseWord, 1);
}
}
}
Expand Down

0 comments on commit 263f389

Please sign in to comment.