Overview

Packages

  • application
    • commands
    • components
      • actions
      • filters
      • leftWidget
      • permissions
      • sortableWidget
      • util
      • webupdater
      • x2flow
        • actions
        • triggers
      • X2GridView
      • X2Settings
    • controllers
    • models
      • embedded
    • modules
      • accounts
        • controllers
        • models
      • actions
        • controllers
        • models
      • calendar
        • controllers
        • models
      • charts
        • models
      • contacts
        • controllers
        • models
      • docs
        • components
        • controllers
        • models
      • groups
        • controllers
        • models
      • marketing
        • components
        • controllers
        • models
      • media
        • controllers
        • models
      • mobile
        • components
      • opportunities
        • controllers
        • models
      • products
        • controllers
        • models
      • quotes
        • controllers
        • models
      • services
        • controllers
        • models
      • template
        • models
      • users
        • controllers
        • models
      • workflow
        • controllers
        • models
      • x2Leads
        • controllers
        • models
  • Net
  • None
  • PHP
  • system
    • base
    • caching
      • dependencies
    • collections
    • console
    • db
      • ar
      • schema
        • cubrid
        • mssql
        • mysql
        • oci
        • pgsql
        • sqlite
    • i18n
      • gettext
    • logging
    • test
    • utils
    • validators
    • web
      • actions
      • auth
      • filters
      • form
      • helpers
      • renderers
      • services
      • widgets
        • captcha
        • pagers
  • Text
    • Highlighter
  • zii
    • behaviors
    • widgets
      • grid
      • jui

Classes

  • CDateTimeParser
  • CFileHelper
  • CFormatter
  • CLocalizedFormatter
  • CMarkdownParser
  • CPasswordHelper
  • CPropertyValue
  • CTimestamp
  • CVarDumper
  • Overview
  • Package
  • Class
  • Tree

Class CFormatter

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 CFormatter::$dateFormat, one may control how CFormatter::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 <span class="php-var">$formatter</span>-><span class="php-keyword2">date</span>(<span class="php-var">$value</span>) is equivalent to calling <span class="php-var">$formatter</span>->formatDate(<span class="php-var">$value</span>).

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 CFormatter::formatNtext() method will be called to format the attribute value as a HTML-encoded plain text with newlines converted as the HTML <br /> or <p></p> tags.
  • html: the attribute value will be purified and then returned.
  • date: the CFormatter::formatDate() method will be called to format the attribute value as a date.
  • time: the CFormatter::formatTime() method will be called to format the attribute value as a time.
  • datetime: the CFormatter::formatDatetime() method will be called to format the attribute value as a date with time.
  • boolean: the CFormatter::formatBoolean() method will be called to format the attribute value as a boolean display.
  • number: the CFormatter::formatNumber() method will be called to format the attribute value as a number display.
  • email: the CFormatter::formatEmail() method will be called to format the attribute value as a mailto link.
  • image: the CFormatter::formatImage() method will be called to format the attribute value as an image tag where the attribute value is the image URL.
  • url: the CFormatter::formatUrl() method will be called to format the attribute value as a hyperlink where the attribute value is the URL.
  • size: the CFormatter::formatSize() method will be called to format the attribute value, interpreted as a number of bytes, as a size in human readable form.

By default, CApplication registers CFormatter as an application component whose ID is 'format'. Therefore, one may call Yii::app()->format->boolean(<span class="php-num">1</span>). You might want to replace this component with CLocalizedFormatter to enable formatting based on the current locale settings.

CComponent
Extended by CApplicationComponent implements IApplicationComponent
Extended by CFormatter

Direct known subclasses

CLocalizedFormatter
Package: system\utils
Copyright: 2008-2013 Yii Software LLC
License: http://www.yiiframework.com/license/
Author: Qiang Xue <qiang.xue@gmail.com>
Since: 1.1.0
Located at x2engine/framework/utils/CFormatter.php
Methods summary
public mixed
# __call( string $name, array $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.

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

Parameters

$name
string
$name the method name
$parameters
array
$parameters method parameters

Returns

mixed
the method return value

Throws

CException
if current class and its behaviors do not have a method or closure with the given name

Overrides

CComponent::__call()
public string
# format( mixed $value, string $type )

Formats a value based on the given type.

Formats a value based on the given type.

Parameters

$value
mixed
$value the value to be formatted
$type
string
$type 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 CFormatter::formatText().

Returns

string
the formatted data

Throws

CException
if given type is unknown
public string
# formatRaw( mixed $value )

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

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

Parameters

$value
mixed
$value the value to be formatted

Returns

string
the formatted result
public string
# formatText( mixed $value )

Formats the value as a HTML-encoded plain text.

Formats the value as a HTML-encoded plain text.

Parameters

$value
mixed
$value the value to be formatted

Returns

string
the formatted result
public string
# formatNtext( mixed $value, boolean $paragraphs = false, boolean $removeEmptyParagraphs = true )

Formats the value as a HTML-encoded plain text and converts newlines with HTML <br /> or <p></p> tags.

Formats the value as a HTML-encoded plain text and converts newlines with HTML <br /> or <p></p> tags.

Parameters

$value
mixed
$value the value to be formatted
$paragraphs
boolean
$paragraphs whether newlines should be converted to HTML <p></p> tags, false by default meaning that HTML <br /> tags will be used
$removeEmptyParagraphs
boolean
$removeEmptyParagraphs whether empty paragraphs should be removed, defaults to true; makes sense only when $paragraphs parameter is true

