Skip to content

Commit

Permalink
Added delimiter to CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
drolean committed Jan 31, 2018
1 parent 572429a commit 9b497e0
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 160 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Changelog
- 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)
- Add support for laravel 5
- add package discovery for laravel 5
- add support delimiter to a csv

Formatter Bundle
================
Expand Down
9 changes: 5 additions & 4 deletions src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ class Formatter
/**
* Make: Returns an instance of formatter initialized with data and type
*
* @param mixed $data The data that formatter should parse
* @param string $type The type of data formatter is expected to parse
* @param mixed $data The data that formatter should parse
* @param string $type The type of data formatter is expected to parse
* @param string $delimiter The delimitation of data formatter to csv
* @return Formatter
*/
public static function make($data, $type)
public static function make($data, $type, $delimiter = null)
{
if (in_array($type, self::$supportedTypes)) {
$parser = null;
switch ($type) {
case self::CSV:
$parser = new CsvParser($data);
$parser = new CsvParser($data, $delimiter);
break;
case self::JSON:
$parser = new JsonParser($data);
Expand Down
7 changes: 5 additions & 2 deletions src/Parsers/CsvParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

class CsvParser extends Parser
{

private $csv;

public function __construct($data)
public function __construct($data, $delimiter = null)
{
if (is_string($data)) {
$this->csv = Reader::createFromString($data);
if ($delimiter) {
$this->csv->setDelimiter($delimiter);
}
$this->csv->setEnclosure('|');
} else {
throw new InvalidArgumentException(
'CsvParser only accepts (string) [csv] for $data.'
Expand Down
76 changes: 42 additions & 34 deletions tests/unit/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,61 @@

use SoapBox\Formatter\Formatter;

class FormatterTest extends TestCase {
class FormatterTest extends TestCase
{

public function testFormatterProvidesCsvConstant() {
$expected = 'csv';
$actual = Formatter::CSV;
public function testFormatterProvidesCsvConstant()
{
$expected = 'csv';
$actual = Formatter::CSV;

$this->assertEquals($expected, $actual);
}
$this->assertEquals($expected, $actual);
}

public function testFormatterProvidesJsonConstant() {
$expected = 'json';
$actual = Formatter::JSON;
public function testFormatterProvidesJsonConstant()
{
$expected = 'json';
$actual = Formatter::JSON;

$this->assertEquals($expected, $actual);
}
$this->assertEquals($expected, $actual);
}

public function testFormatterProvidesXmlConstant() {
$expected = 'xml';
$actual = Formatter::XML;
public function testFormatterProvidesXmlConstant()
{
$expected = 'xml';
$actual = Formatter::XML;

$this->assertEquals($expected, $actual);
}
$this->assertEquals($expected, $actual);
}

public function testFormatterProvidesArrayConstant() {
$expected = 'array';
$actual = Formatter::ARR;
public function testFormatterProvidesArrayConstant()
{
$expected = 'array';
$actual = Formatter::ARR;

$this->assertEquals($expected, $actual);
}
$this->assertEquals($expected, $actual);
}

public function testFormatterProvidesYamlConstant() {
$expected = 'yaml';
$actual = Formatter::YAML;
public function testFormatterProvidesYamlConstant()
{
$expected = 'yaml';
$actual = Formatter::YAML;

$this->assertEquals($expected, $actual);
}
$this->assertEquals($expected, $actual);
}

/**
* @expectedException InvalidArgumentException
*/
public function testFormatterMakeThrowsInvalidTypeException() {
$formatter = Formatter::make('', 'blue');
}

public function testFormatterMakeReturnsInstanceOfFormatter() {
$formatter = Formatter::make('', Formatter::CSV);
$this->assertTrue($formatter instanceof Formatter);
}
public function testFormatterMakeThrowsInvalidTypeException()
{
$formatter = Formatter::make('', 'blue');
}

public function testFormatterMakeReturnsInstanceOfFormatter()
{
$formatter = Formatter::make('', Formatter::CSV);
$this->assertTrue($formatter instanceof Formatter);
}

}
101 changes: 55 additions & 46 deletions tests/unit/Parsers/ArrayParserTest.php
Original file line number Diff line number Diff line change
@@ -1,58 +1,67 @@
<?php namespace SoapBox\Formatter\Test\Parsers;

use stdClass;
use SoapBox\Formatter\Test\TestCase;
use SoapBox\Formatter\Parsers\Parser;
use SoapBox\Formatter\Parsers\ArrayParser;
use SoapBox\Formatter\Parsers\Parser;
use SoapBox\Formatter\Test\TestCase;
use stdClass;

class ArrayParserTest extends TestCase {
class ArrayParserTest extends TestCase
{

public function testArrayParserIsInstanceOfParserInterface() {
$parser = new ArrayParser(new \stdClass);
$this->assertTrue($parser instanceof Parser);
}
public function testArrayParserIsInstanceOfParserInterface()
{
$parser = new ArrayParser(new \stdClass);
$this->assertTrue($parser instanceof Parser);
}

public function testConstructorAcceptsSerializedArray() {
$expected = [0, 1, 2];
$parser = new ArrayParser(serialize($expected));
$this->assertEquals($expected, $parser->toArray());
}
public function testConstructorAcceptsSerializedArray()
{
$expected = [0, 1, 2];
$parser = new ArrayParser(serialize($expected));
$this->assertEquals($expected, $parser->toArray());
}

public function testConstructorAcceptsObject() {
$expected = ['foo' => 'bar'];
$input = new stdClass;
$input->foo = 'bar';
$parser = new ArrayParser($input);
$this->assertEquals($expected, $parser->toArray());
}
public function testConstructorAcceptsObject()
{
$expected = ['foo' => 'bar'];
$input = new stdClass;
$input->foo = 'bar';
$parser = new ArrayParser($input);
$this->assertEquals($expected, $parser->toArray());
}

/**
* @expectedException InvalidArgumentException
*/
public function testArrayParserThrowsExceptionWithInvalidInputOfEmptyString() {
$parser = new ArrayParser('');
}

public function testtoArrayReturnsArray() {
$parser = new ArrayParser(serialize([0, 1, 2]));
$this->assertTrue(is_array($parser->toArray()));
}

public function testtoJsonReturnsJsonRepresentationOfArray() {
$expected = '[0,1,2]';
$parser = new ArrayParser([0, 1, 2]);
$this->assertEquals($expected, $parser->toJson());
}

public function testtoJsonReturnsJsonRepresentationOfNamedArray() {
$expected = '{"foo":"bar"}';
$parser = new ArrayParser(['foo' => 'bar']);
$this->assertEquals($expected, $parser->toJson());
}

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

public function testtoArrayReturnsArray()
{
$parser = new ArrayParser(serialize([0, 1, 2]));
$this->assertTrue(is_array($parser->toArray()));
}

public function testtoJsonReturnsJsonRepresentationOfArray()
{
$expected = '[0,1,2]';
$parser = new ArrayParser([0, 1, 2]);
$this->assertEquals($expected, $parser->toJson());
}

public function testtoJsonReturnsJsonRepresentationOfNamedArray()
{
$expected = '{"foo":"bar"}';
$parser = new ArrayParser(['foo' => 'bar']);
$this->assertEquals($expected, $parser->toJson());
}

public function testtoCSVFromArrayContainingContentWithCommasWorks()
{
$expected = "\"0\",\"1\",\"2\",\"3\"\n\"a\",\"b\",\"c,e\",\"d\"";
$parser = new ArrayParser(['a', 'b', 'c,e', 'd']);
$this->assertEquals($expected, $parser->toCsv());
}
}
41 changes: 41 additions & 0 deletions tests/unit/Parsers/CsvParserDelemiterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php namespace SoapBox\Formatter\Test\Parsers;

use SoapBox\Formatter\Parsers\CsvParser;
use SoapBox\Formatter\Parsers\Parser;
use SoapBox\Formatter\Test\TestCase;

class CsvParserDelemiterTest extends TestCase
{

private $simpleCsv = 'foo;boo
bar;far';

public function testCsvParserIsInstanceOfParserInterface()
{
$parser = new CsvParser('', ';');
$this->assertTrue($parser instanceof Parser);
}

/**
* @expectedException InvalidArgumentException
*/
public function testConstructorThrowsInvalidExecptionWhenArrayDataIsProvided()
{
$parser = new CsvParser([0, 1, 3], ';');
}

public function testtoArrayReturnsCsvArrayRepresentation()
{
$expected = [['foo' => 'bar', 'boo' => 'far']];
$parser = new CsvParser($this->simpleCsv, ';');
$this->assertEquals($expected, $parser->toArray());
}

public function testtoJsonReturnsJsonRepresentationOfNamedArray()
{
$expected = '[{"foo":"bar","boo":"far"}]';
$parser = new CsvParser($this->simpleCsv, ';');
$this->assertEquals($expected, $parser->toJson());
}

}
51 changes: 28 additions & 23 deletions tests/unit/Parsers/CsvParserTest.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
<?php namespace SoapBox\Formatter\Test\Parsers;

use SoapBox\Formatter\Test\TestCase;
use SoapBox\Formatter\Parsers\Parser;
use SoapBox\Formatter\Parsers\CsvParser;
use SoapBox\Formatter\Parsers\Parser;
use SoapBox\Formatter\Test\TestCase;

class CsvParserTest extends TestCase {
class CsvParserTest extends TestCase
{

private $simpleCsv = 'foo,boo
private $simpleCsv = 'foo,boo
bar,far';

public function testCsvParserIsInstanceOfParserInterface() {
$parser = new CsvParser('');
$this->assertTrue($parser instanceof Parser);
}
public function testCsvParserIsInstanceOfParserInterface()
{
$parser = new CsvParser('');
$this->assertTrue($parser instanceof Parser);
}

/**
* @expectedException InvalidArgumentException
*/
public function testConstructorThrowsInvalidExecptionWhenArrayDataIsProvided() {
$parser = new CsvParser([0, 1, 3]);
}

public function testtoArrayReturnsCsvArrayRepresentation() {
$expected = [['foo' => 'bar', 'boo' => 'far']];
$parser = new CsvParser($this->simpleCsv);
$this->assertEquals($expected, $parser->toArray());
}

public function testtoJsonReturnsJsonRepresentationOfNamedArray() {
$expected = '[{"foo":"bar","boo":"far"}]';
$parser = new CsvParser($this->simpleCsv);
$this->assertEquals($expected, $parser->toJson());
}
public function testConstructorThrowsInvalidExecptionWhenArrayDataIsProvided()
{
$parser = new CsvParser([0, 1, 3]);
}

public function testtoArrayReturnsCsvArrayRepresentation()
{
$expected = [['foo' => 'bar', 'boo' => 'far']];
$parser = new CsvParser($this->simpleCsv);
$this->assertEquals($expected, $parser->toArray());
}

public function testtoJsonReturnsJsonRepresentationOfNamedArray()
{
$expected = '[{"foo":"bar","boo":"far"}]';
$parser = new CsvParser($this->simpleCsv);
$this->assertEquals($expected, $parser->toJson());
}

}
Loading

0 comments on commit 9b497e0

Please sign in to comment.