Skip to content

Commit a49a9be

Browse files
PHP: escape dollar variables
1 parent 52b5230 commit a49a9be

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/exporters/php.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ export class PHPExporter implements FormatExporter {
7070
} else if (data === null || data === undefined) {
7171
return (startIndented ? indent : "") + "null";
7272
} else if (typeof data === "string") {
73-
return (startIndented ? indent : "") + JSON.stringify(data);
73+
return (
74+
(startIndented ? indent : "") +
75+
JSON.stringify(data).replaceAll("${", "\\${")
76+
);
7477
} else if (Array.isArray(data)) {
7578
const elements =
7679
data.length === 0

tests/convert.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,24 @@ $resp1 = $client->search([
394394
],
395395
]);
396396
397+
`,
398+
);
399+
});
400+
401+
it("converts a dollar-variable to php", async () => {
402+
expect(
403+
await convertRequests('POST /my-index/_doc\n{"key":"${value}"}', "php", {
404+
complete: false,
405+
elasticsearchUrl: "https://localhost:9999",
406+
}),
407+
).toEqual(
408+
`$resp = $client->index([
409+
"index" => "my-index",
410+
"body" => [
411+
"key" => "\\\${value}",
412+
],
413+
]);
414+
397415
`,
398416
);
399417
});

0 commit comments

Comments
 (0)