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

Attempt to fix issue #141. #192

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
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"]));
}

}
}