00001 <?
00040 class CurrencyFormatter
00041 {
00042
00046 var $formattingStyles = array(
00047 "standard",
00048 "noDecimals",
00049 "dotThousandsCommaDecimal",
00050 "apostropheThousandsDotDecimal",
00051 "apostropheThousandsNoDecimals",
00052 "spaceThousandsDotDecimal",
00053 "spaceThousandsCommaDecimal",
00054 "indian"
00055 );
00056
00061 var $ISOCodeStyles = array(
00062 "ARS" => "dotThousandsCommaDecimal",
00063 "AUD" => "standard",
00064 "ATS" => "dotThousandsCommaDecimal",
00065 "BEF" => "dotThousandsCommaDecimal",
00066 "REAL" => "dotThousandsCommaDecimal",
00067 "CAD" => "standard",
00068 "COL" => "standard",
00069 "DKR" => "dotThousandsCommaDecimal",
00070 "FIM" => "spaceThousandsCommaDecimal",
00071 "FRF" => "spaceThousandsCommaDecimal",
00072 "DEM" => "dotThousandsCommaDecimal",
00073 "GRD" => "dotThousandsCommaDecimal",
00074 "HKD" => "standard",
00075 "IEP" => "standard",
00076 "ISK" => "dotThousandsCommaDecimal",
00077 "INR" => "indian",
00078 "ITL" => "dotThousandsCommaDecimal",
00079 "YPY" => "noDecimals",
00080 "LTL" => "dotThousandsCommaDecimal",
00081 "MXP" => "standard",
00082 "NLG" => "dotThousandsCommaDecimal",
00083 "NZD" => "standard",
00084 "NOK" => "dotThousandsCommaDecimal",
00085 "SGD" => "standard",
00086 "ZAR" => "standard",
00087 "KRW" => "noDecimals",
00088 "ESP" => "dotThousandsCommaDecimal",
00089 "SEK" => "spaceThousandsDotDecimal",
00090 "CHF" => "apostropheThousandsDotDecimal",
00091 "THB" => "standard",
00092 "GBP" => "standard",
00093 "USD" => "standard",
00094 "CZK" => "dotThousandsCommaDecimal",
00095 "HUF" => "standard",
00096 "LUF" => "apostropheThousandsDotDecimal",
00097 "PLZ" => "spaceThousandsCommaDecimal",
00098 "PTE" => "dotThousandsCommaDecimal",
00099 "TRL" => "standard"
00100 );
00101
00106 var $nonDollarSymbols = array(
00107 "ATS" => "ÖS ",
00108 "BEF" => "BEF ",
00109 "REAL" => "R\$",
00110 "DKR" => " kr",
00111 "FIM" => " mk",
00112 "FRF" => " F",
00113 "DEM" => "DM",
00114 "GRD" => " GRD",
00115 "HKD" => "HK\$",
00116 "IEP" => "IR£",
00117 "ISK" => " kr",
00118 "INR" => "Rs.",
00119 "ITL" => "L. ",
00120 "YPY" => "¥",
00121 "LTL" => " Lt",
00122 "NLG" => "f ",
00123 "NOK" => " kr",
00124 "ZAR" => "R",
00125 "KRW" => "\\",
00126 "ESP" => " Ptas",
00127 "SEK" => " kr",
00128 "CHF" => "SFr ",
00129 "THB" => " Bt",
00130 "GBP" => "£",
00131 "CZK" => " Kc",
00132 "HUF" => "HK\$",
00133 "LUF" => " F",
00134 "PLZ" => " zl",
00135 "PTE" => " Esc",
00136 "TRL" => "TL"
00137 );
00138
00143 var $currenciesWithSymbolsAfterAmount = array(
00144 "DKR", "FIM", "FRF", "DEM", "GRD", "ISK", "ITL", "LTL",
00145 "NOK", "ESP", "SEK", "THB", "CZK", "LUF", "PLZ", "PTE",
00146 "TRL"
00147 );
00148
00153 function CurrencyFormatter()
00154 {
00155
00156 }
00157
00167 function format( $amount, $ISOCode )
00168 {
00169
00170 $style = $this->getStyleForISOCode($ISOCode);
00171 return $this->formatWithStyle( $amount, $style );
00172 }
00173
00179 function formatWithSymbol( $amount, $ISOCode )
00180 {
00181
00182 $amount = $this->format($amount, $ISOCode);
00183 list($prefix, $suffix) = $this->getPrefixSuffixArray($ISOCode);
00184 return $prefix.$amount.$suffix;
00185 }
00186
00198 function validate( $amount, $ISOCode )
00199 {
00200
00201 $amount = $this->removePrefixAndSuffix($amount, $ISOCode);
00202 $style = $this->getStyleForISOCode($ISOCode);
00203 return $this->validateForStyle( $amount, $style );
00204 }
00205
00216 function toDecimal( $amount, $ISOCode )
00217 {
00218
00219 $amount = $this->removePrefixAndSuffix($amount, $ISOCode);
00220 $style = $this->getStyleForISOCode($ISOCode);
00221 return $this->toDecimalForStyle( $amount, $style );
00222 }
00223
00233 function toDecimalForStyle( $amount, $style )
00234 {
00235
00236 list( $thousandsSeparator, $decimalPlace ) = $this->getSeparators($style);
00237 $rv = ereg_replace( $thousandsSeparator, "", $amount );
00238 $rv = ereg_replace( $decimalPlace, ".", $rv );
00239 $rv = $rv + 0;
00240 return $rv;
00241 }
00242
00250 function supportsISOCode( $ISOCode )
00251 {
00252
00253 if( isset($this->ISOCodeStyles[strtoupper($ISOCode)]) )
00254 {
00255 return true;
00256 }
00257 }
00258
00264 function getFormattingStyles()
00265 {
00266
00267 return $this->formattingStyles;
00268 }
00269
00275 function getISOCodes()
00276 {
00277
00278 $codes = array();
00279 while( list($key, $val) = each($this->ISOCodeStyles) )
00280 {
00281 $codes[] = $key;
00282 }
00283 reset($this->ISOCodeStyles);
00284
00285 return $codes;
00286 }
00287
00296 function getStyleForISOCode( $ISOCode )
00297 {
00298
00299 return $this->ISOCodeStyles[strtoupper($ISOCode)];
00300 }
00301
00310 function getSymbol( $ISOCode )
00311 {
00312
00313
00314 if( !$symbol = $this->nonDollarSymbols[strtoupper($ISOCode)] )
00315 {
00316 $symbol = "\$";
00317 }
00318
00319 return $symbol;
00320 }
00321
00330 function symbolIsAfterAmount( $ISOCode )
00331 {
00332
00333 $ISOCode = strtoupper($ISOCode);
00334 for( $i = 0; $i < sizeof($this->currenciesWithSymbolsAfterAmount); $i++ )
00335 {
00336 if( $this->currenciesWithSymbolsAfterAmount[$i] == $ISOCode )
00337 {
00338 return true;
00339 }
00340 }
00341
00342 return false;
00343 }
00344
00356 function getPrefixSuffixArray( $ISOCode )
00357 {
00358
00359 $prefix = $suffix = "";
00360
00361 $symbol = $this->getSymbol( $ISOCode );
00362 if( $this->symbolIsAfterAmount($ISOCode) )
00363 {
00364 $suffix = $symbol;
00365 }
00366 else
00367 {
00368 $prefix = $symbol;
00369 }
00370 return array($prefix, $suffix);
00371 }
00372
00380 function removePrefixAndSuffix( $amount, $ISOCode )
00381 {
00382
00383 list( $prefix, $suffix ) = $this->getPrefixSuffixArray($ISOCode);
00384 $amount = preg_replace("!^".preg_quote($prefix, "!")."!", "", $amount);
00385 $amount = preg_replace("!".preg_quote($suffix, "!")."$!", "", $amount);
00386 return $amount;
00387 }
00388
00397 function getSeparators( $style )
00398 {
00399
00400 switch( $style )
00401 {
00402 case "dotThousandsCommaDecimal" :
00403 $thousandsSeparator = "\.";
00404 $decimalPlace = ",";
00405 break;
00406 case "dotThousandsNoDecimals" :
00407 $thousandsSeparator = "\.";
00408 $decimalPlace = ",";
00409 break;
00410 case "spaceThousandsCommaDecimal" :
00411 $thousandsSeparator = " ";
00412 $decimalPlace = ",";
00413 break;
00414 case "indian" :
00415 $thousandsSeparator = ",";
00416 $decimalPlace = "\.";
00417 break;
00418 case "noDecimals" :
00419 $thousandsSeparator = ",";
00420 $decimalPlace = "\.";
00421 break;
00422 case "spaceThousandsDotDecimal" :
00423 $thousandsSeparator = " ";
00424 $decimalPlace = "\.";
00425 break;
00426 default :
00427 $thousandsSeparator = ",";
00428 $decimalPlace = "\.";
00429 break;
00430 }
00431
00432 return array( $thousandsSeparator, $decimalPlace );
00433 }
00434
00445 function validateForStyle( $amount, $style )
00446 {
00447
00448 list( $thousandsSeparator, $decimalPlace ) = $this->getSeparators($style);
00449 $regExp = "^([0-9]{1,3}".$thousandsSeparator.")*[0-9]*"
00450 ."(".$decimalPlace."[0-9]+){0,1}$";
00451 $rv = ereg($regExp, $amount);
00452
00453 return $rv;
00454 }
00455
00465 function formatWithStyle( $amount, $style )
00466 {
00467
00468 settype($amount, "double");
00469 switch( $style )
00470 {
00471 case "dotThousandsCommaDecimal" :
00472 $rv = number_format($amount, 2, ",", ".");
00473 break;
00474 case "dotThousandsNoDecimals" :
00475 $str = number_format($amount, 2, "", ".");
00476 $rv = substr($str, 0, -3);
00477 break;
00478 case "spaceThousandsCommaDecimal" :
00479 $rv = number_format($amount, 2, ",", " ");
00480 break;
00481 case "indian" :
00482 list( $digits, $decimals ) = explode(".", $amount);
00483 if( ($len = strlen($digits)) >= 5 ) {
00484 $bit = substr($digits, 0, $len - 3) / 100;
00485 $rv = number_format($bit, 2, ",", "," )
00486 .",".substr($digits, $len - 3)
00487 .".$decimals";
00488 }
00489 else
00490 $rv = number_format($amount, 2);
00491 break;
00492 case "noDecimals" :
00493 $str = number_format($amount, 2, "", ",");
00494 $rv = substr($str, 0, -3);
00495 break;
00496 case "spaceThousandsDotDecimal" :
00497 $rv = number_format($amount, 2, ".", " ");
00498 break;
00499 case "apostropheThousandsNoDecimals" :
00500 $rv = number_format($amount, 2, ".", " ");
00501 $rv = substr($str, 0, -3);
00502 break;
00503 case "apostropheThousandsDotDecimal" :
00504 $rv = number_format($amount, 2, ".", "'");
00505 break;
00506 default :
00507 $rv = number_format($amount, 2);
00508 break;
00509 }
00510 return $rv;
00511 }
00512
00513 }
00514
00515 class MCurrencyFormatter extends CurrencyFormatter
00516 {
00517 var $defaultISOCode = 'REAL';
00518
00519 function __construct($ISOCode = NULL)
00520 {
00521 parent::__construct();
00522 if (is_null($ISOCode))
00523 {
00524 $ISOCode = $this->defaultISOCode;
00525 }
00526 }
00527
00528 function format( $amount, $ISOCode = NULL)
00529 {
00530 if (is_null($ISOCode))
00531 {
00532 $ISOCode = $this->defaultISOCode;
00533 }
00534 return parent::format($amount, $ISOCode);
00535 }
00536
00537 function formatWithSymbol( $amount, $ISOCode = NULL)
00538 {
00539 if (is_null($ISOCode))
00540 {
00541 $ISOCode = $this->defaultISOCode;
00542 }
00543 return parent::formatWithSymbol($amount, $ISOCode);
00544 }
00545
00546 function validate( $amount, $ISOCode = NULL)
00547 {
00548 if (is_null($ISOCode))
00549 {
00550 $ISOCode = $this->defaultISOCode;
00551 }
00552 return parent::validate($amount, $ISOCode);
00553 }
00554
00555 function toDecimal( $amount, $ISOCode = NULL)
00556 {
00557 if (is_null($ISOCode))
00558 {
00559 $ISOCode = $this->defaultISOCode;
00560 }
00561 return parent::toDecimal($amount, $ISOCode);
00562 }
00563 }
00564
00565 ?>