@@ -36,17 +36,6 @@ public static function dump($var)
36
36
37
37
38
38
39
- /**
40
- * Returns a PHP representation of a object member.
41
- * @return string
42
- */
43
- public static function dumpMember ($ var )
44
- {
45
- return preg_match ('#^ ' . self ::PHP_IDENT . '$# ' , $ var ) ? $ var : '{ ' . self ::_dump ($ var ) . '} ' ;
46
- }
47
-
48
-
49
-
50
39
private static function _dump (&$ var , $ level = 0 )
51
40
{
52
41
if ($ var instanceof PhpLiteral) {
@@ -138,26 +127,57 @@ private static function _dump(&$var, $level = 0)
138
127
139
128
140
129
141
- /** @return string */
142
- public static function generate ($ statement )
130
+ /**
131
+ * Generates PHP statement.
132
+ * @return string
133
+ */
134
+ public static function format ($ statement )
143
135
{
144
136
$ args = func_get_args ();
145
- unset($ args [0 ]);
146
- foreach ($ args as $ key => $ arg ) {
147
- $ args [$ key ] = self ::_dump ($ arg );
148
- }
149
- if (strpos ($ statement , '? ' ) === FALSE ) {
150
- return $ statement .= '( ' . implode (', ' , $ args ) . '); ' ;
151
- }
137
+ return self ::formatArgs (array_shift ($ args ), $ args );
138
+ }
139
+
152
140
141
+
142
+ /**
143
+ * Generates PHP statement.
144
+ * @return string
145
+ */
146
+ public static function formatArgs ($ statement , array $ args )
147
+ {
153
148
$ a = strpos ($ statement , '? ' );
154
- $ i = 1 ;
155
149
while ($ a !== FALSE ) {
156
- $ statement = substr_replace ($ statement , $ args [$ i ], $ a , 1 );
157
- $ a = strpos ($ statement , '? ' , $ a + strlen ($ args [$ i ]));
158
- $ i ++;
150
+ if (!$ args ) {
151
+ throw new Nette \InvalidArgumentException ('Insufficient number of arguments. ' );
152
+ }
153
+ $ arg = array_shift ($ args );
154
+ if (substr ($ statement , $ a + 1 , 1 ) === '* ' ) { // ?*
155
+ if (!is_array ($ arg )) {
156
+ throw new Nette \InvalidArgumentException ('Argument must be an array. ' );
157
+ }
158
+ $ arg = implode (', ' , array_map (array (__CLASS__ , 'dump ' ), $ arg ));
159
+ $ statement = substr_replace ($ statement , $ arg , $ a , 2 );
160
+
161
+ } else {
162
+ $ arg = substr ($ statement , $ a - 1 , 1 ) === '$ ' || substr ($ statement , $ a - 2 , 2 ) === '-> ' ? self ::formatMember ($ arg ) : self ::_dump ($ arg );
163
+ $ statement = substr_replace ($ statement , $ arg , $ a , 1 );
164
+ }
165
+ $ a = strpos ($ statement , '? ' , $ a + strlen ($ arg ));
159
166
}
160
- return $ statement . '; ' ;
167
+ return $ statement ;
168
+ }
169
+
170
+
171
+
172
+ /**
173
+ * Returns a PHP representation of a object member.
174
+ * @return string
175
+ */
176
+ public static function formatMember ($ name )
177
+ {
178
+ return $ name instanceof PhpLiteral || !preg_match ('#^ ' . self ::PHP_IDENT . '$# ' , $ name )
179
+ ? '{ ' . self ::_dump ($ name ) . '} '
180
+ : $ name ;
161
181
}
162
182
163
183
}
0 commit comments