$card ) { if( preg_match($card['pattern'], $number) ) { return $type; } } return ''; // @codeCoverageIgnore } /** * Protected valid card * * @param string $number * @param string $type * * @return string */ protected static function validCard(string $number, ?string $type = NULL) : bool { return self::validCardFormat($number, $type) && self::validCardNumberLength($number, $type) && self::validLuhnAlgorithm($number, $type); } /** * Protected valid card pattern * * @param string $number * @param string $type * * @return bool */ protected static function validCardFormat($number, $type) : bool { return preg_match(self::getCardFormats()[$type]['pattern'], $number ?? ''); } /** * Protected valid card number length * * @param string $number * @param string $type * * @return bool */ protected static function validCardNumberLength($number, ?string $type = NULL, $function = 'length') : bool { return in_array(strlen($number ?? ''), self::getCardFormats()[$type][$function]); } /** * Protected valid cvc length * * @param int $cvc * @param string $type * * @return bool */ protected static function validCvcLength($cvc, ?string $type = NULL) : bool { return self::validCardNumberLength($cvc, $type, 'cvcLength'); } /** * Protected valid luhn algorithm * * @param string $number * @param string $type * * @return bool */ protected static function validLuhnAlgorithm(string $number, ?string $type = NULL) : bool { return self::getCardFormats()[$type]['luhn'] ? LuhnAlgorithm::check($number) : true; } /** * Protected get card formats * * @return array */ protected static function getCardFormats() : array { return Formats::getList(); } }