Page History
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<?php
class LuhnCdvChecker
{
private $value;
private $cdv;
public function __construct($value, $cdv)
{
$this->value = $value;
$this->cdv = $cdv;
}
public function isCdvCorrect()
{
return $this->calculateCdv() == $this->cdv;
}
public function calculateCdv()
{
$sum = 0;
$j = 1;
$valueAsString = (string) $this->value;
for ($i = $this->getValueLength() - 1; $i > -1; $i--)
{
if ($j % 2 != 0)
{
$result = $valueAsString[$i] * 2;
}
else
{
$result = $valueAsString[$i];
}
foreach(str_split($result) as $digit)
{
$sum += $digit;
}
$j++;
}
return $sum % 10;
}
public function getValueLength()
{
return strlen($this->value);
}
}
$checker = new LuhnCdvChecker(9012028684, 5);
echo 'CDV: ' . $checker->calculateCdv() . "\n"; // 5
echo 'Is correct: ' . ($checker->isCdvCorrect() ? 'true' : 'false') . "\n"; // Is correct: true |
...
Overview
Content Tools