/framework/I18N/TNumberFormat.php

https://bitbucket.org/volatileeight/prado · PHP · 249 lines · 95 code · 24 blank · 130 comment · 6 complexity · 6cf703b40970876d78e8adfb497b01c6 MD5 · raw file

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