Skip to content

Commit fb027e7

Browse files
committed
PhpGenerator\Helpers: added format() & formatArgs()
1 parent c3afc42 commit fb027e7

File tree

3 files changed

+85
-28
lines changed

3 files changed

+85
-28
lines changed

src/PhpGenerator/Helpers.php

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,6 @@ public static function dump($var)
3636

3737

3838

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-
5039
private static function _dump(&$var, $level = 0)
5140
{
5241
if ($var instanceof PhpLiteral) {
@@ -138,26 +127,57 @@ private static function _dump(&$var, $level = 0)
138127

139128

140129

141-
/** @return string */
142-
public static function generate($statement)
130+
/**
131+
* Generates PHP statement.
132+
* @return string
133+
*/
134+
public static function format($statement)
143135
{
144136
$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+
152140

141+
142+
/**
143+
* Generates PHP statement.
144+
* @return string
145+
*/
146+
public static function formatArgs($statement, array $args)
147+
{
153148
$a = strpos($statement, '?');
154-
$i = 1;
155149
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));
159166
}
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 ;
161181
}
162182

163183
}

tests/PhpGenerator/PhpGenerator.dump.phpt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,3 @@ Assert::same( "array(\n\t'a' => 1,\n\tarray(\n\t\t\"\\r\" => \"\\r\",\n\t\t2,\n\
4545
Assert::same( "(object) array(\n\t'a' => 1,\n\t'b' => 2,\n)", Helpers::dump((object) array('a' => 1, 'b' => 2)) );
4646
Assert::same( "(object) array(\n\t'a' => (object) array(\n\t\t'b' => 2,\n\t),\n)" , Helpers::dump((object) array('a' => (object) array('b' => 2))) );
4747
Assert::same( "Test::__set_state(array(\n\t'a' => 1,\n\t'b' => 2,\n\t'c' => 3,\n))", Helpers::dump(new Test) );
48-
49-
Assert::same( "Item", Helpers::dumpMember('Item') );
50-
Assert::same( "{'0Item'}", Helpers::dumpMember('0Item') );
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Utils\PhpGenerator\Helpers::format() & formatArgs()
5+
*
6+
* @author David Grudl
7+
* @package Nette\Utils
8+
* @subpackage UnitTests
9+
*/
10+
11+
use Nette\Utils\PhpGenerator\Helpers;
12+
13+
14+
15+
require __DIR__ . '/../bootstrap.php';
16+
17+
18+
Assert::same( 'func', Helpers::format('func') );
19+
Assert::same( 'func(1)', Helpers::format('func(?)', 1, 2) );
20+
21+
Assert::same( 'func', Helpers::formatArgs('func', array(1, 2)) );
22+
Assert::same( 'func(1)', Helpers::formatArgs('func(?)', array(1, 2)) );
23+
Assert::same( "func(array(\n\t1,\n\t2,\n))", Helpers::formatArgs('func(?)', array(array(1, 2))) );
24+
Assert::same( 'func(1, 2)', Helpers::formatArgs('func(?*)', array(array(1, 2))) );
25+
26+
Assert::throws(function() {
27+
Helpers::formatArgs('func(?*)', array(1, 2));
28+
}, 'Nette\InvalidArgumentException', 'Argument must be an array.');
29+
30+
Assert::throws(function() {
31+
Helpers::formatArgs('func(?, ?, ?)', array(1, 2));
32+
}, 'Nette\InvalidArgumentException', 'Insufficient number of arguments.');
33+
34+
Assert::same( '$a = 2', Helpers::formatArgs('$? = ?', array('a', 2)) );
35+
Assert::same( '$obj->a = 2', Helpers::formatArgs('$obj->? = ?', array('a', 2)) );
36+
Assert::same( '$obj->{1} = 2', Helpers::formatArgs('$obj->? = ?', array(1, 2)) );
37+
Assert::same( '$obj->{\' \'} = 2', Helpers::formatArgs('$obj->? = ?', array(' ', 2)) );
38+
39+
Assert::same( "Item", Helpers::formatMember('Item') );
40+
Assert::same( "{'0Item'}", Helpers::formatMember('0Item') );

0 commit comments

Comments
 (0)