OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / templates / art41_sample1 / library / Artx / Log / Formatter.php
1 <?php
2
3 class Artx_Log_Formatter
4 {
5     function args($args)
6     {
7         $result = array();
8         foreach ($args as $index => $value)
9             $result[] = Artx_Log_Formatter::_formatVar($value);
10         return implode(', ', $result);
11     }
12
13     function _formatVar(&$value)
14     {
15         if (is_null($value))
16             return Artx_Log_Formatter::_formatNull($value);
17         if (is_bool($value))
18             return Artx_Log_Formatter::_formatBool($value);
19         if (is_int($value) || is_float($value))
20             return $value;
21         if (is_object($value))
22             return Artx_Log_Formatter::_formatObject($value);
23         // is_callable should be placed before is_string and is_array:
24         if (is_callable($value))
25             return Artx_Log_Formatter::_formatCallback($value);
26         if (is_string($value))
27             return Artx_Log_Formatter::_formatString($value);
28         if (is_array($value))
29             return Artx_Log_Formatter::_formatArray($value);
30         return gettype($value) . ' { ... }';
31     }
32
33     function _formatNull()
34     {
35         return 'NULL';
36     }
37
38     function _formatString(&$value)
39     {
40         $text = $value;
41         if (strlen($text) > 10)
42             $text = substr($text, 0, 10) . '...';
43         $text = str_replace('\\', '\\\\', $text);
44         $text = str_replace('\'', '\\\'', $text);
45         return '\'' . $text . '\'';
46     }
47
48     function _formatArray(&$value)
49     {
50         return 'array(' . count($value) . ') { ... }';
51     }
52
53     function _formatObject(&$value)
54     {
55         return get_class($value) . '(...) { ... }';
56     }
57
58     function _formatBool(&$value)
59     {
60         return $value ? 'true' : 'false';
61     }
62     
63     function _formatCallback(&$value)
64     {
65         if (is_string($value))
66             return $value . '(...)';
67         if (is_array($value))
68             return (is_string($value[0]) ? ($value[0] . '::') : (get_class($value[0]) . '->')) . $value[1];
69         return '#internal error';
70     }
71 }