respect-validation/library/Rules/CreditCard.php
2015-06-08 12:09:25 -03:00

45 lines
1.1 KiB
PHP

<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
namespace Respect\Validation\Rules;
class CreditCard extends AbstractRule
{
public function validate($input)
{
$input = preg_replace('([^0-9])', '', $input);
if (!empty($input)) {
return $this->verifyMod10($input);
}
return false;
}
private function verifyMod10($input)
{
$sum = 0;
$input = strrev($input);
for ($i = 0; $i < strlen($input); $i++) {
$current = substr($input, $i, 1);
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);
}
}