| 1 |
lars |
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 © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TNumberFormat.php 2510 2008-10-13 10:28:33Z carl $
|
|
|
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 |
* - <b>DefaultText</b>, string,
|
|
|
60 |
* <br>Gets or sets the default text. If Value is not set, DefaultText will be
|
|
|
61 |
* shown instead of the default currency Value/Pattern.
|
|
|
62 |
*
|
|
|
63 |
* @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
64 |
* @version v1.0, last update on Sat Dec 11 17:49:56 EST 2004
|
|
|
65 |
* @package System.I18N
|
|
|
66 |
*/
|
|
|
67 |
class TNumberFormat extends TI18NControl implements IDataRenderer
|
|
|
68 |
{
|
|
|
69 |
/**
|
|
|
70 |
* Default NumberFormat, set to the application culture.
|
|
|
71 |
* @var NumberFormat
|
|
|
72 |
*/
|
|
|
73 |
protected static $formatter;
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Get the number formatting pattern.
|
|
|
77 |
* @return string format pattern.
|
|
|
78 |
*/
|
|
|
79 |
public function getPattern()
|
|
|
80 |
{
|
|
|
81 |
return $this->getViewState('Pattern','');
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Set the number format pattern.
|
|
|
86 |
* @param string format pattern.
|
|
|
87 |
*/
|
|
|
88 |
public function setPattern($pattern)
|
|
|
89 |
{
|
|
|
90 |
$this->setViewState('Pattern',$pattern,'');
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Get the numberic value for this control.
|
|
|
95 |
* @return string number
|
|
|
96 |
*/
|
|
|
97 |
public function getValue()
|
|
|
98 |
{
|
|
|
99 |
return $this->getViewState('Value','');
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Set the numberic value for this control.
|
|
|
104 |
* @param string the number value
|
|
|
105 |
*/
|
|
|
106 |
public function setValue($value)
|
|
|
107 |
{
|
|
|
108 |
$this->setViewState('Value',$value,'');
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* Get the default text value for this control.
|
|
|
113 |
* @return string default text value
|
|
|
114 |
*/
|
|
|
115 |
public function getDefaultText()
|
|
|
116 |
{
|
|
|
117 |
return $this->getViewState('DefaultText','');
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* Set the default text value for this control.
|
|
|
122 |
* @param string default text value
|
|
|
123 |
*/
|
|
|
124 |
public function setDefaultText($value)
|
|
|
125 |
{
|
|
|
126 |
$this->setViewState('DefaultText',$value,'');
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* Get the numberic value for this control.
|
|
|
131 |
* This method is required by {@link IDataRenderer}.
|
|
|
132 |
* It is the same as {@link getValue()}.
|
|
|
133 |
* @return string number
|
|
|
134 |
* @see getValue
|
|
|
135 |
* @since 3.1.2
|
|
|
136 |
*/
|
|
|
137 |
public function getData()
|
|
|
138 |
{
|
|
|
139 |
return $this->getValue();
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
/**
|
|
|
143 |
* Set the numberic value for this control.
|
|
|
144 |
* This method is required by {@link IDataRenderer}.
|
|
|
145 |
* It is the same as {@link setValue()}.
|
|
|
146 |
* @param string the number value
|
|
|
147 |
* @see setValue
|
|
|
148 |
* @since 3.1.2
|
|
|
149 |
*/
|
|
|
150 |
public function setData($value)
|
|
|
151 |
{
|
|
|
152 |
$this->setValue($value);
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
/**
|
|
|
156 |
* Get the formatting type for this control.
|
|
|
157 |
* @return string formatting type.
|
|
|
158 |
*/
|
|
|
159 |
public function getType()
|
|
|
160 |
{
|
|
|
161 |
return $this->getViewState('Type','d');
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
/**
|
|
|
165 |
* Set the formatting type for this control.
|
|
|
166 |
* @param string formatting type, either "decimal", "currency","percentage"
|
|
|
167 |
* or "scientific"
|
|
|
168 |
* @throws TPropertyTypeInvalidException
|
|
|
169 |
*/
|
|
|
170 |
public function setType($type)
|
|
|
171 |
{
|
|
|
172 |
$type = strtolower($type);
|
|
|
173 |
|
|
|
174 |
switch($type)
|
|
|
175 |
{
|
|
|
176 |
case 'decimal':
|
|
|
177 |
$this->setViewState('Type','d',''); break;
|
|
|
178 |
case 'currency':
|
|
|
179 |
$this->setViewState('Type','c',''); break;
|
|
|
180 |
case 'percentage':
|
|
|
181 |
$this->setViewState('Type','p',''); break;
|
|
|
182 |
case 'scientific':
|
|
|
183 |
$this->setViewState('Type','e',''); break;
|
|
|
184 |
default:
|
|
|
185 |
throw new TInvalidDataValueException('numberformat_type_invalid',$type);
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
/**
|
|
|
191 |
* @return string 3 letter currency code. Defaults to 'USD'.
|
|
|
192 |
*/
|
|
|
193 |
public function getCurrency()
|
|
|
194 |
{
|
|
|
195 |
return $this->getViewState('Currency','USD');
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
/**
|
|
|
199 |
* Set the 3-letter ISO 4217 code. For example, the code
|
|
|
200 |
* "USD" represents the US Dollar and "EUR" represents the Euro currency.
|
|
|
201 |
* @param string currency code.
|
|
|
202 |
*/
|
|
|
203 |
public function setCurrency($currency)
|
|
|
204 |
{
|
|
|
205 |
$this->setViewState('Currency', $currency,'');
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
/**
|
|
|
209 |
* Formats the localized number, be it currency or decimal, or percentage.
|
|
|
210 |
* If the culture is not specified, the default application
|
|
|
211 |
* culture will be used.
|
|
|
212 |
* @return string formatted number
|
|
|
213 |
*/
|
|
|
214 |
protected function getFormattedValue()
|
|
|
215 |
{
|
|
|
216 |
$value = $this->getValue();
|
|
|
217 |
$defaultText = $this->getDefaultText();
|
|
|
218 |
if(empty($value) && !empty($defaultText))
|
|
|
219 |
return $this->getDefaultText();
|
|
|
220 |
|
|
|
221 |
$app = $this->getApplication()->getGlobalization();
|
|
|
222 |
//initialized the default class wide formatter
|
|
|
223 |
if(is_null(self::$formatter))
|
|
|
224 |
self::$formatter = new NumberFormat($app->getCulture());
|
|
|
225 |
|
|
|
226 |
$pattern = strlen($this->getPattern()) > 0
|
|
|
227 |
? $this->getPattern() : $this->getType();
|
|
|
228 |
|
|
|
229 |
$culture = $this->getCulture();
|
|
|
230 |
//return the specific cultural formatted number
|
|
|
231 |
if(!empty($culture) && $app->getCulture() != $culture)
|
|
|
232 |
{
|
|
|
233 |
$formatter = new NumberFormat($culture);
|
|
|
234 |
return $formatter->format($this->getValue(),$pattern,
|
|
|
235 |
$this->getCurrency(),
|
|
|
236 |
$this->getCharset());
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
//return the application wide culture formatted number.
|
|
|
240 |
return self::$formatter->format($this->getValue(),$pattern,
|
|
|
241 |
$this->getCurrency(),
|
|
|
242 |
$this->getCharset());
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
public function render($writer)
|
|
|
246 |
{
|
|
|
247 |
$writer->write($this->getFormattedValue());
|
|
|
248 |
}
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
?>
|