Returns

string
the formatted result
public string
# formatHtml( mixed $value )

Formats the value as HTML text without any encoding.

Formats the value as HTML text without any encoding.

Parameters

$value
mixed
$value the value to be formatted

Returns

string
the formatted result
public string
# formatDate( mixed $value )

Formats the value as a date.

Formats the value as a date.

Parameters

$value
mixed
$value the value to be formatted

Returns

string
the formatted result

See

CFormatter::$dateFormat
public string
# formatTime( mixed $value )

Formats the value as a time.

Formats the value as a time.

Parameters

$value
mixed
$value the value to be formatted

Returns

string
the formatted result

See

CFormatter::$timeFormat
public string
# formatDatetime( mixed $value )

Formats the value as a date and time.

Formats the value as a date and time.

Parameters

$value
mixed
$value the value to be formatted

Returns

string
the formatted result

See

CFormatter::$datetimeFormat
protected integer
# normalizeDateValue( mixed $time )

Normalizes an expression as a timestamp.

Normalizes an expression as a timestamp.

Parameters

$time
mixed
$time the time expression to be normalized

Returns

integer
the normalized result as a UNIX timestamp
public string
# formatBoolean( mixed $value )

Formats the value as a boolean.

Formats the value as a boolean.

Parameters

$value
mixed
$value the value to be formatted

Returns

string
the formatted result

See

CFormatter::$booleanFormat
public string
# formatEmail( mixed $value )

Formats the value as a mailto link.

Formats the value as a mailto link.

Parameters

$value
mixed
$value the value to be formatted

Returns

string
the formatted result
public string
# formatImage( mixed $value )

Formats the value as an image tag.

Formats the value as an image tag.

Parameters

$value
mixed
$value the value to be formatted

Returns

string
the formatted result
public string
# formatUrl( mixed $value )

Formats the value as a hyperlink.

Formats the value as a hyperlink.

Parameters

$value
mixed
$value the value to be formatted

Returns

string
the formatted result
public string
# formatNumber( mixed $value )

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

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

Parameters

$value
mixed
$value the value to be formatted

Returns

string
the formatted result

See

CFormatter::$numberFormat
public CHtmlPurifier
# getHtmlPurifier( )

Returns

CHtmlPurifier
the HTML purifier instance
public string
# formatSize( integer $value, boolean $verbose = false )

Formats the value in bytes as a size in human readable form.

Formats the value in bytes as a size in human readable form.

Parameters

$value
integer
$value value in bytes to be formatted
$verbose
boolean
$verbose if full names should be used (e.g. bytes, kilobytes, ...). Defaults to false meaning that short names will be used (e.g. B, KB, ...).

Returns

string
the formatted result

Since

1.1.11

See

CFormatter::$sizeFormat
Methods inherited from CApplicationComponent
getIsInitialized(), init()
Methods inherited from CComponent
__get(), __isset(), __set(), __unset(), asa(), attachBehavior(), attachBehaviors(), attachEventHandler(), canGetProperty(), canSetProperty(), detachBehavior(), detachBehaviors(), detachEventHandler(), disableBehavior(), disableBehaviors(), enableBehavior(), enableBehaviors(), evaluateExpression(), getEventHandlers(), hasEvent(), hasEventHandler(), hasProperty(), raiseEvent()
Properties summary
public string $dateFormat 'Y/m/d'
#

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

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

public string $timeFormat 'h:i:s A'
#

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

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

public string $datetimeFormat 'Y/m/d h:i:s A'
#

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

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

public array $numberFormat array('decimals'=>null, 'decimalSeparator'=>null, 'thousandSeparator'=>null)
#

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.

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 array $booleanFormat array('No','Yes')
#

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 <span class="php-keyword1">array</span>(<span class="php-quote">'No'</span>, <span class="php-quote">'Yes'</span>).

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 <span class="php-keyword1">array</span>(<span class="php-quote">'No'</span>, <span class="php-quote">'Yes'</span>).

public array $htmlPurifierOptions array()
#

the options to be passed to CHtmlPurifier instance used in this class. CHtmlPurifier is used in CFormatter::formatHtml() method, so this property could be useful to customize HTML filtering behavior.

the options to be passed to CHtmlPurifier instance used in this class. CHtmlPurifier is used in CFormatter::formatHtml() method, so this property could be useful to customize HTML filtering behavior.

Since

1.1.13
public array $sizeFormat array( 'base'=>1024, 'decimals'=>2, 'decimalSeparator'=>null, )
#

the format used to format size (bytes). Three elements may be specified: "base", "decimals" and "decimalSeparator". They correspond to the base at which a kilobyte is calculated (1000 or 1024 bytes per kilobyte, defaults to 1024), the number of digits after the decimal point (defaults to 2) and the character displayed as the decimal point. "decimalSeparator" is available since version 1.1.13

the format used to format size (bytes). Three elements may be specified: "base", "decimals" and "decimalSeparator". They correspond to the base at which a kilobyte is calculated (1000 or 1024 bytes per kilobyte, defaults to 1024), the number of digits after the decimal point (defaults to 2) and the character displayed as the decimal point. "decimalSeparator" is available since version 1.1.13

Since

1.1.11
Properties inherited from CApplicationComponent
$behaviors
Magic properties summary
public CHtmlPurifier $htmlPurifier
#

The HTML purifier instance.

The HTML purifier instance.

Magic properties inherited from CApplicationComponent
$isInitialized
API documentation generated by ApiGen 2.8.0