Completed
Push — master ( c90dfa...c2f681 )
by Juliette
23s
created

xvardump.php ➔ object_info()   D

Complexity

Conditions 9
Paths 8

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 9
eloc 19
c 3
b 0
f 0
nc 8
nop 4
dl 0
loc 27
rs 4.909
1
<?php
2
/**
3
 * File: xvardump.php
4
 *
5
 * Group of functions for variable visualisation purposes.
6
 *
7
 * The main function pr_var() can be used as a color-coded alternative for var_dump().
8
 *
9
 * A couple of 'shortcut' functions are provided for known variable types.
10
 * Only use those if you know what you are receiving and don't need variable typing info.
11
 *
12
 * @package xvardump
13
 * @author	Juliette Reinders Folmer, {@link http://www.adviesenzo.nl/ Advies en zo} -
14
 *  <[email protected]>
15
 *
16
 * @version   1.7
17
 * @since     2009-09-30 // Last changed: by Juliette Reinders Folmer
18
 * @copyright Advies en zo, Meedenken en -doen �2005-2009
19
 * @license   http://www.opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
20
 * @license   http://opensource.org/licenses/academic Academic Free License Version 1.2
21
 * @example   example/example.php
22
 */
23
24
// Prevent direct calls to this file.
25
if ( ! defined( 'APP_DIR' ) ) {
26
	header( 'Status: 403 Forbidden' );
27
	header( 'HTTP/1.1 403 Forbidden' );
28
	exit();
29
}
30
31
32
define( 'XVARDUMP_SPACE_LONG',     '&nbsp;&nbsp;&nbsp;&nbsp;' );
33
define( 'XVARDUMP_SPACE_SHORT',    '&nbsp;&nbsp;' );
34
35
define( 'XVARDUMP_CLASS_STRING',   'vt-string' );
36
define( 'XVARDUMP_CLASS_INT',      'vt-int' );
37
define( 'XVARDUMP_CLASS_INT_0',    'vt-int-0' );
38
define( 'XVARDUMP_CLASS_FLOAT',    'vt-float' );
39
define( 'XVARDUMP_CLASS_BOOL',     'vt-bool' );
40
define( 'XVARDUMP_CLASS_B_TRUE',   'vt-b-true' );
41
define( 'XVARDUMP_CLASS_B_FALSE',  'vt-b-false' );
42
define( 'XVARDUMP_CLASS_RESOURCE', 'vt-resource' );
43
define( 'XVARDUMP_CLASS_NULL',     'vt-null' );
44
45
46
/**
47
 * Retrieve the debug info and value of a variable into a string.
48
 *
49
 * @param mixed  $var
50
 * @param string $title
51
 * @param bool   $escape
52
 * @param bool   $short
53
 * @param string $space
54
 *
55
 * @uses pr_var()
56
 * @see pr_var()
57
 *
58
 * @return string  debug info on the variable $var
59
 */
60
function get_var( $var, $title = '', $escape = true, $short = false, $space = '' ) {
61
	ob_start();
62
	pr_var( $var, $title, $escape, $short, $space );
63
	$debug_info = ob_get_clean();
64
	return $debug_info;
65
}
66
67
68
/**
69
 * Prints color-coded debug info and value of a variable to the screen.
70
 *
71
 * If you like, you can customize the color-coding used by changing the
72
 * values of the associated CONSTANTS at the top of this file.
73
 *
74
 * @param mixed  $var      The variable to print the debug info for.
75
 * @param string $title    (optional) If set, prefaces the debug info with
76
 *                         a header containing this title.
77
 *                         Useful if you want to print several states of the
78
 *                         same variable and you want to keep track of which state
79
 *                         you are at.
80
 * @param bool   $escape   (optional) Whether or not to escape html entities in
81
 *                         the $var.
82
 *                         Useful if the $var contains html and you want to see
83
 *                         the source value.
84
 *                         Defaults to true.
85
 * @param bool   $short    (optional) Whether to limit the debug info to color coding.
86
 *                         Defaults to false.
87
 * @param string $space    (optional) Internal variable needed to create the proper
88
 *                         spacing for display of arrays and objects.
89
 * @param bool   $in_array (optional) Internal pointer for whether or not to use
90
 *                         breaks when using short annotation which would give issues
91
 *                         when displaying arrays.
92
 *
93
 * @uses object_info()
94
 */
