|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00025 require_once 'Zend/Validate/Barcode/AdapterInterface.php'; 00026 00033 abstract class Zend_Validate_Barcode_AdapterAbstract 00034 implements Zend_Validate_Barcode_AdapterInterface 00035 { 00040 protected $_length; 00041 00046 protected $_characters; 00047 00052 protected $_checksum; 00053 00058 protected $_hasChecksum = true; 00059 00066 public function checkLength($value) 00067 { 00068 if (!is_string($value)) { 00069 return false; 00070 } 00071 00072 $fixum = strlen($value); 00073 $found = false; 00074 $length = $this->getLength(); 00075 if (is_array($length)) { 00076 foreach ($length as $value) { 00077 if ($fixum == $value) { 00078 $found = true; 00079 } 00080 00081 if ($value == -1) { 00082 $found = true; 00083 } 00084 } 00085 } elseif ($fixum == $length) { 00086 $found = true; 00087 } elseif ($length == -1) { 00088 $found = true; 00089 } elseif ($length == 'even') { 00090 $count = $fixum % 2; 00091 $found = ($count == 0) ? true : false; 00092 } elseif ($length == 'odd') { 00093 $count = $fixum % 2; 00094 $found = ($count == 1) ? true : false; 00095 } 00096 00097 return $found; 00098 } 00099 00106 public function checkChars($value) 00107 { 00108 if (!is_string($value)) { 00109 return false; 00110 } 00111 00112 $characters = $this->getCharacters(); 00113 if ($characters == 128) { 00114 for ($x = 0; $x < 128; ++$x) { 00115 $value = str_replace(chr($x), '', $value); 00116 } 00117 } else { 00118 $chars = str_split($characters); 00119 foreach ($chars as $char) { 00120 $value = str_replace($char, '', $value); 00121 } 00122 } 00123 00124 if (strlen($value) > 0) { 00125 return false; 00126 } 00127 00128 return true; 00129 } 00130 00137 public function checksum($value) 00138 { 00139 $checksum = $this->getChecksum(); 00140 if (!empty($checksum)) { 00141 if (method_exists($this, $checksum)) { 00142 return call_user_func(array($this, $checksum), $value); 00143 } 00144 } 00145 00146 return false; 00147 } 00148 00154 public function getLength() 00155 { 00156 return $this->_length; 00157 } 00158 00164 public function getCharacters() 00165 { 00166 return $this->_characters; 00167 } 00168 00173 public function getChecksum() 00174 { 00175 return $this->_checksum; 00176 } 00177 00183 public function getCheck() 00184 { 00185 return $this->_hasChecksum; 00186 } 00187 00194 public function setCheck($check) 00195 { 00196 $this->_hasChecksum = (boolean) $check; 00197 return $this; 00198 } 00199 00207 protected function _gtin($value) 00208 { 00209 $barcode = substr($value, 0, -1); 00210 $sum = 0; 00211 $length = strlen($barcode) - 1; 00212 00213 for ($i = 0; $i <= $length; $i++) { 00214 if (($i % 2) === 0) { 00215 $sum += $barcode[$length - $i] * 3; 00216 } else { 00217 $sum += $barcode[$length - $i]; 00218 } 00219 } 00220 00221 $calc = $sum % 10; 00222 $checksum = ($calc === 0) ? 0 : (10 - $calc); 00223 if ($value[$length + 1] != $checksum) { 00224 return false; 00225 } 00226 00227 return true; 00228 } 00229 00237 protected function _identcode($value) 00238 { 00239 $barcode = substr($value, 0, -1); 00240 $sum = 0; 00241 $length = strlen($value) - 2; 00242 00243 for ($i = 0; $i <= $length; $i++) { 00244 if (($i % 2) === 0) { 00245 $sum += $barcode[$length - $i] * 4; 00246 } else { 00247 $sum += $barcode[$length - $i] * 9; 00248 } 00249 } 00250 00251 $calc = $sum % 10; 00252 $checksum = ($calc === 0) ? 0 : (10 - $calc); 00253 if ($value[$length + 1] != $checksum) { 00254 return false; 00255 } 00256 00257 return true; 00258 } 00259 00267 protected function _code25($value) 00268 { 00269 $barcode = substr($value, 0, -1); 00270 $sum = 0; 00271 $length = strlen($barcode) - 1; 00272 00273 for ($i = 0; $i <= $length; $i++) { 00274 if (($i % 2) === 0) { 00275 $sum += $barcode[$i] * 3; 00276 } else { 00277 $sum += $barcode[$i]; 00278 } 00279 } 00280 00281 $calc = $sum % 10; 00282 $checksum = ($calc === 0) ? 0 : (10 - $calc); 00283 if ($value[$length + 1] != $checksum) { 00284 return false; 00285 } 00286 00287 return true; 00288 } 00289 00297 protected function _postnet($value) 00298 { 00299 $checksum = substr($value, -1, 1); 00300 $values = str_split(substr($value, 0, -1)); 00301 00302 $check = 0; 00303 foreach($values as $row) { 00304 $check += $row; 00305 } 00306 00307 $check %= 10; 00308 $check = 10 - $check; 00309 if ($check == $checksum) { 00310 return true; 00311 } 00312 00313 return false; 00314 } 00315 }