Skip to content

Commit b97eb09

Browse files
authored
Merge pull request #52 from keboola/adamvyborny-fix-writing-false-value
Fix writing false value
2 parents 59dde98 + 283f002 commit b97eb09

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

phpstan-baseline-8+.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ parameters:
131131
path: src/CsvWriter.php
132132

133133
-
134-
message: "#^Parameter \\#3 \\$subject of function str_replace expects array\\|string, bool\\|float\\|int\\|object\\|string given\\.$#"
134+
message: "#^Parameter \\#3 \\$subject of function str_replace expects array\\|string, float\\|int\\|object\\|string\\|true given\\.$#"
135135
count: 1
136136
path: src/CsvWriter.php
137137

phpstan-baseline.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ parameters:
136136
path: src/CsvWriter.php
137137

138138
-
139-
message: "#^Parameter \\#3 \\$subject of function str_replace expects array\\|string, bool\\|float\\|int\\|object\\|string given\\.$#"
139+
message: "#^Parameter \\#3 \\$subject of function str_replace expects array\\|string, float\\|int\\|object\\|string\\|true given\\.$#"
140140
count: 1
141141
path: src/CsvWriter.php
142142

src/CsvWriter.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,12 @@ public function rowToStr(array $row)
142142
);
143143
}
144144

145-
$return[] = $this->getEnclosure() .
146-
str_replace($this->getEnclosure(), str_repeat($this->getEnclosure(), 2), $column ?? '') .
147-
$this->getEnclosure();
145+
$enclosure = $this->getEnclosure();
146+
$escapedEnclosure = str_repeat($enclosure, 2);
147+
$columnValue = ($column === false) ? '0' : ($column ?? '');
148+
149+
$escapedColumn = str_replace($enclosure, $escapedEnclosure, $columnValue);
150+
$return[] = sprintf('%s%s%s', $enclosure, $escapedColumn, $enclosure);
148151
}
149152
return implode($this->getDelimiter(), $return) . $this->lineBreak;
150153
}

tests/CsvWriteTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ public function testWrite()
5353
[
5454
'column with \n \t \\\\', 'second col',
5555
],
56+
[
57+
1, true,
58+
],
59+
[
60+
2, false,
61+
],
62+
[
63+
3, null,
64+
],
65+
[
66+
'true', 1.123,
67+
],
68+
[
69+
'1', 'null',
70+
],
5671
];
5772

5873
foreach ($rows as $row) {
@@ -70,6 +85,11 @@ public function testWrite()
7085
'"column with enclosure "", and comma inside text","second column enclosure in text """',
7186
"\"columns with\nnew line\",\"columns with\ttab\"",
7287
'"column with \\n \\t \\\\","second col"',
88+
'"1","1"',
89+
'"2","0"',
90+
'"3",""',
91+
'"true","1.123"',
92+
'"1","null"',
7393
'',
7494
]
7595
),

0 commit comments

Comments
 (0)