Last active
October 25, 2023 20:39
-
-
Save matheusgomes17/460f32f70c570a4f2bcf489654a22f0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* User: Matheus Gomes | |
* E-maiL: [email protected] | |
* Date: 07/03/19 | |
* Time: 15:32 | |
*/ | |
class CreditCard | |
{ | |
const AMERICAN_EXPRESS = 'american_express'; | |
const DINERS_CLUB = 'diners_club'; | |
const ELO = 'elo'; | |
const HIPERCARD = 'hipercard'; | |
const MASTERCARD = 'mastercard'; | |
const VISA = 'visa'; | |
/** | |
* @var array | |
*/ | |
private $brands = [ | |
self::AMERICAN_EXPRESS => '/^3[47]\d{13}$/', | |
self::DINERS_CLUB => '/^3(?:0[0-5]|[68]\d)\d{11}$/', | |
self::ELO => '/(4011|431274|438935|451416|457393|4576|457631|457632|504175|627780|636297|636368|636369|(6503[1-3])|(6500(3[5-9]|4[0-9]|5[0-1]))|(6504(0[5-9]|1[0-9]|2[0-9]|3[0-9]))|(650(48[5-9]|49[0-9]|50[0-9]|51[1-9]|52[0-9]|53[0-7]))|(6505(4[0-9]|5[0-9]|6[0-9]|7[0-9]|8[0-9]|9[0-8]))|(6507(0[0-9]|1[0-8]))|(6507(2[0-7]))|(650(90[1-9]|91[0-9]|920))|(6516(5[2-9]|6[0-9]|7[0-9]))|(6550(0[0-9]|1[1-9]))|(6550(2[1-9]|3[0-9]|4[0-9]|5[0-8]))|(506(699|77[0-8]|7[1-6][0-9))|(509([0-9][0-9][0-9])))/', | |
self::HIPERCARD => '/^(606282\d{10}(\d{3})?)|(3841\d{15})$/', | |
self::MASTERCARD => '/^5[1-5]\d{14}$|^2(?:2(?:2[1-9]|[3-9]\d)|[3-6]\d\d|7(?:[01]\d|20))\d{12}$/', | |
self::VISA => '/^4\d{12}(?:\d{3})?$/', | |
]; | |
/** | |
* @param int $number | |
* @return mixed | |
*/ | |
public static function getBrandByCardNumber(int $number) | |
{ | |
return (new CreditCard)->verifyBrand($number); | |
} | |
/** | |
* @param int $number | |
* @return string | |
*/ | |
private function verifyBrand(int $number): string | |
{ | |
switch ($number) { | |
case $this->getBrandPattern($this::AMERICAN_EXPRESS, $number): | |
return $this::AMERICAN_EXPRESS; | |
case $this->getBrandPattern($this::DINERS_CLUB, $number): | |
return $this::DINERS_CLUB; | |
case $this->getBrandPattern($this::ELO, $number): | |
return $this::ELO; | |
case $this->getBrandPattern($this::HIPERCARD, $number): | |
return $this::HIPERCARD; | |
case $this->getBrandPattern($this::MASTERCARD, $number): | |
return $this::MASTERCARD; | |
case $this->getBrandPattern($this::VISA, $number): | |
return $this::VISA; | |
default: | |
return 'is_invalid'; | |
} | |
} | |
/** | |
* @param string $pattern | |
* @param int $number | |
* @return bool | |
*/ | |
private function getBrandPattern(string $pattern, int $number): bool | |
{ | |
return preg_match($this->brands[$pattern], $number) > 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Obrigado!!