Skip to content

Commit

Permalink
SqlProcessor: allow to escape % modifier sign
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Mar 27, 2015
1 parent be23544 commit c542f1d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/SqlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,22 @@ public function process(array $args)

$i = $j;
$fragments[] = preg_replace_callback(
'#%(\??+\w++(?:\[\]){0,2}+)|\[(.+?)\]#S', // %modifier | [identifier]
'#%(\??+\w++(?:\[\]){0,2}+)|(%%)|\[(.+?)\]#S', // %modifier | %% | [identifier]
function ($matches) use ($args, &$j, $last) {
if ($matches[1] !== '') {
if ($j === $last) {
throw new InvalidArgumentException("Missing query parameter for modifier $matches[0].");
}
return $this->processModifier($matches[1], $args[++$j]);

} elseif (!ctype_digit($matches[2])) {
return $this->identifiers->{$matches[2]};
} elseif ($matches[2] !== '') {
return '%';

} elseif (!ctype_digit($matches[3])) {
return $this->identifiers->{$matches[3]};

} else {
return "[$matches[2]]";
return "[$matches[3]]";
}
},
$args[$i]
Expand Down
9 changes: 9 additions & 0 deletions tests/cases/unit/SqlProcessorTest.process.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ class SqlProcessorProcessTest extends TestCase
}


public function testEscape()
{
Assert::same(
'SELECT DATE_FORMAT(publishedDate, "%Y") AS year FROM foo',
$this->parser->process(['SELECT DATE_FORMAT(publishedDate, "%%Y") AS year FROM foo'])
);
}


public function testWrongArguments()
{
Assert::throws(function() {
Expand Down

0 comments on commit c542f1d

Please sign in to comment.