Commit 330b8c25 by Carsten Brandt

Merge branch '2359-formatter-refactored' of https://github.com/Erik-r/yii2-1…

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.
parents bae9e348 a8ad3b83
......@@ -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)
......
<?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 [];
}
}
}
......@@ -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');
}
}
<?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));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment