Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

A Postai LuhnA Posta-féle Luhn ellenőrző összeggel ellátott érték (HungarianPostLuhnCdvValue) entitás egy értéket és a hozzá tartozó ellenőrző összeget tartalmazza.

...

Code Block
languagephp
titlePHP - CDV kód ellenőrzése
linenumberstrue
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++;
        }
		$modulo = $sum % 10;
		return $modulo == return0 ? $sum0 %: 10 - ($modulo);
    }
    
    public function getValueLength()
    {
        return strlen($this->value);
    }
}

$checker = new LuhnCdvChecker("9012028684", 5);
echo 'CDV: ' . $checker->calculateCdv() . "\n"; // CDV: 5
echo 'Is correct: ' . ($checker->isCdvCorrect() ? 'true' : 'false') . "\n"; // Is correct: true 

Példa

...

languagexml
themeConfluence
linenumberstrue

...