Friday, 14 May 2010 14:30
1.
<
2.
function _isValidIBAN($data, $reformat = true, $countryCode=null, $printErrors=false){
3.
4.
5.
$str = $data['account_number'];
6.
7.
if(!empty($str)){
8.
if($reformat){
9.
$str = preg_replace('/[\W_]+/', '', strtoupper($str) );
10.
11.
}
12.
$errors = array();
13.
if( 4 > strlen($str) || strlen($str) > 34 ){
14.
$errors[] = 'Invalid string length';
15.
}
16.
17.
if(is_numeric($str[0]) || is_numeric($str[1])){
18.
$errors[] = 'Invalid chars at 0, 1';
19.
}
20.
if(!is_numeric($str[2]) || !is_numeric($str[3])){
21.
$errors[] = 'Invalid chars at 3, 4';
22.
}
23.
24.
if(!empty($countryCode)){
25.
26.
if(!in_array(strtolower(substr($str, 0, 2)), array_map('strtolower', $countryCode)) ){
27.
$errors[] = 'Invalid Country code. Only accepting '. implode(', ', $countryCode);
28.
}
29.
}
30.
31.
$ibanReplaceChars = range('A','Z');
32.
33.
foreach (range(10,35) as $tempvalue) {
34.
$ibanReplaceValues[]=strval($tempvalue);
35.
}
36.
37.
38.
$tmpIBAN = substr($str, 4).substr($str, 0, 4);
39.
40.
$tmpIBAN = str_replace($ibanReplaceChars, $ibanReplaceValues, $tmpIBAN);
41.
42.
$tmpValue = intval(substr($tmpIBAN, 0, 1));
43.
44.
for ($i = 1; $i < strlen($tmpIBAN); $i++) {
45.
$tmpValue *= 10;
46.
$tmpValue += intval(substr($tmpIBAN,$i,1));
47.
$tmpValue %= 97;
48.
}
49.
50.
if ($tmpValue != 1) {
51.
$errors[] = 'Invalid checksum';
52.
}
53.
54.
if($printErrors){
55.
echo implode("\n", $errors);
56.
}
57.
58.
if(empty($errors)){
59.
return true;
60.
}
61.
}
62.
63.
return false;
64.
}
Barbie
Truck Games Read more:
Tuesday, 11 May 2010 10:32
The optparse-simple gem uses a Polyrex document to fetch, and validate command-line arguments into a simple hash.
*installation*
sudo gem1.9.1 install optparse-simple
Successfully installed optparse-simple-0.2.0
1 gem installed
Installing ri documentation for optparse-simple-0.2.0...
Updating class cache with 3148 classes...
Installing RDoc documentation for optparse-simple-0.2.0...
Polyrex schema: options/option[name,switch,alias,value,mandatory]/error[msg]
*example*
require 'optparse-simple'
args = %w(-s --list=45 refresh filename filexx3)
h = OptParseSimple.new('/home/james/learning/ruby/optionsx.xml', args).to_h
#=> {:list=>"45", :sort=>nil, :refresh=>nil, :file1=>"filename", :file2=>"filexx3"}
file: optionsx.xml
list -l --list \d+ true
You must enter the list switch -l
incorrect value: please supply a valid list integer value e.g. -l=45
sort -s --sort false
invalid sort flag
print -p --print false
copy -c --copy false
refresh refresh true
you must include the refresh switch
file1 true
invalid sort flag
file2 true
invalid sort flag
Note: The error messages within each option are positioned by priority (mandatory being 1st, followed by value).
Resources:
- jrobertson's optparse-simple at master [github.com]
- Using OptParseSimple [dzone.com]
*update: 11-May-2010 @ 5:23pm*
To catch errors use the RuntimeError case e.g.
require 'optparse-simple'
begin
args = %w(-s --list=45 refresh filename filexx3 dhgf)
h = OptParseSimple.new('/home/james/learning/ruby/optionsx.xml', args).to_h
rescue RuntimeError => myStandardError
puts myStandardError
end
#=> invalid option: dhgf not recognised
see Handle exceptions in Ruby [dzone.com]Read more: