From 9f592e3b8bb9f077f5ae9010ca52ae67453c8b3b Mon Sep 17 00:00:00 2001 From: lalita Date: Wed, 1 Dec 2021 10:01:01 +0700 Subject: [PATCH 1/4] chore(latinValidator): prevent emoji --- src/LatinCharValidator.php | 2 +- tests/unit/LatinCharValidatorTest.php | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/LatinCharValidator.php b/src/LatinCharValidator.php index 77c2089..3d96dce 100644 --- a/src/LatinCharValidator.php +++ b/src/LatinCharValidator.php @@ -6,6 +6,6 @@ class LatinCharValidator { 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('/[^\w\x{0080}-\x{FFFF}]+$/u', $txt) == 0 ); } } diff --git a/tests/unit/LatinCharValidatorTest.php b/tests/unit/LatinCharValidatorTest.php index 8d2f167..d570e1c 100644 --- a/tests/unit/LatinCharValidatorTest.php +++ b/tests/unit/LatinCharValidatorTest.php @@ -54,5 +54,9 @@ public function testIsValid() $this->assertFalse(LatinCharValidator::isValid('الْأَبْجَدِيَّة الْعَرَبِيَّة‎')); // Russian chars $this->assertFalse(LatinCharValidator::isValid('Меня зовут Мандли')); + // moji + $this->assertFalse(LatinCharValidator::isValid('Hello World 😀')); + // moji + $this->assertFalse(LatinCharValidator::isValid('Hello World ')); } } From 54fabcc9034884b855eb832f303e1bccea77fed1 Mon Sep 17 00:00:00 2001 From: lalita Date: Wed, 1 Dec 2021 15:26:02 +0700 Subject: [PATCH 2/4] chore(latinValidator): prevent C0 controls --- src/LatinCharValidator.php | 8 +++++++- tests/unit/LatinCharValidatorTest.php | 4 +--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/LatinCharValidator.php b/src/LatinCharValidator.php index 3d96dce..126f030 100644 --- a/src/LatinCharValidator.php +++ b/src/LatinCharValidator.php @@ -4,8 +4,14 @@ 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 && preg_match('/[^\w\x{0080}-\x{FFFF}]+$/u', $txt) == 0 ); + return empty($txt) || (preg_match('/^[\p{Latin}\p{Common}]+$/u', $txt) > 0 && preg_match('/[\x{0000}-\x{001F}]/u', $txt) == 0 ); } } diff --git a/tests/unit/LatinCharValidatorTest.php b/tests/unit/LatinCharValidatorTest.php index d570e1c..1ed6032 100644 --- a/tests/unit/LatinCharValidatorTest.php +++ b/tests/unit/LatinCharValidatorTest.php @@ -54,9 +54,7 @@ public function testIsValid() $this->assertFalse(LatinCharValidator::isValid('الْأَبْجَدِيَّة الْعَرَبِيَّة‎')); // Russian chars $this->assertFalse(LatinCharValidator::isValid('Меня зовут Мандли')); - // moji - $this->assertFalse(LatinCharValidator::isValid('Hello World 😀')); - // moji + // Basic Latin C0 controls $this->assertFalse(LatinCharValidator::isValid('Hello World ')); } } From 7d2d562260f96bb668dd7e77d50e517044e18aa6 Mon Sep 17 00:00:00 2001 From: Turbo Date: Wed, 1 Dec 2021 17:24:27 +0700 Subject: [PATCH 3/4] test: add more c0 char test cases --- tests/unit/LatinCharValidatorTest.php | 32 ++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/tests/unit/LatinCharValidatorTest.php b/tests/unit/LatinCharValidatorTest.php index 1ed6032..e5f2add 100644 --- a/tests/unit/LatinCharValidatorTest.php +++ b/tests/unit/LatinCharValidatorTest.php @@ -55,6 +55,36 @@ public function testIsValid() // Russian chars $this->assertFalse(LatinCharValidator::isValid('Меня зовут Мандли')); // Basic Latin C0 controls - $this->assertFalse(LatinCharValidator::isValid('Hello World ')); + $c0 = [ + '', + '', + '', + '', + '', + '', + ' ', + ' ', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '' + ]; + foreach ($c0 as $item) { + $this->assertFalse(LatinCharValidator::isValid('Hello World ' . $item)); + } } } From bae722cc690f37e4372bc8cd6946a08146044c7c Mon Sep 17 00:00:00 2001 From: Turbo Date: Wed, 1 Dec 2021 17:24:33 +0700 Subject: [PATCH 4/4] style: format --- src/LatinCharValidator.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/LatinCharValidator.php b/src/LatinCharValidator.php index 126f030..952fd22 100644 --- a/src/LatinCharValidator.php +++ b/src/LatinCharValidator.php @@ -6,12 +6,14 @@ class LatinCharValidator { /** * exclude C0 controls (basic latin): https://unicode-table.com/en/blocks/basic-latin/ - * @param string $txt + * + * @param string $txt * * @return bool */ public static function isValid(string $txt): bool { - return empty($txt) || (preg_match('/^[\p{Latin}\p{Common}]+$/u', $txt) > 0 && preg_match('/[\x{0000}-\x{001F}]/u', $txt) == 0 ); + return empty($txt) || (preg_match('/^[\p{Latin}\p{Common}]+$/u', $txt) > 0 + && preg_match('/[\x{0000}-\x{001F}]/u', $txt) == 0); } }