CFormatter

优质
小牛编辑
123浏览
2023-12-01
所有包 | 属性 | 方法
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).

公共属性

隐藏继承属性

属性类型描述定义在
behaviorsarray这个应用组件附加的行为。 这此行为将在应用组件调用init时附加在应用组件上。 请参照CModel::behaviors如何指定此属性值。CApplicationComponent
booleanFormatarraythe text to be displayed when formatting a boolean value.CFormatter
dateFormatstringthe format string to be used to format a date using PHP date() function.CFormatter
datetimeFormatstringthe format string to be used to format a date and time using PHP date() function.CFormatter
htmlPurifierCHtmlPurifierthe HTML purifier instanceCFormatter
isInitializedboolean检查应用组件是否已经初始化。CApplicationComponent
numberFormatarraythe format used to format a number with PHP number_format() function.CFormatter
timeFormatstringthe 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()计算一个PHP表达式,或根据组件上下文执行回调。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 instanceCFormatter
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 属性 只读 public CHtmlPurifier getHtmlPurifier()

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)
$namestringthe method name
$parametersarraymethod parameters
{return}mixedthe method return value
源码: framework/utils/CFormatter.php#85 (显示) publicfunction__call($name,$parameters)
{
if(method_exists($this,'format'.$name))
returncall_user_func_array(array($this,'format'.$name),$parameters);
else
returnparent::__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)
$valuemixedthe value to be formatted
$typestringthe 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}stringthe formatted data
源码: framework/utils/CFormatter.php#100 (显示) publicfunctionformat($value,$type)
{
$method='format'.$type;
if(method_exists($this,$method))
return$this->$method($value);
else
thrownewCException(Yii::t('yii','Unknowntype"{type}".',array('{type}'=>$type)));
}

Formats a value based on the given type.

formatBoolean() 方法
public string formatBoolean(mixed $value)
$valuemixedthe value to be formatted
{return}stringthe formatted result
源码: framework/utils/CFormatter.php#189 (显示) publicfunctionformatBoolean($value)
{
return$value?$this->booleanFormat[1]:$this->booleanFormat[0];
}

Formats the value as a boolean.

参见

  • booleanFormat
formatDate() 方法
public string formatDate(mixed $value)
$valuemixedthe value to be formatted
{return}stringthe formatted result
源码: framework/utils/CFormatter.php#156 (显示) publicfunctionformatDate($value)
{
returndate($this->dateFormat,$value);
}

Formats the value as a date.

参见

  • dateFormat
formatDatetime() 方法
public string formatDatetime(mixed $value)
$valuemixedthe value to be formatted
{return}stringthe formatted result
源码: framework/utils/CFormatter.php#178 (显示) publicfunctionformatDatetime($value)
{
returndate($this->datetimeFormat,$value);
}

Formats the value as a date and time.

参见

  • datetimeFormat
formatEmail() 方法
public string formatEmail(mixed $value)
$valuemixedthe value to be formatted
{return}stringthe formatted result
源码: framework/utils/CFormatter.php#199 (显示) publicfunctionformatEmail($value)
{
returnCHtml::mailto($value);
}

Formats the value as a mailto link.

formatHtml() 方法
public string formatHtml(mixed $value)
$valuemixedthe value to be formatted
{return}stringthe formatted result
源码: framework/utils/CFormatter.php#145 (显示) publicfunctionformatHtml($value)
{
return$this->getHtmlPurifier()->purify($value);
}

Formats the value as HTML text without any encoding.

formatImage() 方法
public string formatImage(mixed $value)
$valuemixedthe value to be formatted
{return}stringthe formatted result
源码: framework/utils/CFormatter.php#209 (显示) publicfunctionformatImage($value)
{
returnCHtml::image($value);
}

Formats the value as an image tag.

formatNtext() 方法
public string formatNtext(mixed $value)
$valuemixedthe value to be formatted
{return}stringthe formatted result
源码: framework/utils/CFormatter.php#135 (显示) publicfunctionformatNtext($value)
{
returnnl2br(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)
$valuemixedthe value to be formatted
{return}stringthe formatted result
源码: framework/utils/CFormatter.php#233 (显示) publicfunctionformatNumber($value)
{
returnnumber_format($value,$this->numberFormat['decimals'],$this->numberFormat['decimalSeparator'],$this->numberFormat['thousandSeparator']);
}

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

参见

  • numberFormat
formatRaw() 方法
public string formatRaw(mixed $value)
$valuemixedthe value to be formatted
{return}stringthe formatted result
源码: framework/utils/CFormatter.php#115 (显示) publicfunctionformatRaw($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)
$valuemixedthe value to be formatted
{return}stringthe formatted result
源码: framework/utils/CFormatter.php#125 (显示) publicfunctionformatText($value)
{
returnCHtml::encode($value);
}

Formats the value as a HTML-encoded plain text.

formatTime() 方法
public string formatTime(mixed $value)
$valuemixedthe value to be formatted
{return}stringthe formatted result
源码: framework/utils/CFormatter.php#167 (显示) publicfunctionformatTime($value)
{
returndate($this->timeFormat,$value);
}

Formats the value as a time.

参见

  • timeFormat
formatUrl() 方法
public string formatUrl(mixed $value)
$valuemixedthe value to be formatted
{return}stringthe formatted result
源码: framework/utils/CFormatter.php#219 (显示) publicfunctionformatUrl($value)
{
$url=$value;
if(strpos($url,'http://')!==0&&strpos($url,'https://')!==0)
$url='http://'.$url;
returnCHtml::link(CHtml::encode($value),$url);
}

Formats the value as a hyperlink.

getHtmlPurifier() 方法
public CHtmlPurifier getHtmlPurifier()
{return}CHtmlPurifierthe HTML purifier instance
源码: framework/utils/CFormatter.php#241 (显示) publicfunctiongetHtmlPurifier()
{
if($this->_htmlPurifier===null)
$this->_htmlPurifier=newCHtmlPurifier;
return$this->_htmlPurifier;
}