CJavaScript.php 3.6 KB

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