1: <?php
2:
3: namespace PHPixie\Debug;
4:
5: class Dumper
6: {
7: public function shortDump($value)
8: {
9: return $this->dump($value, true);
10: }
11:
12: public function dump($value, $short = false)
13: {
14: if(is_string($value)) {
15: return $this->dumpString($value, $short);
16: }
17:
18: if(!$short) {
19: if($value instanceof Tracer\Trace) {
20: return $this->dumpTrace($value);
21: }
22:
23: if($value instanceof \Exception) {
24: return $this->dumpException($value);
25: }
26: }
27:
28: if(is_object($value)) {
29: return $this->dumpObject($value, $short);
30: }
31:
32: if(is_array($value)) {
33: return $this->dumpArray($value, $short);
34: }
35:
36: return $this->dumpScalar($value);
37: }
38:
39: protected function dumpString($value, $short)
40: {
41: if($short && strlen($value) > 15) {
42: $value = substr($value, 0, 12).'...';
43: }
44:
45: return "'$value'";
46: }
47:
48: protected function dumpObject($object, $short)
49: {
50: if($short) {
51: $class = get_class($object);
52: $class = explode('\\', $class);
53: return end($class);
54: }
55:
56: return print_r($object, true);
57: }
58:
59: protected function dumpArray($array, $short)
60: {
61: if($short) {
62: $count = count($array);
63: return "array[$count]";
64: }
65:
66: return print_r($array, true);
67: }
68:
69: protected function dumpTrace($trace)
70: {
71: return $trace->asString();
72: }
73:
74: protected function dumpException($exception)
75: {
76: $string = get_class($exception).": ";
77: $string.= $exception->getMessage();
78:
79: return $string;
80: }
81:
82: protected function dumpScalar($value)
83: {
84: return var_export($value, true);
85: }
86: }