Skip to content

Commit

Permalink
this fixes #4 one line CSV conversions will now work.
Browse files Browse the repository at this point in the history
  • Loading branch information
grahammccarthy committed Mar 26, 2014
1 parent 13f06eb commit b995109
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 33 deletions.
5 changes: 4 additions & 1 deletion src/SoapBox/Formatter/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,6 @@ protected function _from_csv($string, $attributes = array()) {

foreach ($rows as $row) {
$data_fields = str_replace($escape.$enclosure, $enclosure, str_getcsv($row, $delimiter, $enclosure, $escape));

if (count($data_fields) > count($headings)) {
array_push(self::$errors, Lang::get('formatter::formatter.more_data', array('line_number' => $line_number ) ));
} else if (count($data_fields) < count($headings)) {
Expand All @@ -380,6 +379,10 @@ protected function _from_csv($string, $attributes = array()) {
}
}

if(empty($rows) && !empty($headings) && count($headings) > 0) {
$data = $headings;
}

return $data;
}

Expand Down
1 change: 0 additions & 1 deletion src/config/config.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

return array(
'csv' => array(
'delimiter' => ',',
Expand Down
33 changes: 2 additions & 31 deletions tests/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ class FormatterTest extends Orchestra\Testbench\TestCase {
public function setUp() {
parent::setUp();

//$app = m::mock('AppMock');
//$app->shouldReceive('instance')->once()->andReturn($app);

//Illuminate\Support\Facades\Facade::setFacadeApplication($app);
//Illuminate\Support\Facades\Config::swap($config = m::mock('ConfigMock'));
//Illuminate\Support\Facades\Lang::swap($lang = m::mock('ConfigLang'));

//$config->shouldReceive('get')->once()->with('logviewer::log_dirs')->andReturn(array('app' => 'app/storage/logs'));
//$this->logviewer = new Logviewer('app', 'cgi-fcgi', '2013-06-01');
}
/**
* A basic functional test for JSON to Array conversion
Expand All @@ -37,7 +28,6 @@ public function testJsonToArray() {
*/
public function testArrayToJson() {
$data = array('foo'=>'bar', 'bar'=>'foo');

$result = Formatter::make($data)->to_json();
$expected = '{"foo":"bar","bar":"foo"}';
$this->assertEquals($expected, $result);
Expand All @@ -50,14 +40,11 @@ public function testArrayToJson() {
*/
public function testJSONToXMLToArrayToJsonToArray() {
$data = '{"foo":"bar","bar":"foo"}';

$result = Formatter::make($data, 'json')->to_xml();
$result = Formatter::make($result, 'xml')->to_array();
$result = Formatter::make($result, 'array')->to_json();
$result = Formatter::make($result, 'json')->to_array();

$expected = array('foo'=>'bar', 'bar'=>'foo');

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

Expand All @@ -67,26 +54,10 @@ public function testJSONToXMLToArrayToJsonToArray() {
* @return void
*/
public function testCSVToArray() {
$data = "foo,bar,bing,bam,boom";
$data = 'foo,bar,bing,bam,boom';
$result = Formatter::make($data, 'csv')->to_array();
$expected = array('foo'=>'bar', 'bar'=>'foo');
var_dump($result);
var_dump($expected);
die('dead');
$expected = array('foo','bar','bing','bam','boom');
$this->assertEquals($expected, $result);
}

/**
* A basic functional test for CSV data to array
*
* @return void
*/
public function testArrayToCSV() {
$expected = array('foo'=>'bar', 'bar'=>'foo');
$result = Formatter::make($data, 'array')->to_csv();
var_dump($result);

$expected = "foo,bar,bing,bam,boom";
$this->assertEquals($expected, $result);
}
}

0 comments on commit b995109

Please sign in to comment.