|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00025 require_once 'Zend/Validate/Abstract.php'; 00026 00033 class Zend_Validate_Isbn extends Zend_Validate_Abstract 00034 { 00035 const AUTO = 'auto'; 00036 const ISBN10 = '10'; 00037 const ISBN13 = '13'; 00038 const INVALID = 'isbnInvalid'; 00039 const NO_ISBN = 'isbnNoIsbn'; 00040 00046 protected $_messageTemplates = array( 00047 self::INVALID => "Invalid type given, value should be string or integer", 00048 self::NO_ISBN => "'%value%' is no valid ISBN number", 00049 ); 00050 00056 protected $_type = self::AUTO; 00057 00063 protected $_separator = ''; 00064 00072 public function __construct($options = array()) 00073 { 00074 // prepare options 00075 if ($options instanceof Zend_Config) { 00076 $options = $options->toArray(); 00077 } 00078 if (!is_array($options)) { 00082 require_once 'Zend/Validate/Exception.php'; 00083 throw new Zend_Validate_Exception('Invalid options provided.'); 00084 } 00085 00086 // set type 00087 if (array_key_exists('type', $options)) { 00088 $this->setType($options['type']); 00089 } 00090 00091 // set separator 00092 if (array_key_exists('separator', $options)) { 00093 $this->setSeparator($options['separator']); 00094 } 00095 } 00096 00102 protected function _detectFormat() 00103 { 00104 // prepare separator and pattern list 00105 $sep = quotemeta($this->_separator); 00106 $patterns = array(); 00107 $lengths = array(); 00108 00109 // check for ISBN-10 00110 if ($this->_type == self::ISBN10 || $this->_type == self::AUTO) { 00111 if (empty($sep)) { 00112 $pattern = '/^[0-9]{9}[0-9X]{1}$/'; 00113 $length = 10; 00114 } else { 00115 $pattern = "/^[0-9]{1,7}[{$sep}]{1}[0-9]{1,7}[{$sep}]{1}[0-9]{1,7}[{$sep}]{1}[0-9X]{1}$/"; 00116 $length = 13; 00117 } 00118 00119 $patterns[$pattern] = self::ISBN10; 00120 $lengths[$pattern] = $length; 00121 } 00122 00123 // check for ISBN-13 00124 if ($this->_type == self::ISBN13 || $this->_type == self::AUTO) { 00125 if (empty($sep)) { 00126 $pattern = '/^[0-9]{13}$/'; 00127 $length = 13; 00128 } else { 00129 $pattern = "/^[0-9]{1,9}[{$sep}]{1}[0-9]{1,5}[{$sep}]{1}[0-9]{1,9}[{$sep}]{1}[0-9]{1,9}[{$sep}]{1}[0-9]{1}$/"; 00130 $length = 17; 00131 } 00132 00133 $patterns[$pattern] = self::ISBN13; 00134 $lengths[$pattern] = $length; 00135 } 00136 00137 // check pattern list 00138 foreach ($patterns as $pattern => $type) { 00139 if ((strlen($this->_value) == $lengths[$pattern]) && preg_match($pattern, $this->_value)) { 00140 return $type; 00141 } 00142 } 00143 00144 return null; 00145 } 00146 00155 public function isValid($value) 00156 { 00157 if (!is_string($value) && !is_int($value)) { 00158 $this->_error(self::INVALID); 00159 return false; 00160 } 00161 00162 $value = (string) $value; 00163 $this->_setValue($value); 00164 00165 switch ($this->_detectFormat()) { 00166 case self::ISBN10: 00167 // sum 00168 $isbn10 = str_replace($this->_separator, '', $value); 00169 $sum = 0; 00170 for ($i = 0; $i < 9; $i++) { 00171 $sum += (10 - $i) * $isbn10{$i}; 00172 } 00173 00174 // checksum 00175 $checksum = 11 - ($sum % 11); 00176 if ($checksum == 11) { 00177 $checksum = '0'; 00178 } elseif ($checksum == 10) { 00179 $checksum = 'X'; 00180 } 00181 break; 00182 00183 case self::ISBN13: 00184 // sum 00185 $isbn13 = str_replace($this->_separator, '', $value); 00186 $sum = 0; 00187 for ($i = 0; $i < 12; $i++) { 00188 if ($i % 2 == 0) { 00189 $sum += $isbn13{$i}; 00190 } else { 00191 $sum += 3 * $isbn13{$i}; 00192 } 00193 } 00194 // checksum 00195 $checksum = 10 - ($sum % 10); 00196 if ($checksum == 10) { 00197 $checksum = '0'; 00198 } 00199 break; 00200 00201 default: 00202 $this->_error(self::NO_ISBN); 00203 return false; 00204 } 00205 00206 // validate 00207 if (substr($this->_value, -1) != $checksum) { 00208 $this->_error(self::NO_ISBN); 00209 return false; 00210 } 00211 return true; 00212 } 00213 00223 public function setSeparator($separator) 00224 { 00225 // check separator 00226 if (!in_array($separator, array('-', ' ', ''))) { 00230 require_once 'Zend/Validate/Exception.php'; 00231 throw new Zend_Validate_Exception('Invalid ISBN separator.'); 00232 } 00233 00234 $this->_separator = $separator; 00235 return $this; 00236 } 00237 00243 public function getSeparator() 00244 { 00245 return $this->_separator; 00246 } 00247 00255 public function setType($type) 00256 { 00257 // check type 00258 if (!in_array($type, array(self::AUTO, self::ISBN10, self::ISBN13))) { 00262 require_once 'Zend/Validate/Exception.php'; 00263 throw new Zend_Validate_Exception('Invalid ISBN type'); 00264 } 00265 00266 $this->_type = $type; 00267 return $this; 00268 } 00269 00275 public function getType() 00276 { 00277 return $this->_type; 00278 } 00279 }