"Fossies" - the Fresh Open Source Software Archive

Member "OCSNG_UNIX_SERVER_2.6/ocsreports/libraries/tclib/Barcode/Type/Linear/CodeOneOne.php" (17 Apr 2019, 3954 Bytes) of package /linux/privat/OCSNG_UNIX_SERVER_2.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 /**
    3  * CodeOneOne.php
    4  *
    5  * @since       2015-02-21
    6  * @category    Library
    7  * @package     Barcode
    8  * @author      Nicola Asuni <info@tecnick.com>
    9  * @copyright   2015-2015 Nicola Asuni - Tecnick.com LTD
   10  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
   11  * @link        https://github.com/tecnick.com/tc-lib-barcode
   12  *
   13  * This file is part of tc-lib-barcode software library.
   14  */
   15 
   16 namespace Com\Tecnick\Barcode\Type\Linear;
   17 
   18 use \Com\Tecnick\Barcode\Exception as BarcodeException;
   19 
   20 /**
   21  * Com\Tecnick\Barcode\Type\Linear\CodeOneOne;
   22  *
   23  * CodeOneOne Barcode type class
   24  * CODE 11
   25  *
   26  * @since       2015-02-21
   27  * @category    Library
   28  * @package     Barcode
   29  * @author      Nicola Asuni <info@tecnick.com>
   30  * @copyright   2015-2015 Nicola Asuni - Tecnick.com LTD
   31  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
   32  * @link        https://github.com/tecnick.com/tc-lib-barcode
   33  */
   34 class CodeOneOne extends \Com\Tecnick\Barcode\Type\Linear
   35 {
   36     /**
   37      * Barcode format
   38      *
   39      * @var string
   40      */
   41     protected $format = 'CODE11';
   42 
   43     /**
   44      * Map characters to barcodes
   45      *
   46      * @var array
   47      */
   48     protected $chbar = array(
   49         '0' => '111121',
   50         '1' => '211121',
   51         '2' => '121121',
   52         '3' => '221111',
   53         '4' => '112121',
   54         '5' => '212111',
   55         '6' => '122111',
   56         '7' => '111221',
   57         '8' => '211211',
   58         '9' => '211111',
   59         '-' => '112111',
   60         'S' => '112211'
   61     );
   62 
   63     /**
   64      * Calculate the checksum.
   65      *
   66      * @param $code (string) code to represent.
   67      *
   68      * @return char checksum.
   69      */
   70     protected function getChecksum($code)
   71     {
   72         $len = strlen($code);
   73         // calculate check digit C
   74         $ptr = 1;
   75         $ccheck = 0;
   76         for ($pos = ($len - 1); $pos >= 0; --$pos) {
   77             $digit = $code[$pos];
   78             if ($digit == '-') {
   79                 $dval = 10;
   80             } else {
   81                 $dval = intval($digit);
   82             }
   83             $ccheck += ($dval * $ptr);
   84             ++$ptr;
   85             if ($ptr > 10) {
   86                 $ptr = 1;
   87             }
   88         }
   89         $ccheck %= 11;
   90         if ($ccheck == 10) {
   91             $ccheck = '-';
   92         }
   93         if ($len <= 10) {
   94             return $ccheck;
   95         }
   96         // calculate check digit K
   97         $code .= $ccheck;
   98         $ptr = 1;
   99         $kcheck = 0;
  100         for ($pos = $len; $pos >= 0; --$pos) {
  101             $digit = $code[$pos];
  102             if ($digit == '-') {
  103                 $dval = 10;
  104             } else {
  105                 $dval = intval($digit);
  106             }
  107             $kcheck += ($dval * $ptr);
  108             ++$ptr;
  109             if ($ptr > 9) {
  110                 $ptr = 1;
  111             }
  112         }
  113         $kcheck %= 11;
  114         return $ccheck.$kcheck;
  115     }
  116 
  117     /**
  118      * Get the pre-formatted code
  119      *
  120      * @return string
  121      */
  122     protected function formatCode()
  123     {
  124         return 'S'.$this->code.$this->getChecksum($this->code).'S';
  125     }
  126 
  127     /**
  128      * Get the bars array
  129      *
  130      * @return array
  131      *
  132      * @throws BarcodeException in case of error
  133      */
  134     protected function setBars()
  135     {
  136         $this->ncols = 0;
  137         $this->nrows = 1;
  138         $this->bars = array();
  139         $code = $this->formatCode();
  140         $clen = strlen($code);
  141         for ($chr = 0; $chr < $clen; ++$chr) {
  142             $char = $code[$chr];
  143             if (!isset($this->chbar[$char])) {
  144                 throw new BarcodeException('Invalid character: chr('.ord($char).')');
  145             }
  146             for ($pos = 0; $pos < 6; ++$pos) {
  147                 $bar_width = intval($this->chbar[$char][$pos]);
  148                 if ((($pos % 2) == 0) && ($bar_width > 0)) {
  149                     $this->bars[] = array($this->ncols, 0, $bar_width, 1);
  150                 }
  151                 $this->ncols += $bar_width;
  152             }
  153         }
  154         --$this->ncols;
  155     }
  156 }