File indexing completed on 2025-01-19 05:21:37
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_AdapterInterface 0024 */ 0025 // require_once 'Zend/Validate/Barcode/AdapterInterface.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 abstract class Zend_Validate_Barcode_AdapterAbstract 0034 implements Zend_Validate_Barcode_AdapterInterface 0035 { 0036 /** 0037 * Allowed barcode lengths 0038 * @var integer|array|string 0039 */ 0040 protected $_length; 0041 0042 /** 0043 * Allowed barcode characters 0044 * @var string 0045 */ 0046 protected $_characters; 0047 0048 /** 0049 * Callback to checksum function 0050 * @var string|array 0051 */ 0052 protected $_checksum; 0053 0054 /** 0055 * Is a checksum value included? 0056 * @var boolean 0057 */ 0058 protected $_hasChecksum = true; 0059 0060 /** 0061 * Checks the length of a barcode 0062 * 0063 * @param string $value The barcode to check for proper length 0064 * @return boolean 0065 */ 0066 public function checkLength($value) 0067 { 0068 if (!is_string($value)) { 0069 return false; 0070 } 0071 0072 $fixum = strlen($value); 0073 $found = false; 0074 $length = $this->getLength(); 0075 if (is_array($length)) { 0076 foreach ($length as $value) { 0077 if ($fixum == $value) { 0078 $found = true; 0079 } 0080 0081 if ($value == -1) { 0082 $found = true; 0083 } 0084 } 0085 } elseif ($fixum == $length) { 0086 $found = true; 0087 } elseif ($length == -1) { 0088 $found = true; 0089 } elseif ($length == 'even') { 0090 $count = $fixum % 2; 0091 $found = ($count == 0) ? true : false; 0092 } elseif ($length == 'odd') { 0093 $count = $fixum % 2; 0094 $found = ($count == 1) ? true : false; 0095 } 0096 0097 return $found; 0098 } 0099 0100 /** 0101 * Checks for allowed characters within the barcode 0102 * 0103 * @param string $value The barcode to check for allowed characters 0104 * @return boolean 0105 */ 0106 public function checkChars($value) 0107 { 0108 if (!is_string($value)) { 0109 return false; 0110 } 0111 0112 $characters = $this->getCharacters(); 0113 if ($characters == 128) { 0114 for ($x = 0; $x < 128; ++$x) { 0115 $value = str_replace(chr($x), '', $value); 0116 } 0117 } else { 0118 $chars = str_split($characters); 0119 foreach ($chars as $char) { 0120 $value = str_replace($char, '', $value); 0121 } 0122 } 0123 0124 if (strlen($value) > 0) { 0125 return false; 0126 } 0127 0128 return true; 0129 } 0130 0131 /** 0132 * Validates the checksum 0133 * 0134 * @param string $value The barcode to check the checksum for 0135 * @return boolean 0136 */ 0137 public function checksum($value) 0138 { 0139 $checksum = $this->getChecksum(); 0140 if (!empty($checksum)) { 0141 if (method_exists($this, $checksum)) { 0142 return call_user_func(array($this, $checksum), $value); 0143 } 0144 } 0145 0146 return false; 0147 } 0148 0149 /** 0150 * Returns the allowed barcode length 0151 * 0152 * @return string 0153 */ 0154 public function getLength() 0155 { 0156 return $this->_length; 0157 } 0158 0159 /** 0160 * Returns the allowed characters 0161 * 0162 * @return integer|string 0163 */ 0164 public function getCharacters() 0165 { 0166 return $this->_characters; 0167 } 0168 0169 /** 0170 * Returns the checksum function name 0171 * 0172 */ 0173 public function getChecksum() 0174 { 0175 return $this->_checksum; 0176 } 0177 0178 /** 0179 * Returns if barcode uses checksum 0180 * 0181 * @return boolean 0182 */ 0183 public function getCheck() 0184 { 0185 return $this->_hasChecksum; 0186 } 0187 0188 /** 0189 * Sets the checksum validation 0190 * 0191 * @param boolean $check 0192 * @return Zend_Validate_Barcode_AdapterAbstract 0193 */ 0194 public function setCheck($check) 0195 { 0196 $this->_hasChecksum = (boolean) $check; 0197 return $this; 0198 } 0199 0200 /** 0201 * Validates the checksum (Modulo 10) 0202 * GTIN implementation factor 3 0203 * 0204 * @param string $value The barcode to validate 0205 * @return boolean 0206 */ 0207 protected function _gtin($value) 0208 { 0209 $barcode = substr($value, 0, -1); 0210 $sum = 0; 0211 $length = strlen($barcode) - 1; 0212 0213 for ($i = 0; $i <= $length; $i++) { 0214 if (($i % 2) === 0) { 0215 $sum += $barcode[$length - $i] * 3; 0216 } else { 0217 $sum += $barcode[$length - $i]; 0218 } 0219 } 0220 0221 $calc = $sum % 10; 0222 $checksum = ($calc === 0) ? 0 : (10 - $calc); 0223 if ($value[$length + 1] != $checksum) { 0224 return false; 0225 } 0226 0227 return true; 0228 } 0229 0230 /** 0231 * Validates the checksum (Modulo 10) 0232 * IDENTCODE implementation factors 9 and 4 0233 * 0234 * @param string $value The barcode to validate 0235 * @return boolean 0236 */ 0237 protected function _identcode($value) 0238 { 0239 $barcode = substr($value, 0, -1); 0240 $sum = 0; 0241 $length = strlen($value) - 2; 0242 0243 for ($i = 0; $i <= $length; $i++) { 0244 if (($i % 2) === 0) { 0245 $sum += $barcode[$length - $i] * 4; 0246 } else { 0247 $sum += $barcode[$length - $i] * 9; 0248 } 0249 } 0250 0251 $calc = $sum % 10; 0252 $checksum = ($calc === 0) ? 0 : (10 - $calc); 0253 if ($value[$length + 1] != $checksum) { 0254 return false; 0255 } 0256 0257 return true; 0258 } 0259 0260 /** 0261 * Validates the checksum (Modulo 10) 0262 * CODE25 implementation factor 3 0263 * 0264 * @param string $value The barcode to validate 0265 * @return boolean 0266 */ 0267 protected function _code25($value) 0268 { 0269 $barcode = substr($value, 0, -1); 0270 $sum = 0; 0271 $length = strlen($barcode) - 1; 0272 0273 for ($i = 0; $i <= $length; $i++) { 0274 if (($i % 2) === 0) { 0275 $sum += $barcode[$i] * 3; 0276 } else { 0277 $sum += $barcode[$i]; 0278 } 0279 } 0280 0281 $calc = $sum % 10; 0282 $checksum = ($calc === 0) ? 0 : (10 - $calc); 0283 if ($value[$length + 1] != $checksum) { 0284 return false; 0285 } 0286 0287 return true; 0288 } 0289 0290 /** 0291 * Validates the checksum () 0292 * POSTNET implementation 0293 * 0294 * @param string $value The barcode to validate 0295 * @return boolean 0296 */ 0297 protected function _postnet($value) 0298 { 0299 $checksum = substr($value, -1, 1); 0300 $values = str_split(substr($value, 0, -1)); 0301 0302 $check = 0; 0303 foreach($values as $row) { 0304 $check += $row; 0305 } 0306 0307 $check %= 10; 0308 $check = 10 - $check; 0309 if ($check == $checksum) { 0310 return true; 0311 } 0312 0313 return false; 0314 } 0315 }