Source of file CreditCardRule.php
Size: 1,524 Bytes - Last Modified: 2017-11-08T13:54:24+00:00
/home/travis/build/bluzphp/framework/src/Validator/Rule/CreditCardRule.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 | <?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ declare(strict_types=1); namespace Bluz\Validator\Rule; /** * Check credit card number by Mod10 algorithm * * @package Bluz\Validator\Rule * @link https://en.wikipedia.org/wiki/Luhn_algorithm */ class CreditCardRule extends AbstractRule { /** * @var string error template */ protected $description = 'must be a valid Credit Card number'; /** * Check input data * * @param string $input * * @return bool */ public function validate($input): bool { $input = preg_replace('([ \.-])', '', $input); if (!is_numeric($input)) { return false; } return $this->verifyMod10($input); } /** * Verify by Mod10 * * @param string $input * * @return bool */ private function verifyMod10($input) { $sum = 0; $input = strrev($input); $inputLen = strlen($input); for ($i = 0; $i < $inputLen; $i++) { $current = $input[$i]; if ($i % 2 == 1) { $current *= 2; if ($current > 9) { $firstDigit = $current % 10; $secondDigit = ($current - $firstDigit) / 10; $current = $firstDigit + $secondDigit; } } $sum += $current; } return ($sum % 10 == 0); } } |