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

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:  * Postnet.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\Postnet;
 22:  *
 23:  * Postnet Barcode type class
 24:  * POSTNET
 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 Postnet extends \Com\Tecnick\Barcode\Type\Linear
 35: {
 36:     /**
 37:      * Barcode format
 38:      *
 39:      * @var string
 40:      */
 41:     protected $format = 'POSTNET';
 42: 
 43:     /**
 44:      * Map characters to barcodes
 45:      *
 46:      * @var array
 47:      */
 48:     protected $chbar = array(
 49:         '0' => '22111',
 50:         '1' => '11122',
 51:         '2' => '11212',
 52:         '3' => '11221',
 53:         '4' => '12112',
 54:         '5' => '12121',
 55:         '6' => '12211',
 56:         '7' => '21112',
 57:         '8' => '21121',
 58:         '9' => '21211'
 59:     );
 60: 
 61:     /**
 62:      * Calculate the checksum.
 63:      *
 64:      * @param $code (string) code to represent.
 65:      *
 66:      * @return char checksum.
 67:      */
 68:     protected function getChecksum($code)
 69:     {
 70:         $sum = 0;
 71:         $len = strlen($code);
 72:         for ($pos = 0; $pos < $len; ++$pos) {
 73:             $sum += intval($code[$pos]);
 74:         }
 75:         $check = ($sum % 10);
 76:         if ($check > 0) {
 77:             $check = (10 - $check);
 78:         }
 79:         return $check;
 80:     }
 81: 
 82:     /**
 83:      * Format code
 84:      */
 85:     protected function formatCode()
 86:     {
 87:         $code = preg_replace('/[-\s]+/', '', $this->code);
 88:         $this->extcode = $code.$this->getChecksum($code);
 89:     }
 90: 
 91:     /**
 92:      * Get the bars array
 93:      *
 94:      * @return array
 95:      *
 96:      * @throws BarcodeException in case of error
 97:      */
 98:     protected function setBars()
 99:     {
100:         $this->ncols = 0;
101:         $this->nrows = 2;
102:         $this->bars = array();
103:         $this->formatCode();
104:         $clen = strlen($this->extcode);
105:         // start bar
106:         $this->bars[] = array($this->ncols, 0, 1, 2);
107:         $this->ncols += 2;
108:         for ($chr = 0; $chr < $clen; ++$chr) {
109:             $char = $this->extcode[$chr];
110:             if (!isset($this->chbar[$char])) {
111:                 throw new BarcodeException('Invalid character: chr('.ord($char).')');
112:             }
113:             for ($pos = 0; $pos < 5; ++$pos) {
114:                 $bar_height = intval($this->chbar[$char][$pos]);
115:                 $this->bars[] = array($this->ncols, floor(1 / $bar_height), 1, $bar_height);
116:                 $this->ncols += 2;
117:             }
118:         }
119:         // end bar
120:         $this->bars[] = array($this->ncols, 0, 1, 2);
121:         ++$this->ncols;
122:     }
123: }
124: 
 

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