web/lib/Zend/Barcode/Object/Royalmail.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Barcode
       
    17  * @subpackage Object
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Royalmail.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Barcode_Object_ObjectAbstract
       
    25  */
       
    26 require_once 'Zend/Barcode/Object/ObjectAbstract.php';
       
    27 
       
    28 /**
       
    29  * @see Zend_Validate_Barcode
       
    30  */
       
    31 require_once 'Zend/Validate/Barcode.php';
       
    32 
       
    33 /**
       
    34  * Class for generate Royal maim barcode
       
    35  *
       
    36  * @category   Zend
       
    37  * @package    Zend_Barcode
       
    38  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    39  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    40  */
       
    41 class Zend_Barcode_Object_Royalmail extends Zend_Barcode_Object_ObjectAbstract
       
    42 {
       
    43 
       
    44     /**
       
    45      * Coding map
       
    46      * - 0 = Tracker, Ascender and Descender
       
    47      * - 1 = Tracker and Ascender
       
    48      * - 2 = Tracker and Descender
       
    49      * - 3 = Tracker
       
    50      * @var array
       
    51      */
       
    52     protected $_codingMap = array(
       
    53         '0' => '3300', '1' => '3210', '2' => '3201', '3' => '2310', '4' => '2301', '5' => '2211',
       
    54         '6' => '3120', '7' => '3030', '8' => '3021', '9' => '2130', 'A' => '2121', 'B' => '2031',
       
    55         'C' => '3102', 'D' => '3012', 'E' => '3003', 'F' => '2112', 'G' => '2103', 'H' => '2013',
       
    56         'I' => '1320', 'J' => '1230', 'K' => '1221', 'L' => '0330', 'M' => '0321', 'N' => '0231',
       
    57         'O' => '1302', 'P' => '1212', 'Q' => '1203', 'R' => '0312', 'S' => '0303', 'T' => '0213',
       
    58         'U' => '1122', 'V' => '1032', 'W' => '1023', 'X' => '0132', 'Y' => '0123', 'Z' => '0033'
       
    59     );
       
    60 
       
    61     protected $_rows = array(
       
    62         '0' => 1, '1' => 1, '2' => 1, '3' => 1, '4' => 1, '5' => 1,
       
    63         '6' => 2, '7' => 2, '8' => 2, '9' => 2, 'A' => 2, 'B' => 2,
       
    64         'C' => 3, 'D' => 3, 'E' => 3, 'F' => 3, 'G' => 3, 'H' => 3,
       
    65         'I' => 4, 'J' => 4, 'K' => 4, 'L' => 4, 'M' => 4, 'N' => 4,
       
    66         'O' => 5, 'P' => 5, 'Q' => 5, 'R' => 5, 'S' => 5, 'T' => 5,
       
    67         'U' => 0, 'V' => 0, 'W' => 0, 'X' => 0, 'Y' => 0, 'Z' => 0,
       
    68     );
       
    69 
       
    70     protected $_columns = array(
       
    71         '0' => 1, '1' => 2, '2' => 3, '3' => 4, '4' => 5, '5' => 0,
       
    72         '6' => 1, '7' => 2, '8' => 3, '9' => 4, 'A' => 5, 'B' => 0,
       
    73         'C' => 1, 'D' => 2, 'E' => 3, 'F' => 4, 'G' => 5, 'H' => 0,
       
    74         'I' => 1, 'J' => 2, 'K' => 3, 'L' => 4, 'M' => 5, 'N' => 0,
       
    75         'O' => 1, 'P' => 2, 'Q' => 3, 'R' => 4, 'S' => 5, 'T' => 0,
       
    76         'U' => 1, 'V' => 2, 'W' => 3, 'X' => 4, 'Y' => 5, 'Z' => 0,
       
    77     );
       
    78 
       
    79     /**
       
    80      * Default options for Postnet barcode
       
    81      * @return void
       
    82      */
       
    83     protected function _getDefaultOptions()
       
    84     {
       
    85         $this->_barThinWidth = 2;
       
    86         $this->_barHeight = 20;
       
    87         $this->_drawText = false;
       
    88         $this->_stretchText = true;
       
    89         $this->_mandatoryChecksum = true;
       
    90     }
       
    91 
       
    92     /**
       
    93      * Width of the barcode (in pixels)
       
    94      * @return integer
       
    95      */
       
    96     protected function _calculateBarcodeWidth()
       
    97     {
       
    98         $quietZone       = $this->getQuietZone();
       
    99         $startCharacter  = (2 * $this->_barThinWidth) * $this->_factor;
       
   100         $stopCharacter   = (1 * $this->_barThinWidth) * $this->_factor;
       
   101         $encodedData     = (8 * $this->_barThinWidth) * $this->_factor * strlen($this->getText());
       
   102         return $quietZone + $startCharacter + $encodedData + $stopCharacter + $quietZone;
       
   103     }
       
   104 
       
   105     /**
       
   106      * Partial check of interleaved Postnet barcode
       
   107      * @return void
       
   108      */
       
   109     protected function _checkParams()
       
   110     {}
       
   111 
       
   112     /**
       
   113      * Prepare array to draw barcode
       
   114      * @return array
       
   115      */
       
   116     protected function _prepareBarcode()
       
   117     {
       
   118         $barcodeTable = array();
       
   119 
       
   120         // Start character (1)
       
   121         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 5/8);
       
   122         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
       
   123 
       
   124         // Text to encode
       
   125         $textTable = str_split($this->getText());
       
   126         foreach ($textTable as $char) {
       
   127             $bars = str_split($this->_codingMap[$char]);
       
   128             foreach ($bars as $b) {
       
   129                 $barcodeTable[] = array(1 , $this->_barThinWidth , ($b > 1 ? 3/8 : 0) , ($b % 2 ? 5/8 : 1));
       
   130                 $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
       
   131             }
       
   132         }
       
   133 
       
   134         // Stop character (1)
       
   135         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
       
   136         return $barcodeTable;
       
   137     }
       
   138 
       
   139     /**
       
   140      * Get barcode checksum
       
   141      *
       
   142      * @param  string $text
       
   143      * @return int
       
   144      */
       
   145     public function getChecksum($text)
       
   146     {
       
   147         $this->_checkText($text);
       
   148         $values   = str_split($text);
       
   149         $rowvalue = 0;
       
   150         $colvalue = 0;
       
   151         foreach($values as $row) {
       
   152             $rowvalue += $this->_rows[$row];
       
   153             $colvalue += $this->_columns[$row];
       
   154         }
       
   155 
       
   156         $rowvalue %= 6;
       
   157         $colvalue %= 6;
       
   158 
       
   159         $rowchkvalue = array_keys($this->_rows, $rowvalue);
       
   160         $colchkvalue = array_keys($this->_columns, $colvalue);
       
   161         return current(array_intersect($rowchkvalue, $colchkvalue));
       
   162     }
       
   163 }