diff --git a/framework/yii/base/Formatter.php b/framework/yii/base/Formatter.php
index a31fac9..835b657 100644
--- a/framework/yii/base/Formatter.php
+++ b/framework/yii/base/Formatter.php
@@ -38,13 +38,6 @@ class Formatter extends Component
*/
public $datetimeFormat = 'Y/m/d h:i:s A';
/**
- * @var array the format used to format a number with PHP number_format() function.
- * Three elements may be specified: "decimals", "decimalSeparator" and "thousandSeparator".
- * They correspond to the number of digits after the decimal point, the character displayed as the decimal point
- * and the thousands separator character.
- */
- public $numberFormat = array('decimals' => null, 'decimalSeparator' => null, 'thousandSeparator' => null);
- /**
* @var array the text to be displayed when formatting a boolean value. The first element corresponds
* to the text display for false, the second element for true. Defaults to array('No', 'Yes')
.
*/
@@ -274,14 +267,15 @@ class Formatter extends Component
}
/**
- * Formats the value as a decimal number using the PHP number_format() function.
+ * Formats the value as a number with decimal and thousand separators.
+ * This method calls the PHP number_format() function to do the formatting.
* @param mixed $value the value to be formatted
* @param integer $decimals the number of digits after the decimal point
* @param string $decimalSeparator the character displayed as the decimal point
* @param string $thousandSeparator the character displayed as the thousands separator character.
* @return string the formatted result
*/
- public function asDecimal($value, $decimals = 0 , $decimalSeparator = '.' , $thousandSeparator = ',' )
+ public function asNumber($value, $decimals = 0 , $decimalSeparator = '.' , $thousandSeparator = ',' )
{
return number_format($value, $decimals, $decimalSeparator, $thousandSeparator);
}
diff --git a/framework/yii/i18n/Formatter.php b/framework/yii/i18n/Formatter.php
index a0570a0..a90f5c9 100644
--- a/framework/yii/i18n/Formatter.php
+++ b/framework/yii/i18n/Formatter.php
@@ -170,7 +170,7 @@ class Formatter extends \yii\base\Formatter
*/
public function asDecimal($value, $format = null)
{
- $this->createNumberFormatter(NumberFormatter::DECIMAL, $format)->format($value);
+ return $this->createNumberFormatter(NumberFormatter::DECIMAL, $format)->format($value);
}
/**
@@ -183,7 +183,7 @@ class Formatter extends \yii\base\Formatter
*/
public function asCurrency($value, $currency = 'USD', $format = null)
{
- $this->createNumberFormatter(NumberFormatter::CURRENCY, $format)->formatCurrency($value, $currency);
+ return $this->createNumberFormatter(NumberFormatter::CURRENCY, $format)->formatCurrency($value, $currency);
}
/**
@@ -195,7 +195,7 @@ class Formatter extends \yii\base\Formatter
*/
public function asPercent($value, $format = null)
{
- $this->createNumberFormatter(NumberFormatter::PERCENT, $format)->format($value);
+ return $this->createNumberFormatter(NumberFormatter::PERCENT, $format)->format($value);
}
/**
@@ -207,7 +207,7 @@ class Formatter extends \yii\base\Formatter
*/
public function asScientific($value, $format = null)
{
- $this->createNumberFormatter(NumberFormatter::SCIENTIFIC, $format)->format($value);
+ return $this->createNumberFormatter(NumberFormatter::SCIENTIFIC, $format)->format($value);
}
/**
diff --git a/tests/unit/framework/base/FormatterTest.php b/tests/unit/framework/base/FormatterTest.php
index 53d8770..3e7ac5f 100644
--- a/tests/unit/framework/base/FormatterTest.php
+++ b/tests/unit/framework/base/FormatterTest.php
@@ -84,7 +84,7 @@ class FormatterTest extends TestCase
public function testAsHtml()
{
- // todo
+ // todo: dependency on HtmlPurifier
}
public function testAsEmail()
@@ -98,4 +98,61 @@ class FormatterTest extends TestCase
$value = 'http://sample.com/img.jpg';
$this->assertSame("
", $this->formatter->asImage($value));
}
+
+ public function testAsBoolean()
+ {
+ $value = true;
+ $this->assertSame('Yes', $this->formatter->asBoolean($value));
+ $value = false;
+ $this->assertSame('No', $this->formatter->asBoolean($value));
+ $value = "111";
+ $this->assertSame('Yes', $this->formatter->asBoolean($value));
+ $value = "";
+ $this->assertSame('No', $this->formatter->asBoolean($value));
+ }
+
+ public function testAsDate()
+ {
+ $value = time();
+ $this->assertSame(date('Y/m/d', $value), $this->formatter->asDate($value));
+ $this->assertSame(date('Y-m-d', $value), $this->formatter->asDate($value, 'Y-m-d'));
+ }
+
+ public function testAsTime()
+ {
+ $value = time();
+ $this->assertSame(date('h:i:s A', $value), $this->formatter->asTime($value));
+ $this->assertSame(date('h:i:s', $value), $this->formatter->asTime($value, 'h:i:s'));
+ }
+
+ public function testAsDatetime()
+ {
+ $value = time();
+ $this->assertSame(date('Y/m/d h:i:s A', $value), $this->formatter->asDatetime($value));
+ $this->assertSame(date('Y-m-d h:i:s', $value), $this->formatter->asDatetime($value, 'Y-m-d h:i:s'));
+ }
+
+ public function testAsInteger()
+ {
+ $value = 123;
+ $this->assertSame("$value", $this->formatter->asInteger($value));
+ $value = 123.23;
+ $this->assertSame("123", $this->formatter->asInteger($value));
+ $value = 'a';
+ $this->assertSame("0", $this->formatter->asInteger($value));
+ }
+
+ public function testAsDouble()
+ {
+ $value = 123.12;
+ $this->assertSame("123.12", $this->formatter->asDouble($value));
+ $this->assertSame("123.1", $this->formatter->asDouble($value, 1));
+ }
+
+ public function testAsNumber()
+ {
+ $value = 123123.123;
+ $this->assertSame("123,123", $this->formatter->asNumber($value));
+ $this->assertSame("123.123,12", $this->formatter->asNumber($value, 2, ',', '.'));
+ }
}
diff --git a/tests/unit/framework/i18n/FormatterTest.php b/tests/unit/framework/i18n/FormatterTest.php
new file mode 100644
index 0000000..cf479d1
--- /dev/null
+++ b/tests/unit/framework/i18n/FormatterTest.php
@@ -0,0 +1,93 @@
+
+ * @since 2.0
+ */
+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();
+ $this->formatter = new Formatter(array(
+ '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));
+ }
+
+ 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));
+ }
+
+ 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));
+ }
+
+ public function testAsCurrency()
+ {
+ $value = '123';
+ $this->assertSame('$123.00', $this->formatter->asCurrency($value));
+ $value = '123.456';
+ $this->assertSame("$123.46", $this->formatter->asCurrency($value));
+ $value = '-123456.123';
+ $this->assertSame("($123,456.12)", $this->formatter->asCurrency($value));
+ }
+
+ public function testDate()
+ {
+ $time = time();
+ $this->assertSame(date('n/j/y', $time), $this->formatter->asDate($time));
+ $this->assertSame(date('g:i A', $time), $this->formatter->asTime($time));
+ $this->assertSame(date('n/j/y g:i A', $time), $this->formatter->asDatetime($time));
+
+ $this->assertSame(date('M j, Y', $time), $this->formatter->asDate($time, 'long'));
+ $this->assertSame(date('g:i:s A T', $time), $this->formatter->asTime($time, 'long'));
+ $this->assertSame(date('M j, Y g:i:s A T', $time), $this->formatter->asDatetime($time, 'long'));
+ }
+}