source-class-Com.Tecnick.Barcode.Type.Linear.CodeOneOne

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
Overview

Classes

Exceptions

  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   2010-2016 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/tecnickcom/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   2010-2016 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/tecnickcom/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:      * Format code
119:      */
120:     protected function formatCode()
121:     {
122:         $this->extcode = 'S'.$this->code.$this->getChecksum($this->code).'S';
123:     }
124: 
125:     /**
126:      * Get the bars array
127:      *
128:      * @return array
129:      *
130:      * @throws BarcodeException in case of error
131:      */
132:     protected function setBars()
133:     {
134:         $this->ncols = 0;
135:         $this->nrows = 1;
136:         $this->bars = array();
137:         $this->formatCode();
138:         $clen = strlen($this->extcode);
139:         for ($chr = 0; $chr < $clen; ++$chr) {
140:             $char = $this->extcode[$chr];
141:             if (!isset($this->chbar[$char])) {
142:                 throw new BarcodeException('Invalid character: chr('.ord($char).')');
143:             }
144:             for ($pos = 0; $pos < 6; ++$pos) {
145:                 $bar_width = intval($this->chbar[$char][$pos]);
146:                 if ((($pos % 2) == 0) && ($bar_width > 0)) {
147:                     $this->bars[] = array($this->ncols, 0, $bar_width, 1);
148:                 }
149:                 $this->ncols += $bar_width;
150:             }
151:         }
152:         --$this->ncols;
153:     }
154: }
155: 
 

© 2004-2017 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy