PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/web/helpers/CJavaScript.php

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