From 6b4c1bd1bf35709931ac66ebbe162a159bea8153 Mon Sep 17 00:00:00 2001 From: sedhossein Date: Fri, 4 Sep 2020 14:11:41 +0430 Subject: [PATCH] add tests and bug fixing --- src/Pregex.php | 52 ++++++++++---- tests/PregexTest.php | 167 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 205 insertions(+), 14 deletions(-) diff --git a/src/Pregex.php b/src/Pregex.php index d3ed585..51c8f16 100644 --- a/src/Pregex.php +++ b/src/Pregex.php @@ -10,11 +10,17 @@ class Pregex implements PersianValidator { - private static $persian_number = '\x{06F0}-\x{06F9}'; + private static $persian_numbers = '\x{06F0}-\x{06F9}'; private static $arabic_numbers = '\x{0660}-\x{0669}'; - private static $persian_alphabets = '\x0600-\x06FF'; + private static $persian_alphabets = '\x{0621}-\x{06CC}'; + + private static $persian_text = '\x{0600}-\x{06FF}'; + + private static $arabic_common_chars = "\x{0629}\x{0643}\x{0649}-\x{064B}\x{064D}\x{06D5}"; + + private static $spaces = '\x{0020}\x{2000}-\x{200F}\x{2028}-\x{202F}'; private static $banks_names = [ 'bmi' => '603799', @@ -43,11 +49,13 @@ class Pregex implements PersianValidator 'karafarinbank' => '627488', ]; - private static $nameLimit = 40; + public static $nameMaxLimit = 60; + + public static $nameMinLimit = 3; public function IsPersianNumber(string $number): bool { - return (bool)preg_match("/(^[" . self::$persian_number . "]+$)/u", $number); + return (bool)preg_match("/(^[" . self::$persian_numbers . "]+$)/u", $number); } public function IsArabicNumber(string $number): bool @@ -59,7 +67,7 @@ public function IsPersianOrArabicNumber(string $number): bool { return (bool)preg_match("/(^[" . self::$arabic_numbers . - self::$persian_number . + self::$persian_numbers . "]+$)/u", $number); } @@ -157,26 +165,46 @@ public function IsPostalCode(string $value): bool public function IsPersianText(string $value): bool { - return (bool)preg_match("/^[\x{600}-\x{6FF}\x{200c}\x{064b}\x{064d}\x{064c}\x{064e}\x{064f}\x{0650}\x{0651}\x{002E}\s]+$/u", $value); + return (bool)preg_match("/^[" . + self::$persian_text + . "\x{200c}\x{064b}\x{064d}\x{064c}\x{064e}\x{064f}\x{0650}\x{0651}\x{002E}" . + "\s]+$/u", $value); } - public function IsPersianName(string $name): bool + public function IsPersianAlphabet(string $chars): bool { - return $this->IsPersianAlphabet($name) && strlen($name) < self::$nameLimit; + return (bool)preg_match("/(^[" . + self::$arabic_common_chars . + self::$persian_alphabets . + self::$spaces . + "]+$)/u", $chars); } - public function IsPersianAlphabet(string $chars): bool + public function IsPersianName(string $name): bool { - return (bool)preg_match("/^[\u0600-\u06FF\s]+$/u", $chars); + $nameLen = strlen($name); + + return $this->IsPersianAlphabet($name) && + $nameLen <= self::$nameMaxLimit && + $nameLen >= self::$nameMinLimit; } public function IsWithoutPersianAlphabet(string $value): bool { - return !$this->IsPersianAlphabet($value); + $hasPersianChar = (bool)preg_match("/[" . + self::$persian_text . + "]/u", $value); + + return !$hasPersianChar; } public function IsWithoutNumber(string $value): bool { - return (bool)preg_match("/[" . self::$persian_number . self::$arabic_numbers . "]$/u", $value); + $hasPersianNumber = (bool)preg_match("/([" . + self::$persian_numbers . + self::$arabic_numbers . + "]+)/u", $value); + + return !$hasPersianNumber; } } diff --git a/tests/PregexTest.php b/tests/PregexTest.php index ea1f853..135297e 100644 --- a/tests/PregexTest.php +++ b/tests/PregexTest.php @@ -312,7 +312,7 @@ public function ValidPostalCode(): array public function InvalidPostalCode(): array { return [ - ["123456789"], ["00000--00000"], ["asd"], ["9999999999999999"], + ["123456789"], ["00000--00000"], ["asd"], ["9999999999999999"], [""], ]; } @@ -354,6 +354,7 @@ public function valid_persian_texts(): array public function invalid_persian_texts(): array { return [ + [''], ['persian finglish'], ['1234 اعدادی فارسی اند'], ['٠56اعدادی عربی-فارسی نیستند'], @@ -363,5 +364,167 @@ public function invalid_persian_texts(): array ]; } - // TODO: tests + // ============================== Persian alphabets validations ============================== + + /** + * @dataProvider valid_persian_alphabets + */ + public function test_valid_persian_alphabet(string $string) + { + $this->assertEquals(true, (new Pregex)->IsPersianAlphabet($string)); + } + + /** + * @dataProvider invalid_persian_alphabets + */ + public function test_invalid_persian_alphabet(string $string) + { + $this->assertEquals(false, (new Pregex)->IsPersianAlphabet($string)); + } + + public function valid_persian_alphabets(): array + { + return [ + ['ح'], + ['ەيىكًٍ'], + ['گچپژ'], + ['بسم الله'], + ['یک متن بسیار ساده'], + ['تست با فاصله و نیم‌فاصله '], + ['ءآأؤإئابتثجحخدذرزسشصضطظعغفقلمنهوَُِّٕپچژکگھی'], + ['قطعاً همه مئمنیم درّ گران بهاییْ هستندُ جهتِ تستیـ بهتر'], + ]; + } + + public function invalid_persian_alphabets(): array + { + return [ + [''], + ['۱۲۳'], + ['،؛؟ـ٪٫٬'], + ['ممد ۱۲۳ ممد'], + ['.'], ['!'], ['#'], ['﷼'], + ['%'], ['&'], ['*'], ['^'], ['?'], + ]; + } + + // ============================== Name validations ============================== + + /** + * @dataProvider valid_persian_names + */ + public function test_valid_persian_name(string $string) + { + $len = strlen($string); + + $this->assertLessThanOrEqual(Pregex::$nameMaxLimit, $len); + $this->assertGreaterThan(Pregex::$nameMinLimit, $len); + $this->assertEquals(true, (new Pregex)->IsPersianName($string), "names"); + } + + /** + * @dataProvider invalid_persian_names + */ + public function test_invalid_persian_name(string $string) + { + $this->assertEquals(false, (new Pregex)->IsPersianName($string)); + } + + public function valid_persian_names(): array + { + return [ + ['سدحسین'], + ['علی ممد‌پور'], + ['سید حسین حسینی'], + ['سید محمد ابوالقاسم اصغر نژاد علی'], + ]; + } + + public function invalid_persian_names(): array + { + return [ + ['م'], [''], ['ممد؟'], ['ممد. ممدی'], ['ممد با عدد۲'], + ['ممد مممدممد مممدممد مممدممد مممدممد مممدممد مممدممد مممدممد مممدممد مممدممد مممدممد مممدممد مممدممد مممد ممدی'], + ]; + } + + // ============================== Without Persian Alphabet validations ============================== + + /** + * @dataProvider test_invalid_without_persian_alphabet + */ + public function test_valid_without_persian_alphabet(string $string) + { + $this->assertEquals(true, (new Pregex)->IsWithoutPersianAlphabet($string)); + } + + /** + * @dataProvider invalid_without_persian_alphabets + */ + public function test_invalid_without_persian_alphabet(string $string) + { + $this->assertEquals(false, (new Pregex)->IsWithoutPersianAlphabet($string)); + } + + public function valid_without_persian_alphabets(): array + { + return [ + ['We wanna get an english text as an input without any persian or arabic alphabets'], + ['It could consider all of numbers, chars, punctuation marks'], + ['Every english letter and char is valid, like: 123, 0912'], + ['Or marks like: ? ! . # @ and so - on ...'], + ['":)))"'], + ]; + } + + public function invalid_without_persian_alphabets(): array + { + return [ + ['And persian char between english text اینجا.'], + ['م'], ['Or persian punctuation marks like: ؟'], + ['Or persian numbers: ۱۲۳۴۵۶۷۸۹'], + ['Also arabic numbers: ١٠٢٣٤٥٦٧٨٩'], + ['یا یک متن فارسی تمام'], + ]; + } + + // ============================== Without Persian Number validations ============================== + + /** + * @dataProvider valid_without_persian_numbers + */ + public function test_valid_without_persian_number(string $string) + { + $this->assertEquals(true, (new Pregex)->IsWithoutNumber($string)); + } + + /** + * @dataProvider invalid_without_persian_numbers + */ + public function test_invalid_without_persian_number(string $string) + { + $this->assertEquals(false, (new Pregex)->IsWithoutNumber($string)); + } + + public function valid_without_persian_numbers(): array + { + return [ + ['The long text with lot of marks. Right? ok! Lets go, I\'m ready ;)'], + ['And also lot of numbers: 123, 312, 0'], + ['حتی متن فارسی بلند و طویلی که. اماده؟ هر جیزی! هست: باشه٪'], + ['عدد انگلیسی هم خوبه:‌123'], + ['678'], + ]; + } + + public function invalid_without_persian_numbers(): array + { + return [ + ['Also arabic numbers: ١٠٢٣٤٥٦٧٨٩'], + ['Or persian numbers: ۱۲۳۴۵۶۷۸۹'], + ['اما اینجا عدد داریم ۱۲۳'], + ['١٠٢٣٤٥٦٧٨٩'], + ['۱۲۳۴۵۶۷۸۹'], + ]; + } }