~horux-dev/horux-webcli/thfo

« back to all changes in this revision

Viewing changes to yii/framework/web/helpers/CJavaScript.php

  • Committer: Thierry Forchelet
  • Date: 2011-02-25 13:30:15 UTC
  • Revision ID: thierry.forchelet@letux.ch-20110225133015-zxyj9w7sqv8ly971
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * CJavaScript helper class file.
 
4
 *
 
5
 * @author Qiang Xue <qiang.xue@gmail.com>
 
6
 * @link http://www.yiiframework.com/
 
7
 * @copyright Copyright &copy; 2008-2011 Yii Software LLC
 
8
 * @license http://www.yiiframework.com/license/
 
9
 */
 
10
 
 
11
/**
 
12
 * CJavaScript is a helper class containing JavaScript-related handling functions.
 
13
 *
 
14
 * @author Qiang Xue <qiang.xue@gmail.com>
 
15
 * @version $Id: CJavaScript.php 2799 2011-01-01 19:31:13Z qiang.xue $
 
16
 * @package system.web.helpers
 
17
 * @since 1.0
 
18
 */
 
19
class CJavaScript
 
20
{
 
21
        /**
 
22
         * Quotes a javascript string.
 
23
         * After processing, the string can be safely enclosed within a pair of
 
24
         * quotation marks and serve as a javascript string.
 
25
         * @param string $js string to be quoted
 
26
         * @param boolean $forUrl whether this string is used as a URL
 
27
         * @return string the quoted string
 
28
         */
 
29
        public static function quote($js,$forUrl=false)
 
30
        {
 
31
                if($forUrl)
 
32
                        return strtr($js,array('%'=>'%25',"\t"=>'\t',"\n"=>'\n',"\r"=>'\r','"'=>'\"','\''=>'\\\'','\\'=>'\\\\','</'=>'<\/'));
 
33
                else
 
34
                        return strtr($js,array("\t"=>'\t',"\n"=>'\n',"\r"=>'\r','"'=>'\"','\''=>'\\\'','\\'=>'\\\\','</'=>'<\/'));
 
35
        }
 
36
 
 
37
        /**
 
38
         * Encodes a PHP variable into javascript representation.
 
39
         *
 
40
         * Example:
 
41
         * <pre>
 
42
         * $options=array('key1'=>true,'key2'=>123,'key3'=>'value');
 
43
         * echo CJavaScript::encode($options);
 
44
         * // The following javascript code would be generated:
 
45
         * // {'key1':true,'key2':123,'key3':'value'}
 
46
         * </pre>
 
47
         *
 
48
         * For highly complex data structures use {@link jsonEncode} and {@link jsonDecode}
 
49
         * to serialize and unserialize.
 
50
         *
 
51
         * @param mixed $value PHP variable to be encoded
 
52
         * @return string the encoded string
 
53
         */
 
54
        public static function encode($value)
 
55
        {
 
56
                if(is_string($value))
 
57
                {
 
58
                        if(strpos($value,'js:')===0)
 
59
                                return substr($value,3);
 
60
                        else
 
61
                                return "'".self::quote($value)."'";
 
62
                }
 
63
                else if($value===null)
 
64
                        return 'null';
 
65
                else if(is_bool($value))
 
66
                        return $value?'true':'false';
 
67
                else if(is_integer($value))
 
68
                        return "$value";
 
69
                else if(is_float($value))
 
70
                {
 
71
                        if($value===-INF)
 
72
                                return 'Number.NEGATIVE_INFINITY';
 
73
                        else if($value===INF)
 
74
                                return 'Number.POSITIVE_INFINITY';
 
75
                        else
 
76
                                return rtrim(sprintf('%.16F',$value),'0');  // locale-independent representation
 
77
                }
 
78
                else if(is_object($value))
 
79
                        return self::encode(get_object_vars($value));
 
80
                else if(is_array($value))
 
81
                {
 
82
                        $es=array();
 
83
                        if(($n=count($value))>0 && array_keys($value)!==range(0,$n-1))
 
84
                        {
 
85
                                foreach($value as $k=>$v)
 
86
                                        $es[]="'".self::quote($k)."':".self::encode($v);
 
87
                                return '{'.implode(',',$es).'}';
 
88
                        }
 
89
                        else
 
90
                        {
 
91
                                foreach($value as $v)
 
92
                                        $es[]=self::encode($v);
 
93
                                return '['.implode(',',$es).']';
 
94
                        }
 
95
                }
 
96
                else
 
97
                        return '';
 
98
        }
 
99
 
 
100
        /**
 
101
         * Returns the JSON representation of the PHP data.
 
102
         * @param mixed $data the data to be encoded
 
103
         * @return string the JSON representation of the PHP data.
 
104
         */
 
105
        public static function jsonEncode($data)
 
106
        {
 
107
                return CJSON::encode($data);
 
108
        }
 
109
 
 
110
        /**
 
111
         * Decodes a JSON string.
 
112
         * @param string $data the data to be decoded
 
113
         * @param boolean $useArray whether to use associative array to represent object data
 
114
         * @return mixed the decoded PHP data
 
115
         */
 
116
        public static function jsonDecode($data,$useArray=true)
 
117
        {
 
118
                return CJSON::decode($data,$useArray);
 
119
        }
 
120
}