From d1885c8a04f4b5f4e0704088ff4b19e5aa17c0ae Mon Sep 17 00:00:00 2001 From: sedhossein Date: Mon, 24 Aug 2020 22:09:07 +0430 Subject: [PATCH 1/3] just add TODO list --- README.md | 26 ++++++++++++++++++-------- examples/index.php | 8 ++++++++ src/PersianValidator.php | 9 +++++++++ src/Pregex.php | 2 +- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index dd505e0..642dea0 100644 --- a/README.md +++ b/README.md @@ -12,14 +12,17 @@ - [How to use](#how-to-use) - [List of methods](#list-of-methods) - [Installation](#how-to-install) +- [Run Tests](#run-tests) - [TODO list](#todo-list) - [License](#license) + ### Introduction If you having a Persian/Iranian project and need to validate your inputs this light library can help you. Pregex try to make a complete collection of Persian/Iranian validations to make it easy for you. Please kindly feeling free to get in touch with me for any idea you have, or open issue/PR to any bug reporting/fixing. + ### How to use Pregex prepared the bellow methods list to give you all you need for your validations. @@ -67,7 +70,7 @@ function IsCellphone(string $number): bool; ```php function IsIban(string $value): bool; ``` -`IsIban` or also `Sheba` validate Iranian bank Ibans +`IsIban` or also `Sheba` or `International Bank Account Number (IBAN).` validate Iranian bank Ibans --- ```php @@ -81,12 +84,6 @@ function IsCardNumber(string $value): bool; ``` `IsCardNumber` validate Iranian bank card numbers ---- -```php -function IsCardNumber(string $value): bool; -``` -`IsCardNumber` validate Iranian bank card numbers - --- ```php function IsPostalCode(string $value): bool; @@ -101,15 +98,28 @@ function IsPersianText(string $value): bool; ### How to install Install [Composer](https://getcomposer.org) and run following command in your project's root directory: - ```bash composer require sedhossein/pregex ``` +### Run Tests +After installing [Composer](https://getcomposer.org), Clone Pregex Repo and then go to project path(`cd pregex`). +Now enter: +```bash +composer install +``` +Now you fetch all of package dependencies, and you can run bellow command to run tests: +```bash +./vendor/phpunit/phpunit/phpunit --coverage-html ./build/tests/coverage.html +``` +So after running above command you can see coverage report on `./build/tests/coverage.html` + + ### TODO list: - [ ] Comparing with other libraries to add more features + ### license Pregex is initially created by [Sedhossein](https://sedhossein.dev) and released under the [MIT License](http://opensource.org/licenses/mit-license.php). diff --git a/examples/index.php b/examples/index.php index 20e1fe2..7023dc8 100644 --- a/examples/index.php +++ b/examples/index.php @@ -10,18 +10,26 @@ "Yes, It's a persian or arabic number" : "No, It's not a persian or arabic number"; var_dump($result); // Yes, It's a persian or arabic number + + $result = (new Pregex)->IsPersianOrArabicNumber("123456") ? "Yes, It's a persian or arabic number" : "No, It's not a persian or arabic number"; var_dump($result); // No, It's not a persian or arabic number + + $result = (new Pregex)->IsPersianNumber(implode("", $persianNumbers)) ? "Yes, It's a persian number" : "No, It's not a persian number"; var_dump($result); // Yes, It's a persian number + + $result = (new Pregex)->IsCellphone("09123456789") ? "Yes, It's a persian cellphone" : "No, It's not a persian cellphone"; var_dump($result); // Yes, It's a persian cellphone + + $result = (new Pregex)->IsPersianText("سدحسین هستم") ? "Yes, It's a persian text" : "No, It's not a persian text"; var_dump($result); // Yes, It's a persian text diff --git a/src/PersianValidator.php b/src/PersianValidator.php index a44d003..1a5f81f 100644 --- a/src/PersianValidator.php +++ b/src/PersianValidator.php @@ -23,4 +23,13 @@ function IsCardNumber(string $value): bool; function IsPostalCode(string $value): bool; function IsPersianText(string $value): bool; + + // TODO: + function IsPersianName(string $name): bool; + + function IsPersianAddress(string $address): bool; + + function IsPersianAlphabet(string $chars): bool; + + function IsNonPhone(string $value): bool; } diff --git a/src/Pregex.php b/src/Pregex.php index 9344698..44835ec 100644 --- a/src/Pregex.php +++ b/src/Pregex.php @@ -61,7 +61,7 @@ public function IsPersianOrArabicNumber(string $number): bool public function IsEmail(string $email): bool { - return filter_var($email, FILTER_VALIDATE_EMAIL); + return (bool)filter_var($email, FILTER_VALIDATE_EMAIL); } public function IsCellphone(string $number): bool From dae87d852904c3fd2e9a7cf7b20f121244c8c864 Mon Sep 17 00:00:00 2001 From: sedhossein Date: Thu, 3 Sep 2020 19:29:47 +0430 Subject: [PATCH 2/3] add new methods --- src/PersianValidator.php | 29 ++++++++++++++--------------- src/Pregex.php | 36 ++++++++++++++++++++++++++++++------ tests/PregexTest.php | 7 +++++-- 3 files changed, 49 insertions(+), 23 deletions(-) diff --git a/src/PersianValidator.php b/src/PersianValidator.php index 1a5f81f..0ffa198 100644 --- a/src/PersianValidator.php +++ b/src/PersianValidator.php @@ -4,32 +4,31 @@ interface PersianValidator { - function IsPersianNumber(string $number): bool; + public function IsPersianNumber(string $number): bool; - function IsArabicNumber(string $number): bool; + public function IsArabicNumber(string $number): bool; - function IsPersianOrArabicNumber(string $number): bool; + public function IsPersianOrArabicNumber(string $number): bool; - function IsEmail(string $email): bool; + public function IsEmail(string $email): bool; - function IsCellphone(string $number): bool; + public function IsCellphone(string $number): bool; - function IsIban(string $value): bool; + public function IsIban(string $value): bool; - function IsNationalCode(string $value): bool; + public function IsNationalCode(string $value): bool; - function IsCardNumber(string $value): bool; + public function IsCardNumber(string $value): bool; - function IsPostalCode(string $value): bool; + public function IsPostalCode(string $value): bool; - function IsPersianText(string $value): bool; + public function IsPersianText(string $value): bool; - // TODO: - function IsPersianName(string $name): bool; + public function IsPersianName(string $name): bool; - function IsPersianAddress(string $address): bool; + public function IsPersianAlphabet(string $chars): bool; - function IsPersianAlphabet(string $chars): bool; + public function IsWithoutPersianAlphabet(string $value): bool; - function IsNonPhone(string $value): bool; + public function IsWithoutNumber(string $value): bool; } diff --git a/src/Pregex.php b/src/Pregex.php index 35e0c71..d3ed585 100644 --- a/src/Pregex.php +++ b/src/Pregex.php @@ -10,9 +10,11 @@ class Pregex implements PersianValidator { - private static $persian_number_codepoints = '\x{06F0}-\x{06F9}'; + private static $persian_number = '\x{06F0}-\x{06F9}'; - private static $arabic_numbers_codepoints = '\x{0660}-\x{0669}'; + private static $arabic_numbers = '\x{0660}-\x{0669}'; + + private static $persian_alphabets = '\x0600-\x06FF'; private static $banks_names = [ 'bmi' => '603799', @@ -41,21 +43,23 @@ class Pregex implements PersianValidator 'karafarinbank' => '627488', ]; + private static $nameLimit = 40; + public function IsPersianNumber(string $number): bool { - return (bool)preg_match("/(^[" . self::$persian_number_codepoints . "]+$)/u", $number); + return (bool)preg_match("/(^[" . self::$persian_number . "]+$)/u", $number); } public function IsArabicNumber(string $number): bool { - return (bool)preg_match("/(^[" . self::$arabic_numbers_codepoints . "]+$)/u", $number); + return (bool)preg_match("/(^[" . self::$arabic_numbers . "]+$)/u", $number); } public function IsPersianOrArabicNumber(string $number): bool { return (bool)preg_match("/(^[" . - self::$arabic_numbers_codepoints . - self::$persian_number_codepoints . + self::$arabic_numbers . + self::$persian_number . "]+$)/u", $number); } @@ -155,4 +159,24 @@ 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); } + + public function IsPersianName(string $name): bool + { + return $this->IsPersianAlphabet($name) && strlen($name) < self::$nameLimit; + } + + public function IsPersianAlphabet(string $chars): bool + { + return (bool)preg_match("/^[\u0600-\u06FF\s]+$/u", $chars); + } + + public function IsWithoutPersianAlphabet(string $value): bool + { + return !$this->IsPersianAlphabet($value); + } + + public function IsWithoutNumber(string $value): bool + { + return (bool)preg_match("/[" . self::$persian_number . self::$arabic_numbers . "]$/u", $value); + } } diff --git a/tests/PregexTest.php b/tests/PregexTest.php index 659a7e6..ea1f853 100644 --- a/tests/PregexTest.php +++ b/tests/PregexTest.php @@ -146,7 +146,6 @@ public function InvalidEmails(): array ]; } - // ============================== Cellphone Validations ============================== /** @@ -248,6 +247,7 @@ public function InvalidIBan(): array ["asd"], ["123"], ["000000000000000000000000"], [""] ]; } + // ============================== Card Validations ============================== /** @@ -316,7 +316,6 @@ public function InvalidPostalCode(): array ]; } - // ============================== Persian text/alphabets validations ============================== /** @@ -339,12 +338,14 @@ public function valid_persian_texts(): array { return [ ['گچپژ'], + ['ەيىكًٍ'], ['بسم الله'], ['حروفی همچون ٪هٔيأؤئء'], ['تست با فاصله و نیم‌فاصله '], ['آمار اختلاص چندین٪ کم شده'], ['ویرگول ها ٪هٔيأؤئء٫٬همراهی '], ['۰۱۲۳۴۵۶۷۸۹ اعدادی فارسی اند'], + ['ءآأؤإئابتثجحخدذرزسشصضطظعغفقلمنهوَُِّٕپچژکگھی'], ['قطعاً همه مئمنیم درّ گران بهاییْ هستندُ جهتِ تستیـ بهتر'], ['تست، قواعدی و نگارشی پشت ژرفای ثبت. آسمان دست‌؛ یالا؟'], ]; @@ -361,4 +362,6 @@ public function invalid_persian_texts(): array ['qwتست، قواعدی: و نگارشی پشت ژرفای ثبت.دست‌؛ یالا؟ew'], ]; } + + // TODO: tests } From 6b4c1bd1bf35709931ac66ebbe162a159bea8153 Mon Sep 17 00:00:00 2001 From: sedhossein Date: Fri, 4 Sep 2020 14:11:41 +0430 Subject: [PATCH 3/3] 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: ۱۲۳۴۵۶۷۸۹'], + ['اما اینجا عدد داریم ۱۲۳'], + ['١٠٢٣٤٥٦٧٨٩'], + ['۱۲۳۴۵۶۷۸۹'], + ]; + } }