yiisoft/yii2 — pull request #3790 by Erik-r (2014-06-11 07:53:31+00:00)
Title: Refactored formatter class #2359

Formatter class in 'yii\base\formatter.php' has been rewritten. Formatter class in 'yii\i18n' is a dummy only for compatibility reason. In user guide formatter.md describes the usage and all functions.

Main change is a merge between yii\base\formatter and yii\i18n\formatter plus some further functions like unformat and format pattern conversion.

The motivation to refact formatter was the confusion with the format patterns and the behaviour between version of 'yii\base' and 'yii\i18n'. While base works with PHP syntax i18n version does accept ICU patterns from intl only. Further only i18n version supports terms like 'short', 'medium', 'long' or 'full'.

Most application should be able to react on locale settings (base is OK for US application only). If a developper creates an international application he has to convert between PHP format patterns und ICU format patterns and versa. I found 3rd party extension already (Kartik-V dataControl) which uses 'yii::$app->formatter->dateFormat'. The extension has no knowledge which class is used. Either it gets php pattern like 'd-m-Y' back or it gets ICU short term 'short' or 'dd-MM-yyyy' back. It's obvious that in case of i18n the extension will fail.

My refactored formatter class has following advantages:

* Class works with our without installed "intl" php extension with some restrictions. (no translated name of month or days without intl).
* Interface communication works per default with PHP format patterns (also if intl is loaded).
* Named date formats like 'short', 'medium', 'long' or 'full' is possible without intl also.
* ICU format patterns can be used as parameter also if defined in another parameter.
* Invalid Input date like '2014-07-35' doesn't crash anymore. It formats like '2014-08-04'. (yii\base\formatter uses DateTime constructor which crashes with a fatal error which can't be catched).
* Dates larger than 2038 are supported correctly.
* asDecimal, asNumber, asDouble are synonyms and have same behaviour. Floats are rounded correctly.
* For numbers and currency a rounding rule (roundingIncrement) can be set. (For Swiss currency 5 cents rounding is already defined by default).
* "Unformat" function which reverse almost all formats. (dates, integer, decimals, perCent, Size etc.)
* Conversion functions of date patterns from PHP to ICU or ICU to PHP
* SetLocale function (chainable) which sets another locale setting (changes internally all patterns).
* Formatter.md has been written.