diff --git a/src/Loggers/Formatters/ReplaceQueryParams.php b/src/Loggers/Formatters/ReplaceQueryParams.php index 8ad95b17..4e446e51 100644 --- a/src/Loggers/Formatters/ReplaceQueryParams.php +++ b/src/Loggers/Formatters/ReplaceQueryParams.php @@ -18,7 +18,7 @@ public function format($sql, array $params = null) if (is_array($params)) { foreach ($params as $param) { $param = $this->convertParam($param); - $sql = preg_replace('/\?/', "\"$param\"", $sql, 1); + $sql = preg_replace('/\?/', "$param", $sql, 1); } } @@ -42,9 +42,25 @@ protected function convertParam($param) } } } elseif (is_array($param)) { - $param = implode(',', $param); + if (count($param) !== count($param, COUNT_RECURSIVE)) { + $param = json_encode($param, JSON_UNESCAPED_UNICODE); + } else { + $param = implode( + ', ', + array_map( + function ($part) { + return '"' . (string) $part . '"'; + }, + $param + ) + ); + + return '(' . $param . ')'; + } + } else { + $param = e($param); } - return (string) e($param); + return '"' . (string) $param . '"'; } } diff --git a/tests/Loggers/Formatters/ReplaceQueryParamsTest.php b/tests/Loggers/Formatters/ReplaceQueryParamsTest.php index 93356427..f0071acb 100644 --- a/tests/Loggers/Formatters/ReplaceQueryParamsTest.php +++ b/tests/Loggers/Formatters/ReplaceQueryParamsTest.php @@ -73,6 +73,28 @@ public function test_can_replace_datetime_objects() ); } + public function test_can_replace_array_param() + { + $sql = 'SELECT * FROM table WHERE column IN ?'; + $params = [['value1', 'value2']]; + + $this->assertEquals( + 'SELECT * FROM table WHERE column IN ("value1", "value2")', + $this->formatter->format($sql, $params) + ); + } + + public function test_can_replace_nested_array_param() + { + $sql = 'SELECT * FROM table WHERE column = ?'; + $params = [['key1' => 'value1', 'key2' => ['key21' => 'value22']]]; + + $this->assertEquals( + 'SELECT * FROM table WHERE column = "' . json_encode(reset($params), JSON_UNESCAPED_UNICODE) . '"', + $this->formatter->format($sql, $params) + ); + } + protected function tearDown() { m::close();