Merge branch 'rupay-card-validation' into 2.2

This commit is contained in:
Alexandre Gomes Gaigalas 2023-02-13 18:53:04 -03:00
commit 8e345b798a
3 changed files with 18 additions and 7 deletions

View file

@ -16,6 +16,7 @@ v::creditCard('Discover')->validate('6011000990139424'); // true
v::creditCard('JCB')->validate('3566002020360505'); // true
v::creditCard('Mastercard')->validate('5376747397208720'); // true
v::creditCard('Visa')->validate('4024007153361885'); // true
v::creaditCard('RuPay')->validate('6062973831636410') // true
```
The current supported brands are:
@ -26,6 +27,7 @@ The current supported brands are:
- JCB (`'JCB'` or `CreditCard::JCB`)
- Mastercard (`'American_Express'` or `CreditCard::MASTERCARD`)
- Visa (`'Visa'` or `CreditCard::VISA`)
- RuPay (`'RuPay'` or `CreditCard::RUPAY`)
It ignores any non-numeric characters, use [Digit](Digit.md),
[NoWhitespace](NoWhitespace.md), or [Regex](Regex.md) when appropriate.
@ -40,12 +42,13 @@ v::digit()->creditCard()->validate('5376747397208720'); // true
## Changelog
Version | Description
--------|-------------
1.1.0 | Allow the define credit card brand
0.3.9 | Created
| Version | Description |
| ------- | ---------------------------------- |
| 1.1.0 | Allow the define credit card brand |
| 0.3.9 | Created |
---
***
See also:
- [Decimal](Decimal.md)

View file

@ -31,6 +31,7 @@ use function sprintf;
* @author Jean Pimentel <jeanfap@gmail.com>
* @author Nick Lombard <github@jigsoft.co.za>
* @author William Espindola <oi@williamespindola.com.br>
* @author Rakshit Arora <rakshit087@gmail.com>
*/
final class CreditCard extends AbstractRule
{
@ -48,6 +49,8 @@ final class CreditCard extends AbstractRule
public const VISA = 'Visa';
public const RUPAY = 'RuPay';
private const BRAND_REGEX_LIST = [
self::ANY => '/^[0-9]+$/',
self::AMERICAN_EXPRESS => '/^3[47]\d{13}$/',
@ -56,6 +59,7 @@ final class CreditCard extends AbstractRule
self::JCB => '/^(?:2131|1800|35\d{3})\d{11}$/',
self::MASTERCARD => '/(5[1-5]|2[2-7])\d{14}$/',
self::VISA => '/^4\d{12}(?:\d{3})?$/',
self::RUPAY => '/^6(?!011)(?:0[0-9]{14}|52[12][0-9]{12})$/',
];
/**
@ -99,4 +103,4 @@ final class CreditCard extends AbstractRule
return preg_match(self::BRAND_REGEX_LIST[$this->brand], $input) > 0;
}
}
}

View file

@ -25,6 +25,7 @@ use Respect\Validation\Test\RuleTestCase;
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Jean Pimentel <jeanfap@gmail.com>
* @author William Espindola <oi@williamespindola.com.br>
* @author Rakshit Arora <rakshit087@gmail.com>
*/
final class CreditCardTest extends RuleTestCase
{
@ -54,6 +55,7 @@ final class CreditCardTest extends RuleTestCase
$jcb = new CreditCard(CreditCard::JCB);
$master = new CreditCard(CreditCard::MASTERCARD);
$visa = new CreditCard(CreditCard::VISA);
$rupay = new CreditCard(CreditCard::RUPAY);
return [
[$general, 5555444433331111], // MasterCard 5 BIN Range
@ -73,6 +75,8 @@ final class CreditCardTest extends RuleTestCase
[$discover, '6011000990139424'],
[$general, '3566002020360505'], // JBC
[$jcb, '3566002020360505'],
[$general, '6522447005971501'], // RuPay
[$rupay, '6062973831636410'],
];
}
@ -104,4 +108,4 @@ final class CreditCardTest extends RuleTestCase
[$jcb, '38520000023237'], // Diners Club
];
}
}
}