[ Index ]
 

Code source de PRADO 3.0.6

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/framework/I18N/ -> TNumberFormat.php (source)

   1  <?php
   2  /**
   3   * TNumberFromat component.
   4   *
   5   * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
   6   * @link http://www.pradosoft.com/
   7   * @copyright Copyright &copy; 2005 PradoSoft
   8   * @license http://www.pradosoft.com/license/
   9   * @version $Id: TNumberFormat.php 1397 2006-09-07 07:55:53Z wei $
  10   * @package System.I18N
  11   */
  12  
  13  /**
  14   * Get the NumberFormat class.
  15   */
  16  Prado::using('System.I18N.core.NumberFormat');
  17  
  18  /**
  19   * Get the parent control class.
  20   */
  21  Prado::using('System.I18N.TI18NControl');
  22  
  23  /**
  24    * To format numbers in locale sensitive manner use
  25    * <code>
  26    * <com:TNumberFormat Pattern="0.##" value="2.0" />
  27    * </code>
  28    *
  29    * Numbers can be formatted as currency, percentage, decimal or scientific
  30    * numbers by specifying the Type attribute. The known types are
  31    * "currency", "percentage", "decimal" and "scientific".
  32    *
  33    * If someone from US want to see sales figures from a store in
  34    * Germany (say using the EURO currency), formatted using the german
  35    * currency, you would need to use the attribute Culture="de_DE" to get
  36    * the currency right, e.g. 100,00 â‚?. The decimal and grouping separator is
  37    * then also from the de_DE locale. This may lead to some confusion because
  38    * people from US know the "," as thousand separator. Therefore a "Currency"
  39    * attribute is available, so that the output from the following example
  40    * results in â‚?100.00
  41    * <code>
  42    * <com:TNumberFormat Type="currency" Culture="en_US" Currency="EUR" Value="100" />
  43    * </code>
  44    *
  45    * Namespace: System.I18N
  46    *
  47    * Properties
  48    * - <b>Value</b>, number,
  49    *   <br>Gets or sets the number to format. The tag content is used as Value
  50    *   if the Value property is not specified.
  51    * - <b>Type</b>, string,
  52    *   <br>Gets or sets the formatting type. The valid types are
  53    *    'decimal', 'currency', 'percentage' and 'scientific'.
  54    * - <b>Currency</b>, string,
  55    *   <br>Gets or sets the currency symbol for the currency format.
  56    *   The default is 'USD' if the Currency property is not specified.
  57    * - <b>Pattern</b>, string,
  58    *   <br>Gets or sets the custom number formatting pattern.
  59    *
  60    * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
  61    * @version v1.0, last update on Sat Dec 11 17:49:56 EST 2004
  62    * @package System.I18N
  63    */
  64  class TNumberFormat extends TI18NControl
  65  {
  66      /**
  67       * Default NumberFormat, set to the application culture.
  68       * @var NumberFormat
  69       */
  70      protected static $formatter;
  71  
  72      /**
  73       * Get the number formatting pattern.
  74       * @return string format pattern.
  75       */
  76  	public function getPattern()
  77      {
  78          return $this->getViewState('Pattern','');
  79      }
  80  
  81      /**
  82       * Set the number format pattern.
  83       * @param string format pattern.
  84       */
  85  	public function setPattern($pattern)
  86      {
  87          $this->setViewState('Pattern',$pattern,'');
  88      }
  89  
  90      /**
  91       * Get the numberic value for this control.
  92       * @return string number
  93       */
  94  	public function getValue()
  95      {
  96          return $this->getViewState('Value','');
  97      }
  98  
  99      /**
 100       * Set the numberic value for this control.
 101       * @param string the number value
 102       */
 103  	public function setValue($value)
 104      {
 105          $this->setViewState('Value',$value,'');
 106      }
 107  
 108      /**
 109       * Get the formatting type for this control.
 110       * @return string formatting type.
 111       */
 112  	public function getType()
 113      {
 114          return $this->getViewState('Type','d');
 115      }
 116  
 117      /**
 118       * Set the formatting type for this control.
 119       * @param string formatting type, either "decimal", "currency","percentage"
 120       * or "scientific"
 121       * @throws TPropertyTypeInvalidException
 122       */
 123  	public function setType($type)
 124      {
 125          $type = strtolower($type);
 126  
 127          switch($type)
 128          {
 129              case 'decimal':
 130                  $this->setViewState('Type','d',''); break;
 131              case 'currency':
 132                  $this->setViewState('Type','c',''); break;
 133              case 'percentage':
 134                  $this->setViewState('Type','p',''); break;
 135              case 'scientific':
 136                  $this->setViewState('Type','e',''); break;
 137              default:
 138                  throw new TInvalidDataValueException('numberformat_type_invalid',$type);
 139          }
 140  
 141      }
 142  
 143      /**
 144       * @return string 3 letter currency code. Defaults to 'USD'.
 145       */
 146  	public function getCurrency()
 147      {
 148          return $this->getViewState('Currency','USD');
 149      }
 150  
 151      /**
 152       * Set the 3-letter ISO 4217 code. For example, the code
 153       * "USD" represents the US Dollar and "EUR" represents the Euro currency.
 154       * @param string currency code.
 155       */
 156  	public function setCurrency($currency)
 157      {
 158          $this->setViewState('Currency', $currency,'');
 159      }
 160  
 161      /**
 162       * Formats the localized number, be it currency or decimal, or percentage.
 163       * If the culture is not specified, the default application
 164       * culture will be used.
 165       * @return string formatted number
 166       */
 167  	protected function getFormattedValue()
 168      {
 169          $app = $this->getApplication()->getGlobalization();
 170          //initialized the default class wide formatter
 171          if(is_null(self::$formatter))
 172              self::$formatter = new NumberFormat($app->getCulture());
 173  
 174          $pattern = strlen($this->getPattern()) > 0
 175                          ? $this->getPattern() : $this->getType();
 176  
 177          $culture = $this->getCulture();
 178          //return the specific cultural formatted number
 179          if(!empty($culture) && $app->getCulture() != $culture)
 180          {
 181              $formatter = new NumberFormat($culture);
 182              return $formatter->format($this->getValue(),$pattern,
 183                                        $this->getCurrency(),
 184                                        $this->getCharset());
 185          }
 186  
 187          //return the application wide culture formatted number.
 188          return self::$formatter->format($this->getValue(),$pattern,
 189                                          $this->getCurrency(),
 190                                          $this->getCharset());
 191      }
 192  
 193  	public function render($writer)
 194      {
 195          $writer->write($this->getFormattedValue());
 196      }
 197  }
 198  
 199  ?>


Généré le : Sun Feb 25 21:07:04 2007 par Balluche grâce à PHPXref 0.7