web/lib/Zend/Barcode/Object/Ean5.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: Ean5.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Barcode_Object_Ean13
       
    25  */
       
    26 require_once 'Zend/Barcode/Object/Ean13.php';
       
    27 
       
    28 /**
       
    29  * @see Zend_Validate_Barcode
       
    30  */
       
    31 require_once 'Zend/Validate/Barcode.php';
       
    32 
       
    33 /**
       
    34  * Class for generate Ean5 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_Ean5 extends Zend_Barcode_Object_Ean13
       
    42 {
       
    43 
       
    44     protected $_parities = array(
       
    45         0 => array('B','B','A','A','A'),
       
    46         1 => array('B','A','B','A','A'),
       
    47         2 => array('B','A','A','B','A'),
       
    48         3 => array('B','A','A','A','B'),
       
    49         4 => array('A','B','B','A','A'),
       
    50         5 => array('A','A','B','B','A'),
       
    51         6 => array('A','A','A','B','B'),
       
    52         7 => array('A','B','A','B','A'),
       
    53         8 => array('A','B','A','A','B'),
       
    54         9 => array('A','A','B','A','B')
       
    55     );
       
    56 
       
    57     /**
       
    58      * Default options for Ean5 barcode
       
    59      * @return void
       
    60      */
       
    61     protected function _getDefaultOptions()
       
    62     {
       
    63         $this->_barcodeLength = 5;
       
    64     }
       
    65 
       
    66     /**
       
    67      * Width of the barcode (in pixels)
       
    68      * @return integer
       
    69      */
       
    70     protected function _calculateBarcodeWidth()
       
    71     {
       
    72         $quietZone       = $this->getQuietZone();
       
    73         $startCharacter  = (5 * $this->_barThinWidth) * $this->_factor;
       
    74         $middleCharacter = (2 * $this->_barThinWidth) * $this->_factor;
       
    75         $encodedData     = (7 * $this->_barThinWidth) * $this->_factor;
       
    76         return $quietZone + $startCharacter + ($this->_barcodeLength - 1) * $middleCharacter + $this->_barcodeLength * $encodedData + $quietZone;
       
    77     }
       
    78 
       
    79     /**
       
    80      * Prepare array to draw barcode
       
    81      * @return array
       
    82      */
       
    83     protected function _prepareBarcode()
       
    84     {
       
    85         $barcodeTable = array();
       
    86 
       
    87         // Start character (01011)
       
    88         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
       
    89         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
       
    90         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
       
    91         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
       
    92         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
       
    93 
       
    94         $firstCharacter = true;
       
    95         $textTable = str_split($this->getText());
       
    96 
       
    97         // Characters
       
    98         for ($i = 0; $i < $this->_barcodeLength; $i++) {
       
    99             if ($firstCharacter) {
       
   100                 $firstCharacter = false;
       
   101             } else {
       
   102                 // Intermediate character (01)
       
   103                 $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
       
   104                 $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
       
   105             }
       
   106             $bars = str_split($this->_codingMap[$this->_getParity($i)][$textTable[$i]]);
       
   107             foreach ($bars as $b) {
       
   108                 $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
       
   109             }
       
   110         }
       
   111 
       
   112         return $barcodeTable;
       
   113     }
       
   114 
       
   115     /**
       
   116      * Get barcode checksum
       
   117      *
       
   118      * @param  string $text
       
   119      * @return int
       
   120      */
       
   121     public function getChecksum($text)
       
   122     {
       
   123         $this->_checkText($text);
       
   124         $checksum = 0;
       
   125 
       
   126         for ($i = 0 ; $i < $this->_barcodeLength; $i ++) {
       
   127             $checksum += intval($text{$i}) * ($i % 2 ? 9 : 3);
       
   128         }
       
   129 
       
   130         return ($checksum % 10);
       
   131     }
       
   132 
       
   133     protected function _getParity($i)
       
   134     {
       
   135         $checksum = $this->getChecksum($this->getText());
       
   136         return $this->_parities[$checksum][$i];
       
   137     }
       
   138 
       
   139     /**
       
   140      * Retrieve text to encode
       
   141      * @return string
       
   142      */
       
   143     public function getText()
       
   144     {
       
   145         return $this->_addLeadingZeros($this->_text);
       
   146     }
       
   147 }