Iban.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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-2009 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. * Validates IBAN Numbers (International Bank Account Numbers)
  27. *
  28. * @category Zend
  29. * @package Zend_Validate
  30. * @copyright Copyright (c) 2005-2009 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 => "'%value%' does not have IBAN",
  45. self::FALSEFORMAT => "'%value%' has a false 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. if (array_key_exists('locale', $locale)) {
  111. $locale = $locale['locale'];
  112. } else {
  113. require_once 'Zend/Validate/Exception.php';
  114. throw new Zend_Validate_Exception("Missing option 'locale'");
  115. }
  116. }
  117. if ($locale !== null) {
  118. $this->setLocale($locale);
  119. }
  120. }
  121. /**
  122. * Returns the locale option
  123. *
  124. * @return string|Zend_Locale|null
  125. */
  126. public function getLocale()
  127. {
  128. return $this->_locale;
  129. }
  130. /**
  131. * Sets the locale option
  132. *
  133. * @param string|Zend_Locale $locale
  134. * @return Zend_Validate_Date provides a fluent interface
  135. */
  136. public function setLocale($locale = null)
  137. {
  138. require_once 'Zend/Locale.php';
  139. $locale = Zend_Locale::findLocale($locale);
  140. if (strlen($locale) < 4) {
  141. require_once 'Zend/Validate/Exception.php';
  142. throw new Zend_Validate_Exception('Region must be given for IBAN validation');
  143. }
  144. $this->_locale = $locale;
  145. return $this;
  146. }
  147. /**
  148. * Defined by Zend_Validate_Interface
  149. *
  150. * Returns true if $value is a valid IBAN
  151. *
  152. * @param string $value
  153. * @return boolean
  154. */
  155. public function isValid($value)
  156. {
  157. $value = strtoupper($value);
  158. $this->_setValue($value);
  159. if (empty($this->_locale)) {
  160. $region = substr($value, 0, 2);
  161. } else {
  162. $region = new Zend_Locale($this->_locale);
  163. $region = $region->getRegion();
  164. }
  165. if (!array_key_exists($region, $this->_ibanregex)) {
  166. $this->_setValue($region);
  167. $this->_error(self::NOTSUPPORTED);
  168. return false;
  169. }
  170. if (!preg_match($this->_ibanregex[$region], $value)) {
  171. $this->_error(self::FALSEFORMAT);
  172. return false;
  173. }
  174. $format = substr($value, 4) . substr($value, 0, 4);
  175. $format = str_replace(
  176. array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  177. 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'),
  178. array('10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22',
  179. '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35'),
  180. $format);
  181. $temp = intval(substr($format, 0, 1));
  182. $len = strlen($format);
  183. for ($x = 1; $x < $len; ++$x) {
  184. $temp *= 10;
  185. $temp += intval(substr($format, $x, 1));
  186. $temp %= 97;
  187. }
  188. if ($temp != 1) {
  189. $this->_error(self::CHECKFAILED);
  190. return false;
  191. }
  192. return true;
  193. }
  194. }