Skip to content

Commit

Permalink
Merge branch 'bugfix-141'
Browse files Browse the repository at this point in the history
  • Loading branch information
hktang committed May 29, 2024
2 parents 75084f5 + 22da1b8 commit 22dfaf0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/Util/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class StringHelper

const CONJUNCTIONS = [
'and', // en
"et", // fr
"et", // fr
'nor', // en
'or', // en
'ou', // fr
Expand Down Expand Up @@ -176,7 +176,7 @@ public static function capitalizeForTitle($titleString)
$wordArray = preg_split($pattern, $titleString); //explode(" ", $titleString);

$wordList = new ArrayList(...$wordArray);
return $wordList
$convertedString = $wordList
->map(function(string $word) {
$wordParts = explode("-", $word);
if (count($wordParts) > 1) {
Expand All @@ -187,8 +187,36 @@ public static function capitalizeForTitle($titleString)
$word = implode("-", $casedWordParts);
}
return StringHelper::keepLowerCase($word) ? $word : StringHelper::mb_ucfirst($word);
})
->collectToString($delimiter);
})->collectToString($delimiter);

return self::replaceDelimeters($titleString, $convertedString);
}

/**
* Compare two strings, return a new string that uses the characters of the
* first string and the capitalizations from the second string.
*
* For example, given the strings "hello worlD" and "Hello/WORLD", the
* function will return "Hello WORLD".
*
* @param string $original
* @param string $converted
*
* @return string
*/
private static function replaceDelimeters(string $original, string $converted): string {
$result = "";

$originalChars = str_split($original);
$convertedChars = str_split($converted);

foreach($originalChars as $i => $char) {
$result .= mb_strtolower($char) === mb_strtolower($convertedChars[$i])
? $convertedChars[$i]
: $char;
}

return $result;
}

/**
Expand All @@ -202,7 +230,7 @@ public static function keepLowerCase($word)
// because of static, compilation is done only one time
if ($lcDic === null) {
$lcDic = array_flip(array_merge(
self::PREPOSITIONS,
self::PREPOSITIONS,
self::ARTICLES,
self::ADVERBS,
self::CONJUNCTIONS,
Expand Down
22 changes: 22 additions & 0 deletions tests/src/Util/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,26 @@ public function testIsCyrillicString()
static::assertFalse(StringHelper::isCyrillicString("Nicht"));
static::assertFalse(StringHelper::isCyrillicString("пеpеводчиком"));
}

public function testCapitalizeForTitle(){

$testItems = [
["test"=> "Hello/wORLD", "expected" => "Hello/WORLD"],
["test"=> "Title/with a slash", "expected" => "Title/With a Slash"],
["test"=> "Title/with two/ slashes", "expected" => "Title/With Two/ Slashes"],
["test"=> "///Title/with Consecutive/ slashes", "expected" => "///Title/With Consecutive/ Slashes"],
["test"=> "/\/|/Title/with wierd/ slashes", "expected" => "/\/|/Title/With Wierd/ Slashes"],
["test"=> "?/\/|/Title/with Consecutive/ slashes", "expected" => "?/\/|/Title/With Consecutive/ Slashes"],
["test"=> "?/\/|/Title/with /Consecutive/ slashes", "expected" => "?/\/|/Title/With /Consecutive/ Slashes"],
["test"=> "?/\/|/Title/with Con/secutive slashes", "expected" => "?/\/|/Title/With Con/Secutive Slashes"],
["test"=> "braintree/Braintree_php", "expected" => "Braintree/Braintree_php"], // issue105
["test"=> "T7/G7 task force", "expected" => "T7/G7 Task Force"], // issue141
//["test"=> "ö/ö örb å ø", "expected" => "Ö/Ö Örb Å Ø"], // Doesn't work
];

foreach($testItems as $item){
static::assertEquals($item["expected"], StringHelper::capitalizeForTitle($item["test"]));
}

}
}

0 comments on commit 22dfaf0

Please sign in to comment.