0 follower

CJavaScript

Package system.web.helpers
Inheritance class CJavaScript
Since 1.0
Source Code framework/web/helpers/CJavaScript.php
CJavaScript is a helper class containing JavaScript-related handling functions.

Public Methods

Hide inherited methods

MethodDescriptionDefined By
encode() Encodes a PHP variable into javascript representation. CJavaScript
jsonDecode() Decodes a JSON string. CJavaScript
jsonEncode() Returns the JSON representation of the PHP data. CJavaScript
quote() Quotes a javascript string. CJavaScript

Method Details

encode() method
public static string encode(mixed $value, boolean $safe=false)
$value mixed PHP variable to be encoded
$safe boolean If true, 'js:' will not be allowed. In case of wrapping code with CJavaScriptExpression JavaScript expression will stay as is no matter what value this parameter is set to. Default is false. This parameter is available since 1.1.11.
{return} string the encoded string
Source Code: framework/web/helpers/CJavaScript.php#63 (show)
public static function encode($value,$safe=false)
{
    if(
is_string($value))
    {
        if(
strpos($value,'js:')===&& $safe===false)
            return 
substr($value,3);
        else
            return 
"'".self::quote($value)."'";
    }
    elseif(
$value===null)
        return 
'null';
    elseif(
is_bool($value))
        return 
$value?'true':'false';
    elseif(
is_integer($value))
        return 
"$value";
    elseif(
is_float($value))
    {
        if(
$value===-INF)
            return 
'Number.NEGATIVE_INFINITY';
        elseif(
$value===INF)
            return 
'Number.POSITIVE_INFINITY';
        else
            return 
str_replace(',','.',(float)$value);  // locale-independent representation
    
}
    elseif(
$value instanceof CJavaScriptExpression)
        return 
$value->__toString();
    elseif(
is_object($value))
        return 
self::encode(get_object_vars($value),$safe);
    elseif(
is_array($value))
    {
        
$es=array();
        if((
$n=count($value))>&& array_keys($value)!==range(0,$n-1))
        {
            foreach(
$value as $k=>$v)
                
$es[]="'".self::quote($k)."':".self::encode($v,$safe);
            return 
'{'.implode(',',$es).'}';
        }
        else
        {
            foreach(
$value as $v)
                
$es[]=self::encode($v,$safe);
            return 
'['.implode(',',$es).']';
        }
    }
    else
        return 
'';
}

Encodes a PHP variable into javascript representation.

Example:

$options=array('key1'=>true,'key2'=>123,'key3'=>'value');
echo CJavaScript::encode($options);
// The following javascript code would be generated:
// {'key1':true,'key2':123,'key3':'value'}


For highly complex data structures use jsonEncode and jsonDecode to serialize and unserialize.

If you are encoding user input, make sure $safe is set to true.

jsonDecode() method
public static mixed jsonDecode(string $data, boolean $useArray=true)
$data string the data to be decoded
$useArray boolean whether to use associative array to represent object data
{return} mixed the decoded PHP data
Source Code: framework/web/helpers/CJavaScript.php#127 (show)
public static function jsonDecode($data,$useArray=true)
{
    return 
CJSON::decode($data,$useArray);
}

Decodes a JSON string.

jsonEncode() method
public static string jsonEncode(mixed $data)
$data mixed the data to be encoded
{return} string the JSON representation of the PHP data.
Source Code: framework/web/helpers/CJavaScript.php#116 (show)
public static function jsonEncode($data)
{
    return 
CJSON::encode($data);
}

Returns the JSON representation of the PHP data.

quote() method
public static string quote(string $js, boolean $forUrl=false)
$js string string to be quoted
$forUrl boolean whether this string is used as a URL
{return} string the quoted string
Source Code: framework/web/helpers/CJavaScript.php#28 (show)
public static function quote($js,$forUrl=false)
{
    
$js = (string)$js;

    
Yii::import('system.vendors.zend-escaper.Escaper');
    
$escaper=new Escaper(Yii::app()->charset);
    if(
$forUrl)
        return 
$escaper->escapeUrl($js);
    else
        return 
$escaper->escapeJs($js);
}

Quotes a javascript string. After processing, the string can be safely enclosed within a pair of quotation marks and serve as a javascript string.