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_Code25interleaved
0025  */
0026 // require_once 'Zend/Barcode/Object/Code25interleaved.php';
0027 
0028 /**
0029  * @see Zend_Validate_Barcode
0030  */
0031 // require_once 'Zend/Validate/Barcode.php';
0032 
0033 /**
0034  * Class for generate Identcode 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_Identcode extends Zend_Barcode_Object_Code25interleaved
0042 {
0043 
0044     /**
0045      * Default options for Identcode barcode
0046      * @return void
0047      */
0048     protected function _getDefaultOptions()
0049     {
0050         $this->_barcodeLength = 12;
0051         $this->_mandatoryChecksum = true;
0052     }
0053 
0054     /**
0055      * Retrieve text to display
0056      * @return string
0057      */
0058     public function getTextToDisplay()
0059     {
0060         return preg_replace('/([0-9]{2})([0-9]{3})([0-9]{3})([0-9]{3})([0-9])/',
0061                             '$1.$2 $3.$4 $5',
0062                             $this->getText());
0063     }
0064 
0065     /**
0066      * Check allowed characters
0067      * @param string $value
0068      * @return string
0069      * @throws Zend_Barcode_Object_Exception
0070      */
0071     public function validateText($value)
0072     {
0073         $this->_validateText($value, array('validator' => $this->getType()));
0074     }
0075 
0076     /**
0077      * Get barcode checksum
0078      *
0079      * @param  string $text
0080      * @return int
0081      */
0082     public function getChecksum($text)
0083     {
0084         $this->_checkText($text);
0085         $checksum = 0;
0086 
0087         for ($i = strlen($text); $i > 0; $i --) {
0088             $checksum += intval($text{$i - 1}) * (($i % 2) ? 4 : 9);
0089         }
0090 
0091         $checksum = (10 - ($checksum % 10)) % 10;
0092 
0093         return $checksum;
0094     }
0095 }