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

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:  * EanOneThree.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\EanOneThree;
 22:  *
 23:  * EanOneThree Barcode type class
 24:  * EAN 13
 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 EanOneThree extends \Com\Tecnick\Barcode\Type\Linear
 35: {
 36:     /**
 37:      * Barcode format
 38:      *
 39:      * @var string
 40:      */
 41:     protected $format = 'EAN13';
 42: 
 43:     /**
 44:      * Fixed code length
 45:      *
 46:      * @var int
 47:      */
 48:     protected $code_length = 13;
 49: 
 50:     /**
 51:      * Check digit
 52:      *
 53:      * @var int
 54:      */
 55:     protected $check = '';
 56:     
 57:     /**
 58:      * Map characters to barcodes
 59:      *
 60:      * @var array
 61:      */
 62:     protected $chbar = array(
 63:         'A' => array( // left odd parity
 64:             '0' => '0001101',
 65:             '1' => '0011001',
 66:             '2' => '0010011',
 67:             '3' => '0111101',
 68:             '4' => '0100011',
 69:             '5' => '0110001',
 70:             '6' => '0101111',
 71:             '7' => '0111011',
 72:             '8' => '0110111',
 73:             '9' => '0001011'
 74:         ),
 75:         'B' => array( // left even parity
 76:             '0' => '0100111',
 77:             '1' => '0110011',
 78:             '2' => '0011011',
 79:             '3' => '0100001',
 80:             '4' => '0011101',
 81:             '5' => '0111001',
 82:             '6' => '0000101',
 83:             '7' => '0010001',
 84:             '8' => '0001001',
 85:             '9' => '0010111'
 86:         ),
 87:         'C' => array( // right
 88:             '0' => '1110010',
 89:             '1' => '1100110',
 90:             '2' => '1101100',
 91:             '3' => '1000010',
 92:             '4' => '1011100',
 93:             '5' => '1001110',
 94:             '6' => '1010000',
 95:             '7' => '1000100',
 96:             '8' => '1001000',
 97:             '9' => '1110100'
 98:         )
 99:     );
100: 
101:     /**
102:      * Map parities
103:      *
104:      * @var array
105:      */
106:     protected $parities = array(
107:         '0' => 'AAAAAA',
108:         '1' => 'AABABB',
109:         '2' => 'AABBAB',
110:         '3' => 'AABBBA',
111:         '4' => 'ABAABB',
112:         '5' => 'ABBAAB',
113:         '6' => 'ABBBAA',
114:         '7' => 'ABABAB',
115:         '8' => 'ABABBA',
116:         '9' => 'ABBABA'
117:     );
118: 
119:     /**
120:      * Calculate checksum
121:      *
122:      * @param $code (string) code to represent.
123:      *
124:      * @return char checksum.
125:      *
126:      * @throws BarcodeException in case of error
127:      */
128:     protected function getChecksum($code)
129:     {
130:         $data_len = ($this->code_length - 1);
131:         $code_len = strlen($code);
132:         $sum_a = 0;
133:         for ($pos = 1; $pos < $data_len; $pos += 2) {
134:             $sum_a += $code[$pos];
135:         }
136:         if ($this->code_length > 12) {
137:             $sum_a *= 3;
138:         }
139:         $sum_b = 0;
140:         for ($pos = 0; $pos < $data_len; $pos += 2) {
141:             $sum_b += ($code[$pos]);
142:         }
143:         if ($this->code_length < 13) {
144:             $sum_b *= 3;
145:         }
146:         $this->check = ($sum_a + $sum_b) % 10;
147:         if ($this->check > 0) {
148:             $this->check = (10 - $this->check);
149:         }
150:         if ($code_len == $data_len) {
151:             // add check digit
152:             return $this->check;
153:         } elseif ($this->check !== intval($code[$data_len])) {
154:             // wrong check digit
155:             throw new BarcodeException('Invalid check digit: '.$this->check);
156:         }
157:         return '';
158:     }
159: 
160:     /**
161:      * Format code
162:      */
163:     protected function formatCode()
164:     {
165:         $code = str_pad($this->code, ($this->code_length - 1), '0', STR_PAD_LEFT);
166:         $this->extcode = $code.$this->getChecksum($code);
167:     }
168:     
169:     /**
170:      * Get the bars array
171:      *
172:      * @throws BarcodeException in case of error
173:      */
174:     protected function setBars()
175:     {
176:         if (!is_numeric($this->code)) {
177:             throw new BarcodeException('Inpout code must be a number');
178:         }
179:         $this->formatCode();
180:         $seq = '101'; // left guard bar
181:         $half_len = intval(ceil($this->code_length / 2));
182:         $parity = $this->parities[$this->extcode[0]];
183:         for ($pos = 1; $pos < $half_len; ++$pos) {
184:             $seq .= $this->chbar[$parity[($pos - 1)]][$this->extcode[$pos]];
185:         }
186:         $seq .= '01010'; // center guard bar
187:         for ($pos = $half_len; $pos < $this->code_length; ++$pos) {
188:             $seq .= $this->chbar['C'][$this->extcode[$pos]];
189:         }
190:         $seq .= '101'; // right guard bar
191:         $this->processBinarySequence($seq);
192:     }
193: }
194: 
 

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