web/lib/Zend/Validate/Barcode/Code93.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_Validate
       
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    19  * @version    $Id: Code93.php 20785 2010-01-31 09:43:03Z mikaelkael $
       
    20  */
       
    21 
       
    22 /**
       
    23  * @see Zend_Validate_Barcode_AdapterAbstract
       
    24  */
       
    25 require_once 'Zend/Validate/Barcode/AdapterAbstract.php';
       
    26 
       
    27 /**
       
    28  * @category   Zend
       
    29  * @package    Zend_Validate
       
    30  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    31  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    32  */
       
    33 class Zend_Validate_Barcode_Code93 extends Zend_Validate_Barcode_AdapterAbstract
       
    34 {
       
    35     /**
       
    36      * Allowed barcode lengths
       
    37      * @var integer
       
    38      */
       
    39     protected $_length = -1;
       
    40 
       
    41     /**
       
    42      * Allowed barcode characters
       
    43      * @var string
       
    44      */
       
    45     protected $_characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ -.$/+%';
       
    46 
       
    47     /**
       
    48      * Checksum function
       
    49      * @var string
       
    50      */
       
    51     protected $_checksum = '_code93';
       
    52 
       
    53     /**
       
    54      * Note that the characters !"§& are only synonyms
       
    55      * @var array
       
    56      */
       
    57     protected $_check = array(
       
    58         '0' =>  0, '1' =>  1, '2' =>  2, '3' =>  3, '4' =>  4, '5' =>  5, '6' =>  6,
       
    59         '7' =>  7, '8' =>  8, '9' =>  9, 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13,
       
    60         'E' => 14, 'F' => 15, 'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19, 'K' => 20,
       
    61         'L' => 21, 'M' => 22, 'N' => 23, 'O' => 24, 'P' => 25, 'Q' => 26, 'R' => 27,
       
    62         'S' => 28, 'T' => 29, 'U' => 30, 'V' => 31, 'W' => 32, 'X' => 33, 'Y' => 34,
       
    63         'Z' => 35, '-' => 36, '.' => 37, ' ' => 38, '$' => 39, '/' => 40, '+' => 41,
       
    64         '%' => 42, '!' => 43, '"' => 44, '§' => 45, '&' => 46,
       
    65     );
       
    66 
       
    67     /**
       
    68      * Constructor
       
    69      *
       
    70      * Sets check flag to false.
       
    71      *
       
    72      * @return void
       
    73      */
       
    74     public function __construct()
       
    75     {
       
    76         $this->setCheck(false);
       
    77     }
       
    78 
       
    79     /**
       
    80      * Validates the checksum (Modulo CK)
       
    81      *
       
    82      * @param  string $value The barcode to validate
       
    83      * @return boolean
       
    84      */
       
    85     protected function _code93($value)
       
    86     {
       
    87         $checksum = substr($value, -2, 2);
       
    88         $value    = str_split(substr($value, 0, -2));
       
    89         $count    = 0;
       
    90         $length   = count($value) % 20;
       
    91         foreach($value as $char) {
       
    92             if ($length == 0) {
       
    93                 $length = 20;
       
    94             }
       
    95 
       
    96             $count += $this->_check[$char] * $length;
       
    97             --$length;
       
    98         }
       
    99 
       
   100         $check   = array_search(($count % 47), $this->_check);
       
   101         $value[] = $check;
       
   102         $count   = 0;
       
   103         $length  = count($value) % 15;
       
   104         foreach($value as $char) {
       
   105             if ($length == 0) {
       
   106                 $length = 15;
       
   107             }
       
   108 
       
   109             $count += $this->_check[$char] * $length;
       
   110             --$length;
       
   111         }
       
   112         $check .= array_search(($count % 47), $this->_check);
       
   113 
       
   114         if ($check == $checksum) {
       
   115             return true;
       
   116         }
       
   117 
       
   118         return false;
       
   119     }
       
   120 }