• Модуль: ui
  • Путь к файлу: ~/bitrix/modules/ui/lib/barcode/barcodegenerator.php
  • Класс: BitrixUIBarcodeBarcodeGenerator
  • Вызов: BarcodeGenerator::upc_a_normalize
private function upc_a_normalize($data) {
	$data = preg_replace('/[^0-9*]/', '', $data);
	/* Set length to 12 digits. */
	if (strlen($data) < 5) {
		$data = str_repeat('0', 12);
	} else if (strlen($data) < 12) {
		$system = substr($data, 0, 1);
		$edata = substr($data, 1, -2);
		$epattern = (int)substr($data, -2, 1);
		$check = substr($data, -1);
		if ($epattern < 3) {
			$left = $system . substr($edata, 0, 2) . $epattern;
			$right = substr($edata, 2) . $check;
		} else if ($epattern < strlen($edata)) {
			$left = $system . substr($edata, 0, $epattern);
			$right = substr($edata, $epattern) . $check;
		} else {
			$left = $system . $edata;
			$right = $epattern . $check;
		}
		$center = str_repeat('0', 12 - strlen($left . $right));
		$data = $left . $center . $right;
	} else if (strlen($data) > 12) {
		$left = substr($data, 0, 6);
		$right = substr($data, -6);
		$data = $left . $right;
	}
	/* Replace * with missing or check digit. */
	while (($o = strrpos($data, '*')) !== false) {
		$checksum = 0;
		for ($i = 0; $i < 12; $i++) {
			$digit = substr($data, $i, 1);
			$checksum += (($i % 2) ? 1 : 3) * $digit;
		}
		$checksum *= (($o % 2) ? 9 : 3);
		$left = substr($data, 0, $o);
		$center = substr($checksum, -1);
		$right = substr($data, $o + 1);
		$data = $left . $center . $right;
	}
	return $data;
}