PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/zendframework1/library/Zend/Validate/Isbn.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 278 lines | 135 code | 28 blank | 115 comment | 36 complexity | aedbe250623c9a9ef228d1863104b839 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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  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-2015 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 not a 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. */
  65. public function __construct($options = array())
  66. {
  67. // prepare options
  68. if ($options instanceof Zend_Config) {
  69. $options = $options->toArray();
  70. }
  71. if (!is_array($options)) {
  72. /**
  73. * @see Zend_Validate_Exception
  74. */
  75. #require_once 'Zend/Validate/Exception.php';
  76. throw new Zend_Validate_Exception('Invalid options provided.');
  77. }
  78. // set type
  79. if (array_key_exists('type', $options)) {
  80. $this->setType($options['type']);
  81. }
  82. // set separator
  83. if (array_key_exists('separator', $options)) {
  84. $this->setSeparator($options['separator']);
  85. }
  86. }
  87. /**
  88. * Detect input format.
  89. *
  90. * @return string
  91. */
  92. protected function _detectFormat()
  93. {
  94. // prepare separator and pattern list
  95. $sep = quotemeta($this->_separator);
  96. $patterns = array();
  97. $lengths = array();
  98. // check for ISBN-10
  99. if ($this->_type == self::ISBN10 || $this->_type == self::AUTO) {
  100. if (empty($sep)) {
  101. $pattern = '/^[0-9]{9}[0-9X]{1}$/';
  102. $length = 10;
  103. } else {
  104. $pattern = "/^[0-9]{1,7}[{$sep}]{1}[0-9]{1,7}[{$sep}]{1}[0-9]{1,7}[{$sep}]{1}[0-9X]{1}$/";
  105. $length = 13;
  106. }
  107. $patterns[$pattern] = self::ISBN10;
  108. $lengths[$pattern] = $length;
  109. }
  110. // check for ISBN-13
  111. if ($this->_type == self::ISBN13 || $this->_type == self::AUTO) {
  112. if (empty($sep)) {
  113. $pattern = '/^[0-9]{13}$/';
  114. $length = 13;
  115. } else {
  116. $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}$/";
  117. $length = 17;
  118. }
  119. $patterns[$pattern] = self::ISBN13;
  120. $lengths[$pattern] = $length;
  121. }
  122. // check pattern list
  123. foreach ($patterns as $pattern => $type) {
  124. if ((strlen($this->_value) == $lengths[$pattern]) && preg_match($pattern, $this->_value)) {
  125. return $type;
  126. }
  127. }
  128. return null;
  129. }
  130. /**
  131. * Defined by Zend_Validate_Interface.
  132. *
  133. * Returns true if and only if $value is a valid ISBN.
  134. *
  135. * @param string $value
  136. * @return boolean
  137. */
  138. public function isValid($value)
  139. {
  140. if (!is_string($value) && !is_int($value)) {
  141. $this->_error(self::INVALID);
  142. return false;
  143. }
  144. $value = (string) $value;
  145. $this->_setValue($value);
  146. switch ($this->_detectFormat()) {
  147. case self::ISBN10:
  148. // sum
  149. $isbn10 = str_replace($this->_separator, '', $value);
  150. $sum = 0;
  151. for ($i = 0; $i < 9; $i++) {
  152. $sum += (10 - $i) * $isbn10{$i};
  153. }
  154. // checksum
  155. $checksum = 11 - ($sum % 11);
  156. if ($checksum == 11) {
  157. $checksum = '0';
  158. } elseif ($checksum == 10) {
  159. $checksum = 'X';
  160. }
  161. break;
  162. case self::ISBN13:
  163. // sum
  164. $isbn13 = str_replace($this->_separator, '', $value);
  165. $sum = 0;
  166. for ($i = 0; $i < 12; $i++) {
  167. if ($i % 2 == 0) {
  168. $sum += $isbn13{$i};
  169. } else {
  170. $sum += 3 * $isbn13{$i};
  171. }
  172. }
  173. // checksum
  174. $checksum = 10 - ($sum % 10);
  175. if ($checksum == 10) {
  176. $checksum = '0';
  177. }
  178. break;
  179. default:
  180. $this->_error(self::NO_ISBN);
  181. return false;
  182. }
  183. // validate
  184. if (substr($this->_value, -1) != $checksum) {
  185. $this->_error(self::NO_ISBN);
  186. return false;
  187. }
  188. return true;
  189. }
  190. /**
  191. * Set separator characters.
  192. *
  193. * It is allowed only empty string, hyphen and space.
  194. *
  195. * @param string $separator
  196. * @throws Zend_Validate_Exception When $separator is not valid
  197. * @return Zend_Validate_Isbn Provides a fluent interface
  198. */
  199. public function setSeparator($separator)
  200. {
  201. // check separator
  202. if (!in_array($separator, array('-', ' ', ''))) {
  203. /**
  204. * @see Zend_Validate_Exception
  205. */
  206. #require_once 'Zend/Validate/Exception.php';
  207. throw new Zend_Validate_Exception('Invalid ISBN separator.');
  208. }
  209. $this->_separator = $separator;
  210. return $this;
  211. }
  212. /**
  213. * Get separator characters.
  214. *
  215. * @return string
  216. */
  217. public function getSeparator()
  218. {
  219. return $this->_separator;
  220. }
  221. /**
  222. * Set allowed ISBN type.
  223. *
  224. * @param string $type
  225. * @throws Zend_Validate_Exception When $type is not valid
  226. * @return Zend_Validate_Isbn Provides a fluent interface
  227. */
  228. public function setType($type)
  229. {
  230. // check type
  231. if (!in_array($type, array(self::AUTO, self::ISBN10, self::ISBN13))) {
  232. /**
  233. * @see Zend_Validate_Exception
  234. */
  235. #require_once 'Zend/Validate/Exception.php';
  236. throw new Zend_Validate_Exception('Invalid ISBN type');
  237. }
  238. $this->_type = $type;
  239. return $this;
  240. }
  241. /**
  242. * Get allowed ISBN type.
  243. *
  244. * @return string
  245. */
  246. public function getType()
  247. {
  248. return $this->_type;
  249. }
  250. }