src/Valitron/Validator.php
/**
     * Validate that a field contains a valid credit card
     * optionally filtered by an array
     *
     * @param string  $field
     * @param string $value
     * @param array $params
     * @return bool
     */
Validate that a field contains a valid credit card
optionally filtered by an array

@param string  $field
@param string $value
@param array $params
@return bool
Validate that a field contains a valid credit card optionally filtered by an array @param string $field @param string $value @param array $params @return bool
protected function validateCreditCard($field, $value, $params)
    {
        if (!empty($params)) {
            if (is_array($params[0])) {
                $cards = $params[0];
            } else if (is_string($params[0])){
                $cardType  = $params[0];
                if (isset($params[1]) && is_array($params[1])) {
                    $cards = $params[1];
                    if (!in_array($cardType, $cards)) {
                        return false;
                    }
                }
            }
        }
        $numberIsValid = function () use ($value) {
            $number = preg_replace('/[^0-9]+/', '', $value);
            $sum = 0;
            $strlen = strlen($number);
            if ($strlen < 13) {
                return false;
            }
            for ($i = 0; $i < $strlen; $i++) {
                $digit = (int) substr($number, $strlen - $i - 1, 1);
                if ($i % 2 == 1) {
                    $sub_total = $digit * 2;
                    if ($sub_total > 9) {
                        $sub_total = ($sub_total - 10) + 1;
                    }
                } else {
                    $sub_total = $digit;
                }
                $sum += $sub_total;
            }
            if ($sum > 0 && $sum % 10 == 0) {
                    return true;
            }
                return false;
        };
        if ($numberIsValid()) {
            if (!isset($cards)) {
                return true;
            } else {
                $cardRegex = array(
                    'visa'          => '#^4[0-9]{12}(?:[0-9]{3})?$#',
                    'mastercard'    => '#^5[1-5][0-9]{14}$#',
                    'amex'          => '#^3[47][0-9]{13}$#',
                    'dinersclub'    => '#^3(?:0[0-5]|[68][0-9])[0-9]{11}$#',
                    'discover'      => '#^6(?:011|5[0-9]{2})[0-9]{12}$#',
                );
                if (isset($cardType)) {
                    if (!isset($cards) && !in_array($cardType, array_keys($cardRegex))) {
                        return false;
                    }
                    return (preg_match($cardRegex[$cardType], $value) === 1);
                } else if (isset($cards)) {
                    foreach($cards as $card) {
                        if (in_array($card, array_keys($cardRegex))) {
                            if (preg_match($cardRegex[$card], $value) === 1) {
                                return true;
                            }
                        }
                    }
                } else {
                    foreach($cardRegex as $regex) {
                        if (preg_match($regex, $value) === 1) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }
protected function validateCreditCard ( $ field , $ value , $ params ) { if ( ! empty ( $ params ) ) { if ( is_array ( $ params [ 0 ] ) ) { $ cards = $ params [ 0 ] ; } else if ( is_string ( $ params [ 0 ] ) ) { $ cardType = $ params [ 0 ] ; if ( isset ( $ params [ 1 ] ) && is_array ( $ params [ 1 ] ) ) { $ cards = $ params [ 1 ] ; if ( ! in_array ( $ cardType , $ cards ) ) { return false ; } } } } $ numberIsValid = function ( ) use ( $ value ) { $ number = preg_replace ( '/[^0-9]+/' , '' , $ value ) ; $ sum = 0 ; $ strlen = strlen ( $ number ) ; if ( $ strlen < 13 ) { return false ; } for ( $ i = 0 ; $ i < $ strlen ; $ i ++ ) { $ digit = ( int ) substr ( $ number , $ strlen - $ i - 1 , 1 ) ; if ( $ i % 2 == 1 ) { $ sub_total = $ digit * 2 ; if ( $ sub_total > 9 ) { $ sub_total = ( $ sub_total - 10 ) + 1 ; } } else { $ sub_total = $ digit ; } $ sum += $ sub_total ; } if ( $ sum > 0 && $ sum % 10 == 0 ) { return true ; } return false ; } ; if ( $ numberIsValid ( ) ) { if ( ! isset ( $ cards ) ) { return true ; } else { $ cardRegex = array ( 'visa' => '#^4[0-9]{12}(?:[0-9]{3})?$#' , 'mastercard' => '#^5[1-5][0-9]{14}$#' , 'amex' => '#^3[47][0-9]{13}$#' , 'dinersclub' => '#^3(?:0[0-5]|[68][0-9])[0-9]{11}$#' , 'discover' => '#^6(?:011|5[0-9]{2})[0-9]{12}$#' , ) ; if ( isset ( $ cardType ) ) { if ( ! isset ( $ cards ) && ! in_array ( $ cardType , array_keys ( $ cardRegex ) ) ) { return false ; } return ( preg_match ( $ cardRegex [ $ cardType ] , $ value ) === 1 ) ; } else if ( isset ( $ cards ) ) { foreach ( $ cards as $ card ) { if ( in_array ( $ card , array_keys ( $ cardRegex ) ) ) { if ( preg_match ( $ cardRegex [ $ card ] , $ value ) === 1 ) { return true ; } } } } else { foreach ( $ cardRegex as $ regex ) { if ( preg_match ( $ regex , $ value ) === 1 ) { return true ; } } } } } return false ; }
Validate that a field contains a valid credit card
 optionally filtered by an array
Validate that a field contains a valid credit card optionally filtered by an array
/**
         * I there has been an array of valid cards supplied, or the name of the users card
         * or the name and an array of valid cards
         */ /**
             * array of valid cards
             */ /**
         * Luhn algorithm
         *
         * @return bool
         */ // if we don't have any valid cards specified and the card we've been given isn't in our regex array // we only need to test against one card type // if we have cards, check our users card against only the ones we have // if the card is valid, we want to stop looping // loop through every card // until we find a valid one // if we've got this far, the card has passed no validation so it's invalid!