PageRenderTime 30ms CodeModel.GetById 13ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 1ms

/framework/vendor/smarty3/lib/libs/plugins/modifier.debug_print_var.php

http://zoop.googlecode.com/
PHP | 87 lines | 63 code | 4 blank | 20 comment | 5 complexity | b44185a75bdb3b074ec03c4e3dde9bab MD5 | raw file
 1<?php
 2/**
 3* Smarty plugin
 4* 
 5* @package Smarty
 6* @subpackage Debug
 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* 
16* @link http://smarty.php.net/manual/en/language.modifier.debug.print.var.php 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*/
23function smarty_modifier_debug_print_var ($var, $depth = 0, $length = 40)
24{
25    $_replace = array("\n" => '<i>\n</i>',
26        "\r" => '<i>\r</i>',
27        "\t" => '<i>\t</i>'
28        );
29
30    switch (gettype($var)) {
31        case 'array' :
32            $results = '<b>Array (' . count($var) . ')</b>';
33            foreach ($var as $curr_key => $curr_val) {
34                $results .= '<br>' . str_repeat('&nbsp;', $depth * 2)
35                 . '<b>' . strtr($curr_key, $_replace) . '</b> =&gt; '
36                 . smarty_modifier_debug_print_var($curr_val, ++$depth, $length);
37                $depth--;
38            } 
39            break;
40        case 'object' :
41            $object_vars = get_object_vars($var);
42            $results = '<b>' . get_class($var) . ' Object (' . count($object_vars) . ')</b>';
43            foreach ($object_vars as $curr_key => $curr_val) {
44                $results .= '<br>' . str_repeat('&nbsp;', $depth * 2)
45                 . '<b> -&gt;' . strtr($curr_key, $_replace) . '</b> = '
46                 . smarty_modifier_debug_print_var($curr_val, ++$depth, $length);
47                $depth--;
48            } 
49            break;
50        case 'boolean' :
51        case 'NULL' :
52        case 'resource' :
53            if (true === $var) {
54                $results = 'true';
55            } elseif (false === $var) {
56                $results = 'false';
57            } elseif (null === $var) {
58                $results = 'null';
59            } else {
60                $results = htmlspecialchars((string) $var);
61            } 
62            $results = '<i>' . $results . '</i>';
63            break;
64        case 'integer' :
65        case 'float' :
66            $results = htmlspecialchars((string) $var);
67            break;
68        case 'string' :
69            $results = strtr($var, $_replace);
70            if (strlen($var) > $length) {
71                $results = substr($var, 0, $length - 3) . '...';
72            } 
73            $results = htmlspecialchars('"' . $results . '"');
74            break;
75        case 'unknown type' :
76        default :
77            $results = strtr((string) $var, $_replace);
78            if (strlen($results) > $length) {
79                $results = substr($results, 0, $length - 3) . '...';
80            } 
81            $results = htmlspecialchars($results);
82    } 
83
84    return $results;
85} 
86
87?>