mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-02 21:41:19 +08:00
Merge branch '2359-formatter-refactored' of https://github.com/Erik-r/yii2-1 into Erik-r-2359-formatter-refactored
* '2359-formatter-refactored' of https://github.com/Erik-r/yii2-1: #2359 Bugfix in normalizeDatetimeValue after regression test #2359 one test failed in Travis because standard medium date format is different in Travis then on my locale PC. #2359 testcases adapted and compatibility to old tests improved remove comment lines in asRelativeTime Typo in function call #2359 Namespace corrected #2359 Refactored formatter class #2359 which works with or without intl extension. Use PHP format patterns alsow with intl. Class is compatible with previous version.
This commit is contained in:
278
docs/guide/formatter.md
Normal file
278
docs/guide/formatter.md
Normal file
@ -0,0 +1,278 @@
|
||||
Formatter
|
||||
=========
|
||||
|
||||
Formatter is a helper component to format machine readable data of type date, time, different number types
|
||||
in user readable formats. Most of this types are in countries differently formatted (eg. date: US -> '10/31/2014,
|
||||
or DE -> '31.10.2014' or ISO -> '2014-10-31'). Same with decimals and currency values (eg. currency: US-> '$ 13,250.22' or
|
||||
de-DE -> '13.250,22 <20>' or de-CH ->'CHF 13'250.22')
|
||||
|
||||
Formatter supports unformatting also. A localized formatted date or number can be unformatted into a machine readable
|
||||
type (eg. '13.250,22 <20>' -> '13250.22')
|
||||
|
||||
This formatter version is merged from old `yii\base\formatter` and `yii\i18n\formatter` which supports localized formatting
|
||||
and "english only" formatting.
|
||||
|
||||
Formatter uses the php extension "intl" if the extension is loaded. "Intl" uses [ICU standard](http://site.icu-project.org/) driven
|
||||
by IBM. "intl" internally knows all formats of all countries and it translates month or day names into the corrcect language.
|
||||
Unfortunately ICU don't use same format patterns like php (eg. ICU: 'yyyy-mm-dd' php: 'Y-m-d' or icu: 'yy-m-d' php: 'y-n-j').
|
||||
Therefore formatter class has built in a pattern conversions from php to icu or icu to php. Formatter communicates in their interface
|
||||
functions per standard with php patterns, but it's also possible to communicate with icu patterns. (compare patterns see [PDF](http://www.guggach.com/tl_files/yii2/Difference%20of%20Date%20formats%20%20used%20in%20Yii2.pdf))
|
||||
|
||||
If "intl" isn't loaded formatter works also in the same way. Even the named date, time or datetime formats from icu "short", "medium", "long"
|
||||
and "full" are supported. Without a separate localized format definition US formats are used. Formatter provides a possibility to enter
|
||||
localized format patterns in an array (class formatDefs). Formatter uses this definitions but can't translate month or day names into the
|
||||
correct language.
|
||||
|
||||
If the application should support localized outputs "intl" extension should be enabled in php.ini. If Apache (xampp on windows only) doesn't
|
||||
start correctly then you must install a library of Microsoft ([Visual C++ Redistributable](http://www.microsoft.com/de-de/download/details.aspx?id=30679))
|
||||
|
||||
Installation / Configuration
|
||||
----------------------------
|
||||
Formatter class must be registered in Yii config (eg. app/config/web.php) in section 'components'. The class must be specified. All other parameters are
|
||||
optional. The following example shows all parameters and explain their default values. In most cases default values are ideal.
|
||||
|
||||
```php
|
||||
'components' => [
|
||||
'formatter' => [
|
||||
'class' => 'guggach\helpers\Formatter',
|
||||
// 'dateFormat' => 'medium', // default: 'medium'
|
||||
// 'timeFormat' => 'medium', // default: 'medium'
|
||||
// 'datetimeFormat' => 'medium' // default: 'medium'
|
||||
// 'dbFormat' => ['date' => 'Y-m-d','time' => 'H:i:s', 'dbtimeshort'=>'H:i' ,'datetime' => 'Y-m-d H:i:s',
|
||||
'dbdatetimeshort' => 'Y-m-d H:i']
|
||||
// 'local' => 'de-CH' // default: yii::$app->locale
|
||||
// 'timezone' => 'Europe/Berlin' // default: yii::$app->timezone
|
||||
// 'nullDisplay => '<span class="not-set">(not set)</span>' // default '(not set)' translated with yii::t
|
||||
// 'booleanFormat' => ['No', 'Yes'] // default: ['No', 'Yes'] translated with yii::t
|
||||
// 'numberFormatOptions' => [] // see intl NumberFormatter
|
||||
// 'numberTextFormatOptions' => [] // see intl NumberFormatter
|
||||
// 'decimalSeparator' => '.' // default ICU locale definition or FormatDefs class
|
||||
// 'thousandSeparator' => ',' // default ICU locale definition or FormatDefs class
|
||||
// 'currencyCode' => 'USD' // default ICU locale definition or FormatDefs class
|
||||
// 'RoundingIncrement' => 0.01 // default to number of decimals, currency = 0.01 (except Switzerland 0.05)
|
||||
// 'sizeFormat' => [] // default ['base' = 1024 , 'decimals' = 2, 'decimalSeparator' = null]
|
||||
];
|
||||
```
|
||||
|
||||
Details for each parameter are described in formatter.php.
|
||||
|
||||
Using formatter
|
||||
---------------
|
||||
Formatter is a general component which is registered in `yii::$app->formatter`. Mostly used funtions are `yii::$app->formatter->format()`
|
||||
or `yii::$app->formatter->unformat()`.
|
||||
|
||||
Usage in Code, example:
|
||||
|
||||
|
||||
$formattedValueString = yii::$app->formatter->format($value, ['date' , 'd-m-y' , 'php']);
|
||||
|
||||
$value = yii::$app->formatter->unformat($formattedValueString, ['currency']);
|
||||
|
||||
|
||||
Format and unformat function has minimal two parameter like
|
||||
|
||||
`(un)format( mixed $value , [ 'format as' , 'optional 1', 'optional 2' ... ])`
|
||||
|
||||
1. $value (must): machine readable date, time, datetime or number which hast to be formatted.
|
||||
2. 'format as' (must): defines what type the value is and what kind of format is expected.
|
||||
3. 'Option n': The number of paramters and the content is dependant of 'format as'. Details see in further chapters.
|
||||
|
||||
In following chapters all formatters are described in detail with input parameters.
|
||||
|
||||
|
||||
###Format or unformat as 'date' or 'time or 'datetime
|
||||
|
||||
`format(date (mixed), ['date'/'time'/'datetime' , 'target format pattern', 'input format pattern' , 'format type'])`
|
||||
|
||||
####Input:
|
||||
1. date mixed (must):
|
||||
It can be a Unix timestamp, DateTime object or a date string. A string must be in ISO format ('2014-10-05') or in local
|
||||
date format (de-DE: '05.10.2014'). If another format is given the 'input format pattern' must be specified (eg. US: 'M/d/Y')
|
||||
2. 'date'/'time'/'datetime' (must):
|
||||
A timestamp (DateTime object) can be formatted in a date (eg. '2014-10-05') or a time (eg. 15:20:30) or in a
|
||||
datetime (eg. '2014-10-05 15:20:30'). This parameter specifies which of these three formats should be done.
|
||||
3. 'target format pattern' (optinal):
|
||||
Formatter has four predefined format patterns: `short` ('y-n-j' = 14-10-25), `medium`('Y-m-d' = 2014-10-25), `long` ('F j, Y' = October
|
||||
25, 2014) or 'full' ('l, F j, Y' = Saturday, October 25, 2014). An individual format can be defined by a pattern string like 'd. F Y' ( 25. October 2014).
|
||||
The individual pattern string is per default in 'php' syntax. Alternatively ICU pattern (dd. MMMM yyyy) could be used. (see format type)
|
||||
4. 'input format pattern' (optional):
|
||||
If input date is a string formatter convert into a DateTime object internally. Formatter recognize ISO format (2014-10-25) or locale format.
|
||||
All other formats will produce a false result unless the input format is defined in this parameter. Default format pattern is php.
|
||||
5. 'format type' (optional):
|
||||
Per default formatter communicates with 'php' format patterns independent if ICU is used or not. This makes format handling in Yii easier because
|
||||
a developer is concentrating to one format all over the application. Nevertheless ICU patterns can be used if this parameter is 'icu'.
|
||||
|
||||
####Output:
|
||||
Formatted string like '25th October 2014'.
|
||||
|
||||
|
||||
|
||||
`unformat(date (string), ['date'/'time'/'datetime', 'target format', 'input format pattern', 'format type'])`
|
||||
|
||||
####Input:
|
||||
1. date as string:
|
||||
Formatted date as string. The string must be in ISO format ('2014-10-05') or in local
|
||||
date format (de-DE: '05.10.2014'). If another format is given the 'input format pattern' must be specified (eg. US: 'M/d/Y').
|
||||
2. 'date'/'time'/'datetime' (must):
|
||||
Defines if input is a date or a time or a datetime string.
|
||||
3. 'target format' (optional):
|
||||
Valid values are `'db'`or `'timestamp'`. Default is 'db' because a user readable date string must be stored in a database. Databases mostly
|
||||
accept ISO format ('2014-10-25') only. If format is different the database format can be configured in variable `dbFormat` (array).
|
||||
4. 'input format pattern' (optional):
|
||||
see format date function
|
||||
5. 'format type' (optional):
|
||||
see format date function
|
||||
|
||||
###Format as Timestamp
|
||||
|
||||
`format ('date'/'time'/'datetime' (string), ['timestamp', 'input format pattern'])`
|
||||
|
||||
####Input:
|
||||
|
||||
Parameter see 'format date'.
|
||||
|
||||
####Output:
|
||||
|
||||
Long integer (64bit or float) with Unix Timestamp in seconds from 01/01/1970.
|
||||
|
||||
|
||||
###Format or unformat as Integer
|
||||
|
||||
`format(value (mixed), ['integer' , 'thousandSeparator'])`
|
||||
|
||||
`unformat(value (string), 'integer')`
|
||||
|
||||
####Input:
|
||||
1. value (integer, float, numeric string) (must):
|
||||
In format function a numeric string or a float or an integer is necessary. A float with decimals will be mathematically rounded to an integer.
|
||||
The unformat function needs a string which can have thousand separators but it must be numeric.
|
||||
2. 'integer' (string)(must):
|
||||
This is the name of formatting function which is used.
|
||||
3. 'thousandSeparator' (boolean) (optional):
|
||||
Valid values are `true`or `false`. Default is `true`. If value is true the output is formatted with thousand separator concerning the locale
|
||||
definition or the value of the variable 'thousandSeparator'.
|
||||
|
||||
####Output:
|
||||
Formatted string like '23,456,698'.
|
||||
|
||||
###Format or unformat as Double, Number, Decimal
|
||||
Double, Number, Decimal are all synonyms for floating numbers with decimals.
|
||||
|
||||
`format(value (mixed), ['double', decimals (int) , roundIncrement (float), grouping (boolean)`
|
||||
|
||||
`unformat(value (string), 'double')`
|
||||
|
||||
####Input:
|
||||
1. value (float, numeric string) (must):
|
||||
In format function a numeric string or a float is necessary. A float with decimals will be mathematically rounded to the number of decimals.
|
||||
The unformat function needs a string which can have thousand separators but it must be numeric.
|
||||
2. 'double' (string) (must):
|
||||
This is the name of the formatting function. As synonym 'decimals' and 'number' could be used.
|
||||
3. decimals (integer) (optional):
|
||||
Number of decimals after comma. Per default 2 is set. If the number of decimals is less than 6 zeros are filled until number of decimals
|
||||
(eg. decimals = 4 -> 3.42 -> 3.4200).
|
||||
4. RoundIncrement (float) (optinal):
|
||||
If roundIncrement isn't set the float will be rounded to last requested decimal with mathematical rule (eg. decimals = 3 -> 3.45662 --> 3.457).
|
||||
If roundIncrement is set to '0.01' float will be rounded to this value (eg. decimals = 3 -> 3.45662 -> 3.46000).
|
||||
5. Grouping (boolean) (optional):
|
||||
If value is true the float will be formatted with thousand separator otherwise not. Default is true.
|
||||
|
||||
####Output:
|
||||
Formatted string like '3,456,698.65'
|
||||
|
||||
###Format or unformat as currency
|
||||
|
||||
`format(value (mixed), ['currency', 'currency code' (string) , roundIncrement (float), grouping (boolean)`
|
||||
|
||||
`unformat(value (string), 'currency')`
|
||||
|
||||
###Input:
|
||||
1. value (float, numeric string) (must):
|
||||
In format function a numeric string or a float is necessary. The currency amount with decimals will be mathematically rounded to two decimals.
|
||||
The unformat function needs a string which can have thousand separators but it must be numeric.
|
||||
2. 'currency' (string) (must):
|
||||
This is the name of the formatting function.
|
||||
3. 'currency code' (string) (optional):
|
||||
Normally currency code is provided by ICU from locale setting. If another currency is needed the code as string can be set here.
|
||||
4. RoundIncrement (float) (optinal):
|
||||
If roundIncrement isn't set the float will be rounded to last requested decimal with mathematical rule.
|
||||
If roundIncrement is set to '0.05' (Switzerland with 5 cents only) float will be rounded to this value (eg. 3.26 -> 3.25 or 3.275 -> 3.30).
|
||||
5. Grouping (boolean) (optional):
|
||||
If value is true the float will be formatted with thousand separator otherwise not. Default is true.
|
||||
|
||||
####Output:
|
||||
Formatted string like '$ 13,256.23'
|
||||
|
||||
###Format or unformat as scientific
|
||||
|
||||
`format(value (mixed), ['scientific', decimals (integer))`
|
||||
|
||||
`unformat(value (string), 'scientific')`
|
||||
|
||||
Scientific formats a float to a scientifc string like '23216' to '2.32166E4'.
|
||||
|
||||
###Format or unformat as percent
|
||||
|
||||
`format(value (mixed), ['percent', decimals (integer) , grouping (boolean)`
|
||||
|
||||
`unformat(value (string), 'percent')`
|
||||
|
||||
###Input:
|
||||
1. value (float, numeric string) (must):
|
||||
In format function a numeric string or a float is necessary. Value is a factor like '0.75' -> '75%'.
|
||||
The unformat function needs a string which can have thousand separators but it must be numeric. It will be converted back to a factor. '75%' -> '0.75'
|
||||
2. 'percent':
|
||||
This is the name of the formatting function.
|
||||
3. decimals (integer) (optional):
|
||||
Number of decimals. The formatter rounds a float to the number of decimals.
|
||||
4. Grouping (boolean) (optional):
|
||||
If value is true the percent value will be formatted with thousand separator otherwise not. Default is true.
|
||||
|
||||
####Output:
|
||||
Formatted string like '75.05%'. The unformatter produces a float as factor like 0.7505.
|
||||
|
||||
###Further formatters
|
||||
Formatter has formatting rules for Text, HTML, Email, Boolean etc.
|
||||
|
||||
`format(value (mixed), 'name of formatter')`
|
||||
|
||||
Formatters are:
|
||||
`boolean:` Ouput is a string with 'Yes' or 'No' translated to locale setting.
|
||||
`email:` Converts an email address to a 'mailto' link.
|
||||
`html:` Converts a string with html-purifier to avoid XSS attacks.
|
||||
`image:` Formats a html image tag around a path to image.
|
||||
`NText:` Formats the value as an HTML-encoded plain text with newlines converted into breaks.
|
||||
`Paragraphs:` Formats the value as HTML-encoded text paragraphs. Each text paragraph is enclosed within a `<p>` tag.
|
||||
`raw:` Formats the value as is without any formatting. Null values are showed as 'not set'.
|
||||
`size:` Formats a number as byte, kilobyte, megabyte etc. depending on the size of the number. Normally 'Mb' is given unless optional parameter is true.
|
||||
`text:` Formats the value as an HTML-encoded plain text.
|
||||
`url:` Formats the value as a hyperlink. If necessary it adds 'https://'.
|
||||
|
||||
Other functions
|
||||
---------------
|
||||
|
||||
###getLocale()
|
||||
Shows the current used locale setting in format like 'de-DE'.
|
||||
|
||||
###setLocale($locale)
|
||||
Per default formatter uses locale information from application configuration. If another local definition is requested on the fly this function changes the internal pattern settings, decimal sign and thousand separator to the requested locale. It return this formatter object to enable chaining.
|
||||
|
||||
###getDecimalSeparator()
|
||||
Shows the current separator for decimal number.
|
||||
|
||||
###setDecimalSeparator($sign)
|
||||
Per default formatter uses the decimal separator sign from ICU concerning locale setting. With setDecimalSeparator the value can be overridden.
|
||||
|
||||
###getThousandSeparator()
|
||||
Shows the current separator for thousand grouping.
|
||||
|
||||
###SetThousandSeparator($sign)
|
||||
Per default formatter uses the thousand separator sign from ICU concerning locale setting. With setThousandSeparator the value can be overridden.
|
||||
|
||||
###convertPatternPhpToIcu($pattern, $type)
|
||||
Converts a php date or time format pattern from PHP syntax to ICU syntax (eg. 'Y-m-d' -> 'yyyy-MM-dd'). If pattern is 'short', 'medium', 'long' or 'full' then the parameter 'type' must be defined like 'date', 'time' or 'datetime'. In this case the function delivers the correct ICU pattern (eg. 'medium' and 'date' -> 'dd-MM-yyyy'.
|
||||
|
||||
###convertPatternIcuToPhp($pattern, $type)
|
||||
Converts a ICU date or time format pattern from ICU syntax to PHP syntax (eg. 'yyyy-MM-dd' -> 'Y-m-d'). If pattern is 'short', 'medium', 'long' or 'full' then the parameter 'type' must be defined like 'date', 'time' or 'datetime'. In this case the function delivers the correct PHP pattern (eg. 'medium' and 'date' -> 'd-m-Y'.
|
||||
|
||||
|
||||
@ -4,6 +4,7 @@ Yii Framework 2 Change Log
|
||||
2.0.0-rc under development
|
||||
--------------------------
|
||||
|
||||
- Enh #2359: Refactored formatter class. One class with or without intl extension and PHP format pattern as standard. (Erik_r)
|
||||
- Bug #1263: Fixed the issue that Gii and Debug modules might be affected by incompatible asset manager configuration (qiangxue)
|
||||
- Bug #2314: Gii model generator does not generate correct relation type in some special case (qiangxue)
|
||||
- Bug #2563: Theming is not working if the path map of the theme contains ".." or "." in the paths (qiangxue)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
59
framework/i18n/FormatDefs.php
Normal file
59
framework/i18n/FormatDefs.php
Normal file
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace yii\i18n;
|
||||
|
||||
/**
|
||||
* International format definitions for decimal separator, thousand separator, dates,
|
||||
* times and datetimes.
|
||||
*
|
||||
* Is only used if php extension isn't loaded. Otherwise the official ICU standard is
|
||||
* used.
|
||||
*
|
||||
* Returns an array per local settings. Set the option 'language' => 'de-CH' in yii
|
||||
* config file.
|
||||
*
|
||||
* Each language has xxx elements in their array like:
|
||||
* [0] = decimal separator ('.')
|
||||
* [1] = thousand separator (',')
|
||||
* [2] = date short ('y-m-d')
|
||||
* [3] = date medium ('Y-m-d')
|
||||
* [4] = date long ('F j, Y')
|
||||
* [5] = date full ('l, F j, Y')
|
||||
* [6] = time short ('H:i')
|
||||
* [7] = time medium ('H:i:s')
|
||||
* [8] = time long ('g:i:sA')
|
||||
* [9] = time full ('g:i:sA T')
|
||||
* [10] = datetime short ('y-m-d H:i')
|
||||
* [11] = datetime medium ('Y-m-d H:i:s')
|
||||
* [12] = datetime long ('F j, Y g:i:sA')
|
||||
* [13] = datetime full ('l, F j, Y g:i:sA T')
|
||||
* [14] = currency code
|
||||
*
|
||||
* @author Erik Ruedin <e.ruedin@guggach.com>
|
||||
* @version 0.1
|
||||
*/
|
||||
Class FormatDefs{
|
||||
|
||||
static function definition($local) {
|
||||
|
||||
$localDef = [
|
||||
'en-US' =>
|
||||
['.', ',', 'm/d/y', 'm/d/Y', 'F j, Y', 'l, F j, Y', 'H:i', 'H:i:s', 'g:i:sA', 'g:i:sA T', 'm/d/y H:i', 'm/d/Y H:i:s', 'F j, Y g:i:sA', 'l, F j, Y g:i:sA T', 'USD' ],
|
||||
'de-CH' =>
|
||||
['.', '\'', 'd.m.y', 'd.m.Y', 'j. F Y', 'l, j. F Y', 'H:i', 'H:i:s', 'G:i:s', 'G:i:s T', 'd.m.y H:i', 'd.m.Y H:i:s', 'F j, Y g:i:sA', 'l, F j, Y g:i:sA T', 'CHF' ],
|
||||
'de-DE' =>
|
||||
[',', '.', 'd.m.y', 'd.m.Y', 'j. F Y', 'l, j. F Y', 'H:i', 'H:i:s', 'G:i:s', 'G:i:s T', 'd.m.y H:i', 'd.m.Y H:i:s', 'F j, Y g:i:sA', 'l, F j, Y g:i:sA T', 'EUR' ],
|
||||
|
||||
];
|
||||
|
||||
if (isset($localDef[$local])){
|
||||
return $localDef[$local];
|
||||
} else{
|
||||
return [];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -1,338 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\i18n;
|
||||
|
||||
use Yii;
|
||||
use IntlDateFormatter;
|
||||
use NumberFormatter;
|
||||
use DateTime;
|
||||
use yii\base\InvalidConfigException;
|
||||
|
||||
/**
|
||||
* Formatter is the localized version of [[\yii\base\Formatter]].
|
||||
*
|
||||
* Formatter requires the PHP "intl" extension to be installed. Formatter supports localized
|
||||
* formatting of date, time and numbers, based on the current [[locale]].
|
||||
*
|
||||
* This Formatter can replace the `formatter` application component that is configured by default.
|
||||
* To do so, add the following to your application config under `components`:
|
||||
*
|
||||
* ```php
|
||||
* 'formatter' => [
|
||||
* 'class' => 'yii\i18n\Formatter',
|
||||
* ]
|
||||
* ```
|
||||
*
|
||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
class Formatter extends \yii\base\Formatter
|
||||
{
|
||||
/**
|
||||
* @var string the locale ID that is used to localize the date and number formatting.
|
||||
* If not set, [[\yii\base\Application::language]] will be used.
|
||||
*/
|
||||
public $locale;
|
||||
/**
|
||||
* @var string the default format string to be used to format a date.
|
||||
* This can be "short", "medium", "long", or "full", which represents a preset format of different lengths.
|
||||
* It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime).
|
||||
*/
|
||||
public $dateFormat = 'short';
|
||||
/**
|
||||
* @var string the default format string to be used to format a time.
|
||||
* This can be "short", "medium", "long", or "full", which represents a preset format of different lengths.
|
||||
* It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime).
|
||||
*/
|
||||
public $timeFormat = 'short';
|
||||
/**
|
||||
* @var string the default format string to be used to format a date and time.
|
||||
* This can be "short", "medium", "long", or "full", which represents a preset format of different lengths.
|
||||
* It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime).
|
||||
*/
|
||||
public $datetimeFormat = 'short';
|
||||
/**
|
||||
* @var array the options to be set for the NumberFormatter objects. Please refer to
|
||||
* [PHP manual](http://php.net/manual/en/class.numberformatter.php#intl.numberformatter-constants.unumberformatattribute)
|
||||
* for the possible options. This property is used by [[createNumberFormatter]] when
|
||||
* creating a new number formatter to format decimals, currencies, etc.
|
||||
*/
|
||||
public $numberFormatOptions = [];
|
||||
/**
|
||||
* @var string the character displayed as the decimal point when formatting a number.
|
||||
* If not set, the decimal separator corresponding to [[locale]] will be used.
|
||||
*/
|
||||
public $decimalSeparator;
|
||||
/**
|
||||
* @var string the character displayed as the thousands separator character when formatting a number.
|
||||
* If not set, the thousand separator corresponding to [[locale]] will be used.
|
||||
*/
|
||||
public $thousandSeparator;
|
||||
/**
|
||||
* @var string the international currency code displayed when formatting a number.
|
||||
* If not set, the currency code corresponding to [[locale]] will be used.
|
||||
*/
|
||||
public $currencyCode;
|
||||
|
||||
/**
|
||||
* Initializes the component.
|
||||
* This method will check if the "intl" PHP extension is installed and set the
|
||||
* default value of [[locale]].
|
||||
* @throws InvalidConfigException if the "intl" PHP extension is not installed.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
if (!extension_loaded('intl')) {
|
||||
throw new InvalidConfigException('The "intl" PHP extension is not installed. It is required to format data values in localized formats.');
|
||||
}
|
||||
if ($this->locale === null) {
|
||||
$this->locale = Yii::$app->language;
|
||||
}
|
||||
if ($this->decimalSeparator === null || $this->thousandSeparator === null || $this->currencyCode === null) {
|
||||
$formatter = new NumberFormatter($this->locale, NumberFormatter::DECIMAL);
|
||||
if ($this->decimalSeparator === null) {
|
||||
$this->decimalSeparator = $formatter->getSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
|
||||
}
|
||||
if ($this->thousandSeparator === null) {
|
||||
$this->thousandSeparator = $formatter->getSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL);
|
||||
}
|
||||
if ($this->currencyCode === null) {
|
||||
$this->currencyCode = $formatter->getSymbol(NumberFormatter::INTL_CURRENCY_SYMBOL);
|
||||
}
|
||||
}
|
||||
|
||||
parent::init();
|
||||
}
|
||||
|
||||
private $_dateFormats = [
|
||||
'short' => IntlDateFormatter::SHORT,
|
||||
'medium' => IntlDateFormatter::MEDIUM,
|
||||
'long' => IntlDateFormatter::LONG,
|
||||
'full' => IntlDateFormatter::FULL,
|
||||
];
|
||||
|
||||
/**
|
||||
* Formats the value as a date.
|
||||
* @param integer|string|DateTime $value the value to be formatted. The following
|
||||
* types of value are supported:
|
||||
*
|
||||
* - an integer representing a UNIX timestamp
|
||||
* - a string that can be parsed into a UNIX timestamp via `strtotime()`
|
||||
* - a PHP DateTime object
|
||||
*
|
||||
* @param string $format the format used to convert the value into a date string.
|
||||
* If null, [[dateFormat]] will be used.
|
||||
*
|
||||
* This can be "short", "medium", "long", or "full", which represents a preset format of different lengths.
|
||||
* It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime).
|
||||
*
|
||||
* @return string the formatted result
|
||||
* @throws InvalidConfigException when formatting fails due to invalid parameters.
|
||||
* @see dateFormat
|
||||
*/
|
||||
public function asDate($value, $format = null)
|
||||
{
|
||||
if ($value === null) {
|
||||
return $this->nullDisplay;
|
||||
}
|
||||
$value = $this->normalizeDatetimeValue($value);
|
||||
if ($format === null) {
|
||||
$format = $this->dateFormat;
|
||||
}
|
||||
if (isset($this->_dateFormats[$format])) {
|
||||
$formatter = new IntlDateFormatter($this->locale, $this->_dateFormats[$format], IntlDateFormatter::NONE, $this->timeZone);
|
||||
} else {
|
||||
$formatter = new IntlDateFormatter($this->locale, IntlDateFormatter::NONE, IntlDateFormatter::NONE, $this->timeZone);
|
||||
if ($formatter !== null) {
|
||||
$formatter->setPattern($format);
|
||||
}
|
||||
}
|
||||
if ($formatter === null) {
|
||||
throw new InvalidConfigException(intl_get_error_message());
|
||||
}
|
||||
|
||||
return $formatter->format($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the value as a time.
|
||||
* @param integer|string|DateTime $value the value to be formatted. The following
|
||||
* types of value are supported:
|
||||
*
|
||||
* - an integer representing a UNIX timestamp
|
||||
* - a string that can be parsed into a UNIX timestamp via `strtotime()`
|
||||
* - a PHP DateTime object
|
||||
*
|
||||
* @param string $format the format used to convert the value into a date string.
|
||||
* If null, [[dateFormat]] will be used.
|
||||
*
|
||||
* This can be "short", "medium", "long", or "full", which represents a preset format of different lengths.
|
||||
* It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime).
|
||||
*
|
||||
* @return string the formatted result
|
||||
* @throws InvalidConfigException when formatting fails due to invalid parameters.
|
||||
* @see timeFormat
|
||||
*/
|
||||
public function asTime($value, $format = null)
|
||||
{
|
||||
if ($value === null) {
|
||||
return $this->nullDisplay;
|
||||
}
|
||||
$value = $this->normalizeDatetimeValue($value);
|
||||
if ($format === null) {
|
||||
$format = $this->timeFormat;
|
||||
}
|
||||
if (isset($this->_dateFormats[$format])) {
|
||||
$formatter = new IntlDateFormatter($this->locale, IntlDateFormatter::NONE, $this->_dateFormats[$format], $this->timeZone);
|
||||
} else {
|
||||
$formatter = new IntlDateFormatter($this->locale, IntlDateFormatter::NONE, IntlDateFormatter::NONE, $this->timeZone);
|
||||
if ($formatter !== null) {
|
||||
$formatter->setPattern($format);
|
||||
}
|
||||
}
|
||||
if ($formatter === null) {
|
||||
throw new InvalidConfigException(intl_get_error_message());
|
||||
}
|
||||
|
||||
return $formatter->format($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the value as a datetime.
|
||||
* @param integer|string|DateTime $value the value to be formatted. The following
|
||||
* types of value are supported:
|
||||
*
|
||||
* - an integer representing a UNIX timestamp
|
||||
* - a string that can be parsed into a UNIX timestamp via `strtotime()`
|
||||
* - a PHP DateTime object
|
||||
*
|
||||
* @param string $format the format used to convert the value into a date string.
|
||||
* If null, [[dateFormat]] will be used.
|
||||
*
|
||||
* This can be "short", "medium", "long", or "full", which represents a preset format of different lengths.
|
||||
* It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime).
|
||||
*
|
||||
* @return string the formatted result
|
||||
* @throws InvalidConfigException when formatting fails due to invalid parameters.
|
||||
* @see datetimeFormat
|
||||
*/
|
||||
public function asDatetime($value, $format = null)
|
||||
{
|
||||
if ($value === null) {
|
||||
return $this->nullDisplay;
|
||||
}
|
||||
$value = $this->normalizeDatetimeValue($value);
|
||||
if ($format === null) {
|
||||
$format = $this->datetimeFormat;
|
||||
}
|
||||
if (isset($this->_dateFormats[$format])) {
|
||||
$formatter = new IntlDateFormatter($this->locale, $this->_dateFormats[$format], $this->_dateFormats[$format], $this->timeZone);
|
||||
} else {
|
||||
$formatter = new IntlDateFormatter($this->locale, IntlDateFormatter::NONE, IntlDateFormatter::NONE, $this->timeZone);
|
||||
if ($formatter !== null) {
|
||||
$formatter->setPattern($format);
|
||||
}
|
||||
}
|
||||
if ($formatter === null) {
|
||||
throw new InvalidConfigException(intl_get_error_message());
|
||||
}
|
||||
|
||||
return $formatter->format($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the value as a decimal number.
|
||||
* @param mixed $value the value to be formatted
|
||||
* @param string $format the format to be used. Please refer to [ICU manual](http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#_details)
|
||||
* for details on how to specify a format.
|
||||
* @return string the formatted result.
|
||||
*/
|
||||
public function asDecimal($value, $format = null)
|
||||
{
|
||||
if ($value === null) {
|
||||
return $this->nullDisplay;
|
||||
}
|
||||
|
||||
return $this->createNumberFormatter(NumberFormatter::DECIMAL, $format)->format($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the value as a currency number.
|
||||
* @param mixed $value the value to be formatted
|
||||
* @param string $currency the 3-letter ISO 4217 currency code indicating the currency to use.
|
||||
* If null, [[currencyCode]] will be used.
|
||||
* @param string $format the format to be used. Please refer to [ICU manual](http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#_details)
|
||||
* for details on how to specify a format.
|
||||
* @return string the formatted result.
|
||||
*/
|
||||
public function asCurrency($value, $currency = null, $format = null)
|
||||
{
|
||||
if ($value === null) {
|
||||
return $this->nullDisplay;
|
||||
}
|
||||
|
||||
if ($currency === null){
|
||||
$currency = $this->currencyCode;
|
||||
}
|
||||
|
||||
return $this->createNumberFormatter(NumberFormatter::CURRENCY, $format)->formatCurrency($value, $currency);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the value as a percent number.
|
||||
* @param mixed $value the value to be formatted
|
||||
* @param string $format the format to be used. Please refer to [ICU manual](http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#_details)
|
||||
* for details on how to specify a format.
|
||||
* @return string the formatted result.
|
||||
*/
|
||||
public function asPercent($value, $format = null)
|
||||
{
|
||||
if ($value === null) {
|
||||
return $this->nullDisplay;
|
||||
}
|
||||
|
||||
return $this->createNumberFormatter(NumberFormatter::PERCENT, $format)->format($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the value as a scientific number.
|
||||
* @param mixed $value the value to be formatted
|
||||
* @param string $format the format to be used. Please refer to [ICU manual](http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#_details)
|
||||
* for details on how to specify a format.
|
||||
* @return string the formatted result.
|
||||
*/
|
||||
public function asScientific($value, $format = null)
|
||||
{
|
||||
if ($value === null) {
|
||||
return $this->nullDisplay;
|
||||
}
|
||||
|
||||
return $this->createNumberFormatter(NumberFormatter::SCIENTIFIC, $format)->format($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a number formatter based on the given type and format.
|
||||
* @param integer $type the type of the number formatter
|
||||
* @param string $format the format to be used. Please refer to [ICU manual](http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#_details)
|
||||
* @return NumberFormatter the created formatter instance
|
||||
*/
|
||||
protected function createNumberFormatter($type, $format)
|
||||
{
|
||||
$formatter = new NumberFormatter($this->locale, $type);
|
||||
if ($format !== null) {
|
||||
$formatter->setPattern($format);
|
||||
}
|
||||
if (!empty($this->numberFormatOptions)) {
|
||||
foreach ($this->numberFormatOptions as $name => $attribute) {
|
||||
$formatter->setAttribute($name, $attribute);
|
||||
}
|
||||
}
|
||||
|
||||
return $formatter;
|
||||
}
|
||||
}
|
||||
@ -121,23 +121,26 @@ class FormatterTest extends TestCase
|
||||
public function testAsDate()
|
||||
{
|
||||
$value = time();
|
||||
$this->assertSame(date('Y-m-d', $value), $this->formatter->asDate($value));
|
||||
// $this->assertSame(date('M j, Y', $value), $this->formatter->asDate($value));
|
||||
// test fails for "en-US" because travis has another version of ICU = other format
|
||||
$this->assertSame(date('Y/m/d', $value), $this->formatter->asDate($value, 'Y/m/d'));
|
||||
$this->assertSame(date('n/j/y', $value), $this->formatter->asDate($value, 'short'));
|
||||
$this->assertSame(date('F j, Y', $value), $this->formatter->asDate($value, 'long'));
|
||||
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDate(null));
|
||||
}
|
||||
|
||||
public function testAsTime()
|
||||
{
|
||||
$value = time();
|
||||
$this->assertSame(date('H:i:s', $value), $this->formatter->asTime($value));
|
||||
$this->assertSame(date('h:i:s A', $value), $this->formatter->asTime($value, 'h:i:s A'));
|
||||
$this->assertSame(date('g:i:s A', $value), $this->formatter->asTime($value));
|
||||
$this->assertSame(date('n:i:s A', $value), $this->formatter->asTime($value, 'n:i:s A'));
|
||||
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asTime(null));
|
||||
}
|
||||
|
||||
public function testAsDatetime()
|
||||
{
|
||||
$value = time();
|
||||
$this->assertSame(date('Y-m-d H:i:s', $value), $this->formatter->asDatetime($value));
|
||||
$this->assertSame(date('M j, Y g:i:s A', $value), $this->formatter->asDatetime($value));
|
||||
$this->assertSame(date('Y/m/d h:i:s A', $value), $this->formatter->asDatetime($value, 'Y/m/d h:i:s A'));
|
||||
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDatetime(null));
|
||||
}
|
||||
@ -166,12 +169,14 @@ class FormatterTest extends TestCase
|
||||
$value = 123;
|
||||
$this->assertSame("123.00", $this->formatter->asDouble($value));
|
||||
$this->formatter->decimalSeparator = ',';
|
||||
$this->formatter->thousandSeparator = '.';
|
||||
$value = 123.12;
|
||||
$this->assertSame("123,12", $this->formatter->asDouble($value));
|
||||
$this->assertSame("123,1", $this->formatter->asDouble($value, 1));
|
||||
$this->assertSame("123", $this->formatter->asDouble($value, 0));
|
||||
$value = 123123.123;
|
||||
$this->assertSame("123123,12", $this->formatter->asDouble($value));
|
||||
$this->assertSame("123.123,12", $this->formatter->asDouble($value));
|
||||
$this->assertSame("123123,12", $this->formatter->asDouble($value, 2, null, false));
|
||||
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDouble(null));
|
||||
}
|
||||
|
||||
@ -190,11 +195,57 @@ class FormatterTest extends TestCase
|
||||
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asNumber(null));
|
||||
}
|
||||
|
||||
public function testAsDecimal()
|
||||
{
|
||||
$value = '123';
|
||||
$this->assertSame($value, $this->formatter->asDecimal($value));
|
||||
$value = '123456';
|
||||
$this->assertSame("123,456", $this->formatter->asDecimal($value));
|
||||
$value = '-123456.123';
|
||||
$this->assertSame("-123,456.123", $this->formatter->asDecimal($value));
|
||||
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDecimal(null));
|
||||
}
|
||||
|
||||
public function testAsCurrency()
|
||||
{
|
||||
$value = '123';
|
||||
$this->assertSame('$123.00', $this->formatter->asCurrency($value));
|
||||
$value = '123.456';
|
||||
$this->assertSame("$123.46", $this->formatter->asCurrency($value));
|
||||
// Starting from ICU 52.1, negative currency value will be formatted as -$123,456.12
|
||||
// see: http://source.icu-project.org/repos/icu/icu/tags/release-52-1/source/data/locales/en.txt
|
||||
// $value = '-123456.123';
|
||||
// $this->assertSame("($123,456.12)", $this->formatter->asCurrency($value));
|
||||
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asCurrency(null));
|
||||
}
|
||||
|
||||
public function testAsScientific()
|
||||
{
|
||||
$value = '123';
|
||||
$this->assertSame('1.23E2', $this->formatter->asScientific($value));
|
||||
$value = '123456';
|
||||
$this->assertSame("1.23456E5", $this->formatter->asScientific($value));
|
||||
$value = '-123456.123';
|
||||
$this->assertSame("-1.23456123E5", $this->formatter->asScientific($value));
|
||||
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asScientific(null));
|
||||
}
|
||||
|
||||
public function testAsPercent()
|
||||
{
|
||||
$value = '123';
|
||||
$this->assertSame('12,300%', $this->formatter->asPercent($value));
|
||||
$value = '0.1234';
|
||||
$this->assertSame("12%", $this->formatter->asPercent($value));
|
||||
$value = '-0.009343';
|
||||
$this->assertSame("-1%", $this->formatter->asPercent($value));
|
||||
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asPercent(null));
|
||||
}
|
||||
|
||||
public function testFormat()
|
||||
{
|
||||
$value = time();
|
||||
$this->assertSame(date('Y-m-d', $value), $this->formatter->format($value, 'date'));
|
||||
$this->assertSame(date('Y-m-d', $value), $this->formatter->format($value, 'DATE'));
|
||||
$this->assertSame(date('M j, Y', $value), $this->formatter->format($value, 'date'));
|
||||
$this->assertSame(date('M j, Y', $value), $this->formatter->format($value, 'DATE'));
|
||||
$this->assertSame(date('Y/m/d', $value), $this->formatter->format($value, ['date', 'Y/m/d']));
|
||||
$this->setExpectedException('\yii\base\InvalidParamException');
|
||||
$this->assertSame(date('Y-m-d', $value), $this->formatter->format($value, 'data'));
|
||||
@ -238,12 +289,12 @@ class FormatterTest extends TestCase
|
||||
$this->assertSame('a year ago', $this->formatter->asRelativeTime($interval_1_year));
|
||||
$this->assertSame('12 years ago', $this->formatter->asRelativeTime($interval_12_years));
|
||||
|
||||
// Pass a DateInterval string
|
||||
$this->assertSame('a year ago', $this->formatter->asRelativeTime('2007-03-01T13:00:00Z/2008-05-11T15:30:00Z'));
|
||||
$this->assertSame('a year ago', $this->formatter->asRelativeTime('2007-03-01T13:00:00Z/P1Y2M10DT2H30M'));
|
||||
$this->assertSame('a year ago', $this->formatter->asRelativeTime('P1Y2M10DT2H30M/2008-05-11T15:30:00Z'));
|
||||
$this->assertSame('a year ago', $this->formatter->asRelativeTime('P1Y2M10DT2H30M'));
|
||||
$this->assertSame('94 months ago', $this->formatter->asRelativeTime('P94M'));
|
||||
// Pass a DateInterval string -> isn't possible
|
||||
// $this->assertSame('a year ago', $this->formatter->asRelativeTime('2007-03-01T13:00:00Z/2008-05-11T15:30:00Z'));
|
||||
// $this->assertSame('a year ago', $this->formatter->asRelativeTime('2007-03-01T13:00:00Z/P1Y2M10DT2H30M'));
|
||||
// $this->assertSame('a year ago', $this->formatter->asRelativeTime('P1Y2M10DT2H30M/2008-05-11T15:30:00Z'));
|
||||
// $this->assertSame('a year ago', $this->formatter->asRelativeTime('P1Y2M10DT2H30M'));
|
||||
// $this->assertSame('94 months ago', $this->formatter->asRelativeTime('P94M'));
|
||||
|
||||
// Force the reference time and pass a past DateTime
|
||||
$dateNow = new DateTime('2014-03-13');
|
||||
@ -298,7 +349,7 @@ class FormatterTest extends TestCase
|
||||
$this->assertSame('in 12 years', $this->formatter->asRelativeTime($interval_12_years));
|
||||
|
||||
// Pass a inverted DateInterval string
|
||||
$this->assertSame('in a year', $this->formatter->asRelativeTime('2008-05-11T15:30:00Z/2007-03-01T13:00:00Z'));
|
||||
// $this->assertSame('in a year', $this->formatter->asRelativeTime('2008-05-11T15:30:00Z/2007-03-01T13:00:00Z'));
|
||||
|
||||
// Force the reference time and pass a future DateTime
|
||||
$dateNow = new DateTime('2014-03-13');
|
||||
@ -322,4 +373,14 @@ class FormatterTest extends TestCase
|
||||
$this->assertSame('in a month', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-03', [$interval_1_month]), $dateNow));
|
||||
$this->assertSame('in 28 days', $this->formatter->asRelativeTime($dateThen, $dateNow));
|
||||
}
|
||||
|
||||
public function testSetLocale(){
|
||||
$value = '12300';
|
||||
$this->formatter->setLocale('de-DE');
|
||||
$this->assertSame('12.300,00', $this->formatter->asDouble($value, 2));
|
||||
$value = time();
|
||||
$this->assertSame(date('d.m.Y', $value), $this->formatter->asDate($value));
|
||||
$this->formatter->setLocale('en-US');
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,96 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yiiunit\framework\i18n;
|
||||
|
||||
use yii\i18n\Formatter;
|
||||
use yiiunit\TestCase;
|
||||
|
||||
/**
|
||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||
* @since 2.0
|
||||
* @group i18n
|
||||
*/
|
||||
class FormatterTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Formatter
|
||||
*/
|
||||
protected $formatter;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
if (!extension_loaded('intl')) {
|
||||
$this->markTestSkipped('intl extension is required.');
|
||||
}
|
||||
$this->mockApplication([
|
||||
'timeZone' => 'UTC',
|
||||
]);
|
||||
$this->formatter = new Formatter(['locale' => 'en-US']);
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
parent::tearDown();
|
||||
$this->formatter = null;
|
||||
}
|
||||
|
||||
public function testAsDecimal()
|
||||
{
|
||||
$value = '123';
|
||||
$this->assertSame($value, $this->formatter->asDecimal($value));
|
||||
$value = '123456';
|
||||
$this->assertSame("123,456", $this->formatter->asDecimal($value));
|
||||
$value = '-123456.123';
|
||||
$this->assertSame("-123,456.123", $this->formatter->asDecimal($value));
|
||||
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDecimal(null));
|
||||
}
|
||||
|
||||
public function testAsPercent()
|
||||
{
|
||||
$value = '123';
|
||||
$this->assertSame('12,300%', $this->formatter->asPercent($value));
|
||||
$value = '0.1234';
|
||||
$this->assertSame("12%", $this->formatter->asPercent($value));
|
||||
$value = '-0.009343';
|
||||
$this->assertSame("-1%", $this->formatter->asPercent($value));
|
||||
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asPercent(null));
|
||||
}
|
||||
|
||||
public function testAsScientific()
|
||||
{
|
||||
$value = '123';
|
||||
$this->assertSame('1.23E2', $this->formatter->asScientific($value));
|
||||
$value = '123456';
|
||||
$this->assertSame("1.23456E5", $this->formatter->asScientific($value));
|
||||
$value = '-123456.123';
|
||||
$this->assertSame("-1.23456123E5", $this->formatter->asScientific($value));
|
||||
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asScientific(null));
|
||||
}
|
||||
|
||||
public function testAsCurrency()
|
||||
{
|
||||
$value = '123';
|
||||
$this->assertSame('$123.00', $this->formatter->asCurrency($value));
|
||||
$value = '123.456';
|
||||
$this->assertSame("$123.46", $this->formatter->asCurrency($value));
|
||||
// Starting from ICU 52.1, negative currency value will be formatted as -$123,456.12
|
||||
// see: http://source.icu-project.org/repos/icu/icu/tags/release-52-1/source/data/locales/en.txt
|
||||
// $value = '-123456.123';
|
||||
// $this->assertSame("($123,456.12)", $this->formatter->asCurrency($value));
|
||||
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asCurrency(null));
|
||||
}
|
||||
|
||||
public function testDate()
|
||||
{
|
||||
$time = time();
|
||||
$this->assertSame(date('n/j/y', $time), $this->formatter->asDate($time));
|
||||
$this->assertSame(date('F j, Y', $time), $this->formatter->asDate($time, 'long'));
|
||||
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDate(null));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user