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

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:  * RoyalMailFourCC.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\RoyalMailFourCc;
 22:  *
 23:  * RoyalMailFourCC Barcode type class
 24:  * RMS4CC (Royal Mail 4-state Customer Bar Code)
 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 RoyalMailFourCc extends \Com\Tecnick\Barcode\Type\Linear
 35: {
 36:     /**
 37:      * Barcode format
 38:      *
 39:      * @var string
 40:      */
 41:     protected $format = 'RMS4CC';
 42: 
 43:     /**
 44:      * Map characters to barcodes
 45:      *
 46:      * @var array
 47:      */
 48:     protected $chbar = array(
 49:         '0' => '3322',
 50:         '1' => '3412',
 51:         '2' => '3421',
 52:         '3' => '4312',
 53:         '4' => '4321',
 54:         '5' => '4411',
 55:         '6' => '3142',
 56:         '7' => '3232',
 57:         '8' => '3241',
 58:         '9' => '4132',
 59:         'A' => '4141',
 60:         'B' => '4231',
 61:         'C' => '3124',
 62:         'D' => '3214',
 63:         'E' => '3223',
 64:         'F' => '4114',
 65:         'G' => '4123',
 66:         'H' => '4213',
 67:         'I' => '1342',
 68:         'J' => '1432',
 69:         'K' => '1441',
 70:         'L' => '2332',
 71:         'M' => '2341',
 72:         'N' => '2431',
 73:         'O' => '1324',
 74:         'P' => '1414',
 75:         'Q' => '1423',
 76:         'R' => '2314',
 77:         'S' => '2323',
 78:         'T' => '2413',
 79:         'U' => '1144',
 80:         'V' => '1234',
 81:         'W' => '1243',
 82:         'X' => '2134',
 83:         'Y' => '2143',
 84:         'Z' => '2233'
 85:     );
 86: 
 87:     /**
 88:      * Characters used for checksum
 89:      *
 90:      * @var array
 91:      */
 92:     protected $chksum = array(
 93:         '0' => '11',
 94:         '1' => '12',
 95:         '2' => '13',
 96:         '3' => '14',
 97:         '4' => '15',
 98:         '5' => '10',
 99:         '6' => '21',
100:         '7' => '22',
101:         '8' => '23',
102:         '9' => '24',
103:         'A' => '25',
104:         'B' => '20',
105:         'C' => '31',
106:         'D' => '32',
107:         'E' => '33',
108:         'F' => '34',
109:         'G' => '35',
110:         'H' => '30',
111:         'I' => '41',
112:         'J' => '42',
113:         'K' => '43',
114:         'L' => '44',
115:         'M' => '45',
116:         'N' => '40',
117:         'O' => '51',
118:         'P' => '52',
119:         'Q' => '53',
120:         'R' => '54',
121:         'S' => '55',
122:         'T' => '50',
123:         'U' => '01',
124:         'V' => '02',
125:         'W' => '03',
126:         'X' => '04',
127:         'Y' => '05',
128:         'Z' => '00'
129:     );
130: 
131:     /**
132:      * Calculate the checksum.
133:      *
134:      * @param $code (string) code to represent.
135:      *
136:      * @return char checksum.
137:      *
138:      * @throws BarcodeException in case of error
139:      */
140:     protected function getChecksum($code)
141:     {
142:         $row = 0;
143:         $col = 0;
144:         $len = strlen($code);
145:         for ($pos = 0; $pos < $len; ++$pos) {
146:             $char = $code[$pos];
147:             if (!isset($this->chksum[$char])) {
148:                 throw new BarcodeException('Invalid character: chr('.ord($char).')');
149:             }
150:             $row += intval($this->chksum[$char][0]);
151:             $col += intval($this->chksum[$char][1]);
152:         }
153:         $row %= 6;
154:         $col %= 6;
155:         $check = array_keys($this->chksum, $row.$col);
156:         return $check[0];
157:     }
158: 
159:     /**
160:      * Format code
161:      */
162:     protected function formatCode()
163:     {
164:         $code = strtoupper($this->code);
165:         $this->extcode = $code.$this->getChecksum($code);
166:     }
167:     
168:     /**
169:      * Get the central bars
170:      *
171:      * @throws BarcodeException in case of error
172:      */
173:     protected function getCoreBars()
174:     {
175:         $this->formatCode();
176:         $clen = strlen($this->extcode);
177:         for ($chr = 0; $chr < $clen; ++$chr) {
178:             $char = $this->extcode[$chr];
179:             for ($pos = 0; $pos < 4; ++$pos) {
180:                 switch ($this->chbar[$char][$pos]) {
181:                     case '1':
182:                         $this->bars[] = array($this->ncols, 0, 1, 2);
183:                         break;
184:                     case '2':
185:                         $this->bars[] = array($this->ncols, 0, 1, 3);
186:                         break;
187:                     case '3':
188:                         $this->bars[] = array($this->ncols, 1, 1, 1);
189:                         break;
190:                     case '4':
191:                         $this->bars[] = array($this->ncols, 1, 1, 2);
192:                         break;
193:                 }
194:                 $this->ncols +=2;
195:             }
196:         }
197:     }
198:     
199:     /**
200:      * Get the bars array
201:      *
202:      * @throws BarcodeException in case of error
203:      */
204:     protected function setBars()
205:     {
206:         $this->ncols = 0;
207:         $this->nrows = 3;
208:         $this->bars = array();
209:         
210:         // start bar
211:         $this->bars[] = array($this->ncols, 0, 1, 2);
212:         $this->ncols += 2;
213: 
214:         $this->getCoreBars();
215:   
216:         // stop bar
217:         $this->bars[] = array($this->ncols, 0, 1, 3);
218:         ++$this->ncols;
219:     }
220: }
221: 
 

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