Skip to content

Commit 2489d9e

Browse files
authored
Merge pull request #25 from keboola/odin-string-obj
feat: support objects convertible to strings
2 parents e2ac897 + eaf2257 commit 2489d9e

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

src/CsvFile.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,14 @@ public function rowToStr(array $row)
340340
{
341341
$return = [];
342342
foreach ($row as $column) {
343-
if (!is_scalar($column) && !is_null($column)) {
343+
if (!(
344+
is_scalar($column)
345+
|| is_null($column)
346+
|| (
347+
is_object($column)
348+
&& method_exists($column, '__toString')
349+
)
350+
)) {
344351
$type = gettype($column);
345352
throw new Exception(
346353
"Cannot write {$type} into a column",

tests/CsvFileTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Keboola\Csv\Tests;
44

55
use Keboola\Csv\CsvFile;
6+
use Keboola\Csv\Exception;
67
use PHPUnit\Framework\TestCase;
78

89
class CsvFileTest extends TestCase
@@ -228,6 +229,83 @@ public function testWrite()
228229
foreach ($rows as $row) {
229230
$csvFile->writeRow($row);
230231
}
232+
$data = file_get_contents($fileName);
233+
self::assertEquals(
234+
implode(
235+
"\n",
236+
[
237+
'"col1","col2"',
238+
'"line without enclosure","second column"',
239+
'"enclosure "" in column","hello \\"',
240+
'"line with enclosure","second column"',
241+
'"column with enclosure "", and comma inside text","second column enclosure in text """',
242+
"\"columns with\nnew line\",\"columns with\ttab\"",
243+
'"column with \\n \\t \\\\","second col"',
244+
'',
245+
]
246+
),
247+
$data
248+
);
249+
@unlink($fileName);
250+
}
251+
252+
public function testWriteInvalidObject()
253+
{
254+
$fileName = __DIR__ . '/data/_out.csv';
255+
if (file_exists($fileName)) {
256+
unlink($fileName);
257+
}
258+
259+
$csvFile = new CsvFile($fileName);
260+
261+
$rows = [
262+
[
263+
'col1', 'col2',
264+
],
265+
[
266+
'1', new \stdClass(),
267+
],
268+
];
269+
270+
$csvFile->writeRow($rows[0]);
271+
self::expectException(Exception::class);
272+
self::expectExceptionMessage("Cannot write object into a column");
273+
$csvFile->writeRow($rows[1]);
274+
@unlink($fileName);
275+
}
276+
277+
public function testWriteValidObject()
278+
{
279+
$fileName = __DIR__ . '/data/_out.csv';
280+
if (file_exists($fileName)) {
281+
unlink($fileName);
282+
}
283+
284+
$csvFile = new CsvFile($fileName);
285+
$rows = [
286+
[
287+
'col1', 'col2',
288+
],
289+
[
290+
'1', new StringObject(),
291+
],
292+
];
293+
294+
$csvFile->writeRow($rows[0]);
295+
$csvFile->writeRow($rows[1]);
296+
$data = file_get_contents($fileName);
297+
self::assertEquals(
298+
implode(
299+
"\n",
300+
[
301+
'"col1","col2"' ,
302+
'"1","me string"',
303+
'',
304+
]
305+
),
306+
$data
307+
);
308+
@unlink($fileName);
231309
}
232310

233311
public function testIterator()

tests/StringObject.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Keboola\Csv\Tests;
4+
5+
class StringObject
6+
{
7+
public function __toString()
8+
{
9+
return "me string";
10+
}
11+
}

0 commit comments

Comments
 (0)