Inspekt\Inspekt::isCcnum PHP Method

isCcnum() public static method

Returns true if it is a valid credit card number format. The optional second argument allows developers to indicate the type.
public static isCcnum ( mixed $value, mixed $type = null ) : boolean
$value mixed
$type mixed
return boolean
    public static function isCcnum($value, $type = null)
    {
        /**
         * @todo Type-specific checks
         */
        if (isset($type)) {
            throw new Exception('Type-specific cc checks are not yet supported');
        }
        $value = self::getDigits($value);
        $length = strlen($value);
        if ($length < 13 || $length > 19) {
            return false;
        }
        $sum = 0;
        $weight = 2;
        for ($i = $length - 2; $i >= 0; $i--) {
            $digit = $weight * $value[$i];
            $sum += floor($digit / 10) + $digit % 10;
            $weight = $weight % 2 + 1;
        }
        $mod = (10 - $sum % 10) % 10;
        return $mod == $value[$length - 1];
    }

Usage Example

Beispiel #1
0
 /**
  * @throws \Inspekt\Exception
  * @expectedException \Inspekt\Exception
  */
 public function testIsCcnumType()
 {
     $input = '5105105105105100';
     $this->assertTrue(Inspekt::isCcnum($input, 'mastercard'));
 }