diff --git a/src/Detector.php b/src/Detector.php index c0a1637..5957a53 100644 --- a/src/Detector.php +++ b/src/Detector.php @@ -10,7 +10,7 @@ private function isVisa(string $card) : bool private function isMasterCard(string $card) : bool { - return preg_match("/^5$|^5[1-5][0-9]{0,14}$/i", $card); + return preg_match("/^5[1-5][0-9]{5,}|222[1-9][0-9]{3,}|22[3-9][0-9]{4,}|2[3-6][0-9]{5,}|27[01][0-9]{4,}|2720[0-9]{3,}$/i", $card); } private function isAmex(string $card) : bool @@ -23,13 +23,25 @@ private function isDiscover(string $card) : bool return preg_match("/^6$|^6[05]$|^601[1]?$|^65[0-9][0-9]?$|^6(?:011|5[0-9]{2})[0-9]{0,12}$/i", $card); } + private function isJCB(string $card) : bool + { + return preg_match("/^(?:2131|1800|35[0-9]{3})[0-9]{3,}$/i", $card); + } + + private function isDinersClub(string $card) : bool + { + return preg_match("/^3(?:0[0-5]|[68][0-9])[0-9]{4,}$/i", $card); + } + public function detect(string $card) : string { $cardTypes = [ 'Visa', - 'MasterCard', 'Amex', - 'Discover' + 'MasterCard', + 'Discover', + 'JCB', + 'DinersClub' ]; foreach ($cardTypes as $cardType) { $method = 'is' . $cardType; diff --git a/tests/unit/DetectorTest.php b/tests/unit/DetectorTest.php index f00b2b1..dd656b1 100644 --- a/tests/unit/DetectorTest.php +++ b/tests/unit/DetectorTest.php @@ -15,8 +15,18 @@ public function setup() $this->cards = [ 'Visa' => '4111111111111111', 'MasterCard' => '5555555555554444', + 'MasterCard1' => '2221000000000009', + 'MasterCard2' => '2223000048400011', + 'MasterCard3' => '2223016768738313', + 'MasterCard4' => '5105510551055105', 'Discover' => '6011111111111117', 'Amex' => '378282246310005', + 'Amex1' => '371449635398431', + 'AmexCorp' => '378734493671000', + 'JCB' => '3530111333300000', + 'JCB1' => '3566002020360505', + 'Diners' => '30569309025904', + 'Diners1' => '38520000023237', 'Invalid' => '7877787778777877' ]; } @@ -29,6 +39,10 @@ public function testCanInstantiateAssembler() public function testIsMasterCard() { $this->assertEquals('MasterCard', $this->Detector->detect($this->cards['MasterCard'])); + $this->assertEquals('MasterCard', $this->Detector->detect($this->cards['MasterCard1'])); + $this->assertEquals('MasterCard', $this->Detector->detect($this->cards['MasterCard2'])); + $this->assertEquals('MasterCard', $this->Detector->detect($this->cards['MasterCard3'])); + $this->assertEquals('MasterCard', $this->Detector->detect($this->cards['MasterCard4'])); } public function testIsDiscover() @@ -39,10 +53,24 @@ public function testIsDiscover() public function testIsAmex() { $this->assertEquals('Amex', $this->Detector->detect($this->cards['Amex'])); + $this->assertEquals('Amex', $this->Detector->detect($this->cards['Amex1'])); + $this->assertEquals('Amex', $this->Detector->detect($this->cards['AmexCorp'])); } public function testIsInvalid() { $this->assertEquals('Invalid Card', $this->Detector->detect($this->cards['Invalid'])); } + + public function testIsJCB() + { + $this->assertEquals('JCB', $this->Detector->detect($this->cards['JCB'])); + $this->assertEquals('JCB', $this->Detector->detect($this->cards['JCB1'])); + } + + public function testIsDiners() + { + $this->assertEquals('DinersClub', $this->Detector->detect($this->cards['Diners'])); + $this->assertEquals('DinersClub', $this->Detector->detect($this->cards['Diners1'])); + } }