File indexing completed on 2024-06-16 05:30:32

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_Validate
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Validate_Barcode_AdapterAbstract
0024  */
0025 // require_once 'Zend/Validate/Barcode/AdapterAbstract.php';
0026 
0027 /**
0028  * @category   Zend
0029  * @package    Zend_Validate
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 class Zend_Validate_Barcode_Code93 extends Zend_Validate_Barcode_AdapterAbstract
0034 {
0035     /**
0036      * Allowed barcode lengths
0037      * @var integer
0038      */
0039     protected $_length = -1;
0040 
0041     /**
0042      * Allowed barcode characters
0043      * @var string
0044      */
0045     protected $_characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ -.$/+%';
0046 
0047     /**
0048      * Checksum function
0049      * @var string
0050      */
0051     protected $_checksum = '_code93';
0052 
0053     /**
0054      * Note that the characters !"§& are only synonyms
0055      * @var array
0056      */
0057     protected $_check = array(
0058         '0' =>  0, '1' =>  1, '2' =>  2, '3' =>  3, '4' =>  4, '5' =>  5, '6' =>  6,
0059         '7' =>  7, '8' =>  8, '9' =>  9, 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13,
0060         'E' => 14, 'F' => 15, 'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19, 'K' => 20,
0061         'L' => 21, 'M' => 22, 'N' => 23, 'O' => 24, 'P' => 25, 'Q' => 26, 'R' => 27,
0062         'S' => 28, 'T' => 29, 'U' => 30, 'V' => 31, 'W' => 32, 'X' => 33, 'Y' => 34,
0063         'Z' => 35, '-' => 36, '.' => 37, ' ' => 38, '$' => 39, '/' => 40, '+' => 41,
0064         '%' => 42, '!' => 43, '"' => 44, '§' => 45, '&' => 46,
0065     );
0066 
0067     /**
0068      * Constructor
0069      *
0070      * Sets check flag to false.
0071      */
0072     public function __construct()
0073     {
0074         $this->setCheck(false);
0075     }
0076 
0077     /**
0078      * Validates the checksum (Modulo CK)
0079      *
0080      * @param  string $value The barcode to validate
0081      * @return boolean
0082      */
0083     protected function _code93($value)
0084     {
0085         $checksum = substr($value, -2, 2);
0086         $value    = str_split(substr($value, 0, -2));
0087         $count    = 0;
0088         $length   = count($value) % 20;
0089         foreach($value as $char) {
0090             if ($length == 0) {
0091                 $length = 20;
0092             }
0093 
0094             $count += $this->_check[$char] * $length;
0095             --$length;
0096         }
0097 
0098         $check   = array_search(($count % 47), $this->_check);
0099         $value[] = $check;
0100         $count   = 0;
0101         $length  = count($value) % 15;
0102         foreach($value as $char) {
0103             if ($length == 0) {
0104                 $length = 15;
0105             }
0106 
0107             $count += $this->_check[$char] * $length;
0108             --$length;
0109         }
0110         $check .= array_search(($count % 47), $this->_check);
0111 
0112         if ($check == $checksum) {
0113             return true;
0114         }
0115 
0116         return false;
0117     }
0118 }