web/lib/Zend/Validate/Barcode/Issn.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: Issn.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_Issn extends Zend_Validate_Barcode_AdapterAbstract
       
    34 {
       
    35     /**
       
    36      * Allowed barcode lengths
       
    37      * @var integer
       
    38      */
       
    39     protected $_length = array(8, 13);
       
    40 
       
    41     /**
       
    42      * Allowed barcode characters
       
    43      * @var string
       
    44      */
       
    45     protected $_characters = '0123456789X';
       
    46 
       
    47     /**
       
    48      * Checksum function
       
    49      * @var string
       
    50      */
       
    51     protected $_checksum = '_gtin';
       
    52 
       
    53     /**
       
    54      * Allows X on length of 8 chars
       
    55      *
       
    56      * @param  string $value The barcode to check for allowed characters
       
    57      * @return boolean
       
    58      */
       
    59     public function checkChars($value)
       
    60     {
       
    61         if (strlen($value) != 8) {
       
    62             if (strpos($value, 'X') !== false) {
       
    63                 return false;
       
    64             }
       
    65         }
       
    66 
       
    67         return parent::checkChars($value);
       
    68     }
       
    69 
       
    70     /**
       
    71      * Validates the checksum
       
    72      *
       
    73      * @param  string $value The barcode to check the checksum for
       
    74      * @return boolean
       
    75      */
       
    76     public function checksum($value)
       
    77     {
       
    78         if (strlen($value) == 8) {
       
    79             $this->_checksum = '_issn';
       
    80         } else {
       
    81             $this->_checksum = '_gtin';
       
    82         }
       
    83 
       
    84         return parent::checksum($value);
       
    85     }
       
    86 
       
    87     /**
       
    88      * Validates the checksum ()
       
    89      * ISSN implementation (reversed mod11)
       
    90      *
       
    91      * @param  string $value The barcode to validate
       
    92      * @return boolean
       
    93      */
       
    94     protected function _issn($value)
       
    95     {
       
    96         $checksum = substr($value, -1, 1);
       
    97         $values   = str_split(substr($value, 0, -1));
       
    98         $check    = 0;
       
    99         $multi    = 8;
       
   100         foreach($values as $token) {
       
   101             if ($token == 'X') {
       
   102                 $token = 10;
       
   103             }
       
   104 
       
   105             $check += ($token * $multi);
       
   106             --$multi;
       
   107         }
       
   108 
       
   109         $check %= 11;
       
   110         $check  = 11 - $check;
       
   111         if ($check == $checksum) {
       
   112             return true;
       
   113         } else if (($check == 10) && ($checksum == 'X')) {
       
   114             return true;
       
   115         }
       
   116 
       
   117         return false;
       
   118     }
       
   119 }