Commit 3eccd850 by Qiang Xue

Added Formatter::nullDisplay.

parent 05182207
...@@ -37,6 +37,10 @@ class Formatter extends Component ...@@ -37,6 +37,10 @@ class Formatter extends Component
*/ */
public $datetimeFormat = 'Y/m/d h:i:s A'; 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 * @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')`. * to the text display for false, the second element for true. Defaults to `array('No', 'Yes')`.
*/ */
...@@ -61,6 +65,9 @@ class Formatter extends Component ...@@ -61,6 +65,9 @@ class Formatter extends Component
if (empty($this->booleanFormat)) { if (empty($this->booleanFormat)) {
$this->booleanFormat = array(Yii::t('yii', 'No'), Yii::t('yii', 'Yes')); $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 ...@@ -71,6 +78,9 @@ class Formatter extends Component
*/ */
public function asRaw($value) public function asRaw($value)
{ {
if ($value === null) {
return $this->nullDisplay;
}
return $value; return $value;
} }
...@@ -81,6 +91,9 @@ class Formatter extends Component ...@@ -81,6 +91,9 @@ class Formatter extends Component
*/ */
public function asText($value) public function asText($value)
{ {
if ($value === null) {
return $this->nullDisplay;
}
return Html::encode($value); return Html::encode($value);
} }
...@@ -91,6 +104,9 @@ class Formatter extends Component ...@@ -91,6 +104,9 @@ class Formatter extends Component
*/ */
public function asNtext($value) public function asNtext($value)
{ {
if ($value === null) {
return $this->nullDisplay;
}
return nl2br(Html::encode($value)); return nl2br(Html::encode($value));
} }
...@@ -103,6 +119,9 @@ class Formatter extends Component ...@@ -103,6 +119,9 @@ class Formatter extends Component
*/ */
public function asParagraphs($value) public function asParagraphs($value)
{ {
if ($value === null) {
return $this->nullDisplay;
}
return str_replace('<p></p>', '', return str_replace('<p></p>', '',
'<p>' . preg_replace('/[\r\n]{2,}/', "</p>\n<p>", Html::encode($value)) . '</p>' '<p>' . preg_replace('/[\r\n]{2,}/', "</p>\n<p>", Html::encode($value)) . '</p>'
); );
...@@ -118,6 +137,9 @@ class Formatter extends Component ...@@ -118,6 +137,9 @@ class Formatter extends Component
*/ */
public function asHtml($value, $config = null) public function asHtml($value, $config = null)
{ {
if ($value === null) {
return $this->nullDisplay;
}
return HtmlPurifier::process($value, $config); return HtmlPurifier::process($value, $config);
} }
...@@ -128,6 +150,9 @@ class Formatter extends Component ...@@ -128,6 +150,9 @@ class Formatter extends Component
*/ */
public function asEmail($value) public function asEmail($value)
{ {
if ($value === null) {
return $this->nullDisplay;
}
return Html::mailto($value); return Html::mailto($value);
} }
...@@ -138,6 +163,9 @@ class Formatter extends Component ...@@ -138,6 +163,9 @@ class Formatter extends Component
*/ */
public function asImage($value) public function asImage($value)
{ {
if ($value === null) {
return $this->nullDisplay;
}
return Html::img($value); return Html::img($value);
} }
...@@ -148,6 +176,9 @@ class Formatter extends Component ...@@ -148,6 +176,9 @@ class Formatter extends Component
*/ */
public function asUrl($value) public function asUrl($value)
{ {
if ($value === null) {
return $this->nullDisplay;
}
$url = $value; $url = $value;
if (strpos($url, 'http://') !== 0 && strpos($url, 'https://') !== 0) { if (strpos($url, 'http://') !== 0 && strpos($url, 'https://') !== 0) {
$url = 'http://' . $url; $url = 'http://' . $url;
...@@ -163,6 +194,9 @@ class Formatter extends Component ...@@ -163,6 +194,9 @@ class Formatter extends Component
*/ */
public function asBoolean($value) public function asBoolean($value)
{ {
if ($value === null) {
return $this->nullDisplay;
}
return $value ? $this->booleanFormat[1] : $this->booleanFormat[0]; return $value ? $this->booleanFormat[1] : $this->booleanFormat[0];
} }
...@@ -183,6 +217,9 @@ class Formatter extends Component ...@@ -183,6 +217,9 @@ class Formatter extends Component
*/ */
public function asDate($value, $format = null) public function asDate($value, $format = null)
{ {
if ($value === null) {
return $this->nullDisplay;
}
$value = $this->normalizeDatetimeValue($value); $value = $this->normalizeDatetimeValue($value);
return date($format === null ? $this->dateFormat : $format, $value); return date($format === null ? $this->dateFormat : $format, $value);
} }
...@@ -204,6 +241,9 @@ class Formatter extends Component ...@@ -204,6 +241,9 @@ class Formatter extends Component
*/ */
public function asTime($value, $format = null) public function asTime($value, $format = null)
{ {
if ($value === null) {
return $this->nullDisplay;
}
$value = $this->normalizeDatetimeValue($value); $value = $this->normalizeDatetimeValue($value);
return date($format === null ? $this->timeFormat : $format, $value); return date($format === null ? $this->timeFormat : $format, $value);
} }
...@@ -225,6 +265,9 @@ class Formatter extends Component ...@@ -225,6 +265,9 @@ class Formatter extends Component
*/ */
public function asDatetime($value, $format = null) public function asDatetime($value, $format = null)
{ {
if ($value === null) {
return $this->nullDisplay;
}
$value = $this->normalizeDatetimeValue($value); $value = $this->normalizeDatetimeValue($value);
return date($format === null ? $this->datetimeFormat : $format, $value); return date($format === null ? $this->datetimeFormat : $format, $value);
} }
...@@ -256,6 +299,9 @@ class Formatter extends Component ...@@ -256,6 +299,9 @@ class Formatter extends Component
*/ */
public function asInteger($value) public function asInteger($value)
{ {
if ($value === null) {
return $this->nullDisplay;
}
if (is_string($value) && preg_match('/^(-?\d+)/', $value, $matches)) { if (is_string($value) && preg_match('/^(-?\d+)/', $value, $matches)) {
return $matches[1]; return $matches[1];
} else { } else {
...@@ -274,6 +320,9 @@ class Formatter extends Component ...@@ -274,6 +320,9 @@ class Formatter extends Component
*/ */
public function asDouble($value, $decimals = 2) public function asDouble($value, $decimals = 2)
{ {
if ($value === null) {
return $this->nullDisplay;
}
if ($this->decimalSeparator === null) { if ($this->decimalSeparator === null) {
return sprintf("%.{$decimals}f", $value); return sprintf("%.{$decimals}f", $value);
} else { } else {
...@@ -292,6 +341,9 @@ class Formatter extends Component ...@@ -292,6 +341,9 @@ class Formatter extends Component
*/ */
public function asNumber($value, $decimals = 0) public function asNumber($value, $decimals = 0)
{ {
if ($value === null) {
return $this->nullDisplay;
}
$ds = isset($this->decimalSeparator) ? $this->decimalSeparator: '.'; $ds = isset($this->decimalSeparator) ? $this->decimalSeparator: '.';
$ts = isset($this->thousandSeparator) ? $this->thousandSeparator: ','; $ts = isset($this->thousandSeparator) ? $this->thousandSeparator: ',';
return number_format($value, $decimals, $ds, $ts); return number_format($value, $decimals, $ds, $ts);
......
...@@ -120,6 +120,9 @@ class Formatter extends \yii\base\Formatter ...@@ -120,6 +120,9 @@ class Formatter extends \yii\base\Formatter
*/ */
public function asDate($value, $format = null) public function asDate($value, $format = null)
{ {
if ($value === null) {
return $this->nullDisplay;
}
$value = $this->normalizeDatetimeValue($value); $value = $this->normalizeDatetimeValue($value);
if ($format === null) { if ($format === null) {
$format = $this->dateFormat; $format = $this->dateFormat;
...@@ -153,6 +156,9 @@ class Formatter extends \yii\base\Formatter ...@@ -153,6 +156,9 @@ class Formatter extends \yii\base\Formatter
*/ */
public function asTime($value, $format = null) public function asTime($value, $format = null)
{ {
if ($value === null) {
return $this->nullDisplay;
}
$value = $this->normalizeDatetimeValue($value); $value = $this->normalizeDatetimeValue($value);
if ($format === null) { if ($format === null) {
$format = $this->timeFormat; $format = $this->timeFormat;
...@@ -186,6 +192,9 @@ class Formatter extends \yii\base\Formatter ...@@ -186,6 +192,9 @@ class Formatter extends \yii\base\Formatter
*/ */
public function asDatetime($value, $format = null) public function asDatetime($value, $format = null)
{ {
if ($value === null) {
return $this->nullDisplay;
}
$value = $this->normalizeDatetimeValue($value); $value = $this->normalizeDatetimeValue($value);
if ($format === null) { if ($format === null) {
$format = $this->datetimeFormat; $format = $this->datetimeFormat;
...@@ -208,6 +217,9 @@ class Formatter extends \yii\base\Formatter ...@@ -208,6 +217,9 @@ class Formatter extends \yii\base\Formatter
*/ */
public function asDecimal($value, $format = null) public function asDecimal($value, $format = null)
{ {
if ($value === null) {
return $this->nullDisplay;
}
return $this->createNumberFormatter(NumberFormatter::DECIMAL, $format)->format($value); return $this->createNumberFormatter(NumberFormatter::DECIMAL, $format)->format($value);
} }
...@@ -221,6 +233,9 @@ class Formatter extends \yii\base\Formatter ...@@ -221,6 +233,9 @@ class Formatter extends \yii\base\Formatter
*/ */
public function asCurrency($value, $currency = 'USD', $format = null) public function asCurrency($value, $currency = 'USD', $format = null)
{ {
if ($value === null) {
return $this->nullDisplay;
}
return $this->createNumberFormatter(NumberFormatter::CURRENCY, $format)->formatCurrency($value, $currency); return $this->createNumberFormatter(NumberFormatter::CURRENCY, $format)->formatCurrency($value, $currency);
} }
...@@ -233,6 +248,9 @@ class Formatter extends \yii\base\Formatter ...@@ -233,6 +248,9 @@ class Formatter extends \yii\base\Formatter
*/ */
public function asPercent($value, $format = null) public function asPercent($value, $format = null)
{ {
if ($value === null) {
return $this->nullDisplay;
}
return $this->createNumberFormatter(NumberFormatter::PERCENT, $format)->format($value); return $this->createNumberFormatter(NumberFormatter::PERCENT, $format)->format($value);
} }
...@@ -245,6 +263,9 @@ class Formatter extends \yii\base\Formatter ...@@ -245,6 +263,9 @@ class Formatter extends \yii\base\Formatter
*/ */
public function asScientific($value, $format = null) public function asScientific($value, $format = null)
{ {
if ($value === null) {
return $this->nullDisplay;
}
return $this->createNumberFormatter(NumberFormatter::SCIENTIFIC, $format)->format($value); return $this->createNumberFormatter(NumberFormatter::SCIENTIFIC, $format)->format($value);
} }
......
...@@ -42,6 +42,7 @@ class FormatterTest extends TestCase ...@@ -42,6 +42,7 @@ class FormatterTest extends TestCase
$this->assertSame($value, $this->formatter->asRaw($value)); $this->assertSame($value, $this->formatter->asRaw($value));
$value = '<>'; $value = '<>';
$this->assertSame($value, $this->formatter->asRaw($value)); $this->assertSame($value, $this->formatter->asRaw($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asRaw(null));
} }
public function testAsText() public function testAsText()
...@@ -52,6 +53,7 @@ class FormatterTest extends TestCase ...@@ -52,6 +53,7 @@ class FormatterTest extends TestCase
$this->assertSame("$value", $this->formatter->asText($value)); $this->assertSame("$value", $this->formatter->asText($value));
$value = '<>'; $value = '<>';
$this->assertSame('&lt;&gt;', $this->formatter->asText($value)); $this->assertSame('&lt;&gt;', $this->formatter->asText($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asText(null));
} }
public function testAsNtext() public function testAsNtext()
...@@ -64,6 +66,7 @@ class FormatterTest extends TestCase ...@@ -64,6 +66,7 @@ class FormatterTest extends TestCase
$this->assertSame('&lt;&gt;', $this->formatter->asNtext($value)); $this->assertSame('&lt;&gt;', $this->formatter->asNtext($value));
$value = "123\n456"; $value = "123\n456";
$this->assertSame("123<br />\n456", $this->formatter->asNtext($value)); $this->assertSame("123<br />\n456", $this->formatter->asNtext($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asNtext(null));
} }
public function testAsParagraphs() public function testAsParagraphs()
...@@ -80,6 +83,7 @@ class FormatterTest extends TestCase ...@@ -80,6 +83,7 @@ class FormatterTest extends TestCase
$this->assertSame("<p>123</p>\n<p>456</p>", $this->formatter->asParagraphs($value)); $this->assertSame("<p>123</p>\n<p>456</p>", $this->formatter->asParagraphs($value));
$value = "123\n\n\n456"; $value = "123\n\n\n456";
$this->assertSame("<p>123</p>\n<p>456</p>", $this->formatter->asParagraphs($value)); $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() public function testAsHtml()
...@@ -91,12 +95,14 @@ class FormatterTest extends TestCase ...@@ -91,12 +95,14 @@ class FormatterTest extends TestCase
{ {
$value = 'test@sample.com'; $value = 'test@sample.com';
$this->assertSame("<a href=\"mailto:$value\">$value</a>", $this->formatter->asEmail($value)); $this->assertSame("<a href=\"mailto:$value\">$value</a>", $this->formatter->asEmail($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asEmail(null));
} }
public function testAsImage() public function testAsImage()
{ {
$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));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asImage(null));
} }
public function testAsBoolean() public function testAsBoolean()
...@@ -109,6 +115,7 @@ class FormatterTest extends TestCase ...@@ -109,6 +115,7 @@ class FormatterTest extends TestCase
$this->assertSame('Yes', $this->formatter->asBoolean($value)); $this->assertSame('Yes', $this->formatter->asBoolean($value));
$value = ""; $value = "";
$this->assertSame('No', $this->formatter->asBoolean($value)); $this->assertSame('No', $this->formatter->asBoolean($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asBoolean(null));
} }
public function testAsDate() public function testAsDate()
...@@ -116,6 +123,7 @@ class FormatterTest extends TestCase ...@@ -116,6 +123,7 @@ class FormatterTest extends TestCase
$value = time(); $value = time();
$this->assertSame(date('Y/m/d', $value), $this->formatter->asDate($value)); $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(date('Y-m-d', $value), $this->formatter->asDate($value, 'Y-m-d'));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDate(null));
} }
public function testAsTime() public function testAsTime()
...@@ -123,6 +131,7 @@ class FormatterTest extends TestCase ...@@ -123,6 +131,7 @@ class FormatterTest extends TestCase
$value = time(); $value = time();
$this->assertSame(date('h:i:s A', $value), $this->formatter->asTime($value)); $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(date('h:i:s', $value), $this->formatter->asTime($value, 'h:i:s'));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asTime(null));
} }
public function testAsDatetime() public function testAsDatetime()
...@@ -130,6 +139,7 @@ class FormatterTest extends TestCase ...@@ -130,6 +139,7 @@ class FormatterTest extends TestCase
$value = time(); $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 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(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() public function testAsInteger()
...@@ -144,6 +154,7 @@ class FormatterTest extends TestCase ...@@ -144,6 +154,7 @@ class FormatterTest extends TestCase
$this->assertSame("-123", $this->formatter->asInteger($value)); $this->assertSame("-123", $this->formatter->asInteger($value));
$value = "-123abc"; $value = "-123abc";
$this->assertSame("-123", $this->formatter->asInteger($value)); $this->assertSame("-123", $this->formatter->asInteger($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asInteger(null));
} }
public function testAsDouble() public function testAsDouble()
...@@ -161,6 +172,7 @@ class FormatterTest extends TestCase ...@@ -161,6 +172,7 @@ class FormatterTest extends TestCase
$this->assertSame("123", $this->formatter->asDouble($value, 0)); $this->assertSame("123", $this->formatter->asDouble($value, 0));
$value = 123123.123; $value = 123123.123;
$this->assertSame("123123,12", $this->formatter->asDouble($value)); $this->assertSame("123123,12", $this->formatter->asDouble($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDouble(null));
} }
public function testAsNumber() public function testAsNumber()
...@@ -175,5 +187,6 @@ class FormatterTest extends TestCase ...@@ -175,5 +187,6 @@ class FormatterTest extends TestCase
$this->formatter->thousandSeparator = ''; $this->formatter->thousandSeparator = '';
$this->assertSame("123123", $this->formatter->asNumber($value)); $this->assertSame("123123", $this->formatter->asNumber($value));
$this->assertSame("123123,12", $this->formatter->asNumber($value, 2)); $this->assertSame("123123,12", $this->formatter->asNumber($value, 2));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asNumber(null));
} }
} }
...@@ -47,6 +47,7 @@ class FormatterTest extends TestCase ...@@ -47,6 +47,7 @@ class FormatterTest extends TestCase
$this->assertSame("123,456", $this->formatter->asDecimal($value)); $this->assertSame("123,456", $this->formatter->asDecimal($value));
$value = '-123456.123'; $value = '-123456.123';
$this->assertSame("-123,456.123", $this->formatter->asDecimal($value)); $this->assertSame("-123,456.123", $this->formatter->asDecimal($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDecimal(null));
} }
public function testAsPercent() public function testAsPercent()
...@@ -57,6 +58,7 @@ class FormatterTest extends TestCase ...@@ -57,6 +58,7 @@ class FormatterTest extends TestCase
$this->assertSame("12%", $this->formatter->asPercent($value)); $this->assertSame("12%", $this->formatter->asPercent($value));
$value = '-0.009343'; $value = '-0.009343';
$this->assertSame("-1%", $this->formatter->asPercent($value)); $this->assertSame("-1%", $this->formatter->asPercent($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asPercent(null));
} }
public function testAsScientific() public function testAsScientific()
...@@ -67,6 +69,7 @@ class FormatterTest extends TestCase ...@@ -67,6 +69,7 @@ class FormatterTest extends TestCase
$this->assertSame("1.23456E5", $this->formatter->asScientific($value)); $this->assertSame("1.23456E5", $this->formatter->asScientific($value));
$value = '-123456.123'; $value = '-123456.123';
$this->assertSame("-1.23456123E5", $this->formatter->asScientific($value)); $this->assertSame("-1.23456123E5", $this->formatter->asScientific($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asScientific(null));
} }
public function testAsCurrency() public function testAsCurrency()
...@@ -77,6 +80,7 @@ class FormatterTest extends TestCase ...@@ -77,6 +80,7 @@ class FormatterTest extends TestCase
$this->assertSame("$123.46", $this->formatter->asCurrency($value)); $this->assertSame("$123.46", $this->formatter->asCurrency($value));
$value = '-123456.123'; $value = '-123456.123';
$this->assertSame("($123,456.12)", $this->formatter->asCurrency($value)); $this->assertSame("($123,456.12)", $this->formatter->asCurrency($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asCurrency(null));
} }
public function testDate() public function testDate()
...@@ -84,5 +88,6 @@ class FormatterTest extends TestCase ...@@ -84,5 +88,6 @@ class FormatterTest extends TestCase
$time = time(); $time = time();
$this->assertSame(date('n/j/y', $time), $this->formatter->asDate($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(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