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 Royal maim 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_Royalmail extends Zend_Barcode_Object_ObjectAbstract
0042 {
0043 
0044     /**
0045      * Coding map
0046      * - 0 = Tracker, Ascender and Descender
0047      * - 1 = Tracker and Ascender
0048      * - 2 = Tracker and Descender
0049      * - 3 = Tracker
0050      * @var array
0051      */
0052     protected $_codingMap = array(
0053         '0' => '3300', '1' => '3210', '2' => '3201', '3' => '2310', '4' => '2301', '5' => '2211',
0054         '6' => '3120', '7' => '3030', '8' => '3021', '9' => '2130', 'A' => '2121', 'B' => '2031',
0055         'C' => '3102', 'D' => '3012', 'E' => '3003', 'F' => '2112', 'G' => '2103', 'H' => '2013',
0056         'I' => '1320', 'J' => '1230', 'K' => '1221', 'L' => '0330', 'M' => '0321', 'N' => '0231',
0057         'O' => '1302', 'P' => '1212', 'Q' => '1203', 'R' => '0312', 'S' => '0303', 'T' => '0213',
0058         'U' => '1122', 'V' => '1032', 'W' => '1023', 'X' => '0132', 'Y' => '0123', 'Z' => '0033'
0059     );
0060 
0061     protected $_rows = array(
0062         '0' => 1, '1' => 1, '2' => 1, '3' => 1, '4' => 1, '5' => 1,
0063         '6' => 2, '7' => 2, '8' => 2, '9' => 2, 'A' => 2, 'B' => 2,
0064         'C' => 3, 'D' => 3, 'E' => 3, 'F' => 3, 'G' => 3, 'H' => 3,
0065         'I' => 4, 'J' => 4, 'K' => 4, 'L' => 4, 'M' => 4, 'N' => 4,
0066         'O' => 5, 'P' => 5, 'Q' => 5, 'R' => 5, 'S' => 5, 'T' => 5,
0067         'U' => 0, 'V' => 0, 'W' => 0, 'X' => 0, 'Y' => 0, 'Z' => 0,
0068     );
0069 
0070     protected $_columns = array(
0071         '0' => 1, '1' => 2, '2' => 3, '3' => 4, '4' => 5, '5' => 0,
0072         '6' => 1, '7' => 2, '8' => 3, '9' => 4, 'A' => 5, 'B' => 0,
0073         'C' => 1, 'D' => 2, 'E' => 3, 'F' => 4, 'G' => 5, 'H' => 0,
0074         'I' => 1, 'J' => 2, 'K' => 3, 'L' => 4, 'M' => 5, 'N' => 0,
0075         'O' => 1, 'P' => 2, 'Q' => 3, 'R' => 4, 'S' => 5, 'T' => 0,
0076         'U' => 1, 'V' => 2, 'W' => 3, 'X' => 4, 'Y' => 5, 'Z' => 0,
0077     );
0078 
0079     /**
0080      * Default options for Postnet barcode
0081      * @return void
0082      */
0083     protected function _getDefaultOptions()
0084     {
0085         $this->_barThinWidth = 2;
0086         $this->_barHeight = 20;
0087         $this->_drawText = false;
0088         $this->_stretchText = true;
0089         $this->_mandatoryChecksum = true;
0090     }
0091 
0092     /**
0093      * Width of the barcode (in pixels)
0094      * @return integer
0095      */
0096     protected function _calculateBarcodeWidth()
0097     {
0098         $quietZone       = $this->getQuietZone();
0099         $startCharacter  = (2 * $this->_barThinWidth) * $this->_factor;
0100         $stopCharacter   = (1 * $this->_barThinWidth) * $this->_factor;
0101         $encodedData     = (8 * $this->_barThinWidth) * $this->_factor * strlen($this->getText());
0102         return $quietZone + $startCharacter + $encodedData + $stopCharacter + $quietZone;
0103     }
0104 
0105     /**
0106      * Partial check of interleaved Postnet barcode
0107      * @return void
0108      */
0109     protected function _checkParams()
0110     {}
0111 
0112     /**
0113      * Prepare array to draw barcode
0114      * @return array
0115      */
0116     protected function _prepareBarcode()
0117     {
0118         $barcodeTable = array();
0119 
0120         // Start character (1)
0121         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 5/8);
0122         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
0123 
0124         // Text to encode
0125         $textTable = str_split($this->getText());
0126         foreach ($textTable as $char) {
0127             $bars = str_split($this->_codingMap[$char]);
0128             foreach ($bars as $b) {
0129                 $barcodeTable[] = array(1 , $this->_barThinWidth , ($b > 1 ? 3/8 : 0) , ($b % 2 ? 5/8 : 1));
0130                 $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
0131             }
0132         }
0133 
0134         // Stop character (1)
0135         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
0136         return $barcodeTable;
0137     }
0138 
0139     /**
0140      * Get barcode checksum
0141      *
0142      * @param  string $text
0143      * @return int
0144      */
0145     public function getChecksum($text)
0146     {
0147         $this->_checkText($text);
0148         $values   = str_split($text);
0149         $rowvalue = 0;
0150         $colvalue = 0;
0151         foreach($values as $row) {
0152             $rowvalue += $this->_rows[$row];
0153             $colvalue += $this->_columns[$row];
0154         }
0155 
0156         $rowvalue %= 6;
0157         $colvalue %= 6;
0158 
0159         $rowchkvalue = array_keys($this->_rows, $rowvalue);
0160         $colchkvalue = array_keys($this->_columns, $colvalue);
0161         return current(array_intersect($rowchkvalue, $colchkvalue));
0162     }
0163 }