Skip to content

Commit

Permalink
Merge pull request #21 from crazyfactory/Remove_whitespaces_tabs_and_…
Browse files Browse the repository at this point in the history
…new-lines-from-latin_validator

fix(latin validator): remove whitespaces tabs and new-lines
  • Loading branch information
turbo8p authored Dec 7, 2021
2 parents 3619681 + 07345f9 commit 1745997
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/LatinCharValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class LatinCharValidator
*/
public static function isValid(string $txt): bool
{
// remove whitespaces tabs and new-lines
$txt = preg_replace('/\s+/S', ' ', $txt);

return empty($txt) || (preg_match('/^[\p{Latin}\p{Common}]+$/u', $txt) > 0
&& preg_match('/[\x{0000}-\x{001F}]/u', $txt) == 0);
}
Expand Down
17 changes: 15 additions & 2 deletions tests/unit/LatinCharValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ public function testIsValid()
'',
'',
'',
' ',
' ',
'',
'',
'',
Expand All @@ -86,5 +84,20 @@ public function testIsValid()
foreach ($c0 as $item) {
$this->assertFalse(LatinCharValidator::isValid('Hello World ' . $item));
}

$exceptions = [
' ', // tab
' ', // newline
"\n",
"\r\n",
"\r"
];

foreach ($exceptions as $item) {
$this->assertTrue(LatinCharValidator::isValid('Hello World ' . $item));
}

$string = "test3 theguy\nfubar 5\nGelbe Tür!2\n154 5thfdd test\t\ttab";
$this->assertTrue(LatinCharValidator::isValid($string));
}
}

0 comments on commit 1745997

Please sign in to comment.