54 lines
1.7 KiB
PHP
54 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace KupShop\KupShopBundle\Util;
|
|
|
|
class EANValidator
|
|
{
|
|
public static function getEAN13checkDigit($digits): int
|
|
{
|
|
// first change digits to a string so that we can access individual numbers
|
|
$digits = (string) $digits;
|
|
// 1. Add the values of the digits in the even-numbered positions: 2, 4, 6, etc.
|
|
$even_sum = $digits[1] + $digits[3] + $digits[5] + $digits[7] + $digits[9] + $digits[11];
|
|
// 2. Multiply this result by 3.
|
|
$even_sum_three = $even_sum * 3;
|
|
// 3. Add the values of the digits in the odd-numbered positions: 1, 3, 5, etc.
|
|
$odd_sum = $digits[0] + $digits[2] + $digits[4] + $digits[6] + $digits[8] + $digits[10];
|
|
// 4. Sum the results of steps 2 and 3.
|
|
$total_sum = $even_sum_three + $odd_sum;
|
|
// 5. The check character is the smallest number which, when added to the result in step 4, produces a multiple of 10.
|
|
$next_ten = ceil($total_sum / 10) * 10;
|
|
$check_digit = $next_ten - $total_sum;
|
|
|
|
return $check_digit;
|
|
}
|
|
|
|
public static function checkEAN($ean): bool
|
|
{
|
|
if (strlen($ean) !== 13) {
|
|
return false;
|
|
}
|
|
|
|
$ean = formatEAN($ean);
|
|
|
|
$check_digit = static::getEAN13checkDigit(substr($ean, 0, 12));
|
|
|
|
return $check_digit === (int) $ean[12];
|
|
}
|
|
|
|
public static function generateEan($id)
|
|
{
|
|
if ($idV = ($id['id_variation'] ?? null)) {
|
|
$digits = '201'.sprintf('%09d', $idV);
|
|
} elseif ($idP = ($id['id_product'] ?? null)) {
|
|
$digits = '200'.sprintf('%09d', $idP);
|
|
} else {
|
|
return '';
|
|
}
|
|
|
|
$check_digit = static::getEAN13checkDigit($digits);
|
|
|
|
return $digits.$check_digit;
|
|
}
|
|
}
|