File indexing completed on 2024-12-22 05:36:29

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Barcode
0017  * @subpackage Object
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Barcode_Object_ObjectAbstract
0025  */
0026 // require_once 'Zend/Barcode/Object/ObjectAbstract.php';
0027 
0028 /**
0029  * @see Zend_Validate_Barcode
0030  */
0031 // require_once 'Zend/Validate/Barcode.php';
0032 
0033 /**
0034  * Class for generate Postnet barcode
0035  *
0036  * @category   Zend
0037  * @package    Zend_Barcode
0038  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0039  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0040  */
0041 class Zend_Barcode_Object_Postnet extends Zend_Barcode_Object_ObjectAbstract
0042 {
0043 
0044     /**
0045      * Coding map
0046      * - 0 = half bar
0047      * - 1 = complete bar
0048      * @var array
0049      */
0050     protected $_codingMap = array(
0051         0 => "11000",
0052         1 => "00011",
0053         2 => "00101",
0054         3 => "00110",
0055         4 => "01001",
0056         5 => "01010",
0057         6 => "01100",
0058         7 => "10001",
0059         8 => "10010",
0060         9 => "10100"
0061     );
0062 
0063     /**
0064      * Default options for Postnet barcode
0065      * @return void
0066      */
0067     protected function _getDefaultOptions()
0068     {
0069         $this->_barThinWidth = 2;
0070         $this->_barHeight = 20;
0071         $this->_drawText = false;
0072         $this->_stretchText = true;
0073         $this->_mandatoryChecksum = true;
0074     }
0075 
0076     /**
0077      * Width of the barcode (in pixels)
0078      * @return integer
0079      */
0080     protected function _calculateBarcodeWidth()
0081     {
0082         $quietZone       = $this->getQuietZone();
0083         $startCharacter  = (2 * $this->_barThinWidth) * $this->_factor;
0084         $stopCharacter   = (1 * $this->_barThinWidth) * $this->_factor;
0085         $encodedData     = (10 * $this->_barThinWidth) * $this->_factor * strlen($this->getText());
0086         return $quietZone + $startCharacter + $encodedData + $stopCharacter + $quietZone;
0087     }
0088 
0089     /**
0090      * Partial check of interleaved Postnet barcode
0091      * @return void
0092      */
0093     protected function _checkParams()
0094     {}
0095 
0096     /**
0097      * Prepare array to draw barcode
0098      * @return array
0099      */
0100     protected function _prepareBarcode()
0101     {
0102         $barcodeTable = array();
0103 
0104         // Start character (1)
0105         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
0106         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
0107 
0108         // Text to encode
0109         $textTable = str_split($this->getText());
0110         foreach ($textTable as $char) {
0111             $bars = str_split($this->_codingMap[$char]);
0112             foreach ($bars as $b) {
0113                 $barcodeTable[] = array(1 , $this->_barThinWidth , 0.5 - $b * 0.5 , 1);
0114                 $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
0115             }
0116         }
0117 
0118         // Stop character (1)
0119         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
0120         return $barcodeTable;
0121     }
0122 
0123     /**
0124      * Get barcode checksum
0125      *
0126      * @param  string $text
0127      * @return int
0128      */
0129     public function getChecksum($text)
0130     {
0131         $this->_checkText($text);
0132         $sum = array_sum(str_split($text));
0133         $checksum = (10 - ($sum % 10)) % 10;
0134         return $checksum;
0135     }
0136 }