95
function pr_var( $var, $title = '', $escape = true, $short = false, $space = '', $in_array = false ) {
96
97
	if ( is_string( $title ) && $title !== '' ) {
98
		echo '<h4 style="clear: both;">', ( ( $escape === true ) ? htmlentities( $title, ENT_QUOTES ) : $title ), "</h4>\n";
99
	}
100
101
	if ( is_array( $var ) ) {
102
		if ( $var !== array() ) {
103
			echo 'Array: ', $space, "(<br />\n";
104
			$spacing = ( ( $short !== true ) ? $space . XVARDUMP_SPACE_LONG : $space . XVARDUMP_SPACE_SHORT );
105
			foreach ( $var as $key => $value ) {
106
				echo $spacing, '[', ( ( $escape === true ) ? htmlentities( $key, ENT_QUOTES ) : $key );
107
				if ( $short !== true ) {
108
					echo ' ';
109
					switch ( true ) {
110
						case ( is_string( $key ) ):
111
							echo '<b class="', XVARDUMP_CLASS_STRING, '"><i>(string)</i></b>';
112
							break;
113
114
						case ( is_int( $key ) ):
115
							echo '<b class="', XVARDUMP_CLASS_INT, '"><i>(int)</i></b>';
116
							break;
117
118
						case ( is_float( $key ) ):
119
							echo '<b class="', XVARDUMP_CLASS_FLOAT, '"><i>(float)</i></b>';
120
							break;
121
122
						default:
123
							echo '(unknown)';
124
							break;
125
					}
126
				}
127
				echo '] => ';
128
				pr_var( $value, '', $escape, $short, $spacing, true );
129
			}
130
			echo $space, ")<br />\n\n";
131
		}
132
		else {
133
			echo 'array()<br />';
134
		}
135
	}
136
	else if ( is_string( $var ) ) {
137
		echo '<span class="', XVARDUMP_CLASS_STRING, '">';
138
		if ( $short !== true ) {
139
			echo '<b><i>string[', strlen( $var ), ']</i></b> : ';
140
		}
141
		echo '&lsquo;',
142
			( ( $escape === true ) ? str_replace( '  ', ' &nbsp;', htmlentities( $var, ENT_QUOTES, 'UTF-8' ) ) : str_replace( '  ', ' &nbsp;', $var ) ),
143
			"&rsquo;</span><br />\n";
144
	}
145
	else if ( is_bool( $var ) ) {
146
		if ( $short !== true ) {
147
			echo '<span class="', XVARDUMP_CLASS_BOOL, '"><b><i>bool</i></b> : ', $var, ' ( = ';
148
		}
149
		else {
150
			echo '<b class="', XVARDUMP_CLASS_BOOL, '"><i>b</i></b> ';
151
		}
152 View Code Duplication
		if ( $var === false ) {
153
			echo '<i class="', XVARDUMP_CLASS_B_FALSE, '">false</i>';
154
		}
155
		else if ( $var === true ) {
156
			echo '<i class="', XVARDUMP_CLASS_B_TRUE, '">true</i>';
157
		}
158
		else {
159
			echo '<i>undetermined</i>';
160
		}
161
		if ( $short !== true ) {
162
			echo ' )</span>';
163
		}
164
		echo "<br />\n";
165
	}
166
	else if ( is_int( $var ) ) {
167
		echo '<span class="', XVARDUMP_CLASS_INT, '">';
168
		if ( $short !== true ) {
169
			echo '<b><i>int</i></b> : ';
170
		}
171
		if ( $var === 0 ) {
172
			echo '<span class="', XVARDUMP_CLASS_INT_0, '">', $var, '</span>';
173
		}
174
		else {
175
			echo $var;
176
		}
177
		echo "</span><br />\n";
178
	}
179
	else if ( is_float( $var ) ) {
180
		echo '<span class="', XVARDUMP_CLASS_FLOAT, '">';
181
		if ( $short !== true ) {
182
			echo '<b><i>float</i></b> : ';
183
		}
184
		echo $var, "</span><br />\n";
185
	}
186
	else if ( is_null( $var ) ) {
187
		echo '<span class="', XVARDUMP_CLASS_NULL, '">';
188
		if ( $short !== true ) {
189
			echo '<b><i>';
190
		}
191
		echo 'null';
192
		if ( $short !== true ) {
193
			echo '</i></b> : ', $var, ' ( = <i>NULL</i> )', "</span><br />\n";
194
		}
195
		else if ( $in_array === true ) {
196
			echo "</span><br />\n";
197
		}
198
		else {
199
			echo "</span>\n";
200
		}
201
	}
202
	else if ( is_resource( $var ) ) {
203
		echo '<span class="', XVARDUMP_CLASS_RESOURCE, '">';
204
		if ( $short !== true ) {
205
			echo '<b><i>resource</i></b> : ';
206
		}
207
		echo $var;
208
		if ( $short !== true ) {
209
			echo ' ( = <i>RESOURCE</i> )';
210
		}
211
		echo "</span><br />\n";
212
	}
213
	else if ( is_object( $var ) ) {
214
		echo "Object: \n", $space, "(<br />\n";
215
		$spacing = ( ( $short !== true ) ? $space . XVARDUMP_SPACE_LONG : $space . XVARDUMP_SPACE_SHORT );
216
		object_info( $var, $escape, $short, $spacing );
217
		echo $space, ")<br />\n\n";
218
	}
219
	else {
220
		echo 'I haven&#39;t got a clue what this is: ', gettype( $var ), "<br />\n";
221
	}
222
}
223
224
225
/**
226
 * Internal function to print debug info on an object.
227
 *
228
 * @param object $obj    Object to print debug info on.
229
 * @param bool   $escape @see pr_var().
230
 * @param bool   $short  @see pr_var().
231
 * @param string $space  @see pr_var().
232
 *
233
 * @internal
234
 * @uses pr_var()
235
 */
