Skip to content
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
10 changes: 9 additions & 1 deletion src/Extensions/TextExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,15 @@ public function words(int $words = 3): string
$words = $this->wordsAsArray($words);

// Remove any uppercase / comma / dots
return strtolower(preg_replace('/[.,]/', '', implode(' ', $words)));
$words = preg_replace('/[.,]/', '', implode(' ', $words));

if (function_exists('mb_strtolower')) {
return mb_strtolower($words);
}

trigger_error('You do not have the `mbstring` extension enabled; FakerPHP might not handle uncommon characters correctly.', E_USER_WARNING);

return strtolower($words);
}

public function sentencesAsArray(int $sentences = 3): array
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/Extensions/TextExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ public function testWordsWithDefaultValue(): void
}
}

public function testWordsLowercasesNonAsciiCharacters(): void
{
$extension = new \Xefi\Faker\Extensions\TextExtension(new \Random\Randomizer());
(new ReflectionClass($extension))->getProperty('paragraphs')->setValue($extension, [[['Ёлка', 'Écrit', 'Über.']]]);

$this->assertEqualsCanonicalizing(['ёлка', 'écrit', 'über'], explode(' ', $extension->words(3)));
}

public static function wordsProvider()
{
return [
Expand Down
Loading