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