236
function object_info( $obj, $escape, $short, $space ) {
237
	echo $space, '<b><i>Class</i></b>: ', get_class( $obj ), " (<br />\n";
238
	$spacing    = ( ( $short !== true ) ? $space . XVARDUMP_SPACE_LONG : $space . XVARDUMP_SPACE_SHORT );
239
	$properties = get_object_vars( $obj );
240
	if ( is_array( $properties ) && $properties !== array() ) {
241
		foreach ( $properties as $var => $val ) {
242
			if ( is_array( $val ) ) {
243
				echo $spacing, '<b><i>property</i></b>: ', $var, "<b><i> (array)</i></b>\n";
244
				pr_var( $val, '', $escape, $short, $spacing );
245
			}
246
			else {
247
				echo $spacing, '<b><i>property</i></b>: ', $var, ' = ';
248
				pr_var( $val, '', $escape, $short, $spacing );
249
			}
250
		}
251
	}
252
	unset( $properties, $var, $val );
253
254
	$methods = get_class_methods( $obj );
255
	if ( is_array( $methods ) && $methods !== array() ) {
256
		foreach ( $methods as $method ) {
257
			echo $spacing, '<b><i>method</i></b>: ', $method, "<br />\n";
258
		}
259
	}
260
	unset( $methods, $method );
261
	echo $space, ")<br />\n\n";
262
}
263
264
265
/**
266
 * Function to dump all defined variables.
267
 *
268
 * @uses	pr_var()
269
 */
270
function dump_all() {
271
	$var = get_defined_vars();
272
	pr_var( $var, 'Dump of all defined variables' );
273
}
274
275
276
/**
277
 * Alias for pr_str().
278
 *
279
 * @param string $var
280
 */
281
function pr_string( $var ) {
282
	pr_str( $var );
283
}
284
285
286
/**
287
 * Alias for pr_bool().
288
 *
289
 * @param bool $var
290
 */
291
function pr_boolean( $var ) {
292
	pr_bool( $var );
293
}
294
295
296
/**
297
 * Alias for pr_int().
298
 *
299
 * @param int $var
300
 */
301
function pr_integer( $var ) {
302
	pr_int( $var );
303
}
304
305
306
/**
307
 * Alias for pr_flt().
308
 *
309
 * @param float $var
310
 */
311
function pr_float( $var ) {
312
	pr_flt( $var );
313
}
314
315
316
/**
317
 * Shortcut function to print a string variable.
318
 *
319
 * @param string $var
320
 */
321
function pr_str( $var ) {
322
	if ( is_string( $var ) ) {
323
		echo '<span class="', XVARDUMP_CLASS_STRING, '">&lsquo;', str_replace( '  ', ' &nbsp;', $var ), "&rsquo;</span>\n";
324
	}
325
	else {
326
		echo 'E: not a string';
327
	}
328
}
329
330
331
/**
332
 * Shortcut function to print a boolean variable.
333
 *
334
 * @param bool $var
335
 */
336
function pr_bool( $var ) {
337
	if ( is_bool( $var ) ) {
338 View Code Duplication
		if ( $var === false ) {
339
			echo '<span class="', XVARDUMP_CLASS_B_FALSE, '">false', "</span>\n";
340
		}
341
		else if ( $var === true ) {
342
			echo '<span class="', XVARDUMP_CLASS_B_TRUE, '">true', "</span>\n";
343
		}
344
		else {
345
			echo 'E: boolean value undetermined';
346
		}
347
	}
348
	else {
349
		echo 'E: not boolean';
350
	}
351
}
352
353
354
/**
355
 * Shortcut function to print an integer variable.
356
 *
357
 * Will print 0 value in red, other values in blue.
358
 *
359
 * @param int $var
360
 */
361
function pr_int( $var ) {
362
	if ( is_int( $var ) ) {
363
		if ( $var === 0 ) {
364
			echo '<span class="', XVARDUMP_CLASS_INT_0, '">', $var, "</span>\n";
365
		}
366
		else {
367
			echo '<span class="', XVARDUMP_CLASS_INT, '">', $var, "</span>\n";
368
		}
369
	}
370
	else {
371
		echo 'E: not an integer';
372
	}
373
}
374
375
376
/**
377
 * Shortcut function to print a float variable.
378
 *
379
 * @param float $var
380
 */
381
function pr_flt( $var ) {
382
	if ( is_float( $var ) ) {
383
		echo '<span class="', XVARDUMP_CLASS_FLOAT, '">', $var, "</span>\n";
384
	}
385
	else {
386
		echo 'E: not a float';
387
	}
388
}
389