Browse Source

Added Formatter::nullDisplay.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
3eccd850fe
  1. 52
      framework/yii/base/Formatter.php
  2. 21
      framework/yii/i18n/Formatter.php
  3. 13
      tests/unit/framework/base/FormatterTest.php
  4. 5
      tests/unit/framework/i18n/FormatterTest.php

52
framework/yii/base/Formatter.php

@ -37,6 +37,10 @@ class Formatter extends Component
*/
public $datetimeFormat = 'Y/m/d h:i:s A';
/**
* @var string the text to be displayed when formatting a null. Defaults to '(not set)'.
*/
public $nullDisplay;
/**
* @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')`.
*/
@ -61,6 +65,9 @@ class Formatter extends Component
if (empty($this->booleanFormat)) {
$this->booleanFormat = array(Yii::t('yii', 'No'), Yii::t('yii', 'Yes'));
}
if ($this->nullDisplay === null) {
$this->nullDisplay = Yii::t('yii', '(not set)');
}
}
/**
@ -71,6 +78,9 @@ class Formatter extends Component
*/
public function asRaw($value)
{
if ($value === null) {
return $this->nullDisplay;
}
return $value;
}
@ -81,6 +91,9 @@ class Formatter extends Component
*/
public function asText($value)
{
if ($value === null) {
return $this->nullDisplay;
}
return Html::encode($value);
}
@ -91,6 +104,9 @@ class Formatter extends Component
*/
public function asNtext($value)
{
if ($value === null) {
return $this->nullDisplay;
}
return nl2br(Html::encode($value));
}
@ -103,6 +119,9 @@ class Formatter extends Component
*/
public function asParagraphs($value)
{
if ($value === null) {
return $this->nullDisplay;
}
return str_replace('<p></p>', '',
'<p>' . preg_replace('/[\r\n]{2,}/', "</p>\n<p>", Html::encode($value)) . '</p>'
);
@ -118,6 +137,9 @@ class Formatter extends Component
*/
public function asHtml($value, $config = null)
{
if ($value === null) {
return $this->nullDisplay;
}
return HtmlPurifier::process($value, $config);
}
@ -128,6 +150,9 @@ class Formatter extends Component
*/
public function asEmail($value)
{
if ($value === null) {
return $this->nullDisplay;
}
return Html::mailto($value);
}
@ -138,6 +163,9 @@ class Formatter extends Component
*/
public function asImage($value)
{
if ($value === null) {
return $this->nullDisplay;
}
return Html::img($value);
}
@ -148,6 +176,9 @@ class Formatter extends Component
*/
public function asUrl($value)
{
if ($value === null) {
return $this->nullDisplay;
}
$url = $value;
if (strpos($url, 'http://') !== 0 && strpos($url, 'https://') !== 0) {
$url = 'http://' . $url;
@ -163,6 +194,9 @@ class Formatter extends Component
*/
public function asBoolean($value)
{
if ($value === null) {
return $this->nullDisplay;
}
return $value ? $this->booleanFormat[1] : $this->booleanFormat[0];
}
@ -183,6 +217,9 @@ class Formatter extends Component
*/
public function asDate($value, $format = null)
{
if ($value === null) {
return $this->nullDisplay;
}
$value = $this->normalizeDatetimeValue($value);
return date($format === null ? $this->dateFormat : $format, $value);
}
@ -204,6 +241,9 @@ class Formatter extends Component
*/
public function asTime($value, $format = null)
{
if ($value === null) {
return $this->nullDisplay;
}
$value = $this->normalizeDatetimeValue($value);
return date($format === null ? $this->timeFormat : $format, $value);
}
@ -225,6 +265,9 @@ class Formatter extends Component
*/
public function asDatetime($value, $format = null)
{
if ($value === null) {
return $this->nullDisplay;
}
$value = $this->normalizeDatetimeValue($value);
return date($format === null ? $this->datetimeFormat : $format, $value);
}
@ -256,6 +299,9 @@ class Formatter extends Component
*/
public function asInteger($value)
{
if ($value === null) {
return $this->nullDisplay;
}
if (is_string($value) && preg_match('/^(-?\d+)/', $value, $matches)) {
return $matches[1];
} else {
@ -274,6 +320,9 @@ class Formatter extends Component
*/
public function asDouble($value, $decimals = 2)
{
if ($value === null) {
return $this->nullDisplay;
}
if ($this->decimalSeparator === null) {
return sprintf("%.{$decimals}f", $value);
} else {
@ -292,6 +341,9 @@ class Formatter extends Component
*/
public function asNumber($value, $decimals = 0)
{
if ($value === null) {
return $this->nullDisplay;
}
$ds = isset($this->decimalSeparator) ? $this->decimalSeparator: '.';
$ts = isset($this->thousandSeparator) ? $this->thousandSeparator: ',';
return number_format($value, $decimals, $ds, $ts);

21
framework/yii/i18n/Formatter.php

@ -120,6 +120,9 @@ class Formatter extends \yii\base\Formatter
*/
public function asDate($value, $format = null)
{
if ($value === null) {
return $this->nullDisplay;
}
$value = $this->normalizeDatetimeValue($value);
if ($format === null) {
$format = $this->dateFormat;
@ -153,6 +156,9 @@ class Formatter extends \yii\base\Formatter
*/
public function asTime($value, $format = null)
{
if ($value === null) {
return $this->nullDisplay;
}
$value = $this->normalizeDatetimeValue($value);
if ($format === null) {
$format = $this->timeFormat;
@ -186,6 +192,9 @@ class Formatter extends \yii\base\Formatter
*/
public function asDatetime($value, $format = null)
{
if ($value === null) {
return $this->nullDisplay;
}
$value = $this->normalizeDatetimeValue($value);
if ($format === null) {
$format = $this->datetimeFormat;
@ -208,6 +217,9 @@ class Formatter extends \yii\base\Formatter
*/
public function asDecimal($value, $format = null)
{
if ($value === null) {
return $this->nullDisplay;
}
return $this->createNumberFormatter(NumberFormatter::DECIMAL, $format)->format($value);
}
@ -221,6 +233,9 @@ class Formatter extends \yii\base\Formatter
*/
public function asCurrency($value, $currency = 'USD', $format = null)
{
if ($value === null) {
return $this->nullDisplay;
}
return $this->createNumberFormatter(NumberFormatter::CURRENCY, $format)->formatCurrency($value, $currency);
}
@ -233,6 +248,9 @@ class Formatter extends \yii\base\Formatter
*/
public function asPercent($value, $format = null)
{
if ($value === null) {
return $this->nullDisplay;
}
return $this->createNumberFormatter(NumberFormatter::PERCENT, $format)->format($value);
}
@ -245,6 +263,9 @@ class Formatter extends \yii\base\Formatter
*/
public function asScientific($value, $format = null)
{
if ($value === null) {
return $this->nullDisplay;
}
return $this->createNumberFormatter(NumberFormatter::SCIENTIFIC, $format)->format($value);
}

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

@ -42,6 +42,7 @@ class FormatterTest extends TestCase
$this->assertSame($value, $this->formatter->asRaw($value));
$value = '<>';
$this->assertSame($value, $this->formatter->asRaw($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asRaw(null));
}
public function testAsText()
@ -52,6 +53,7 @@ class FormatterTest extends TestCase
$this->assertSame("$value", $this->formatter->asText($value));
$value = '<>';
$this->assertSame('&lt;&gt;', $this->formatter->asText($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asText(null));
}
public function testAsNtext()
@ -64,6 +66,7 @@ class FormatterTest extends TestCase
$this->assertSame('&lt;&gt;', $this->formatter->asNtext($value));
$value = "123\n456";
$this->assertSame("123<br />\n456", $this->formatter->asNtext($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asNtext(null));
}
public function testAsParagraphs()
@ -80,6 +83,7 @@ class FormatterTest extends TestCase
$this->assertSame("<p>123</p>\n<p>456</p>", $this->formatter->asParagraphs($value));
$value = "123\n\n\n456";
$this->assertSame("<p>123</p>\n<p>456</p>", $this->formatter->asParagraphs($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asParagraphs(null));
}
public function testAsHtml()
@ -91,12 +95,14 @@ class FormatterTest extends TestCase
{
$value = 'test@sample.com';
$this->assertSame("<a href=\"mailto:$value\">$value</a>", $this->formatter->asEmail($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asEmail(null));
}
public function testAsImage()
{
$value = 'http://sample.com/img.jpg';
$this->assertSame("<img src=\"$value\" alt=\"\">", $this->formatter->asImage($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asImage(null));
}
public function testAsBoolean()
@ -109,6 +115,7 @@ class FormatterTest extends TestCase
$this->assertSame('Yes', $this->formatter->asBoolean($value));
$value = "";
$this->assertSame('No', $this->formatter->asBoolean($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asBoolean(null));
}
public function testAsDate()
@ -116,6 +123,7 @@ class FormatterTest extends TestCase
$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'));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDate(null));
}
public function testAsTime()
@ -123,6 +131,7 @@ class FormatterTest extends TestCase
$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'));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asTime(null));
}
public function testAsDatetime()
@ -130,6 +139,7 @@ class FormatterTest extends TestCase
$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'));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDatetime(null));
}
public function testAsInteger()
@ -144,6 +154,7 @@ class FormatterTest extends TestCase
$this->assertSame("-123", $this->formatter->asInteger($value));
$value = "-123abc";
$this->assertSame("-123", $this->formatter->asInteger($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asInteger(null));
}
public function testAsDouble()
@ -161,6 +172,7 @@ class FormatterTest extends TestCase
$this->assertSame("123", $this->formatter->asDouble($value, 0));
$value = 123123.123;
$this->assertSame("123123,12", $this->formatter->asDouble($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDouble(null));
}
public function testAsNumber()
@ -175,5 +187,6 @@ class FormatterTest extends TestCase
$this->formatter->thousandSeparator = '';
$this->assertSame("123123", $this->formatter->asNumber($value));
$this->assertSame("123123,12", $this->formatter->asNumber($value, 2));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asNumber(null));
}
}

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

@ -47,6 +47,7 @@ class FormatterTest extends TestCase
$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()
@ -57,6 +58,7 @@ class FormatterTest extends TestCase
$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()
@ -67,6 +69,7 @@ class FormatterTest extends TestCase
$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()
@ -77,6 +80,7 @@ class FormatterTest extends TestCase
$this->assertSame("$123.46", $this->formatter->asCurrency($value));
$value = '-123456.123';
$this->assertSame("($123,456.12)", $this->formatter->asCurrency($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asCurrency(null));
}
public function testDate()
@ -84,5 +88,6 @@ class FormatterTest extends TestCase
$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));
}
}

Loading…
Cancel
Save