PageRenderTime 34ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/concreteOLD/libraries/3rdparty/Zend/Validate/Iban.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 223 lines | 133 code | 20 blank | 70 comment | 16 complexity | ce37697ac8e3581b64d45bb9304a942d 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: Iban.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. * Validates IBAN Numbers (International Bank Account Numbers)
  27. *
  28. * @category Zend
  29. * @package Zend_Validate
  30. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Validate_Iban extends Zend_Validate_Abstract
  34. {
  35. const NOTSUPPORTED = 'ibanNotSupported';
  36. const FALSEFORMAT = 'ibanFalseFormat';
  37. const CHECKFAILED = 'ibanCheckFailed';
  38. /**
  39. * Validation failure message template definitions
  40. *
  41. * @var array
  42. */
  43. protected $_messageTemplates = array(
  44. self::NOTSUPPORTED => "Unknown country within the IBAN '%value%'",
  45. self::FALSEFORMAT => "'%value%' has a false IBAN format",
  46. self::CHECKFAILED => "'%value%' has failed the IBAN check",
  47. );
  48. /**
  49. * Optional locale
  50. *
  51. * @var string|Zend_Locale|null
  52. */
  53. protected $_locale;
  54. /**
  55. * IBAN regexes by region
  56. *
  57. * @var array
  58. */
  59. protected $_ibanregex = array(
  60. 'AD' => '/^AD[0-9]{2}[0-9]{8}[A-Z0-9]{12}$/',
  61. 'AT' => '/^AT[0-9]{2}[0-9]{5}[0-9]{11}$/',
  62. 'BA' => '/^BA[0-9]{2}[0-9]{6}[0-9]{10}$/',
  63. 'BE' => '/^BE[0-9]{2}[0-9]{3}[0-9]{9}$/',
  64. 'BG' => '/^BG[0-9]{2}[A-Z]{4}[0-9]{4}[0-9]{2}[A-Z0-9]{8}$/',
  65. 'CH' => '/^CH[0-9]{2}[0-9]{5}[A-Z0-9]{12}$/',
  66. 'CS' => '/^CS[0-9]{2}[0-9]{3}[0-9]{15}$/',
  67. 'CY' => '/^CY[0-9]{2}[0-9]{8}[A-Z0-9]{16}$/',
  68. 'CZ' => '/^CZ[0-9]{2}[0-9]{4}[0-9]{16}$/',
  69. 'DE' => '/^DE[0-9]{2}[0-9]{8}[0-9]{10}$/',
  70. 'DK' => '/^DK[0-9]{2}[0-9]{4}[0-9]{10}$/',
  71. 'EE' => '/^EE[0-9]{2}[0-9]{4}[0-9]{12}$/',
  72. 'ES' => '/^ES[0-9]{2}[0-9]{8}[0-9]{12}$/',
  73. 'FR' => '/^FR[0-9]{2}[0-9]{10}[A-Z0-9]{13}$/',
  74. 'FI' => '/^FI[0-9]{2}[0-9]{6}[0-9]{8}$/',
  75. 'GB' => '/^GB[0-9]{2}[A-Z]{4}[0-9]{14}$/',
  76. 'GI' => '/^GI[0-9]{2}[A-Z]{4}[A-Z0-9]{15}$/',
  77. 'GR' => '/^GR[0-9]{2}[0-9]{7}[A-Z0-9]{16}$/',
  78. 'HR' => '/^HR[0-9]{2}[0-9]{7}[0-9]{10}$/',
  79. 'HU' => '/^HU[0-9]{2}[0-9]{7}[0-9]{1}[0-9]{15}[0-9]{1}$/',
  80. 'IE' => '/^IE[0-9]{2}[A-Z0-9]{4}[0-9]{6}[0-9]{8}$/',
  81. 'IS' => '/^IS[0-9]{2}[0-9]{4}[0-9]{18}$/',
  82. 'IT' => '/^IT[0-9]{2}[A-Z]{1}[0-9]{10}[A-Z0-9]{12}$/',
  83. 'LI' => '/^LI[0-9]{2}[0-9]{5}[A-Z0-9]{12}$/',
  84. 'LU' => '/^LU[0-9]{2}[0-9]{3}[A-Z0-9]{13}$/',
  85. 'LT' => '/^LT[0-9]{2}[0-9]{5}[0-9]{11}$/',
  86. 'LV' => '/^LV[0-9]{2}[A-Z]{4}[A-Z0-9]{13}$/',
  87. 'MK' => '/^MK[0-9]{2}[A-Z]{3}[A-Z0-9]{10}[0-9]{2}$/',
  88. 'MT' => '/^MT[0-9]{2}[A-Z]{4}[0-9]{5}[A-Z0-9]{18}$/',
  89. 'NL' => '/^NL[0-9]{2}[A-Z]{4}[0-9]{10}$/',
  90. 'NO' => '/^NO[0-9]{2}[0-9]{4}[0-9]{7}$/',
  91. 'PL' => '/^PL[0-9]{2}[0-9]{8}[0-9]{16}$/',
  92. 'PT' => '/^PT[0-9]{2}[0-9]{8}[0-9]{13}$/',
  93. 'RO' => '/^RO[0-9]{2}[A-Z]{4}[A-Z0-9]{16}$/',
  94. 'SE' => '/^SE[0-9]{2}[0-9]{3}[0-9]{17}$/',
  95. 'SI' => '/^SI[0-9]{2}[0-9]{5}[0-9]{8}[0-9]{2}$/',
  96. 'SK' => '/^SK[0-9]{2}[0-9]{4}[0-9]{16}$/',
  97. 'TN' => '/^TN[0-9]{2}[0-9]{5}[0-9]{15}$/',
  98. 'TR' => '/^TR[0-9]{2}[0-9]{5}[A-Z0-9]{17}$/'
  99. );
  100. /**
  101. * Sets validator options
  102. *
  103. * @param string|Zend_Config|Zend_Locale $locale OPTIONAL
  104. * @return void
  105. */
  106. public function __construct($locale = null)
  107. {
  108. if ($locale instanceof Zend_Config) {
  109. $locale = $locale->toArray();
  110. }
  111. if (is_array($locale)) {
  112. if (array_key_exists('locale', $locale)) {
  113. $locale = $locale['locale'];
  114. } else {
  115. $locale = null;
  116. }
  117. }
  118. if (empty($locale)) {
  119. require_once 'Zend/Registry.php';
  120. if (Zend_Registry::isRegistered('Zend_Locale')) {
  121. $locale = Zend_Registry::get('Zend_Locale');
  122. }
  123. }
  124. if ($locale !== null) {
  125. $this->setLocale($locale);
  126. }
  127. }
  128. /**
  129. * Returns the locale option
  130. *
  131. * @return string|Zend_Locale|null
  132. */
  133. public function getLocale()
  134. {
  135. return $this->_locale;
  136. }
  137. /**
  138. * Sets the locale option
  139. *
  140. * @param string|Zend_Locale $locale
  141. * @return Zend_Validate_Date provides a fluent interface
  142. */
  143. public function setLocale($locale = null)
  144. {
  145. if ($locale !== false) {
  146. require_once 'Zend/Locale.php';
  147. $locale = Zend_Locale::findLocale($locale);
  148. if (strlen($locale) < 4) {
  149. require_once 'Zend/Validate/Exception.php';
  150. throw new Zend_Validate_Exception('Region must be given for IBAN validation');
  151. }
  152. }
  153. $this->_locale = $locale;
  154. return $this;
  155. }
  156. /**
  157. * Defined by Zend_Validate_Interface
  158. *
  159. * Returns true if $value is a valid IBAN
  160. *
  161. * @param string $value
  162. * @return boolean
  163. */
  164. public function isValid($value)
  165. {
  166. $value = strtoupper($value);
  167. $this->_setValue($value);
  168. if (empty($this->_locale)) {
  169. $region = substr($value, 0, 2);
  170. } else {
  171. $region = new Zend_Locale($this->_locale);
  172. $region = $region->getRegion();
  173. }
  174. if (!array_key_exists($region, $this->_ibanregex)) {
  175. $this->_setValue($region);
  176. $this->_error(self::NOTSUPPORTED);
  177. return false;
  178. }
  179. if (!preg_match($this->_ibanregex[$region], $value)) {
  180. $this->_error(self::FALSEFORMAT);
  181. return false;
  182. }
  183. $format = substr($value, 4) . substr($value, 0, 4);
  184. $format = str_replace(
  185. array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  186. 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'),
  187. array('10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22',
  188. '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35'),
  189. $format);
  190. $temp = intval(substr($format, 0, 1));
  191. $len = strlen($format);
  192. for ($x = 1; $x < $len; ++$x) {
  193. $temp *= 10;
  194. $temp += intval(substr($format, $x, 1));
  195. $temp %= 97;
  196. }
  197. if ($temp != 1) {
  198. $this->_error(self::CHECKFAILED);
  199. return false;
  200. }
  201. return true;
  202. }
  203. }