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_Issn extends Zend_Validate_Barcode_AdapterAbstract
0034 {
0035     /**
0036      * Allowed barcode lengths
0037      * @var integer
0038      */
0039     protected $_length = array(8, 13);
0040 
0041     /**
0042      * Allowed barcode characters
0043      * @var string
0044      */
0045     protected $_characters = '0123456789X';
0046 
0047     /**
0048      * Checksum function
0049      * @var string
0050      */
0051     protected $_checksum = '_gtin';
0052 
0053     /**
0054      * Allows X on length of 8 chars
0055      *
0056      * @param  string $value The barcode to check for allowed characters
0057      * @return boolean
0058      */
0059     public function checkChars($value)
0060     {
0061         if (strlen($value) != 8) {
0062             if (strpos($value, 'X') !== false) {
0063                 return false;
0064             }
0065         }
0066 
0067         return parent::checkChars($value);
0068     }
0069 
0070     /**
0071      * Validates the checksum
0072      *
0073      * @param  string $value The barcode to check the checksum for
0074      * @return boolean
0075      */
0076     public function checksum($value)
0077     {
0078         if (strlen($value) == 8) {
0079             $this->_checksum = '_issn';
0080         } else {
0081             $this->_checksum = '_gtin';
0082         }
0083 
0084         return parent::checksum($value);
0085     }
0086 
0087     /**
0088      * Validates the checksum ()
0089      * ISSN implementation (reversed mod11)
0090      *
0091      * @param  string $value The barcode to validate
0092      * @return boolean
0093      */
0094     protected function _issn($value)
0095     {
0096         $checksum = substr($value, -1, 1);
0097         $values   = str_split(substr($value, 0, -1));
0098         $check    = 0;
0099         $multi    = 8;
0100         foreach($values as $token) {
0101             if ($token == 'X') {
0102                 $token = 10;
0103             }
0104 
0105             $check += ($token * $multi);
0106             --$multi;
0107         }
0108 
0109         $check %= 11;
0110         $check  = 11 - $check;
0111         if ($check == $checksum) {
0112             return true;
0113         } else if (($check == 10) && ($checksum == 'X')) {
0114             return true;
0115         }
0116 
0117         return false;
0118     }
0119 }