Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Iban.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Abstract.php';
26 
36 {
37  const NOTSUPPORTED = 'ibanNotSupported';
38  const FALSEFORMAT = 'ibanFalseFormat';
39  const CHECKFAILED = 'ibanCheckFailed';
40 
46  protected $_messageTemplates = array(
47  self::NOTSUPPORTED => "Unknown country within the IBAN '%value%'",
48  self::FALSEFORMAT => "'%value%' has a false IBAN format",
49  self::CHECKFAILED => "'%value%' has failed the IBAN check",
50  );
51 
57  protected $_locale;
58 
64  protected $_ibanregex = array(
65  'AD' => '/^AD[0-9]{2}[0-9]{8}[A-Z0-9]{12}$/',
66  'AE' => '/^AE[0-9]{2}[0-9]{3}[0-9]{16}$/',
67  'AL' => '/^AL[0-9]{2}[0-9]{8}[A-Z0-9]{16}$/',
68  'AT' => '/^AT[0-9]{2}[0-9]{5}[0-9]{11}$/',
69  'AZ' => '/^AZ[0-9]{2}[0-9]{4}[A-Z0-9]{20}$/',
70  'BA' => '/^BA[0-9]{2}[0-9]{6}[0-9]{10}$/',
71  'BE' => '/^BE[0-9]{2}[0-9]{3}[0-9]{9}$/',
72  'BG' => '/^BG[0-9]{2}[A-Z]{4}[0-9]{4}[0-9]{2}[A-Z0-9]{8}$/',
73  'BH' => '/^BH[0-9]{2}[A-Z]{4}[A-Z0-9]{14}$/',
74  'BR' => '/^BR[0-9]{2}[0-9]{8}[0-9]{5}[0-9]{10}[A-Z]{1}[A-Z0-9]{1}$/',
75  'CH' => '/^CH[0-9]{2}[0-9]{5}[A-Z0-9]{12}$/',
76  'CR' => '/^CR[0-9]{2}[0-9]{3}[0-9]{14}$/',
77  'CS' => '/^CS[0-9]{2}[0-9]{3}[0-9]{15}$/',
78  'CY' => '/^CY[0-9]{2}[0-9]{8}[A-Z0-9]{16}$/',
79  'CZ' => '/^CZ[0-9]{2}[0-9]{4}[0-9]{16}$/',
80  'DE' => '/^DE[0-9]{2}[0-9]{8}[0-9]{10}$/',
81  'DK' => '/^DK[0-9]{2}[0-9]{4}[0-9]{10}$/',
82  'DO' => '/^DO[0-9]{2}[A-Z0-9]{4}[0-9]{20}$/',
83  'EE' => '/^EE[0-9]{2}[0-9]{4}[0-9]{12}$/',
84  'ES' => '/^ES[0-9]{2}[0-9]{8}[0-9]{12}$/',
85  'FR' => '/^FR[0-9]{2}[0-9]{10}[A-Z0-9]{11}[0-9]{2}$/',
86  'FI' => '/^FI[0-9]{2}[0-9]{6}[0-9]{8}$/',
87  'FO' => '/^FO[0-9]{2}[0-9]{4}[0-9]{9}[0-9]{1}$/',
88  'GB' => '/^GB[0-9]{2}[A-Z]{4}[0-9]{14}$/',
89  'GE' => '/^GE[0-9]{2}[A-Z]{2}[0-9]{16}$/',
90  'GI' => '/^GI[0-9]{2}[A-Z]{4}[A-Z0-9]{15}$/',
91  'GL' => '/^GL[0-9]{2}[0-9]{4}[0-9]{9}[0-9]{1}$/',
92  'GR' => '/^GR[0-9]{2}[0-9]{7}[A-Z0-9]{16}$/',
93  'GT' => '/^GT[0-9]{2}[A-Z0-9]{4}[A-Z0-9]{20}$/',
94  'HR' => '/^HR[0-9]{2}[0-9]{7}[0-9]{10}$/',
95  'HU' => '/^HU[0-9]{2}[0-9]{7}[0-9]{1}[0-9]{15}[0-9]{1}$/',
96  'IE' => '/^IE[0-9]{2}[A-Z0-9]{4}[0-9]{6}[0-9]{8}$/',
97  'IL' => '/^IL[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{13}$/',
98  'IS' => '/^IS[0-9]{2}[0-9]{4}[0-9]{18}$/',
99  'IT' => '/^IT[0-9]{2}[A-Z]{1}[0-9]{10}[A-Z0-9]{12}$/',
100  'KW' => '/^KW[0-9]{2}[A-Z]{4}[0-9]{3}[0-9]{22}$/',
101  'KZ' => '/^KZ[A-Z]{2}[0-9]{2}[0-9]{3}[A-Z0-9]{13}$/',
102  'LB' => '/^LB[0-9]{2}[0-9]{4}[A-Z0-9]{20}$/',
103  'LI' => '/^LI[0-9]{2}[0-9]{5}[A-Z0-9]{12}$/',
104  'LU' => '/^LU[0-9]{2}[0-9]{3}[A-Z0-9]{13}$/',
105  'LT' => '/^LT[0-9]{2}[0-9]{5}[0-9]{11}$/',
106  'LV' => '/^LV[0-9]{2}[A-Z]{4}[A-Z0-9]{13}$/',
107  'MC' => '/^MC[0-9]{2}[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}$/',
108  'MD' => '/^MD[0-9]{2}[A-Z0-9]{20}$/',
109  'ME' => '/^ME[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}$/',
110  'MK' => '/^MK[0-9]{2}[0-9]{3}[A-Z0-9]{10}[0-9]{2}$/',
111  'MR' => '/^MR13[0-9]{5}[0-9]{5}[0-9]{11}[0-9]{2}$/',
112  'MU' => '/^MU[0-9]{2}[A-Z]{4}[0-9]{2}[0-9]{2}[0-9]{12}[0-9]{3}[A-Z]{2}$/',
113  'MT' => '/^MT[0-9]{2}[A-Z]{4}[0-9]{5}[A-Z0-9]{18}$/',
114  'NL' => '/^NL[0-9]{2}[A-Z]{4}[0-9]{10}$/',
115  'NO' => '/^NO[0-9]{2}[0-9]{4}[0-9]{7}$/',
116  'PK' => '/^PK[0-9]{2}[A-Z]{4}[0-9]{16}$/',
117  'PL' => '/^PL[0-9]{2}[0-9]{8}[0-9]{16}$/',
118  'PS' => '/^PS[0-9]{2}[A-Z]{4}[0-9]{21}$/',
119  'PT' => '/^PT[0-9]{2}[0-9]{8}[0-9]{13}$/',
120  'RO' => '/^RO[0-9]{2}[A-Z]{4}[A-Z0-9]{16}$/',
121  'RS' => '/^RS[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}$/',
122  'SA' => '/^SA[0-9]{2}[0-9]{2}[A-Z0-9]{18}$/',
123  'SE' => '/^SE[0-9]{2}[0-9]{3}[0-9]{17}$/',
124  'SI' => '/^SI[0-9]{2}[0-9]{5}[0-9]{8}[0-9]{2}$/',
125  'SK' => '/^SK[0-9]{2}[0-9]{4}[0-9]{16}$/',
126  'SM' => '/^SM[0-9]{2}[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}$/',
127  'TN' => '/^TN[0-9]{2}[0-9]{5}[0-9]{15}$/',
128  'TR' => '/^TR[0-9]{2}[0-9]{5}[A-Z0-9]{17}$/',
129  'VG' => '/^VG[0-9]{2}[A-Z]{4}[0-9]{16}$/'
130  );
131 
137  public function __construct($locale = null)
138  {
139  if ($locale instanceof Zend_Config) {
140  $locale = $locale->toArray();
141  }
142 
143  if (is_array($locale)) {
144  if (array_key_exists('locale', $locale)) {
145  $locale = $locale['locale'];
146  } else {
147  $locale = null;
148  }
149  }
150 
151  if (empty($locale)) {
152  #require_once 'Zend/Registry.php';
153  if (Zend_Registry::isRegistered('Zend_Locale')) {
154  $locale = Zend_Registry::get('Zend_Locale');
155  }
156  }
157 
158  if ($locale !== null) {
159  $this->setLocale($locale);
160  }
161  }
162 
168  public function getLocale()
169  {
170  return $this->_locale;
171  }
172 
181  public function setLocale($locale = null)
182  {
183  if ($locale !== false) {
184  #require_once 'Zend/Locale.php';
185  $locale = Zend_Locale::findLocale($locale);
186  if (strlen($locale) < 4) {
187  #require_once 'Zend/Validate/Exception.php';
188  throw new Zend_Validate_Exception('Region must be given for IBAN validation');
189  }
190  }
191 
192  $this->_locale = $locale;
193  return $this;
194  }
195 
204  public function isValid($value)
205  {
206  $value = strtoupper($value);
207  $this->_setValue($value);
208 
209  if (empty($this->_locale)) {
210  $region = substr($value, 0, 2);
211  } else {
212  $region = new Zend_Locale($this->_locale);
213  $region = $region->getRegion();
214  }
215 
216  if (!array_key_exists($region, $this->_ibanregex)) {
217  $this->_setValue($region);
218  $this->_error(self::NOTSUPPORTED);
219  return false;
220  }
221 
222  if (!preg_match($this->_ibanregex[$region], $value)) {
223  $this->_error(self::FALSEFORMAT);
224  return false;
225  }
226 
227  $format = substr($value, 4) . substr($value, 0, 4);
228  $format = str_replace(
229  array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
230  'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'),
231  array('10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22',
232  '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35'),
233  $format);
234 
235  $temp = intval(substr($format, 0, 1));
236  $len = strlen($format);
237  for ($x = 1; $x < $len; ++$x) {
238  $temp *= 10;
239  $temp += intval(substr($format, $x, 1));
240  $temp %= 97;
241  }
242 
243  if ($temp != 1) {
244  $this->_error(self::CHECKFAILED);
245  return false;
246  }
247 
248  return true;
249  }
250 }
__construct($locale=null)
Definition: Iban.php:137
const NOTSUPPORTED
Definition: Iban.php:37
const FALSEFORMAT
Definition: Iban.php:38
setLocale($locale=null)
Definition: Iban.php:181
_error($messageKey, $value=null)
Definition: Abstract.php:284
static isRegistered($index)
Definition: Registry.php:178
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
static findLocale($locale=null)
Definition: Locale.php:1740
const CHECKFAILED
Definition: Iban.php:39
isValid($value)
Definition: Iban.php:204
static get($index)
Definition: Registry.php:141