smarty/plugins/modifier.debug_print_var.php
changeset 0 c63de9eb7045
equal deleted inserted replaced
-1:000000000000 0:c63de9eb7045
       
     1 <?php
       
     2 /**
       
     3  * Smarty plugin
       
     4  * @package Smarty
       
     5  * @subpackage plugins
       
     6  */
       
     7 
       
     8 
       
     9 /**
       
    10  * Smarty debug_print_var modifier plugin
       
    11  *
       
    12  * Type:     modifier<br>
       
    13  * Name:     debug_print_var<br>
       
    14  * Purpose:  formats variable contents for display in the console
       
    15  * @link http://smarty.php.net/manual/en/language.modifier.debug.print.var.php
       
    16  *          debug_print_var (Smarty online manual)
       
    17  * @author   Monte Ohrt <monte at ohrt dot com>
       
    18  * @param array|object
       
    19  * @param integer
       
    20  * @param integer
       
    21  * @return string
       
    22  */
       
    23 function smarty_modifier_debug_print_var($var, $depth = 0, $length = 40)
       
    24 {
       
    25     $_replace = array(
       
    26         "\n" => '<i>\n</i>',
       
    27         "\r" => '<i>\r</i>',
       
    28         "\t" => '<i>\t</i>'
       
    29     );
       
    30 
       
    31     switch (gettype($var)) {
       
    32         case 'array' :
       
    33             $results = '<b>Array (' . count($var) . ')</b>';
       
    34             foreach ($var as $curr_key => $curr_val) {
       
    35                 $results .= '<br>' . str_repeat('&nbsp;', $depth * 2)
       
    36                     . '<b>' . strtr($curr_key, $_replace) . '</b> =&gt; '
       
    37                     . smarty_modifier_debug_print_var($curr_val, ++$depth, $length);
       
    38                     $depth--;
       
    39             }
       
    40             break;
       
    41         case 'object' :
       
    42             $object_vars = get_object_vars($var);
       
    43             $results = '<b>' . get_class($var) . ' Object (' . count($object_vars) . ')</b>';
       
    44             foreach ($object_vars as $curr_key => $curr_val) {
       
    45                 $results .= '<br>' . str_repeat('&nbsp;', $depth * 2)
       
    46                     . '<b> -&gt;' . strtr($curr_key, $_replace) . '</b> = '
       
    47                     . smarty_modifier_debug_print_var($curr_val, ++$depth, $length);
       
    48                     $depth--;
       
    49             }
       
    50             break;
       
    51         case 'boolean' :
       
    52         case 'NULL' :
       
    53         case 'resource' :
       
    54             if (true === $var) {
       
    55                 $results = 'true';
       
    56             } elseif (false === $var) {
       
    57                 $results = 'false';
       
    58             } elseif (null === $var) {
       
    59                 $results = 'null';
       
    60             } else {
       
    61                 $results = htmlspecialchars((string) $var);
       
    62             }
       
    63             $results = '<i>' . $results . '</i>';
       
    64             break;
       
    65         case 'integer' :
       
    66         case 'float' :
       
    67             $results = htmlspecialchars((string) $var);
       
    68             break;
       
    69         case 'string' :
       
    70             $results = strtr($var, $_replace);
       
    71             if (strlen($var) > $length ) {
       
    72                 $results = substr($var, 0, $length - 3) . '...';
       
    73             }
       
    74             $results = htmlspecialchars('"' . $results . '"');
       
    75             break;
       
    76         case 'unknown type' :
       
    77         default :
       
    78             $results = strtr((string) $var, $_replace);
       
    79             if (strlen($results) > $length ) {
       
    80                 $results = substr($results, 0, $length - 3) . '...';
       
    81             }
       
    82             $results = htmlspecialchars($results);
       
    83     }
       
    84 
       
    85     return $results;
       
    86 }
       
    87 
       
    88 /* vim: set expandtab: */
       
    89 
       
    90 ?>