Skip to content

Commit

Permalink
Merge pull request #20 from crazyfactory/prevent-emoji-validator
Browse files Browse the repository at this point in the history
feat(latin validator): exclude c0 latin chars
  • Loading branch information
turbo8p authored Dec 1, 2021
2 parents f33acc7 + bae722c commit 3619681
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/LatinCharValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@

class LatinCharValidator
{
/**
* exclude C0 controls (basic latin): https://unicode-table.com/en/blocks/basic-latin/
*
* @param string $txt
*
* @return bool
*/
public static function isValid(string $txt): bool
{
return empty($txt) || preg_match('/^[\p{Latin}\p{Common}]+$/u', $txt) > 0;
return empty($txt) || (preg_match('/^[\p{Latin}\p{Common}]+$/u', $txt) > 0
&& preg_match('/[\x{0000}-\x{001F}]/u', $txt) == 0);
}
}
32 changes: 32 additions & 0 deletions tests/unit/LatinCharValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,37 @@ public function testIsValid()
$this->assertFalse(LatinCharValidator::isValid('الْأَبْجَدِيَّة الْعَرَبِيَّة‎'));
// Russian chars
$this->assertFalse(LatinCharValidator::isValid('Меня зовут Мандли'));
// Basic Latin C0 controls
$c0 = [
'',
'',
'',
'',
'',
'',
' ',
' ',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
''
];
foreach ($c0 as $item) {
$this->assertFalse(LatinCharValidator::isValid('Hello World ' . $item));
}
}
}

0 comments on commit 3619681

Please sign in to comment.