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_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 Ean8 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_Ean8 extends Zend_Barcode_Object_Ean13
0042 {
0043 
0044     /**
0045      * Default options for Postnet barcode
0046      * @return void
0047      */
0048     protected function _getDefaultOptions()
0049     {
0050         $this->_barcodeLength = 8;
0051         $this->_mandatoryChecksum = true;
0052     }
0053 
0054     /**
0055      * Width of the barcode (in pixels)
0056      * @return integer
0057      */
0058     protected function _calculateBarcodeWidth()
0059     {
0060         $quietZone       = $this->getQuietZone();
0061         $startCharacter  = (3 * $this->_barThinWidth) * $this->_factor;
0062         $middleCharacter = (5 * $this->_barThinWidth) * $this->_factor;
0063         $stopCharacter   = (3 * $this->_barThinWidth) * $this->_factor;
0064         $encodedData     = (7 * $this->_barThinWidth) * $this->_factor * 8;
0065         return $quietZone + $startCharacter + $middleCharacter + $encodedData + $stopCharacter + $quietZone;
0066     }
0067 
0068         /**
0069      * Prepare array to draw barcode
0070      * @return array
0071      */
0072     protected function _prepareBarcode()
0073     {
0074         $barcodeTable = array();
0075         $height = ($this->_drawText) ? 1.1 : 1;
0076 
0077         // Start character (101)
0078         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0079         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
0080         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0081 
0082         $textTable = str_split($this->getText());
0083 
0084         // First part
0085         for ($i = 0; $i < 4; $i++) {
0086             $bars = str_split($this->_codingMap['A'][$textTable[$i]]);
0087             foreach ($bars as $b) {
0088                 $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
0089             }
0090         }
0091 
0092         // Middle character (01010)
0093         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
0094         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0095         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
0096         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0097         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
0098 
0099         // Second part
0100         for ($i = 4; $i < 8; $i++) {
0101             $bars = str_split($this->_codingMap['C'][$textTable[$i]]);
0102             foreach ($bars as $b) {
0103                 $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
0104             }
0105         }
0106 
0107         // Stop character (101)
0108         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0109         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
0110         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0111         return $barcodeTable;
0112     }
0113 
0114     /**
0115      * Partial function to draw text
0116      * @return void
0117      */
0118     protected function _drawText()
0119     {
0120         if ($this->_drawText) {
0121             $text = $this->getTextToDisplay();
0122             $characterWidth = (7 * $this->_barThinWidth) * $this->_factor;
0123             $leftPosition = $this->getQuietZone() + (3 * $this->_barThinWidth) * $this->_factor;
0124             for ($i = 0; $i < $this->_barcodeLength; $i ++) {
0125                 $this->_addText(
0126                     $text{$i},
0127                     $this->_fontSize * $this->_factor,
0128                     $this->_rotate(
0129                         $leftPosition,
0130                         (int) $this->_withBorder * 2
0131                             + $this->_factor * ($this->_barHeight + $this->_fontSize) + 1
0132                     ),
0133                     $this->_font,
0134                     $this->_foreColor,
0135                     'left',
0136                     - $this->_orientation
0137                 );
0138                 switch ($i) {
0139                     case 3:
0140                         $factor = 4;
0141                         break;
0142                     default:
0143                         $factor = 0;
0144                 }
0145                 $leftPosition = $leftPosition + $characterWidth + ($factor * $this->_barThinWidth * $this->_factor);
0146             }
0147         }
0148     }
0149 
0150     /**
0151      * Particular validation for Ean8 barcode objects
0152      * (to suppress checksum character substitution)
0153      *
0154      * @param string $value
0155      * @param array  $options
0156      * @throws Zend_Barcode_Object_Exception
0157      */
0158     protected function _validateText($value, $options = array())
0159     {
0160         $validator = new Zend_Validate_Barcode(array(
0161             'adapter'  => 'ean8',
0162             'checksum' => false,
0163         ));
0164 
0165         $value = $this->_addLeadingZeros($value, true);
0166 
0167         if (!$validator->isValid($value)) {
0168             $message = implode("\n", $validator->getMessages());
0169 
0170             /**
0171              * @see Zend_Barcode_Object_Exception
0172              */
0173             // require_once 'Zend/Barcode/Object/Exception.php';
0174             throw new Zend_Barcode_Object_Exception($message);
0175         }
0176     }
0177 }