You are here

function BARCODE::_upceEncode in Barcode 6

1 call to BARCODE::_upceEncode()
BARCODE::_upceBarcode in ./barcode.inc.php

File

./barcode.inc.php, line 1259

Class

BARCODE

Code

function _upceEncode($barnumber, $encbit, $checkdigit) {
  $leftOdd = array(
    "0001101",
    "0011001",
    "0010011",
    "0111101",
    "0100011",
    "0110001",
    "0101111",
    "0111011",
    "0110111",
    "0001011",
  );
  $leftEven = array(
    "0100111",
    "0110011",
    "0011011",
    "0100001",
    "0011101",
    "0111001",
    "0000101",
    "0010001",
    "0001001",
    "0010111",
  );
  $encTable0 = array(
    "EEEOOO",
    "EEOEOO",
    "EEOOEO",
    "EEOOOE",
    "EOEEOO",
    "EOOEEO",
    "EOOOEE",
    "EOEOEO",
    "EOEOOE",
    "EOOEOE",
  );
  $encTable1 = array(
    "OOOEEE",
    "OOEOEE",
    "OOEEOE",
    "OOEEEO",
    "OEOOEE",
    "OEEOOE",
    "OEEEOO",
    "OEOEOE",
    "OEOEEO",
    "OEEOEO",
  );
  $guards = array(
    "bab",
    "ababa",
    "b",
  );
  if ($encbit == 0) {
    $encTable = $encTable0;
  }
  elseif ($encbit == 1) {
    $encTable = $encTable1;
  }
  else {
    $this->_error = "Not an UPC-E barcode number";
    return false;
  }
  $mfcStr = "";
  $prodStr = "";
  $checkdigit;
  $encTable[$checkdigit];
  for ($i = 0; $i < strlen($barnumber); $i++) {
    $num = (int) $barnumber[$i];
    $even = substr($encTable[$checkdigit], $i, 1) == 'E';
    if (!$even) {
      $mfcStr .= $leftOdd[$num];
    }
    else {
      $mfcStr .= $leftEven[$num];
    }
  }
  return $guards[0] . $mfcStr . $guards[1] . $guards[2];
}