RSI helpers  0.1
RSI helpers
Valid.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi;
4 
5 /**
6  * Validators.
7  */
8 class Valid{
9 
10  const POSTCODE_FORMATS = [ //country code => [mask, defaults, format, address format (* = street, # = number, % postcode, @ = place, & = state/province)]
11  //http://publications.europa.eu/code/nl/nl-390105.htm
12  'BE' => ['(\\d{4})',[],'$1',"* #\n% @@\nBELGIUM"],
13  'BG' => ['(\\d{4})',[],'$1',"* #\n% @@\nBULGARIA"],
14  'CZ' => ['(\\d{3})-?(\\d{2})',[],'$1 $2',"* #\n% @@\nCZECH REPUBLIC"],
15  'DK' => ['(\\d{4})',[],'$1',"* #\n% @@\nDENMARK"],
16  'DE' => ['(?:DE?-?)?(\\d{5})',[],'$1',"* #\n% @@\nGERMANY"],
17  'EE' => ['(\\d{5})',[],'$1',"* #\n% @\NESTONIA"],
18  'IE' => ['(\\w{7})',[],'$1',"* #\n@@\n%\nIRELAND"],
19  'GR' => ['(\\d{3})-?(\\d{2})',[],'$1 $2',"* #\n% @@\nGREECE"],
20  'ES' => ['(\\d{5})',[],'$1',"* #\n% @@\n&\nSPAIN"],
21  'FR' => ['(\\d{5})',[],'$1',"* #\n% @@\nFRANCE"],
22  'HR' => ['(HR)?-*(\\d{5})',['HR'],'$1-$2',"* #\n% @\nCROATIA"],
23  'IT' => ['(\\d{5})',[],'$1',"* #\n% @@ &&\nITALY"],
24  'CY' => ['(\\d{4})',[],'$1',"* #\n% @\nCYPRUS"],
25  'LV' => ['(LV)?-?(\\d{4})',['LV'],'$1-$2',"* #\n@, %\nLATVIA"],
26  'LT' => ['(LT)?-?(\\d{5})',['LT'],'$1-$2',"* #\n% @\nLITHUANIA"],
27  'LU' => ['(L)?-?(\\d{4})',['L'],'$1-$2',"* #\n% @@\nLUXEMBOURG"],
28  'HU' => ['(\\d{4})',[],'$1',"@\n* #\n%\nHUNGARY"],
29  'MT' => ['([A-Z]{3})-*(\\d{4})',[],'$1 $2',"* #\n@\n%\nMALTA"],
30  'NL' => ['(\\d{4})-?([A-Z]{2})',[],'$1 $2',"* #\n% @@\nTHE NETHERLANDS"],
31  'AT' => ['(\\d{4})',[],'$1',"* #\n% @@\nAUSTRIA"],
32  'PL' => ['(\\d{2})-?(\\d{3})',[],'$1-$2',"* #\n% @@\nPOLAND"],
33  'PT' => ['(\\d{4})-?(\\d{5})',[],'$1-$2',"* #\n% @@\nPORTUGAL"],
34  'RO' => ['(\\d{6})',[],'$1',"* #\n% @\nROMANIA"],
35  'SI' => ['(SI)?-?(\\d{4})',['SI'],'$1-$2',"* #\n@, %\nSLOVENIA"],
36  'SK' => ['(\\d{3})-?(\\d{2})',[],'$1 $2',"* #\n% @@\nSLOVAK REPUBLIC"],
37  'FI' => ['(FI|AX)?-?(\\d{5})',['FI'],'$1-$2',"* #\n% @@\nFINLAND"],
38  'SE' => ['(SE)?-?(\\d{5})',['SE'],'$1-$2',"* #\n@@, %\nSWEDEN"],
39  'GB' => ['(\\w{2,4})-?(\\d[A-Z]{2})',[],'$1 $2',"# *\n@\n%\nUNITED KINGDOM"],
40  //https://personal.help.royalmail.com/app/answers/detail/a_id/115/related/1
41  'AU' => ['(\\d{4})',[],'$1',"# *\n@@ && %\nAUSTRALIA"],
42  'CA' => ['(\\w{3})-?(\\w{3})',[],'$1 $2',"# *\n@@ && %\nCANADA"],
43  'US' => ['(\\d{5})(-\\d{4})?',[],'$1$2',"# *\n@@ && %\nUNITED STATES OF AMERICA"]
44  ];
45  const IBAN_FORMATS = [ //www/fred/tool/iban.php
46  'AL' => '\\d{8}\\w{16}', //Albania
47  'AD' => '\\d{8}\\w{12}', //Andorra
48  'AT' => '\\d{16}', //Austria
49  'AZ' => '\\w{4}\\d{20}', //Azerbaijan
50  'BH' => '[A-Z]{4}\\w{14}', //Bahrain
51  'BE' => '\\d{12}', //Belgium
52  'BA' => '\\d{16}', //Bosnia and Herzegovina
53  'BR' => '\\d{23}[A-Z]{1}\\w{1}', //Brazil
54  'BG' => '[A-Z]{4}\\d{6}\\w{8}', //Bulgaria
55  'CR' => '\\d{17}', //Costa Rica
56  'HR' => '\\d{17}', //Croatia
57  'CY' => '\\d{8}\\w{16}', //Cyprus
58  'CZ' => '\\d{20}', //Czech Republic
59  'DK' => '\\d{14}', //Denmark
60  'DO' => '[A-Z]{4}\\d{20}', //Dominican Republic
61  'TL' => '\\d{19}', //East Timor
62  'EE' => '\\d{16}', //Estonia
63  'FO' => '\\d{14}', //Faroe Islands
64  'FI' => '\\d{14}', //Finland
65  'FR' => '\\d{10}\\w{11}\\d{2}', //France
66  'GE' => '\\w{2}\\d{16}', //Georgia
67  'DE' => '\\d{18}', //Germany
68  'GI' => '[A-Z]{4}\\w{15}', //Gibraltar
69  'GR' => '\\d{7}\\w{16}', //Greece
70  'GL' => '\\d{14}', //Greenland
71  'GT' => '\\w{4}\\w{20}', //Guatemala
72  'HU' => '\\d{24}', //Hungary
73  'IS' => '\\d{22}', //Iceland
74  'IE' => '\\w{4}\\d{14}', //Ireland
75  'IL' => '\\d{19}', //Israel
76  'IT' => '[A-Z]{1}\\d{10}\\w{12}', //Italy
77  'JO' => '[A-Z]{4}\\d{22}', //Jordan
78  'KZ' => '\\d{3}\\w{13}', //Kazakhstan
79  'XK' => '\\d{4}\\d{10}\\d{2}', //Kosovo
80  'KW' => '[A-Z]{4}\\w{22}', //Kuwait
81  'LV' => '[A-Z]{4}\\w{13}', //Latvia
82  'LB' => '\\d{4}\\w{20}', //Lebanon
83  'LI' => '\\d{5}\\w{12}', //Liechtenstein
84  'LT' => '\\d{16}', //Lithuania
85  'LU' => '\\d{3}\\w{13}', //Luxembourg
86  'MK' => '\\d{3}\\w{10}\\d{2}', //Macedonia
87  'MT' => '[A-Z]{4}\\d{5}\\w{18}', //Malta
88  'MR' => '\\d{23}', //Mauritania
89  'MU' => '[A-Z]{4}\\d{19}[A-Z]{3}', //Mauritius
90  'MC' => '\\d{10}\\w{11}\\d{2}', //Monaco
91  'MD' => '\\w{2}\\w{18}', //Moldova
92  'ME' => '\\d{18}', //Montenegro
93  'NL' => '[A-Z]{4}\\d{10}', //Netherlands
94  'NO' => '\\d{11}', //Norway
95  'PK' => '\\w{4}\\d{16}', //Pakistan
96  'PS' => '\\w{4}\\d{21}', //Palestinian territories
97  'PL' => '\\d{24}', //Poland
98  'PT' => '\\d{21}', //Portugal
99  'QA' => '[A-Z]{4}\\w{21}', //Qatar
100  'RO' => '[A-Z]{4}\\w{16}', //Romania
101  'SM' => '[A-Z]{1}\\d{10}\\w{12}', //San Marino
102  'SA' => '\\d{2}\\w{18}', //Saudi Arabia
103  'RS' => '\\d{18}', //Serbia
104  'SK' => '\\d{20}', //Slovakia
105  'SI' => '\\d{15}', //Slovenia
106  'ES' => '\\d{20}', //Spain
107  'SE' => '\\d{20}', //Sweden
108  'CH' => '\\d{5}\\w{12}', //Switzerland
109  'TN' => '\\d{20}', //Tunisia
110  'TR' => '\\d{5}\\w{17}', //Turkey
111  'AE' => '\\d{3}\\d{16}', //United Arab Emirates
112  'GB' => '[A-Z]{4}\\d{14}', //United Kingdom
113  'VG' => '\\w{4}\\d{16}', //Virgin Islands, British
114  ];
115 
116  /**
117  * Check an e-mail address for syntax and host.
118  * @param string $value E-mail address.
119  * @return bool True if both the e-mail address and the host are valid.
120  */
121  public static function email($value){
122  return filter_var($value,FILTER_VALIDATE_EMAIL) && checkdnsrr(substr(strrchr($value,'@'),1));
123  }
124  /**
125  * Check a postal code for syntax.
126  * @param string $country Country code (ISO 3166-1 alpha-2).
127  * @param string $value Postal code.
128  * @return string|bool Formatted postal code when valid. False when invalid.
129  */
130  public static function postcode($country,$value){
131  $value = trim(preg_replace('/[^\\dA-Z]+/','-',strtoupper($value)),'-');
132  if(($format = Record::get(self::POSTCODE_FORMATS,strtoupper($country))) && ($value = (bool)preg_match('/^' . array_shift($format) . '$/',$value,$matches))){
133  foreach(array_shift($format) as $index => $default) if(!$matches[++$index]) $matches[$index] = $default;
134  $value = array_shift($format);
135  foreach($matches as $index => $match) $value = str_replace('$' . $index,$match,$value);
136  }
137  return $value;
138  }
139  /**
140  * Check all parts of an address for syntax.
141  * @param string $country Country code (ISO 3166-1 alpha-2).
142  * @param string $street Street name.
143  * @param string $number House number.
144  * @param string $postcode Postal code.
145  * @param string $town Town or city.
146  * @param string $state State or province.
147  * @return string|bool Formatted address when valid. False when invalid.
148  */
149  public static function address($country,$street,$number,$postcode,$town,$state = null){
150  $value = false;
151  if(
152  ($format = Record::get(self::POSTCODE_FORMATS,strtoupper($country))) &&
153  $street && $number && ($postcode = self::postcode($country,$postcode)) && $town
154  ){
155  $value = array_pop($format);
156  foreach(['*' => $street,'#' => $number,'%' => $postcode,'@' => $town,'&' => $state] as $k => $v)
157  $value = strtr($value,[$k . $k => mb_strtoupper($v),$k => $v]);
158  }
159  return $value;
160  }
161  /**
162  * Check if an International Bank Account Number (IBAN) is valid.
163  * @param string $value IBAN (may have extra formatting characters).
164  * @return string|bool Formatted IBAN when valid. False when invalid.
165  */
166  public static function iban($value){
167  $value = strtoupper(preg_replace('/[\\W_]/','',$value));
168  if(!preg_match('/^[A-Z]{2}(0[2-9]|[1-8]\d|9[0-8])' . Record::get(self::IBAN_FORMATS,substr($value,0,2),'\\w{10,30}') . '$/',$value)) return false;
169  $check = substr($value,4) . substr($value,0,4);
170  for($c = 'A'; $c <= 'Z'; $c++) $check = str_replace($c,ord($c) - 55,$check);
171  return bcmod($check,97) == 1 ? $value : false;
172  }
173  /**
174  * Check if an European VAT number is valid.
175  * @param string $value VAT number (may have extra formatting characters).
176  * @param array $info Returns extra info related to a valid VAT number.
177  * @return bool True when valid. If the VAT number could not be checked online this will always be true, $info then holds an
178  * error message.
179  */
180  public static function vatNo($value,&$info = null){
181  $value = strtoupper(preg_replace('/\\W/','',$value));
182  try{
183  $soap = @new \SoapClient('http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl',['exceptions' => 1]);
184  $info = (array)$soap->checkVat(['countryCode' => substr($value,0,2),'vatNumber' => substr($value,2)]);
185  return $info['valid'];
186  }
187  catch(\SoapFault $e){
188  $info = ['error' => $e->faultstring];
189  }
190  return true; //benefit of doubt
191  }
192 
193 }
static postcode($country, $value)
Check a postal code for syntax.
Definition: Valid.php:130
static iban($value)
Check if an International Bank Account Number (IBAN) is valid.
Definition: Valid.php:166
static address($country, $street, $number, $postcode, $town, $state=null)
Check all parts of an address for syntax.
Definition: Valid.php:149
static vatNo($value, &$info=null)
Check if an European VAT number is valid.
Definition: Valid.php:180
const n const static email($value)
Check an e-mail address for syntax and host.
Definition: Valid.php:121
Definition: Color.php:3
Validators.
Definition: Valid.php:8