home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTML / Javascript / Convert.php
Encoding:
PHP Script  |  2008-07-02  |  12.1 KB  |  381 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Tal Peer <tal@php.net>                                      |
  17. // |          Pierre-Alain Joye <paj@pearfr.org>                          |
  18. // +----------------------------------------------------------------------+
  19. // $Id: Convert.php,v 1.15 2005/08/18 08:55:07 alan_k Exp $
  20. /**
  21.  * A class for converting PHP variables into JavaScript variables
  22.  *
  23.  * Usage example:
  24.  * <code>
  25.  * $js = new HTML_Javascript_Convert()
  26.  * $a = array('foo','bar','buz',1,2,3);
  27.  * $b = $js->convertVar($a, 'arr', true);
  28.  * </code>
  29.  * or
  30.  * <code>
  31.  * echo HTML_Javascript_Convert::convertArray($a);
  32.  * </code>
  33.  *
  34.  * @author Tal Peer <tal@php.net>
  35.  * @author Pierre-Alain Joye <paj@pearfr.org>
  36.  * @package HTML_Javascript
  37.  * @subpackage Convert
  38.  * @version 1.1.1
  39.  * @licence http://www.php.net/license/3_0.txt PHP Licence 3.0
  40.  * @access public
  41.  * @example examples/js.php How to use Convert
  42.  */
  43.  
  44. /**
  45.  * Invalid variable error
  46.  */
  47. define('HTML_JAVASCRIPT_CONVERT_ERROR_INVVAR', 502, true);
  48.  
  49. if(!defined('HTML_JAVASCRIPT_NL')){
  50.     /** Linefeed to use, default set to Unix linefeed.
  51.      * Define it before the include/require if you want to
  52.      * override it.
  53.      */
  54.     define('HTML_JAVASCRIPT_NL',"\n");
  55. }
  56.  
  57. /**
  58.  * PHP to Javascript conversion classes
  59.  *
  60.  * It converts Variable or value to Javascript.
  61.  *
  62.  * @package HTML_Javascript
  63.  * @subpackage Convert
  64.  */
  65. class HTML_Javascript_Convert
  66. {
  67.     // {{{ escapeString
  68.  
  69.     /**
  70.      * Used to terminate escape characters in strings,
  71.      * as javascript doesn't allow them
  72.      *
  73.      * @param   string  the string to be processed
  74.      * @return  mixed   the processed string
  75.      *
  76.      * @access public
  77.      * @source
  78.      */
  79.     function escapeString($str)
  80.     {
  81.         $js_escape = array(
  82.             "\r"    => '\r',
  83.             "\n"    => '\n',
  84.             "\t"    => '\t',
  85.             "'"     => "\\'",
  86.             '"'     => '\"',
  87.             '\\' => '\\\\'
  88.         );
  89.  
  90.         return strtr($str,$js_escape);
  91.     }
  92.  
  93.     // }}} escapeString
  94.     // {{{ convertVar
  95.  
  96.     /**
  97.      * Converts  a PHP variable into a JS variable
  98.      * you can safely provide strings, arrays
  99.      * or booleans as arguments for this function
  100.      *
  101.      * @access public
  102.      * @param  mixed   $var     the variable to convert
  103.      * @param  string  $varname the variable name to declare
  104.      * @param  boolean $global  if true, the JS var will be global
  105.      * @return mixed   a PEAR_Error if no script was started
  106.      *                 or the converted variable
  107.      */
  108.     function convertVar($var, $varname, $global = false)
  109.     {
  110.         $var_type    = gettype($var);
  111.         switch ( $var_type ) {
  112.             case 'boolean':
  113.                 return HTML_Javascript_Convert::convertBoolean(
  114.                             $var, $varname, $global
  115.                         );
  116.                         
  117.             case 'integer':
  118.             case 'double':
  119.                 $ret = '';
  120.                 if ($global) {
  121.                     $ret = 'var ';
  122.                 }
  123.                 $ret .= $varname.' = '.$var;
  124.                 return $ret.';'.HTML_JAVASCRIPT_NL;
  125.                 
  126.             case 'string':
  127.                 return HTML_Javascript_Convert::convertString(
  128.                             $var, $varname, $global
  129.                         );
  130.                         
  131.             case 'array':
  132.                 return HTML_Javascript_Convert::convertArray(
  133.                             $var, $varname, $global
  134.                         );
  135.                         
  136.             case 'NULL':
  137.                 return HTML_Javascript_Convert::convertNull(
  138.                             $varname, $global
  139.                         );
  140.                         
  141.             default:
  142.                 return HTML_Javascript_Convert::raiseError(
  143.                         HTML_JAVASCRIPT_ERROR_CONVERT_INVVAR, __FUNCTION__.':'.$var_type
  144.                     );
  145.         }
  146.     }
  147.  
  148.     // }}} convertVar
  149.     // {{{ raiseError
  150.  
  151.     /**
  152.      * A custom error handler
  153.      *
  154.      * @access public
  155.      * @param  integer $code the error code
  156.      * @return mixed   false if the error code is invalid,
  157.      *                 or a PEAR_Error otherwise
  158.      */
  159.     function raiseError($code,$str='')
  160.     {
  161.         require_once 'PEAR.php';
  162.         switch ($code) {
  163.             case HTML_JAVASCRIPT_CONVERT_ERROR_INVVAR:
  164.                 return PEAR::raiseError(
  165.                     'Invalid variable:'.$str, $code 
  166.                 );
  167.                 
  168.             default:
  169.                 return PEAR::raiseError(
  170.                     'Unknown Error:'.$str, $code 
  171.                 );
  172.         }
  173.     }
  174.  
  175.     // }}} raiseError
  176.     // {{{ convertString
  177.  
  178.     /**
  179.      * Converts  a PHP string into a JS string
  180.      *
  181.      * @access public
  182.      * @param  string  $str     the string to convert
  183.      * @param  string  $varname the variable name to declare
  184.      * @param  boolean $global  if true, the JS var will be global
  185.      * @return mixed   a PEAR_Error if no script was started
  186.      *                 or the converted string
  187.      */
  188.     function convertString($str, $varname, $global = false)
  189.     {
  190.         $var = '';
  191.         if ($global) {
  192.             $var = 'var ';
  193.         }
  194.         $str = HTML_Javascript_Convert::escapeString($str);
  195.         $var .= $varname.' = "'.$str.'"';
  196.         return $var.';'.HTML_JAVASCRIPT_NL;;
  197.     }
  198.  
  199.     // }}} convertString
  200.     // {{{ convertBoolean
  201.  
  202.     /**
  203.      * Converts a PHP boolean variable into a JS boolean variable.
  204.      * Note this function does not check the type of $bool, only if
  205.      * the expression $bool is true or false.
  206.      *
  207.      * @access public
  208.      * @param  boolean $bool    the boolean variable
  209.      * @param  string  $varname the variable name to declare
  210.      * @param  boolean $global  set to true to make the JS variable global
  211.      * @return string  the value as javascript 
  212.      */
  213.     function convertBoolean($bool, $varname, $global = false)
  214.     {
  215.         $var = '';
  216.         if ($global) {
  217.             $var = 'var ';
  218.         }
  219.         $var    .= $varname.' = ';
  220.         $var    .= $bool?'true':'false';
  221.         return $var.';'.HTML_JAVASCRIPT_NL;
  222.     }
  223.  
  224.     // }}} convertBoolean
  225.     // {{{ convertNull
  226.  
  227.     /**
  228.      * Converts a PHP null variable into a JS null value.
  229.      *
  230.      * @access public
  231.      * @param  string  $varname the variable name to declare
  232.      * @param  boolean $global  set to true to make the JS variable global
  233.      * @return string  the value as javascript 
  234.      */
  235.     function convertNull($varname, $global = false)
  236.     {
  237.         $var = '';
  238.         if($global) {
  239.             $var = 'var ';
  240.         }
  241.         return $varname.' = null;'.HTML_JAVASCRIPT_NL;
  242.     }
  243.  
  244.     // }}} convertNull
  245.     
  246.     // {{{ convertArray
  247.  
  248.     /**
  249.      * Converts  a PHP array into a JS array
  250.      * supports of multu-dimensional array.
  251.      * Keeps keys as they are (associative arrays).
  252.      *
  253.      * @access public
  254.      * @param  string  $arr     the array to convert
  255.      * @param  string  $varname the variable name to declare
  256.      * @param  boolean $global  if true, the JS var will be global
  257.      * @param  int     $level   Not public, used for recursive calls
  258.      * @return mixed   a PEAR_Error if no script was started
  259.      *                 or the converted array
  260.      */
  261.     function convertArray($arr, $varname, $global = false, $level=0)
  262.     {
  263.         $var = '';
  264.         if ($global) {
  265.             $var = 'var ';
  266.         }
  267.         if ( is_array($arr) ){
  268.             $length = sizeof($arr);
  269.             $var    .= $varname . ' = Array('. $length .')'.HTML_JAVASCRIPT_NL;
  270.             foreach ( $arr as $key=>$cell ){
  271.                 $jskey  = '"' . $key . '"';
  272.                 if ( is_array( $cell ) ){
  273.                     $level++;
  274.                     $var    .= HTML_Javascript_Convert::convertArray(
  275.                                     $cell,'tmp'.$level,$global,$level
  276.                                 );
  277.                     $var    .= $varname .
  278.                                 "[$jskey] = tmp$level".
  279.                                 HTML_JAVASCRIPT_NL;
  280.                     $var    .= "tmp$level = null".HTML_JAVASCRIPT_NL;
  281.                 } else {
  282.                     $value  = is_string($cell)?
  283.                                 '"' .
  284.                                 HTML_Javascript_Convert::escapeString($cell) .
  285.                                 '"'
  286.                                 :$cell;
  287.                     $var    .= $varname . "[$jskey] = $value".
  288.                                 HTML_JAVASCRIPT_NL;
  289.                 }
  290.             }
  291.             return $var;
  292.         } else {
  293.             return HTML_Javascript_Convert::raiseError(
  294.                         HTML_JAVASCRIPT_CONVERT_ERROR_INVVAR, __FUNCTION__.':'.gettype($arr)
  295.                     );
  296.         }
  297.     }
  298.  
  299.     // }}} convertArray
  300.     // {{{ convertArrayToProperties
  301.  
  302.     /**
  303.      * Converts a PHP array into a JS object
  304.      * supports of multu-dimensional array.
  305.      * Keeps keys as they are (associative arrays).
  306.      *
  307.      * @access public
  308.      * @param  string  $arr     the array to convert
  309.      * @param  string  $varname the variable name to declare
  310.      * @param  boolean $new     if true, the JS var will be set
  311.      * @return mixed   a PEAR_Error or the converted array
  312.      */
  313.     function convertArrayToProperties( $array, $varname, $global=false, $new=true )
  314.     {
  315.         if(is_array($array)){
  316.             $cnt = sizeof($array)-1;
  317.             $i  = 0;
  318.             $convert = $global?'var ':'';
  319.             $convert .= $new?$varname.'={'.HTML_JAVASCRIPT_NL:'{';
  320.             foreach( $array as $key=>$val) {
  321.                 $key = $key?'"'.$key.'"':"'0'";
  322.                 $convert .= $key.':';
  323.                 if(is_array($val)){
  324.                     $convert .= HTML_Javascript_Convert::convertArrayToProperties($val,'',false, false);
  325.                     $convert .= $i++<$cnt?','.HTML_JAVASCRIPT_NL:'';
  326.                 } else {
  327.                     $convert .= HTML_Javascript_Convert::convertValue($val);
  328.                     $convert .= $i++<$cnt?',':'';
  329.                 }
  330.             }
  331.             $convert .= HTML_JAVASCRIPT_NL.'}';
  332.         }
  333.         if($new){
  334.             $convert .= ';';
  335.         }
  336.         return $convert;
  337.     }
  338.  
  339.     // }}} convertArrayToProperties
  340.     // {{{convertValue
  341.  
  342.     /**
  343.      * Converts a variable value to its javascript equivalent.
  344.      * String variables are escaped (see {@link escapeString()}).
  345.      *
  346.      * @param  string  $param coment
  347.      * @access public
  348.      * @return mixed return
  349.      */
  350.     function convertValue( $val )
  351.     {
  352.         switch ( gettype($val) ) {
  353.             case 'boolean':
  354.                 return $val ? 'true' : 'false';
  355.                 
  356.             case 'integer':
  357.             case 'double':
  358.                 return $val;
  359.                 
  360.             case 'string':
  361.                 return "'".HTML_Javascript_Convert::escapeString($val)."'";
  362.                 
  363.             case 'array':
  364.                 return HTML_Javascript_Convert::convertArray(
  365.                             $val, $varname, $global
  366.                         );
  367.                         
  368.             case 'NULL':
  369.                 return 'null';
  370.                 
  371.             default:
  372.                 return HTML_Javascript_Convert::raiseError(
  373.                         HTML_JAVASCRIPT_ERROR_CONVERT_INVVAR, __FUNCTION__.':'.gettype($val)
  374.                     );
  375.         }
  376.     }
  377.  
  378.     // }}} convertValue
  379.  
  380. }
  381.