File indexing completed on 2024-05-26 06:02:45

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_Ean13
0025  */
0026 // require_once 'Zend/Barcode/Object/Ean13.php';
0027 
0028 /**
0029  * @see Zend_Validate_Barcode
0030  */
0031 // require_once 'Zend/Validate/Barcode.php';
0032 
0033 /**
0034  * Class for generate Ean5 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_Ean5 extends Zend_Barcode_Object_Ean13
0042 {
0043 
0044     protected $_parities = array(
0045         0 => array('B','B','A','A','A'),
0046         1 => array('B','A','B','A','A'),
0047         2 => array('B','A','A','B','A'),
0048         3 => array('B','A','A','A','B'),
0049         4 => array('A','B','B','A','A'),
0050         5 => array('A','A','B','B','A'),
0051         6 => array('A','A','A','B','B'),
0052         7 => array('A','B','A','B','A'),
0053         8 => array('A','B','A','A','B'),
0054         9 => array('A','A','B','A','B')
0055     );
0056 
0057     /**
0058      * Default options for Ean5 barcode
0059      * @return void
0060      */
0061     protected function _getDefaultOptions()
0062     {
0063         $this->_barcodeLength = 5;
0064     }
0065 
0066     /**
0067      * Width of the barcode (in pixels)
0068      * @return integer
0069      */
0070     protected function _calculateBarcodeWidth()
0071     {
0072         $quietZone       = $this->getQuietZone();
0073         $startCharacter  = (5 * $this->_barThinWidth) * $this->_factor;
0074         $middleCharacter = (2 * $this->_barThinWidth) * $this->_factor;
0075         $encodedData     = (7 * $this->_barThinWidth) * $this->_factor;
0076         return $quietZone + $startCharacter + ($this->_barcodeLength - 1) * $middleCharacter + $this->_barcodeLength * $encodedData + $quietZone;
0077     }
0078 
0079     /**
0080      * Prepare array to draw barcode
0081      * @return array
0082      */
0083     protected function _prepareBarcode()
0084     {
0085         $barcodeTable = array();
0086 
0087         // Start character (01011)
0088         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
0089         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
0090         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
0091         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
0092         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
0093 
0094         $firstCharacter = true;
0095         $textTable = str_split($this->getText());
0096 
0097         // Characters
0098         for ($i = 0; $i < $this->_barcodeLength; $i++) {
0099             if ($firstCharacter) {
0100                 $firstCharacter = false;
0101             } else {
0102                 // Intermediate character (01)
0103                 $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
0104                 $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
0105             }
0106             $bars = str_split($this->_codingMap[$this->_getParity($i)][$textTable[$i]]);
0107             foreach ($bars as $b) {
0108                 $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
0109             }
0110         }
0111 
0112         return $barcodeTable;
0113     }
0114 
0115     /**
0116      * Get barcode checksum
0117      *
0118      * @param  string $text
0119      * @return int
0120      */
0121     public function getChecksum($text)
0122     {
0123         $this->_checkText($text);
0124         $checksum = 0;
0125 
0126         for ($i = 0 ; $i < $this->_barcodeLength; $i ++) {
0127             $checksum += intval($text{$i}) * ($i % 2 ? 9 : 3);
0128         }
0129 
0130         return ($checksum % 10);
0131     }
0132 
0133     protected function _getParity($i)
0134     {
0135         $checksum = $this->getChecksum($this->getText());
0136         return $this->_parities[$checksum][$i];
0137     }
0138 
0139     /**
0140      * Retrieve text to encode
0141      * @return string
0142      */
0143     public function getText()
0144     {
0145         return $this->_addLeadingZeros($this->_text);
0146     }
0147 }