Skip to content

Commit 9b497e0

Browse files
committed
Added delimiter to CSV
1 parent 572429a commit 9b497e0

10 files changed

+240
-160
lines changed

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Changelog
99
- JSON parse fix (Instead of only converting the first level to array, use the associative array parameter with true, so all levels will be decoded to array structure)
1010
- Add support for laravel 5
1111
- add package discovery for laravel 5
12+
- add support delimiter to a csv
1213

1314
Formatter Bundle
1415
================

src/Formatter.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,18 @@ class Formatter
2929
/**
3030
* Make: Returns an instance of formatter initialized with data and type
3131
*
32-
* @param mixed $data The data that formatter should parse
33-
* @param string $type The type of data formatter is expected to parse
32+
* @param mixed $data The data that formatter should parse
33+
* @param string $type The type of data formatter is expected to parse
34+
* @param string $delimiter The delimitation of data formatter to csv
3435
* @return Formatter
3536
*/
36-
public static function make($data, $type)
37+
public static function make($data, $type, $delimiter = null)
3738
{
3839
if (in_array($type, self::$supportedTypes)) {
3940
$parser = null;
4041
switch ($type) {
4142
case self::CSV:
42-
$parser = new CsvParser($data);
43+
$parser = new CsvParser($data, $delimiter);
4344
break;
4445
case self::JSON:
4546
$parser = new JsonParser($data);

src/Parsers/CsvParser.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66

77
class CsvParser extends Parser
88
{
9-
109
private $csv;
1110

12-
public function __construct($data)
11+
public function __construct($data, $delimiter = null)
1312
{
1413
if (is_string($data)) {
1514
$this->csv = Reader::createFromString($data);
15+
if ($delimiter) {
16+
$this->csv->setDelimiter($delimiter);
17+
}
18+
$this->csv->setEnclosure('|');
1619
} else {
1720
throw new InvalidArgumentException(
1821
'CsvParser only accepts (string) [csv] for $data.'

tests/unit/FormatterTest.php

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,61 @@
22

33
use SoapBox\Formatter\Formatter;
44

5-
class FormatterTest extends TestCase {
5+
class FormatterTest extends TestCase
6+
{
67

7-
public function testFormatterProvidesCsvConstant() {
8-
$expected = 'csv';
9-
$actual = Formatter::CSV;
8+
public function testFormatterProvidesCsvConstant()
9+
{
10+
$expected = 'csv';
11+
$actual = Formatter::CSV;
1012

11-
$this->assertEquals($expected, $actual);
12-
}
13+
$this->assertEquals($expected, $actual);
14+
}
1315

14-
public function testFormatterProvidesJsonConstant() {
15-
$expected = 'json';
16-
$actual = Formatter::JSON;
16+
public function testFormatterProvidesJsonConstant()
17+
{
18+
$expected = 'json';
19+
$actual = Formatter::JSON;
1720

18-
$this->assertEquals($expected, $actual);
19-
}
21+
$this->assertEquals($expected, $actual);
22+
}
2023

21-
public function testFormatterProvidesXmlConstant() {
22-
$expected = 'xml';
23-
$actual = Formatter::XML;
24+
public function testFormatterProvidesXmlConstant()
25+
{
26+
$expected = 'xml';
27+
$actual = Formatter::XML;
2428

25-
$this->assertEquals($expected, $actual);
26-
}
29+
$this->assertEquals($expected, $actual);
30+
}
2731

28-
public function testFormatterProvidesArrayConstant() {
29-
$expected = 'array';
30-
$actual = Formatter::ARR;
32+
public function testFormatterProvidesArrayConstant()
33+
{
34+
$expected = 'array';
35+
$actual = Formatter::ARR;
3136

32-
$this->assertEquals($expected, $actual);
33-
}
37+
$this->assertEquals($expected, $actual);
38+
}
3439

35-
public function testFormatterProvidesYamlConstant() {
36-
$expected = 'yaml';
37-
$actual = Formatter::YAML;
40+
public function testFormatterProvidesYamlConstant()
41+
{
42+
$expected = 'yaml';
43+
$actual = Formatter::YAML;
3844

39-
$this->assertEquals($expected, $actual);
40-
}
45+
$this->assertEquals($expected, $actual);
46+
}
4147

4248
/**
4349
* @expectedException InvalidArgumentException
4450
*/
45-
public function testFormatterMakeThrowsInvalidTypeException() {
46-
$formatter = Formatter::make('', 'blue');
47-
}
48-
49-
public function testFormatterMakeReturnsInstanceOfFormatter() {
50-
$formatter = Formatter::make('', Formatter::CSV);
51-
$this->assertTrue($formatter instanceof Formatter);
52-
}
51+
public function testFormatterMakeThrowsInvalidTypeException()
52+
{
53+
$formatter = Formatter::make('', 'blue');
54+
}
55+
56+
public function testFormatterMakeReturnsInstanceOfFormatter()
57+
{
58+
$formatter = Formatter::make('', Formatter::CSV);
59+
$this->assertTrue($formatter instanceof Formatter);
60+
}
5361

5462
}
Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,67 @@
11
<?php namespace SoapBox\Formatter\Test\Parsers;
22

3-
use stdClass;
4-
use SoapBox\Formatter\Test\TestCase;
5-
use SoapBox\Formatter\Parsers\Parser;
63
use SoapBox\Formatter\Parsers\ArrayParser;
4+
use SoapBox\Formatter\Parsers\Parser;
5+
use SoapBox\Formatter\Test\TestCase;
6+
use stdClass;
77

8-
class ArrayParserTest extends TestCase {
8+
class ArrayParserTest extends TestCase
9+
{
910

10-
public function testArrayParserIsInstanceOfParserInterface() {
11-
$parser = new ArrayParser(new \stdClass);
12-
$this->assertTrue($parser instanceof Parser);
13-
}
11+
public function testArrayParserIsInstanceOfParserInterface()
12+
{
13+
$parser = new ArrayParser(new \stdClass);
14+
$this->assertTrue($parser instanceof Parser);
15+
}
1416

15-
public function testConstructorAcceptsSerializedArray() {
16-
$expected = [0, 1, 2];
17-
$parser = new ArrayParser(serialize($expected));
18-
$this->assertEquals($expected, $parser->toArray());
19-
}
17+
public function testConstructorAcceptsSerializedArray()
18+
{
19+
$expected = [0, 1, 2];
20+
$parser = new ArrayParser(serialize($expected));
21+
$this->assertEquals($expected, $parser->toArray());
22+
}
2023

21-
public function testConstructorAcceptsObject() {
22-
$expected = ['foo' => 'bar'];
23-
$input = new stdClass;
24-
$input->foo = 'bar';
25-
$parser = new ArrayParser($input);
26-
$this->assertEquals($expected, $parser->toArray());
27-
}
24+
public function testConstructorAcceptsObject()
25+
{
26+
$expected = ['foo' => 'bar'];
27+
$input = new stdClass;
28+
$input->foo = 'bar';
29+
$parser = new ArrayParser($input);
30+
$this->assertEquals($expected, $parser->toArray());
31+
}
2832

2933
/**
3034
* @expectedException InvalidArgumentException
3135
*/
32-
public function testArrayParserThrowsExceptionWithInvalidInputOfEmptyString() {
33-
$parser = new ArrayParser('');
34-
}
35-
36-
public function testtoArrayReturnsArray() {
37-
$parser = new ArrayParser(serialize([0, 1, 2]));
38-
$this->assertTrue(is_array($parser->toArray()));
39-
}
40-
41-
public function testtoJsonReturnsJsonRepresentationOfArray() {
42-
$expected = '[0,1,2]';
43-
$parser = new ArrayParser([0, 1, 2]);
44-
$this->assertEquals($expected, $parser->toJson());
45-
}
46-
47-
public function testtoJsonReturnsJsonRepresentationOfNamedArray() {
48-
$expected = '{"foo":"bar"}';
49-
$parser = new ArrayParser(['foo' => 'bar']);
50-
$this->assertEquals($expected, $parser->toJson());
51-
}
52-
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-
}
36+
public function testArrayParserThrowsExceptionWithInvalidInputOfEmptyString()
37+
{
38+
$parser = new ArrayParser('');
39+
}
40+
41+
public function testtoArrayReturnsArray()
42+
{
43+
$parser = new ArrayParser(serialize([0, 1, 2]));
44+
$this->assertTrue(is_array($parser->toArray()));
45+
}
46+
47+
public function testtoJsonReturnsJsonRepresentationOfArray()
48+
{
49+
$expected = '[0,1,2]';
50+
$parser = new ArrayParser([0, 1, 2]);
51+
$this->assertEquals($expected, $parser->toJson());
52+
}
53+
54+
public function testtoJsonReturnsJsonRepresentationOfNamedArray()
55+
{
56+
$expected = '{"foo":"bar"}';
57+
$parser = new ArrayParser(['foo' => 'bar']);
58+
$this->assertEquals($expected, $parser->toJson());
59+
}
60+
61+
public function testtoCSVFromArrayContainingContentWithCommasWorks()
62+
{
63+
$expected = "\"0\",\"1\",\"2\",\"3\"\n\"a\",\"b\",\"c,e\",\"d\"";
64+
$parser = new ArrayParser(['a', 'b', 'c,e', 'd']);
65+
$this->assertEquals($expected, $parser->toCsv());
66+
}
5867
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php namespace SoapBox\Formatter\Test\Parsers;
2+
3+
use SoapBox\Formatter\Parsers\CsvParser;
4+
use SoapBox\Formatter\Parsers\Parser;
5+
use SoapBox\Formatter\Test\TestCase;
6+
7+
class CsvParserDelemiterTest extends TestCase
8+
{
9+
10+
private $simpleCsv = 'foo;boo
11+
bar;far';
12+
13+
public function testCsvParserIsInstanceOfParserInterface()
14+
{
15+
$parser = new CsvParser('', ';');
16+
$this->assertTrue($parser instanceof Parser);
17+
}
18+
19+
/**
20+
* @expectedException InvalidArgumentException
21+
*/
22+
public function testConstructorThrowsInvalidExecptionWhenArrayDataIsProvided()
23+
{
24+
$parser = new CsvParser([0, 1, 3], ';');
25+
}
26+
27+
public function testtoArrayReturnsCsvArrayRepresentation()
28+
{
29+
$expected = [['foo' => 'bar', 'boo' => 'far']];
30+
$parser = new CsvParser($this->simpleCsv, ';');
31+
$this->assertEquals($expected, $parser->toArray());
32+
}
33+
34+
public function testtoJsonReturnsJsonRepresentationOfNamedArray()
35+
{
36+
$expected = '[{"foo":"bar","boo":"far"}]';
37+
$parser = new CsvParser($this->simpleCsv, ';');
38+
$this->assertEquals($expected, $parser->toJson());
39+
}
40+
41+
}

tests/unit/Parsers/CsvParserTest.php

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,41 @@
11
<?php namespace SoapBox\Formatter\Test\Parsers;
22

3-
use SoapBox\Formatter\Test\TestCase;
4-
use SoapBox\Formatter\Parsers\Parser;
53
use SoapBox\Formatter\Parsers\CsvParser;
4+
use SoapBox\Formatter\Parsers\Parser;
5+
use SoapBox\Formatter\Test\TestCase;
66

7-
class CsvParserTest extends TestCase {
7+
class CsvParserTest extends TestCase
8+
{
89

9-
private $simpleCsv = 'foo,boo
10+
private $simpleCsv = 'foo,boo
1011
bar,far';
1112

12-
public function testCsvParserIsInstanceOfParserInterface() {
13-
$parser = new CsvParser('');
14-
$this->assertTrue($parser instanceof Parser);
15-
}
13+
public function testCsvParserIsInstanceOfParserInterface()
14+
{
15+
$parser = new CsvParser('');
16+
$this->assertTrue($parser instanceof Parser);
17+
}
1618

1719
/**
1820
* @expectedException InvalidArgumentException
1921
*/
20-
public function testConstructorThrowsInvalidExecptionWhenArrayDataIsProvided() {
21-
$parser = new CsvParser([0, 1, 3]);
22-
}
23-
24-
public function testtoArrayReturnsCsvArrayRepresentation() {
25-
$expected = [['foo' => 'bar', 'boo' => 'far']];
26-
$parser = new CsvParser($this->simpleCsv);
27-
$this->assertEquals($expected, $parser->toArray());
28-
}
29-
30-
public function testtoJsonReturnsJsonRepresentationOfNamedArray() {
31-
$expected = '[{"foo":"bar","boo":"far"}]';
32-
$parser = new CsvParser($this->simpleCsv);
33-
$this->assertEquals($expected, $parser->toJson());
34-
}
22+
public function testConstructorThrowsInvalidExecptionWhenArrayDataIsProvided()
23+
{
24+
$parser = new CsvParser([0, 1, 3]);
25+
}
26+
27+
public function testtoArrayReturnsCsvArrayRepresentation()
28+
{
29+
$expected = [['foo' => 'bar', 'boo' => 'far']];
30+
$parser = new CsvParser($this->simpleCsv);
31+
$this->assertEquals($expected, $parser->toArray());
32+
}
33+
34+
public function testtoJsonReturnsJsonRepresentationOfNamedArray()
35+
{
36+
$expected = '[{"foo":"bar","boo":"far"}]';
37+
$parser = new CsvParser($this->simpleCsv);
38+
$this->assertEquals($expected, $parser->toJson());
39+
}
3540

3641
}

0 commit comments

Comments
 (0)