Browse Source

Finished Formatter.

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
9a97037012
  1. 12
      framework/yii/base/Formatter.php
  2. 8
      framework/yii/i18n/Formatter.php
  3. 59
      tests/unit/framework/base/FormatterTest.php
  4. 93
      tests/unit/framework/i18n/FormatterTest.php

12
framework/yii/base/Formatter.php

@ -38,13 +38,6 @@ class Formatter extends Component
*/ */
public $datetimeFormat = 'Y/m/d h:i:s A'; 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 * @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 <code>array('No', 'Yes')</code>. * to the text display for false, the second element for true. Defaults to <code>array('No', 'Yes')</code>.
*/ */
@ -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 mixed $value the value to be formatted
* @param integer $decimals the number of digits after the decimal point * @param integer $decimals the number of digits after the decimal point
* @param string $decimalSeparator the character displayed as the decimal point * @param string $decimalSeparator the character displayed as the decimal point
* @param string $thousandSeparator the character displayed as the thousands separator character. * @param string $thousandSeparator the character displayed as the thousands separator character.
* @return string the formatted result * @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); return number_format($value, $decimals, $decimalSeparator, $thousandSeparator);
} }

8
framework/yii/i18n/Formatter.php

@ -170,7 +170,7 @@ class Formatter extends \yii\base\Formatter
*/ */
public function asDecimal($value, $format = null) 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) 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) 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) public function asScientific($value, $format = null)
{ {
$this->createNumberFormatter(NumberFormatter::SCIENTIFIC, $format)->format($value); return $this->createNumberFormatter(NumberFormatter::SCIENTIFIC, $format)->format($value);
} }
/** /**

59
tests/unit/framework/base/FormatterTest.php

@ -84,7 +84,7 @@ class FormatterTest extends TestCase
public function testAsHtml() public function testAsHtml()
{ {
// todo // todo: dependency on HtmlPurifier
} }
public function testAsEmail() public function testAsEmail()
@ -98,4 +98,61 @@ class FormatterTest extends TestCase
$value = 'http://sample.com/img.jpg'; $value = 'http://sample.com/img.jpg';
$this->assertSame("<img src=\"$value\" alt=\"\" />", $this->formatter->asImage($value)); $this->assertSame("<img src=\"$value\" alt=\"\" />", $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, ',', '.'));
}
} }

93
tests/unit/framework/i18n/FormatterTest.php

@ -0,0 +1,93 @@
<?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
*/
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'));
}
}
Loading…
Cancel
Save