MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
mcurrencyformatter.class
Ir para a documentação deste ficheiro.
1<?php
41{
42
46 var $formattingStyles = array(
47 "standard",
48 "noDecimals",
49 "dotThousandsCommaDecimal",
50 "apostropheThousandsDotDecimal",
51 "apostropheThousandsNoDecimals",
52 "spaceThousandsDotDecimal",
53 "spaceThousandsCommaDecimal",
54 "indian"
55 );
56
61 var $ISOCodeStyles = array(
62 "ARS" => "dotThousandsCommaDecimal",
63 "AUD" => "standard",
64 "ATS" => "dotThousandsCommaDecimal",
65 "BEF" => "dotThousandsCommaDecimal",
66 "REAL" => "dotThousandsCommaDecimal",
67 "CAD" => "standard",
68 "COL" => "standard",
69 "DKR" => "dotThousandsCommaDecimal",
70 "FIM" => "spaceThousandsCommaDecimal",
71 "FRF" => "spaceThousandsCommaDecimal",
72 "DEM" => "dotThousandsCommaDecimal",
73 "GRD" => "dotThousandsCommaDecimal",
74 "HKD" => "standard",
75 "IEP" => "standard",
76 "ISK" => "dotThousandsCommaDecimal",
77 "INR" => "indian",
78 "ITL" => "dotThousandsCommaDecimal",
79 "YPY" => "noDecimals",
80 "LTL" => "dotThousandsCommaDecimal",
81 "MXP" => "standard",
82 "NLG" => "dotThousandsCommaDecimal",
83 "NZD" => "standard",
84 "NOK" => "dotThousandsCommaDecimal",
85 "SGD" => "standard",
86 "ZAR" => "standard",
87 "KRW" => "noDecimals",
88 "ESP" => "dotThousandsCommaDecimal",
89 "SEK" => "spaceThousandsDotDecimal",
90 "CHF" => "apostropheThousandsDotDecimal",
91 "THB" => "standard",
92 "GBP" => "standard",
93 "USD" => "standard",
94 "CZK" => "dotThousandsCommaDecimal",
95 "HUF" => "standard",
96 "LUF" => "apostropheThousandsDotDecimal",
97 "PLZ" => "spaceThousandsCommaDecimal",
98 "PTE" => "dotThousandsCommaDecimal",
99 "TRL" => "standard"
100 );
101
106 var $nonDollarSymbols = array(
107 "ATS" => "&Ouml;S ",
108 "BEF" => "BEF ",
109 "REAL" => "R\$",
110 "DKR" => " kr",
111 "FIM" => " mk",
112 "FRF" => " F",
113 "DEM" => "DM",
114 "GRD" => " GRD",
115 "HKD" => "HK\$",
116 "IEP" => "IR&pound;",
117 "ISK" => " kr",
118 "INR" => "Rs.",
119 "ITL" => "L. ",
120 "YPY" => "&yen;",
121 "LTL" => " Lt",
122 "NLG" => "f ",
123 "NOK" => " kr",
124 "ZAR" => "R",
125 "KRW" => "\\",
126 "ESP" => " Ptas",
127 "SEK" => " kr",
128 "CHF" => "SFr ",
129 "THB" => " Bt",
130 "GBP" => "&pound;",
131 "CZK" => " Kc",
132 "HUF" => "HK\$",
133 "LUF" => " F",
134 "PLZ" => " zl",
135 "PTE" => " Esc",
136 "TRL" => "TL"
137 );
138
144 "DKR", "FIM", "FRF", "DEM", "GRD", "ISK", "ITL", "LTL",
145 "NOK", "ESP", "SEK", "THB", "CZK", "LUF", "PLZ", "PTE",
146 "TRL"
147 );
148
154 {
155 //* debug */ echo "function CurrencyFormatter()<br>\n";
156 }
157
167 function format( $amount, $ISOCode )
168 {
169 //* debug */ echo "function format( $amount, $ISOCode )<br>\n";
170 $style = $this->getStyleForISOCode($ISOCode);
171 return $this->formatWithStyle( $amount, $style );
172 }
173
179 function formatWithSymbol( $amount, $ISOCode )
180 {
181 //* debug */ echo "function formatWithSymbol( $amount, $ISOCode )<br>\n";
182 $amount = $this->format($amount, $ISOCode);
183 list($prefix, $suffix) = $this->getPrefixSuffixArray($ISOCode);
184 return $prefix.$amount.$suffix;
185 }
186
198 function validate( $amount, $ISOCode )
199 {
200 //* debug */ echo "function validate( $amount, $ISOCode )<br>\n";
201 $amount = $this->removePrefixAndSuffix($amount, $ISOCode);
202 $style = $this->getStyleForISOCode($ISOCode);
203 return $this->validateForStyle( $amount, $style );
204 }
205
216 function toDecimal( $amount, $ISOCode )
217 {
218 //* debug */ echo "function toDecimal( $amount, $ISOCode )<br>\n";
219 $amount = $this->removePrefixAndSuffix($amount, $ISOCode);
220 $style = $this->getStyleForISOCode($ISOCode);
221 return $this->toDecimalForStyle( $amount, $style );
222 }
223
233 function toDecimalForStyle( $amount, $style )
234 {
235 //* debug */ echo "function toDecimalForStyle( $amount, $style )<br>\n";
236 list( $thousandsSeparator, $decimalPlace ) = $this->getSeparators($style);
237 $rv = preg_replace( '/'.$thousandsSeparator.'/', "", $amount );
238 $rv = preg_replace( '/'.$decimalPlace.'/', ".", $rv );
239 $rv = $rv + 0;
240 return $rv;
241 }
242
250 function supportsISOCode( $ISOCode )
251 {
252 //* debug */ echo "function supportsISOCode( $ISOCode )<br>\n";
253 if( isset($this->ISOCodeStyles[strtoupper($ISOCode)]) )
254 {
255 return true;
256 }
257 }
258
265 {
266 //* debug */ echo "function getFormattingStyles()<br>\n";
268 }
269
275 function getISOCodes()
276 {
277 //* debug */ echo "function getISOCodes()<br>\n";
278 $codes = array();
279 while( list($key, $val) = each($this->ISOCodeStyles) )
280 {
281 $codes[] = $key;
282 }
283 reset($this->ISOCodeStyles);
284
285 return $codes;
286 }
287
296 function getStyleForISOCode( $ISOCode )
297 {
298 //* debug */ echo "function getStyleForISOCode( $ISOCode )<br>\n";
299 return $this->ISOCodeStyles[strtoupper($ISOCode)];
300 }
301
310 function getSymbol( $ISOCode )
311 {
312 //* debug */ echo "function getSymbol( $ISOCode )<br>\n";
313 //* debug */ echo "function getSymbol( $ISOCode )".NL;
314 if( !$symbol = $this->nonDollarSymbols[strtoupper($ISOCode)] )
315 {
316 $symbol = "\$";
317 }
318
319 return $symbol;
320 }
321
330 function symbolIsAfterAmount( $ISOCode )
331 {
332 //* debug */ echo "function symbolIsAfterAmount( $ISOCode )<br>\n";
333 $ISOCode = strtoupper($ISOCode);
334 for( $i = 0; $i < sizeof($this->currenciesWithSymbolsAfterAmount); $i++ )
335 {
336 if( $this->currenciesWithSymbolsAfterAmount[$i] == $ISOCode )
337 {
338 return true;
339 }
340 }
341
342 return false;
343 }
344
356 function getPrefixSuffixArray( $ISOCode )
357 {
358 //* debug */ echo "function getPrefixSuffixArray( $ISOCode )<br>\n";
359 $prefix = $suffix = "";
360
361 $symbol = $this->getSymbol( $ISOCode );
362 if( $this->symbolIsAfterAmount($ISOCode) )
363 {
364 $suffix = $symbol;
365 }
366 else
367 {
368 $prefix = $symbol;
369 }
370 return array($prefix, $suffix);
371 }
372
380 function removePrefixAndSuffix( $amount, $ISOCode )
381 {
382 //* debug */ echo "function removePrefixAndSuffix( $amount, $ISOCode )".NL;
383 list( $prefix, $suffix ) = $this->getPrefixSuffixArray($ISOCode);
384 $amount = preg_replace("!^".preg_quote($prefix, "!")."!", "", $amount);
385 $amount = preg_replace("!".preg_quote($suffix, "!")."$!", "", $amount);
386 return $amount;
387 }
388
397 function getSeparators( $style )
398 {
399 //* debug */ echo "function getSeparators( $style )<br>\n";
400 switch( $style )
401 {
402 case "dotThousandsCommaDecimal" :
403 $thousandsSeparator = "\.";
404 $decimalPlace = ",";
405 break;
406 case "dotThousandsNoDecimals" :
407 $thousandsSeparator = "\.";
408 $decimalPlace = ",";
409 break;
410 case "spaceThousandsCommaDecimal" :
411 $thousandsSeparator = " ";
412 $decimalPlace = ",";
413 break;
414 case "indian" :
415 $thousandsSeparator = ",";
416 $decimalPlace = "\.";
417 break;
418 case "noDecimals" :
419 $thousandsSeparator = ",";
420 $decimalPlace = "\.";
421 break;
422 case "spaceThousandsDotDecimal" :
423 $thousandsSeparator = " ";
424 $decimalPlace = "\.";
425 break;
426 default :
427 $thousandsSeparator = ",";
428 $decimalPlace = "\.";
429 break;
430 }
431
432 return array( $thousandsSeparator, $decimalPlace );
433 }
434
445 function validateForStyle( $amount, $style )
446 {
447 //* debug */ echo "function validateForStyle( $amount, $style )<br>\n";
448 list( $thousandsSeparator, $decimalPlace ) = $this->getSeparators($style);
449 $regExp = "/^([0-9]{1,3}".$thousandsSeparator.")*[0-9]*"
450 ."(".$decimalPlace."[0-9]+){0,1}$/";
451 $rv = preg_match($regExp, $amount);
452
453 return $rv;
454 }
455
465 function formatWithStyle( $amount, $style )
466 {
467 //* debug */ echo "function formatWithStyle( $amount, $style )<br>\n";
468 settype($amount, "double");
469 switch( $style )
470 {
471 case "dotThousandsCommaDecimal" :
472 $rv = number_format($amount, 2, ",", ".");
473 break;
474 case "dotThousandsNoDecimals" :
475 $str = number_format($amount, 2, "", ".");
476 $rv = substr($str, 0, -3);
477 break;
478 case "spaceThousandsCommaDecimal" :
479 $rv = number_format($amount, 2, ",", " ");
480 break;
481 case "indian" :
482 list( $digits, $decimals ) = explode(".", $amount);
483 if( ($len = strlen($digits)) >= 5 ) {
484 $bit = substr($digits, 0, $len - 3) / 100;
485 $rv = number_format($bit, 2, ",", "," )
486 .",".substr($digits, $len - 3)
487 .".$decimals";
488 }
489 else
490 $rv = number_format($amount, 2);
491 break;
492 case "noDecimals" :
493 $str = number_format($amount, 2, "", ",");
494 $rv = substr($str, 0, -3);
495 break;
496 case "spaceThousandsDotDecimal" :
497 $rv = number_format($amount, 2, ".", " ");
498 break;
499 case "apostropheThousandsNoDecimals" :
500 $rv = number_format($amount, 2, ".", " ");
501 $rv = substr($str, 0, -3);
502 break;
503 case "apostropheThousandsDotDecimal" :
504 $rv = number_format($amount, 2, ".", "'");
505 break;
506 default :
507 $rv = number_format($amount, 2);
508 break;
509 }
510 return $rv;
511 }
512
513}
514
516{
517 var $defaultISOCode = 'REAL';
518
519 function __construct($ISOCode = NULL)
520 {
521 parent::__construct();
522 if (is_null($ISOCode))
523 {
524 $ISOCode = $this->defaultISOCode;
525 }
526 }
527
528 function format( $amount, $ISOCode = NULL)
529 {
530 if (is_null($ISOCode))
531 {
532 $ISOCode = $this->defaultISOCode;
533 }
534 return parent::format($amount, $ISOCode);
535 }
536
537 function formatWithSymbol( $amount, $ISOCode = NULL)
538 {
539 if (is_null($ISOCode))
540 {
541 $ISOCode = $this->defaultISOCode;
542 }
543 return parent::formatWithSymbol($amount, $ISOCode);
544 }
545
546 function validate( $amount, $ISOCode = NULL)
547 {
548 if (is_null($ISOCode))
549 {
550 $ISOCode = $this->defaultISOCode;
551 }
552 return parent::validate($amount, $ISOCode);
553 }
554
555 function toDecimal( $amount, $ISOCode = NULL)
556 {
557 if (is_null($ISOCode))
558 {
559 $ISOCode = $this->defaultISOCode;
560 }
561 return parent::toDecimal($amount, $ISOCode);
562 }
563}
564
565?>
validateForStyle( $amount, $style)
formatWithSymbol( $amount, $ISOCode)
validate( $amount, $ISOCode)
toDecimalForStyle( $amount, $style)
format( $amount, $ISOCode)
removePrefixAndSuffix( $amount, $ISOCode)
toDecimal( $amount, $ISOCode)
formatWithStyle( $amount, $style)
formatWithSymbol( $amount, $ISOCode=NULL)
toDecimal( $amount, $ISOCode=NULL)
format( $amount, $ISOCode=NULL)
validate( $amount, $ISOCode=NULL)