Skip to content
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ composer.lock

/build
/vendor

/.idea/
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
],
"require": {
"php": ">=5.3.0",
"php": ">=5.6.0",
"lib-pcre": ">=7.3"
},
"require-dev": {
Expand Down
69 changes: 62 additions & 7 deletions src/CreditCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class CreditCard
'cvcLength' => array(3),
'luhn' => true,
),
'mir' => array(
'type' => 'mir',
'pattern' => '/^220[0-4]/',
'length' => array(16),
'cvcLength' => array(3),
'luhn' => true,
),
// Credit cards
'visa' => array(
'type' => 'visa',
Expand Down Expand Up @@ -82,7 +89,7 @@ class CreditCard
),
'unionpay' => array(
'type' => 'unionpay',
'pattern' => '/^(62|88)/',
'pattern' => '/^(62|81)/',
'length' => array(16, 17, 18, 19),
'cvcLength' => array(3),
'luhn' => false,
Expand All @@ -94,6 +101,20 @@ class CreditCard
'cvcLength' => array(3),
'luhn' => true,
),
'uatp' => array(
'type' => 'uatp',
'pattern' => '/^1/',
'length' => array(15),
'cvcLength' => array(3),
'luhn' => true,
),
'rupay' => array(
'type' => 'rupay',
'pattern' => '/^(60|6521|6522)/',
'length' => array(16),
'cvcLength' => array(3),
'luhn' => true,
),
);

public static function validCreditCard($number, $type = null)
Expand All @@ -119,6 +140,12 @@ public static function validCreditCard($number, $type = null)
);
}

$ret['validation'] = array(
'pattern' => !empty($type) && self::validPattern($number, $type),
'length' => !empty($type) && self::validLength($number, $type),
'luhn' => !empty($type) && self::validLuhn($number, $type),
);

return $ret;
}

Expand Down Expand Up @@ -147,20 +174,48 @@ public static function validDate($year, $month)
return true;
}

// PROTECTED
// ---------------------------------------------------------

protected static function creditCardType($number)
/**
* @param string $number
* @param string|null $preferBrand
*
* @return string
*/
protected static function creditCardType($number, $preferBrand = null)
{
$matched = [];

foreach (self::$cards as $type => $card) {
if (preg_match($card['pattern'], $number)) {
return $type;
$matched[] = $type;
}
}

return '';
if (!empty($preferBrand) && in_array($preferBrand, $matched)) {
return $preferBrand;
}

return isset($matched[0]) ? $matched[0] : '';
}

/**
* @param string $bin
* @param string|null $preferBrand
*
* @return null|string
*/
public static function determineCreditCardType($bin, $preferBrand = null)
{
$type = self::creditCardType($bin, $preferBrand);

return !empty($type) ? $type : null;
}

/**
* @param $number
* @param $type
*
* @return bool
*/
protected static function validCard($number, $type)
{
return (self::validPattern($number, $type) && self::validLength($number, $type) && self::validLuhn($number, $type));
Expand Down
7 changes: 7 additions & 0 deletions tests/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ class Test extends PHPUnit_Framework_TestCase
'dankort' => array(
'5019717010103742',
),
'mir' => array(
'2200524572467853',
'2201338708835472',
'2202410737880339',
'2203027541752030',
'2204500360586886',
),

// Credit cards
'visa' => array(
Expand Down