Skip to content

Commit c7b90cc

Browse files
committed
Merge pull request #15 from SoapBox/M-CommaSupportCSV
Fix CSV export
2 parents 9149adc + dc3f996 commit c7b90cc

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

src/SoapBox/Formatter/Parsers/Parser.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,35 +121,45 @@ private function csvify($data) {
121121
}
122122

123123
/**
124+
* Ported from laravel-formatter
125+
* https://github.com/SoapBox/laravel-formatter
126+
*
127+
* @author Daniel Berry <daniel@danielberry.me>
128+
* @license MIT License (see LICENSE.readme included in the bundle)
129+
*
124130
* Return a csv representation of the data stored in the parser
125131
*
126132
* @return string An csv string representing the encapsulated data
127133
*/
128-
public function toCsv() {
134+
public function toCsv($newline = "\n", $delimiter = ",", $enclosure = '"', $escape = "\\") {
129135
$data = $this->toArray();
130136

131137
if (ArrayHelpers::isAssociative($data) || !is_array($data[0])) {
132138
$data = [$data];
133139
}
134140

135-
$result = [];
141+
$escaper = function($items) use($enclosure, $escape) {
142+
return array_map(function($item) use($enclosure, $escape) {
143+
return str_replace($enclosure, $escape.$enclosure, $item);
144+
}, $items);
145+
};
136146

137-
$result[] = ArrayHelpers::dotKeys($data[0]);
147+
$headings = ArrayHelpers::dotKeys($data[0]);
148+
$result = [];
138149

139150
foreach ($data as $row) {
140151
$result[] = array_values(ArrayHelpers::dot($row));
141152
}
142153

143-
$output = '';
144-
$count = 0;
145-
foreach ($result as $row) {
146-
if ($count != 0) {
147-
$output .= "\r\n";
148-
}
149-
$count++;
150-
$output .= implode(',', $row);
154+
$data = $result;
155+
156+
$output = $enclosure.implode($enclosure.$delimiter.$enclosure, $escaper($headings)).$enclosure.$newline;
157+
158+
foreach ($data as $row)
159+
{
160+
$output .= $enclosure.implode($enclosure.$delimiter.$enclosure, $escaper((array) $row)).$enclosure.$newline;
151161
}
152162

153-
return $output;
163+
return rtrim($output, $newline);
154164
}
155165
}

tests/unit/ParserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function testJSONToXMLToArrayToJsonToArray() {
6565
}
6666

6767
public function testMultiDimensionalArrayFromJsonToCsv() {
68-
$expected = "simple,date,time,duration_onset,devicename,calc_data.0.0,calc_data.0.1,calc_data.0.2,calc_data.0.3,calc_data.0.4,calc_data.0.5,calc_data.1.0,calc_data.1.1,calc_data.1.2,calc_data.1.3,calc_data.1.4,calc_data.1.5\r\n118,2014-05-20 21:03:59.333,4067,,My Device,1400609039,0,37,0,0,1,1400609039,0,37,0,0,1";
68+
$expected = "\"simple\",\"date\",\"time\",\"duration_onset\",\"devicename\",\"calc_data.0.0\",\"calc_data.0.1\",\"calc_data.0.2\",\"calc_data.0.3\",\"calc_data.0.4\",\"calc_data.0.5\",\"calc_data.1.0\",\"calc_data.1.1\",\"calc_data.1.2\",\"calc_data.1.3\",\"calc_data.1.4\",\"calc_data.1.5\"\n\"118\",\"2014-05-20 21:03:59.333\",\"4067\",\"\",\"My Device\",\"1400609039\",\"0\",\"37\",\"0\",\"0\",\"1\",\"1400609039\",\"0\",\"37\",\"0\",\"0\",\"1\"";
6969

7070
$json =
7171
'{

tests/unit/Parsers/ArrayParserTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,9 @@ public function testtoJsonReturnsJsonRepresentationOfNamedArray() {
5050
$this->assertEquals($expected, $parser->toJson());
5151
}
5252

53+
public function testtoCSVFromArrayContainingContentWithCommasWorks() {
54+
$expected = "\"0\",\"1\",\"2\",\"3\"\n\"a\",\"b\",\"c,e\",\"d\"";
55+
$parser = new ArrayParser(['a','b','c,e','d']);
56+
$this->assertEquals($expected, $parser->toCsv());
57+
}
5358
}

0 commit comments

Comments
 (0)