Skip to content

Commit 5e8ec51

Browse files
Merge pull request #102 from ZoozyCN/1.0
LogFormatter convertParams fix for passing nested array as params
2 parents 103c227 + 656005d commit 5e8ec51

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/Loggers/Formatters/ReplaceQueryParams.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function format($sql, array $params = null)
1818
if (is_array($params)) {
1919
foreach ($params as $param) {
2020
$param = $this->convertParam($param);
21-
$sql = preg_replace('/\?/', "\"$param\"", $sql, 1);
21+
$sql = preg_replace('/\?/', "$param", $sql, 1);
2222
}
2323
}
2424

@@ -42,9 +42,25 @@ protected function convertParam($param)
4242
}
4343
}
4444
} elseif (is_array($param)) {
45-
$param = implode(',', $param);
45+
if (count($param) !== count($param, COUNT_RECURSIVE)) {
46+
$param = json_encode($param, JSON_UNESCAPED_UNICODE);
47+
} else {
48+
$param = implode(
49+
', ',
50+
array_map(
51+
function ($part) {
52+
return '"' . (string) $part . '"';
53+
},
54+
$param
55+
)
56+
);
57+
58+
return '(' . $param . ')';
59+
}
60+
} else {
61+
$param = e($param);
4662
}
4763

48-
return (string) e($param);
64+
return '"' . (string) $param . '"';
4965
}
5066
}

tests/Loggers/Formatters/ReplaceQueryParamsTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,28 @@ public function test_can_replace_datetime_objects()
7373
);
7474
}
7575

76+
public function test_can_replace_array_param()
77+
{
78+
$sql = 'SELECT * FROM table WHERE column IN ?';
79+
$params = [['value1', 'value2']];
80+
81+
$this->assertEquals(
82+
'SELECT * FROM table WHERE column IN ("value1", "value2")',
83+
$this->formatter->format($sql, $params)
84+
);
85+
}
86+
87+
public function test_can_replace_nested_array_param()
88+
{
89+
$sql = 'SELECT * FROM table WHERE column = ?';
90+
$params = [['key1' => 'value1', 'key2' => ['key21' => 'value22']]];
91+
92+
$this->assertEquals(
93+
'SELECT * FROM table WHERE column = "' . json_encode(reset($params), JSON_UNESCAPED_UNICODE) . '"',
94+
$this->formatter->format($sql, $params)
95+
);
96+
}
97+
7698
protected function tearDown()
7799
{
78100
m::close();

0 commit comments

Comments
 (0)