Skip to content

Commit

Permalink
Merge pull request #3 from sedhossein/dev
Browse files Browse the repository at this point in the history
Add new validations
  • Loading branch information
sedhossein authored Sep 4, 2020
2 parents 94bed33 + 6b4c1bd commit 2d04f15
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 29 deletions.
27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@
- [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.


### Requirements
- composer
- PHP 7.2 >=


### How to use
Pregex prepared the bellow methods list to give you all you need for your validations.

Expand Down Expand Up @@ -73,7 +77,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
Expand All @@ -87,12 +91,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;
Expand All @@ -107,15 +105,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).

8 changes: 8 additions & 0 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 18 additions & 10 deletions src/PersianValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +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;

public function IsPersianName(string $name): bool;

public function IsPersianAlphabet(string $chars): bool;

public function IsWithoutPersianAlphabet(string $value): bool;

public function IsWithoutNumber(string $value): bool;
}
68 changes: 60 additions & 8 deletions src/Pregex.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@

class Pregex implements PersianValidator
{
private static $persian_number_codepoints = '\x{06F0}-\x{06F9}';
private static $persian_numbers = '\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 = '\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',
Expand Down Expand Up @@ -41,27 +49,31 @@ class Pregex implements PersianValidator
'karafarinbank' => '627488',
];

public static $nameMaxLimit = 60;

public static $nameMinLimit = 3;

public function IsPersianNumber(string $number): bool
{
return (bool)preg_match("/(^[" . self::$persian_number_codepoints . "]+$)/u", $number);
return (bool)preg_match("/(^[" . self::$persian_numbers . "]+$)/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_numbers .
"]+$)/u", $number);
}

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
Expand Down Expand Up @@ -153,6 +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 IsPersianAlphabet(string $chars): bool
{
return (bool)preg_match("/(^[" .
self::$arabic_common_chars .
self::$persian_alphabets .
self::$spaces .
"]+$)/u", $chars);
}

public function IsPersianName(string $name): bool
{
$nameLen = strlen($name);

return $this->IsPersianAlphabet($name) &&
$nameLen <= self::$nameMaxLimit &&
$nameLen >= self::$nameMinLimit;
}

public function IsWithoutPersianAlphabet(string $value): bool
{
$hasPersianChar = (bool)preg_match("/[" .
self::$persian_text .
"]/u", $value);

return !$hasPersianChar;
}

public function IsWithoutNumber(string $value): bool
{
$hasPersianNumber = (bool)preg_match("/([" .
self::$persian_numbers .
self::$arabic_numbers .
"]+)/u", $value);

return !$hasPersianNumber;
}
}
Loading

0 comments on commit 2d04f15

Please sign in to comment.