1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: class PhpHelpers
22: {
23: const PHP_IDENT = '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*';
24:
25:
26: 27: 28: 29:
30: public static function dump($var)
31: {
32: return self::_dump($var);
33: }
34:
35:
36:
37: private static function _dump(&$var, $level = 0)
38: {
39: if ($var instanceof PhpLiteral) {
40: return $var->value;
41:
42: } elseif (is_float($var)) {
43: $var = var_export($var, TRUE);
44: return strpos($var, '.') === FALSE ? $var . '.0' : $var;
45:
46: } elseif (is_bool($var)) {
47: return $var ? 'TRUE' : 'FALSE';
48:
49: } elseif (is_string($var) && (preg_match('#[^\x09\x20-\x7E\xA0-\x{10FFFF}]#u', $var) || preg_last_error())) {
50: static $table;
51: if ($table === NULL) {
52: foreach (range("\x00", "\xFF") as $ch) {
53: $table[$ch] = ord($ch) < 32 || ord($ch) >= 127
54: ? '\\x' . str_pad(dechex(ord($ch)), 2, '0', STR_PAD_LEFT)
55: : $ch;
56: }
57: $table["\r"] = '\r';
58: $table["\n"] = '\n';
59: $table["\t"] = '\t';
60: $table['$'] = '\\$';
61: $table['\\'] = '\\\\';
62: $table['"'] = '\\"';
63: }
64: return '"' . strtr($var, $table) . '"';
65:
66: } elseif (is_array($var)) {
67: $s = '';
68: $space = str_repeat("\t", $level);
69:
70: static $marker;
71: if ($marker === NULL) {
72: $marker = uniqid("\x00", TRUE);
73: }
74: if (empty($var)) {
75:
76: } elseif ($level > 50 || isset($var[$marker])) {
77: throw new InvalidArgumentException('Nesting level too deep or recursive dependency.');
78:
79: } else {
80: $s .= "\n";
81: $var[$marker] = TRUE;
82: $counter = 0;
83: foreach ($var as $k => &$v) {
84: if ($k !== $marker) {
85: $s .= "$space\t" . ($k === $counter ? '' : self::_dump($k) . " => ") . self::_dump($v, $level + 1) . ",\n";
86: $counter = is_int($k) ? max($k + 1, $counter) : $counter;
87: }
88: }
89: unset($var[$marker]);
90: $s .= $space;
91: }
92: return "array($s)";
93:
94: } elseif (is_object($var)) {
95: $arr = (array) $var;
96: $s = '';
97: $space = str_repeat("\t", $level);
98:
99: static $list = array();
100: if (empty($arr)) {
101:
102: } elseif ($level > 50 || in_array($var, $list, TRUE)) {
103: throw new InvalidArgumentException('Nesting level too deep or recursive dependency.');
104:
105: } else {
106: $s .= "\n";
107: $list[] = $var;
108: foreach ($arr as $k => &$v) {
109: if ($k[0] === "\x00") {
110: $k = substr($k, strrpos($k, "\x00") + 1);
111: }
112: $s .= "$space\t" . self::_dump($k) . " => " . self::_dump($v, $level + 1) . ",\n";
113: }
114: array_pop($list);
115: $s .= $space;
116: }
117: return get_class($var) === 'stdClass'
118: ? "(object) array($s)"
119: : __CLASS__ . "::createObject('" . get_class($var) . "', array($s))";
120:
121: } else {
122: return var_export($var, TRUE);
123: }
124: }
125:
126:
127:
128: 129: 130: 131:
132: public static function format($statement)
133: {
134: $args = func_get_args();
135: return self::formatArgs(array_shift($args), $args);
136: }
137:
138:
139:
140: 141: 142: 143:
144: public static function formatArgs($statement, array $args)
145: {
146: $a = strpos($statement, '?');
147: while ($a !== FALSE) {
148: if (!$args) {
149: throw new InvalidArgumentException('Insufficient number of arguments.');
150: }
151: $arg = array_shift($args);
152: if (substr($statement, $a + 1, 1) === '*') {
153: if (!is_array($arg)) {
154: throw new InvalidArgumentException('Argument must be an array.');
155: }
156: $arg = implode(', ', array_map(array(__CLASS__, 'dump'), $arg));
157: $statement = substr_replace($statement, $arg, $a, 2);
158:
159: } else {
160: $arg = substr($statement, $a - 1, 1) === '$' || in_array(substr($statement, $a - 2, 2), array('->', '::'))
161: ? self::formatMember($arg) : self::_dump($arg);
162: $statement = substr_replace($statement, $arg, $a, 1);
163: }
164: $a = strpos($statement, '?', $a + strlen($arg));
165: }
166: return $statement;
167: }
168:
169:
170:
171: 172: 173: 174:
175: public static function formatMember($name)
176: {
177: return $name instanceof PhpLiteral || !self::isIdentifier($name)
178: ? '{' . self::_dump($name) . '}'
179: : $name ;
180: }
181:
182:
183:
184: 185: 186:
187: public static function isIdentifier($value)
188: {
189: return is_string($value) && preg_match('#^' . self::PHP_IDENT . '\z#', $value);
190: }
191:
192:
193:
194: public static function createObject($class, array $props)
195: {
196: return unserialize('O' . substr(serialize((string) $class), 1, -1) . substr(serialize($props), 1));
197: }
198:
199: }
200: