PageRenderTime 53ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/libraries/3rdparty/Zend/Validate/Isbn.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 279 lines | 139 code | 28 blank | 112 comment | 36 complexity | b3a79bf155c75887b431687f99b30cc4 MD5 | raw file
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Isbn.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * @see Zend_Validate_Abstract
  23. */
  24. require_once 'Zend/Validate/Abstract.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Validate
  28. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Validate_Isbn extends Zend_Validate_Abstract
  32. {
  33. const AUTO = 'auto';
  34. const ISBN10 = '10';
  35. const ISBN13 = '13';
  36. const INVALID = 'isbnInvalid';
  37. const NO_ISBN = 'isbnNoIsbn';
  38. /**
  39. * Validation failure message template definitions.
  40. *
  41. * @var array
  42. */
  43. protected $_messageTemplates = array(
  44. self::INVALID => "Invalid type given. String or integer expected",
  45. self::NO_ISBN => "'%value%' is no valid ISBN number",
  46. );
  47. /**
  48. * Allowed type.
  49. *
  50. * @var string
  51. */
  52. protected $_type = self::AUTO;
  53. /**
  54. * Separator character.
  55. *
  56. * @var string
  57. */
  58. protected $_separator = '';
  59. /**
  60. * Set up options.
  61. *
  62. * @param Zend_Config|array $options
  63. * @throws Zend_Validate_Exception When $options is not valid
  64. * @return void
  65. */
  66. public function __construct($options = array())
  67. {
  68. // prepare options
  69. if ($options instanceof Zend_Config) {
  70. $options = $options->toArray();
  71. }
  72. if (!is_array($options)) {
  73. /**
  74. * @see Zend_Validate_Exception
  75. */
  76. require_once 'Zend/Validate/Exception.php';
  77. throw new Zend_Validate_Exception('Invalid options provided.');
  78. }
  79. // set type
  80. if (array_key_exists('type', $options)) {
  81. $this->setType($options['type']);
  82. }
  83. // set separator
  84. if (array_key_exists('separator', $options)) {
  85. $this->setSeparator($options['separator']);
  86. }
  87. }
  88. /**
  89. * Detect input format.
  90. *
  91. * @return string
  92. */
  93. protected function _detectFormat()
  94. {
  95. // prepare separator and pattern list
  96. $sep = quotemeta($this->_separator);
  97. $patterns = array();
  98. $lengths = array();
  99. // check for ISBN-10
  100. if ($this->_type == self::ISBN10 || $this->_type == self::AUTO) {
  101. if (empty($sep)) {
  102. $pattern = '/^[0-9]{9}[0-9X]{1}$/';
  103. $length = 10;
  104. } else {
  105. $pattern = "/^[0-9]{1,7}[{$sep}]{1}[0-9]{1,7}[{$sep}]{1}[0-9]{1,7}[{$sep}]{1}[0-9X]{1}$/";
  106. $length = 13;
  107. }
  108. $patterns[$pattern] = self::ISBN10;
  109. $lengths[$pattern] = $length;
  110. }
  111. // check for ISBN-13
  112. if ($this->_type == self::ISBN13 || $this->_type == self::AUTO) {
  113. if (empty($sep)) {
  114. $pattern = '/^[0-9]{13}$/';
  115. $length = 13;
  116. } else {
  117. $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}$/";
  118. $length = 17;
  119. }
  120. $patterns[$pattern] = self::ISBN13;
  121. $lengths[$pattern] = $length;
  122. }
  123. // check pattern list
  124. foreach ($patterns as $pattern => $type) {
  125. if ((strlen($this->_value) == $lengths[$pattern]) && preg_match($pattern, $this->_value)) {
  126. return $type;
  127. }
  128. }
  129. return null;
  130. }
  131. /**
  132. * Defined by Zend_Validate_Interface.
  133. *
  134. * Returns true if and only if $value is a valid ISBN.
  135. *
  136. * @param string $value
  137. * @return boolean
  138. */
  139. public function isValid($value)
  140. {
  141. if (!is_string($value) && !is_int($value)) {
  142. $this->_error(self::INVALID);
  143. return false;
  144. }
  145. $value = (string) $value;
  146. $this->_setValue($value);
  147. switch ($this->_detectFormat()) {
  148. case self::ISBN10:
  149. // sum
  150. $isbn10 = str_replace($this->_separator, '', $value);
  151. $sum = 0;
  152. for ($i = 0; $i < 9; $i++) {
  153. $sum += (10 - $i) * $isbn10{$i};
  154. }
  155. // checksum
  156. $checksum = 11 - ($sum % 11);
  157. if ($checksum == 11) {
  158. $checksum = '0';
  159. } elseif ($checksum == 10) {
  160. $checksum = 'X';
  161. }
  162. break;
  163. case self::ISBN13:
  164. // sum
  165. $isbn13 = str_replace($this->_separator, '', $value);
  166. $sum = 0;
  167. for ($i = 0; $i < 12; $i++) {
  168. if ($i % 2 == 0) {
  169. $sum += $isbn13{$i};
  170. } else {
  171. $sum += 3 * $isbn13{$i};
  172. }
  173. }
  174. // checksum
  175. $checksum = 10 - ($sum % 10);
  176. if ($checksum == 10) {
  177. $checksum = '0';
  178. }
  179. break;
  180. default:
  181. $this->_error(self::NO_ISBN);
  182. return false;
  183. }
  184. // validate
  185. if (substr($this->_value, -1) != $checksum) {
  186. $this->_error(self::NO_ISBN);
  187. return false;
  188. }
  189. return true;
  190. }
  191. /**
  192. * Set separator characters.
  193. *
  194. * It is allowed only empty string, hyphen and space.
  195. *
  196. * @param string $separator
  197. * @throws Zend_Validate_Exception When $separator is not valid
  198. * @return Zend_Validate_Isbn Provides a fluent interface
  199. */
  200. public function setSeparator($separator)
  201. {
  202. // check separator
  203. if (!in_array($separator, array('-', ' ', ''))) {
  204. /**
  205. * @see Zend_Validate_Exception
  206. */
  207. require_once 'Zend/Validate/Exception.php';
  208. throw new Zend_Validate_Exception('Invalid ISBN separator.');
  209. }
  210. $this->_separator = $separator;
  211. return $this;
  212. }
  213. /**
  214. * Get separator characters.
  215. *
  216. * @return string
  217. */
  218. public function getSeparator()
  219. {
  220. return $this->_separator;
  221. }
  222. /**
  223. * Set allowed ISBN type.
  224. *
  225. * @param string $type
  226. * @throws Zend_Validate_Exception When $type is not valid
  227. * @return Zend_Validate_Isbn Provides a fluent interface
  228. */
  229. public function setType($type)
  230. {
  231. // check type
  232. if (!in_array($type, array(self::AUTO, self::ISBN10, self::ISBN13))) {
  233. /**
  234. * @see Zend_Validate_Exception
  235. */
  236. require_once 'Zend/Validate/Exception.php';
  237. throw new Zend_Validate_Exception('Invalid ISBN type');
  238. }
  239. $this->_type = $type;
  240. return $this;
  241. }
  242. /**
  243. * Get allowed ISBN type.
  244. *
  245. * @return string
  246. */
  247. public function getType()
  248. {
  249. return $this->_type;
  250. }
  251. }