Skip to content

Commit

Permalink
Escape parameters of object with __toString()
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Oct 8, 2020
1 parent e9c14c5 commit 6e6e69f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Sql/ValueFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function format($value, ?string $paramName = null, ?string $sql = null) :
}

if (is_object($value) && method_exists($value, '__toString')) {
return "'" . $value . "'";
return "'" . Escaper::escape((string) $value) . "'";
}

if (is_array($value)) {
Expand Down
11 changes: 11 additions & 0 deletions tests/Sql/ValueFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function providerFormat() : iterable
yield 'float .0' => ['1', 1.0];
yield 'float .5' => ['1.5', 1.5];
yield 'string' => ["'ping'", 'ping'];
yield 'string escaped' => ["'ping\\\\n'", 'ping\n'];
yield 'null' => ['IS NULL', null];
yield 'array' => ["['a','b','c']", ['a', 'b', 'c']];
yield 'array in array' => ["[['a']]", [['a']]];
Expand All @@ -62,6 +63,16 @@ public function __toString() : string
}
},
];

yield 'Stringable escaped' => [
"'stringable \\\\n'",
new class () {
public function __toString() : string
{
return 'stringable \n';
}
},
];
}

/**
Expand Down

0 comments on commit 6e6e69f

Please sign in to comment.