[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /** 4 * sfNumberFormatInfo class file. 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the BSD License. 8 * 9 * Copyright(c) 2004 by Qiang Xue. All rights reserved. 10 * 11 * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue} 12 * The latest version of PRADO can be obtained from: 13 * {@link http://prado.sourceforge.net/} 14 * 15 * @author Wei Zhuo <weizhuo[at]gmail[dot]com> 16 * @version $Id: sfNumberFormatInfo.class.php 2834 2006-11-27 14:09:05Z fabien $ 17 * @package symfony 18 * @subpackage i18n 19 */ 20 21 /** 22 * sfNumberFormatInfo class 23 * 24 * Defines how numeric values are formatted and displayed, 25 * depending on the culture. Numeric values are formatted using 26 * standard or custom patterns stored in the properties of a 27 * sfNumberFormatInfo. 28 * 29 * This class contains information, such as currency, decimal 30 * separators, and other numeric symbols. 31 * 32 * To create a sfNumberFormatInfo for a specific culture, 33 * create a sfCultureInfo for that culture and retrieve the 34 * sfCultureInfo->NumberFormat property. Or use 35 * sfNumberFormatInfo::getInstance($culture). 36 * To create a sfNumberFormatInfo for the invariant culture, use the 37 * InvariantInfo::getInvariantInfo(). 38 * 39 * 40 * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com> 41 * @version v1.0, last update on Sun Dec 05 14:48:26 EST 2004 42 * @package System.I18N.core 43 */ 44 class sfNumberFormatInfo 45 { 46 /** 47 * ICU number formatting data. 48 * @var array 49 */ 50 protected $data = array(); 51 52 /** 53 * A list of properties that are accessable/writable. 54 * @var array 55 */ 56 protected $properties = array(); 57 58 /** 59 * The number pattern. 60 * @var array 61 */ 62 protected $pattern = array(); 63 64 const DECIMAL = 0; 65 const CURRENCY = 1; 66 const PERCENTAGE = 2; 67 const SCIENTIFIC = 3; 68 69 /** 70 * Allow functions that begins with 'set' to be called directly 71 * as an attribute/property to retrieve the value. 72 * 73 * @return mixed 74 */ 75 function __get($name) 76 { 77 $getProperty = 'get'.$name; 78 if (in_array($getProperty, $this->properties)) 79 { 80 return $this->$getProperty(); 81 } 82 else 83 { 84 throw new sfException('Property '.$name.' does not exists.'); 85 } 86 } 87 88 /** 89 * Allow functions that begins with 'set' to be called directly 90 * as an attribute/property to set the value. 91 */ 92 function __set($name, $value) 93 { 94 $setProperty = 'set'.$name; 95 if (in_array($setProperty, $this->properties)) 96 { 97 $this->$setProperty($value); 98 } 99 else 100 { 101 throw new sfException('Property '.$name.' can not be set.'); 102 } 103 } 104 105 /** 106 * Initializes a new writable instance of the sfNumberFormatInfo class 107 * that is dependent on the ICU data for number, decimal, and currency 108 * formatting information. <b>N.B.</b>You should not initialize this 109 * class directly unless you know what you are doing. Please use use 110 * sfNumberFormatInfo::getInstance() to create an instance. 111 * 112 * @param array ICU data for date time formatting. 113 * @see getInstance() 114 */ 115 function __construct($data = array(), $type = sfNumberFormatInfo::DECIMAL) 116 { 117 $this->properties = get_class_methods($this); 118 119 if (empty($data)) 120 { 121 throw new sfException('Please provide the ICU data to initialize.'); 122 } 123 124 $this->data = $data; 125 126 $this->setPattern($type); 127 } 128 129 /** 130 * Set the pattern for a specific number pattern. The validate patterns 131 * sfNumberFormatInfo::DECIMAL, sfNumberFormatInfo::CURRENCY, 132 * sfNumberFormatInfo::PERCENTAGE, or sfNumberFormatInfo::SCIENTIFIC 133 * 134 * @param int pattern type. 135 */ 136 function setPattern($type = sfNumberFormatInfo::DECIMAL) 137 { 138 if (is_int($type)) 139 { 140 $this->pattern = $this->parsePattern($this->data['NumberPatterns'][$type]); 141 } 142 else 143 { 144 $this->pattern = $this->parsePattern($type); 145 } 146 147 $this->pattern['negInfty'] = $this->data['NumberElements'][6].$this->data['NumberElements'][9]; 148 149 $this->pattern['posInfty'] = $this->data['NumberElements'][11].$this->data['NumberElements'][9]; 150 } 151 152 function getPattern() 153 { 154 return $this->pattern; 155 } 156 157 /** 158 * Gets the default sfNumberFormatInfo that is culture-independent (invariant). 159 * 160 * @return sfNumberFormatInfo default sfNumberFormatInfo. 161 */ 162 public function getInvariantInfo($type = sfNumberFormatInfo::DECIMAL) 163 { 164 static $invariant; 165 if (is_null($invariant)) 166 { 167 $culture = sfCultureInfo::getInvariantCulture(); 168 $invariant = $culture->NumberFormat; 169 $invariant->setPattern($type); 170 } 171 172 return $invariant; 173 } 174 175 /** 176 * Returns the sfNumberFormatInfo associated with the specified culture. 177 * 178 * @param sfCultureInfo the culture that gets the sfNumberFormat property. 179 * @param int the number formatting type, it should be 180 * sfNumberFormatInfo::DECIMAL, sfNumberFormatInfo::CURRENCY, 181 * sfNumberFormatInfo::PERCENTAGE, or sfNumberFormatInfo::SCIENTIFIC 182 * @return sfNumberFormatInfo sfNumberFormatInfo for the specified culture. 183 * @see getCurrencyInstance(); 184 * @see getPercentageInstance(); 185 * @see getScientificInstance(); 186 */ 187 public static function getInstance($culture = null, $type = sfNumberFormatInfo::DECIMAL) 188 { 189 if ($culture instanceof sfCultureInfo) 190 { 191 $formatInfo = $culture->getNumberFormat(); 192 $formatInfo->setPattern($type); 193 194 return $formatInfo; 195 } 196 else if (is_string($culture)) 197 { 198 $sfCultureInfo = new sfCultureInfo($culture); 199 $formatInfo = $sfCultureInfo->getNumberFormat(); 200 $formatInfo->setPattern($type); 201 202 return $formatInfo; 203 } 204 else 205 { 206 $sfCultureInfo = new sfCultureInfo(); 207 $formatInfo = $sfCultureInfo->getNumberFormat(); 208 $formatInfo->setPattern($type); 209 210 return $formatInfo; 211 } 212 } 213 214 /** 215 * Returns the currency format info associated with the specified culture. 216 * 217 * @param sfCultureInfo the culture that gets the NumberFormat property. 218 * @return sfNumberFormatInfo sfNumberFormatInfo for the specified culture. 219 */ 220 public static function getCurrencyInstance($culture = null) 221 { 222 return self::getInstance($culture, self::CURRENCY); 223 } 224 225 /** 226 * Returns the percentage format info associated with the specified culture. 227 * 228 * @param sfCultureInfo the culture that gets the NumberFormat property. 229 * @return sfNumberFormatInfo sfNumberFormatInfo for the specified culture. 230 */ 231 public static function getPercentageInstance($culture = null) 232 { 233 return self::getInstance($culture, self::PERCENTAGE); 234 } 235 236 /** 237 * Returns the scientific format info associated with the specified culture. 238 * 239 * @param sfCultureInfo the culture that gets the NumberFormat property. 240 * @return sfNumberFormatInfo sfNumberFormatInfo for the specified culture. 241 */ 242 public static function getScientificInstance($culture = null) 243 { 244 return self::getInstance($culture, self::SCIENTIFIC); 245 } 246 247 /** 248 * Parse the given pattern and return a list of known properties. 249 * 250 * @param string a number pattern. 251 * @return array list of pattern properties. 252 */ 253 protected function parsePattern($pattern) 254 { 255 $pattern = explode(';', $pattern); 256 257 $negative = null; 258 if (count($pattern) > 1) 259 { 260 $negative = $pattern[1]; 261 } 262 $pattern = $pattern[0]; 263 264 $comma = ','; 265 $dot = '.'; 266 $digit = '0'; 267 $hash = '#'; 268 269 // find the first group point, and decimal point 270 $groupPos1 = strrpos($pattern, $comma); 271 $decimalPos = strrpos($pattern, $dot); 272 273 $groupPos2 = false; 274 $groupSize1 = false; 275 $groupSize2 = false; 276 $decimalPoints = is_int($decimalPos) ? -1 : false; 277 278 $info['negPref'] = $this->data['NumberElements'][6]; 279 $info['negPost'] = ''; 280 281 $info['negative'] = $negative; 282 $info['positive'] = $pattern; 283 284 // find the negative prefix and postfix 285 if ($negative) 286 { 287 $prefixPostfix = $this->getPrePostfix($negative); 288 $info['negPref'] = $prefixPostfix[0]; 289 $info['negPost'] = $prefixPostfix[1]; 290 } 291 292 $posfix = $this->getPrePostfix($pattern); 293 $info['posPref'] = $posfix[0]; 294 $info['posPost'] = $posfix[1]; 295 296 if (is_int($groupPos1)) 297 { 298 // get the second group 299 $groupPos2 = strrpos(substr($pattern, 0, $groupPos1), $comma); 300 301 // get the number of decimal digits 302 if (is_int($decimalPos)) 303 { 304 $groupSize1 = $decimalPos - $groupPos1 - 1; 305 } 306 else 307 { 308 // no decimal point, so traverse from the back 309 // to find the groupsize 1. 310 for ($i = strlen($pattern) - 1; $i >= 0; $i--) 311 { 312 if ($pattern{$i} == $digit || $pattern{$i} == $hash) 313 { 314 $groupSize1 = $i - $groupPos1; 315 break; 316 } 317 } 318 } 319 320 // get the second group size 321 if (is_int($groupPos2)) 322 { 323 $groupSize2 = $groupPos1 - $groupPos2 - 1; 324 } 325 } 326 327 if (is_int($decimalPos)) 328 { 329 for ($i = strlen($pattern) - 1; $i >= 0; $i--) 330 { 331 if ($pattern{$i} == $dot) 332 { 333 break; 334 } 335 if ($pattern{$i} == $digit) 336 { 337 $decimalPoints = $i - $decimalPos; 338 break; 339 } 340 } 341 } 342 343 $info['groupPos1'] = $groupPos1; 344 $info['groupSize1'] = $groupSize1; 345 $info['groupPos2'] = $groupPos2; 346 $info['groupSize2'] = $groupSize2; 347 $info['decimalPos'] = $decimalPos; 348 $info['decimalPoints'] = $decimalPoints; 349 350 return $info; 351 } 352 353 /** 354 * Get the prefix and postfix of a pattern. 355 * 356 * @param string pattern 357 * @return array of prefix and postfix, array(prefix,postfix). 358 */ 359 protected function getPrePostfix($pattern) 360 { 361 $regexp = '/[#,\.0]+/'; 362 $result = preg_split($regexp, $pattern); 363 364 return array($result[0], $result[1]); 365 } 366 367 /** 368 * Indicates the number of decimal places. 369 * 370 * @return int number of decimal places. 371 */ 372 function getDecimalDigits() 373 { 374 return $this->pattern['decimalPoints']; 375 } 376 377 /** 378 * Set the number of decimal places. 379 * 380 * @param int number of decimal places. 381 */ 382 function setDecimalDigits($value) 383 { 384 return $this->pattern['decimalPoints'] = $value; 385 } 386 387 /** 388 * Gets the string to use as the decimal separator. 389 * 390 * @return string decimal separator. 391 */ 392 function getDecimalSeparator() 393 { 394 return $this->data['NumberElements'][0]; 395 } 396 397 /** 398 * Set the string to use as the decimal separator. 399 * 400 * @param string the decimal point 401 */ 402 function setDecimalSeparator($value) 403 { 404 return $this->data['NumberElements'][0] = $value; 405 } 406 407 /** 408 * Gets the string that separates groups of digits to the left 409 * of the decimal in currency values. 410 * 411 * @param parameter 412 * @return string currency group separator. 413 */ 414 function getGroupSeparator() 415 { 416 return $this->data['NumberElements'][1]; 417 } 418 419 /** 420 * Set the string to use as the group separator. 421 * 422 * @param string the group separator. 423 */ 424 function setGroupSeparator($value) 425 { 426 return $this->data['NumberElements'][1] = $value; 427 } 428 429 /** 430 * Gets the number of digits in each group to the left of the decimal 431 * There can be two grouping sizes, this fucntion 432 * returns <b>array(group1, group2)</b>, if there is only 1 grouping size, 433 * group2 will be false. 434 * 435 * @return array grouping size(s). 436 */ 437 function getGroupSizes() 438 { 439 $group1 = $this->pattern['groupSize1']; 440 $group2 = $this->pattern['groupSize2']; 441 442 return array($group1, $group2); 443 } 444 445 /** 446 * Set the number of digits in each group to the left of the decimal. 447 * There can be two grouping sizes, the value should 448 * be an <b>array(group1, group2)</b>, if there is only 1 grouping size, 449 * group2 should be false. 450 * 451 * @param array grouping size(s). 452 */ 453 function setGroupSizes($groupSize) 454 { 455 $this->pattern['groupSize1'] = $groupSize[0]; 456 $this->pattern['groupSize2'] = $groupSize[1]; 457 } 458 459 /** 460 * Gets the format pattern for negative values. 461 * The negative pattern is composed of a prefix, and postfix. 462 * This function returns <b>array(prefix, postfix)</b>. 463 * 464 * @return arary negative pattern. 465 */ 466 function getNegativePattern() 467 { 468 $prefix = $this->pattern['negPref']; 469 $postfix = $this->pattern['negPost']; 470 471 return array($prefix, $postfix); 472 } 473 474 /** 475 * Set the format pattern for negative values. 476 * The negative pattern is composed of a prefix, and postfix in the form 477 * <b>array(prefix, postfix)</b>. 478 * 479 * @param arary negative pattern. 480 */ 481 function setNegativePattern($pattern) 482 { 483 $this->pattern['negPref'] = $pattern[0]; 484 $this->pattern['negPost'] = $pattern[1]; 485 } 486 487 /** 488 * Gets the format pattern for positive values. 489 * The positive pattern is composed of a prefix, and postfix. 490 * This function returns <b>array(prefix, postfix)</b>. 491 * 492 * @return arary positive pattern. 493 */ 494 function getPositivePattern() 495 { 496 $prefix = $this->pattern['posPref']; 497 $postfix = $this->pattern['posPost']; 498 499 return array($prefix, $postfix); 500 } 501 502 /** 503 * Set the format pattern for positive values. 504 * The positive pattern is composed of a prefix, and postfix in the form 505 * <b>array(prefix, postfix)</b>. 506 * 507 * @param arary positive pattern. 508 */ 509 function setPositivePattern($pattern) 510 { 511 $this->pattern['posPref'] = $pattern[0]; 512 $this->pattern['posPost'] = $pattern[1]; 513 } 514 515 /** 516 * Gets the string to use as the currency symbol. 517 * 518 * @return string currency symbol. 519 */ 520 function getCurrencySymbol($currency = 'USD') 521 { 522 if (isset($this->pattern['symbol'])) 523 { 524 return $this->pattern['symbol']; 525 } 526 else 527 { 528 return $this->data['Currencies'][$currency][0]; 529 } 530 } 531 532 /** 533 * Set the string to use as the currency symbol. 534 * 535 * @param string currency symbol. 536 */ 537 function setCurrencySymbol($symbol) 538 { 539 $this->pattern['symbol'] = $symbol; 540 } 541 542 /** 543 * Gets the string that represents negative infinity. 544 * 545 * @return string negative infinity. 546 */ 547 function getNegativeInfinitySymbol() 548 { 549 return $this->pattern['negInfty']; 550 } 551 552 /** 553 * Set the string that represents negative infinity. 554 * 555 * @param string negative infinity. 556 */ 557 function setNegativeInfinitySymbol($value) 558 { 559 $this->pattern['negInfty'] = $value; 560 } 561 562 /** 563 * Gets the string that represents positive infinity. 564 * 565 * @return string positive infinity. 566 */ 567 function getPositiveInfinitySymbol() 568 { 569 return $this->pattern['posInfty']; 570 } 571 572 /** 573 * Set the string that represents positive infinity. 574 * 575 * @param string positive infinity. 576 */ 577 function setPositiveInfinitySymbol($value) 578 { 579 $this->pattern['posInfty'] = $value; 580 } 581 582 /** 583 * Gets the string that denotes that the associated number is negative. 584 * 585 * @return string negative sign. 586 */ 587 function getNegativeSign() 588 { 589 return $this->data['NumberElements'][6]; 590 } 591 592 /** 593 * Set the string that denotes that the associated number is negative. 594 * 595 * @param string negative sign. 596 */ 597 function setNegativeSign($value) 598 { 599 $this->data['NumberElements'][6] = $value; 600 } 601 602 /** 603 * Gets the string that denotes that the associated number is positive. 604 * 605 * @return string positive sign. 606 */ 607 function getPositiveSign() 608 { 609 return $this->data['NumberElements'][11]; 610 } 611 612 /** 613 * Set the string that denotes that the associated number is positive. 614 * 615 * @param string positive sign. 616 */ 617 function setPositiveSign($value) 618 { 619 $this->data['NumberElements'][11] = $value; 620 } 621 622 /** 623 * Gets the string that represents the IEEE NaN (not a number) value. 624 * 625 * @return string NaN symbol. 626 */ 627 function getNaNSymbol() 628 { 629 return $this->data['NumberElements'][10]; 630 } 631 632 /** 633 * Set the string that represents the IEEE NaN (not a number) value. 634 * 635 * @param string NaN symbol. 636 */ 637 function setNaNSymbol($value) 638 { 639 $this->data['NumberElements'][10] = $value; 640 } 641 642 /** 643 * Gets the string to use as the percent symbol. 644 * 645 * @return string percent symbol. 646 */ 647 function getPercentSymbol() 648 { 649 return $this->data['NumberElements'][3]; 650 } 651 652 /** 653 * Set the string to use as the percent symbol. 654 * 655 * @param string percent symbol. 656 */ 657 function setPercentSymbol($value) 658 { 659 $this->data['NumberElements'][3] = $value; 660 } 661 662 /** 663 * Gets the string to use as the per mille symbol. 664 * 665 * @return string percent symbol. 666 */ 667 function getPerMilleSymbol() 668 { 669 return $this->data['NumberElements'][8]; 670 } 671 672 /** 673 * Set the string to use as the per mille symbol. 674 * 675 * @param string percent symbol. 676 */ 677 function setPerMilleSymbol($value) 678 { 679 $this->data['NumberElements'][8] = $value; 680 } 681 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |