Yii Framework v1.1.10 绫诲弬鑰

CFormatter

system.utils
缁ф壙 class CFormatter » CApplicationComponent » CComponent
瀹炵幇 IApplicationComponent
婧愯嚜 1.1.0
鐗堟湰 $Id: CFormatter.php 3553 2012-02-06 22:07:58Z alexander.makarow $
婧愮爜 framework/utils/CFormatter.php
CFormatter provides a set of commonly used data formatting methods.

The formatting methods provided by CFormatter are all named in the form of formatXyz. The behavior of some of them may be configured via the properties of CFormatter. For example, by configuring dateFormat, one may control how formatDate formats the value into a date string.

For convenience, CFormatter also implements the mechanism of calling formatting methods with their shortcuts (called types). In particular, if a formatting method is named formatXyz, then its shortcut method is xyz (case-insensitive). For example, calling $formatter->date($value) is equivalent to calling $formatter->formatDate($value).

Currently, the following types are recognizable:
  • raw: the attribute value will not be changed at all.
  • text: the attribute value will be HTML-encoded when rendering.
  • ntext: the formatNtext method will be called to format the attribute value as a HTML-encoded plain text with newlines converted as the HTML <br /> tags.
  • html: the attribute value will be purified and then returned.
  • date: the formatDate method will be called to format the attribute value as a date.
  • time: the formatTime method will be called to format the attribute value as a time.
  • datetime: the formatDatetime method will be called to format the attribute value as a date with time.
  • boolean: the formatBoolean method will be called to format the attribute value as a boolean display.
  • number: the formatNumber method will be called to format the attribute value as a number display.
  • email: the formatEmail method will be called to format the attribute value as a mailto link.
  • image: the formatImage method will be called to format the attribute value as an image tag where the attribute value is the image URL.
  • url: the formatUrl method will be called to format the attribute value as a hyperlink where the attribute value is the URL.


By default, CApplication registers CFormatter as an application component whose ID is 'format'. Therefore, one may call Yii::app()->format->boolean(1).

鍏叡灞炴

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
behaviors array 杩欎釜搴旂敤缁勪欢闄勫姞鐨勮涓恒 杩欐琛屼负灏嗗湪搴旂敤缁勪欢璋冪敤init鏃堕檮鍔犲湪搴旂敤缁勪欢涓娿 璇峰弬鐓CModel::behaviors濡備綍鎸囧畾姝ゅ睘鎬у笺 CApplicationComponent
booleanFormat array the text to be displayed when formatting a boolean value. CFormatter
dateFormat string the format string to be used to format a date using PHP date() function. CFormatter
datetimeFormat string the format string to be used to format a date and time using PHP date() function. CFormatter
htmlPurifier CHtmlPurifier the HTML purifier instance CFormatter
isInitialized boolean 妫鏌ュ簲鐢ㄧ粍浠舵槸鍚﹀凡缁忓垵濮嬪寲銆 CApplicationComponent
numberFormat array the format used to format a number with PHP number_format() function. CFormatter
timeFormat string the format string to be used to format a time using PHP date() function. CFormatter

鍏叡鏂规硶

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
__call() Calls the format method when its shortcut is invoked. CFormatter
__get() 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 CComponent
__isset() 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 CComponent
__set() 璁剧疆涓涓粍浠剁殑灞炴у笺 CComponent
__unset() 璁剧疆涓涓粍浠剁殑灞炴т负null銆 CComponent
asa() 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° CComponent
attachBehavior() 闄勫姞涓涓涓哄埌缁勪欢銆 CComponent
attachBehaviors() 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 CComponent
attachEventHandler() 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 CComponent
canGetProperty() 纭畾灞炴ф槸鍚﹀彲璇汇 CComponent
canSetProperty() 纭畾灞炴ф槸鍚﹀彲鍐欍 CComponent
detachBehavior() 浠庣粍浠朵腑鍒嗙涓涓涓恒 CComponent
detachBehaviors() 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 CComponent
detachEventHandler() 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 CComponent
disableBehavior() 绂佺敤涓涓檮鍔犺涓恒 CComponent
disableBehaviors() 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
enableBehavior() 鍚敤涓涓檮鍔犺涓恒 CComponent
enableBehaviors() 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
evaluateExpression() 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 CComponent
format() Formats a value based on the given type. CFormatter
formatBoolean() Formats the value as a boolean. CFormatter
formatDate() Formats the value as a date. CFormatter
formatDatetime() Formats the value as a date and time. CFormatter
formatEmail() Formats the value as a mailto link. CFormatter
formatHtml() Formats the value as HTML text without any encoding. CFormatter
formatImage() Formats the value as an image tag. CFormatter
formatNtext() Formats the value as a HTML-encoded plain text and converts newlines with HTML br tags. CFormatter
formatNumber() Formats the value as a number using PHP number_format() function. CFormatter
formatRaw() Formats the value as is without any formatting. CFormatter
formatText() Formats the value as a HTML-encoded plain text. CFormatter
formatTime() Formats the value as a time. CFormatter
formatUrl() Formats the value as a hyperlink. CFormatter
getEventHandlers() 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 CComponent
getHtmlPurifier() 杩斿洖the HTML purifier instance CFormatter
getIsInitialized() 妫鏌ュ簲鐢ㄧ粍浠舵槸鍚﹀凡缁忓垵濮嬪寲銆 CApplicationComponent
hasEvent() 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 CComponent
hasEventHandler() 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 CComponent
hasProperty() 纭畾灞炴ф槸鍚﹁瀹氫箟銆 CComponent
init() 鍒濆鍖栧簲鐢ㄧ粍浠躲 CApplicationComponent
raiseEvent() 鍙戣捣涓涓簨浠躲 CComponent

灞炴ц缁

booleanFormat 灞炴
public array $booleanFormat;

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').

dateFormat 灞炴
public string $dateFormat;

the format string to be used to format a date using PHP date() function. Defaults to 'Y/m/d'.

datetimeFormat 灞炴
public string $datetimeFormat;

the format string to be used to format a date and time using PHP date() function. Defaults to 'Y/m/d h:i:s A'.

htmlPurifier 灞炴 鍙

the HTML purifier instance

numberFormat 灞炴
public array $numberFormat;

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.

timeFormat 灞炴
public string $timeFormat;

the format string to be used to format a time using PHP date() function. Defaults to 'h:i:s A'.

鏂规硶璇︾粏

__call() 鏂规硶
public mixed __call(string $name, array $parameters)
$name string the method name
$parameters array method parameters
{return} mixed the method return value
婧愮爜锛 framework/utils/CFormatter.php#85 (鏄剧ず)
public function __call($name,$parameters)
{
    if(
method_exists($this,'format'.$name))
        return 
call_user_func_array(array($this,'format'.$name),$parameters);
    else
        return 
parent::__call($name,$parameters);
}

Calls the format method when its shortcut is invoked. This is a PHP magic method that we override to implement the shortcut format methods.

format() 鏂规硶
public string format(mixed $value, string $type)
$value mixed the value to be formatted
$type string the data type. This must correspond to a format method available in CFormatter. For example, we can use 'text' here because there is method named formatText.
{return} string the formatted data
婧愮爜锛 framework/utils/CFormatter.php#100 (鏄剧ず)
public function format($value,$type)
{
    
$method='format'.$type;
    if(
method_exists($this,$method))
        return 
$this->$method($value);
    else
        throw new 
CException(Yii::t('yii','Unknown type "{type}".',array('{type}'=>$type)));
}

Formats a value based on the given type.

formatBoolean() 鏂规硶
public string formatBoolean(mixed $value)
$value mixed the value to be formatted
{return} string the formatted result
婧愮爜锛 framework/utils/CFormatter.php#189 (鏄剧ず)
public function formatBoolean($value)
{
    return 
$value $this->booleanFormat[1] : $this->booleanFormat[0];
}

Formats the value as a boolean.

鍙傝

formatDate() 鏂规硶
public string formatDate(mixed $value)
$value mixed the value to be formatted
{return} string the formatted result
婧愮爜锛 framework/utils/CFormatter.php#156 (鏄剧ず)
public function formatDate($value)
{
    return 
date($this->dateFormat,$value);
}

Formats the value as a date.

鍙傝

formatDatetime() 鏂规硶
public string formatDatetime(mixed $value)
$value mixed the value to be formatted
{return} string the formatted result
婧愮爜锛 framework/utils/CFormatter.php#178 (鏄剧ず)
public function formatDatetime($value)
{
    return 
date($this->datetimeFormat,$value);
}

Formats the value as a date and time.

鍙傝

formatEmail() 鏂规硶
public string formatEmail(mixed $value)
$value mixed the value to be formatted
{return} string the formatted result
婧愮爜锛 framework/utils/CFormatter.php#199 (鏄剧ず)
public function formatEmail($value)
{
    return 
CHtml::mailto($value);
}

Formats the value as a mailto link.

formatHtml() 鏂规硶
public string formatHtml(mixed $value)
$value mixed the value to be formatted
{return} string the formatted result
婧愮爜锛 framework/utils/CFormatter.php#145 (鏄剧ず)
public function formatHtml($value)
{
    return 
$this->getHtmlPurifier()->purify($value);
}

Formats the value as HTML text without any encoding.

formatImage() 鏂规硶
public string formatImage(mixed $value)
$value mixed the value to be formatted
{return} string the formatted result
婧愮爜锛 framework/utils/CFormatter.php#209 (鏄剧ず)
public function formatImage($value)
{
    return 
CHtml::image($value);
}

Formats the value as an image tag.

formatNtext() 鏂规硶
public string formatNtext(mixed $value)
$value mixed the value to be formatted
{return} string the formatted result
婧愮爜锛 framework/utils/CFormatter.php#135 (鏄剧ず)
public function formatNtext($value)
{
    return 
nl2br(CHtml::encode($value));
}

Formats the value as a HTML-encoded plain text and converts newlines with HTML br tags.

formatNumber() 鏂规硶
public string formatNumber(mixed $value)
$value mixed the value to be formatted
{return} string the formatted result
婧愮爜锛 framework/utils/CFormatter.php#233 (鏄剧ず)
public function formatNumber($value)
{
    return 
number_format($value,$this->numberFormat['decimals'],$this->numberFormat['decimalSeparator'],$this->numberFormat['thousandSeparator']);
}

Formats the value as a number using PHP number_format() function.

鍙傝

formatRaw() 鏂规硶
public string formatRaw(mixed $value)
$value mixed the value to be formatted
{return} string the formatted result
婧愮爜锛 framework/utils/CFormatter.php#115 (鏄剧ず)
public function formatRaw($value)
{
    return 
$value;
}

Formats the value as is without any formatting. This method simply returns back the parameter without any format.

formatText() 鏂规硶
public string formatText(mixed $value)
$value mixed the value to be formatted
{return} string the formatted result
婧愮爜锛 framework/utils/CFormatter.php#125 (鏄剧ず)
public function formatText($value)
{
    return 
CHtml::encode($value);
}

Formats the value as a HTML-encoded plain text.

formatTime() 鏂规硶
public string formatTime(mixed $value)
$value mixed the value to be formatted
{return} string the formatted result
婧愮爜锛 framework/utils/CFormatter.php#167 (鏄剧ず)
public function formatTime($value)
{
    return 
date($this->timeFormat,$value);
}

Formats the value as a time.

鍙傝

formatUrl() 鏂规硶
public string formatUrl(mixed $value)
$value mixed the value to be formatted
{return} string the formatted result
婧愮爜锛 framework/utils/CFormatter.php#219 (鏄剧ず)
public function formatUrl($value)
{
    
$url=$value;
    if(
strpos($url,'http://')!==&& strpos($url,'https://')!==0)
        
$url='http://'.$url;
    return 
CHtml::link(CHtml::encode($value),$url);
}

Formats the value as a hyperlink.

getHtmlPurifier() 鏂规硶
public CHtmlPurifier getHtmlPurifier()
{return} CHtmlPurifier the HTML purifier instance
婧愮爜锛 framework/utils/CFormatter.php#241 (鏄剧ず)
public function getHtmlPurifier()
{
    if(
$this->_htmlPurifier===null)
        
$this->_htmlPurifier=new CHtmlPurifier;
    return 
$this->_htmlPurifier;
}

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.