diff --git a/.travis.yml b/.travis.yml index a198238..b1eb5d4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,4 +8,5 @@ before_script: - curl -s http://getcomposer.org/installer | php - php composer.phar install --dev -script: phpunit \ No newline at end of file +script: phpunit + diff --git a/composer.json b/composer.json index 02135ab..149e924 100644 --- a/composer.json +++ b/composer.json @@ -1,10 +1,11 @@ { "name": "soapbox/laravel-formatter", "type": "library", - "description": "A Laravel 4 formatting library. Convert data output between XML/CSV/JSON/TXT/YAML/etc. The project builds on the work of Daniel Berry's Laravel 3 Formatter Bundle.", + "description": "A Laravel 4 formatting library that converts data output between XML, CSV, JSON, TXT, YAML and a few others. ", "keywords": ["laravel", "formatter", "data","convert","csv", "xml", "yaml"], "homepage": "http://github.com/SoapBox/laravel-formatter", "license": "MIT", + "version": "1.1", "authors": [ { "name": "Graham McCarthy", @@ -14,7 +15,16 @@ ], "require": { "php": ">=5.3.0", - "illuminate/support": "4.*" + "illuminate/support": ">=4.0,<4.2", + "illuminate/foundation": ">=4.0,<4.2", + "illuminate/config": ">=4.0,<4.2", + "illuminate/session": ">=4.0,<4.2", + "illuminate/filesystem": ">=4.0,<4.2", + "illuminate/view": ">=4.0,<4.2" + }, + "require-dev": { + "orchestra/testbench": "2.1.*", + "mockery/mockery": "dev-master" }, "autoload": { "psr-0": { @@ -23,4 +33,4 @@ } }, "minimum-stability": "dev" -} +} \ No newline at end of file diff --git a/readme.md b/readme.md index 425708c..c3991a7 100644 --- a/readme.md +++ b/readme.md @@ -1,7 +1,9 @@ Formatter Bundle ================ -A Laravel 4 Formatter Package based on the work done by @dberry37388 with FuelPHP's Formatter class. +[![Build Status](https://travis-ci.org/SoapBox/laravel-formatter.svg?branch=master)](https://travis-ci.org/SoapBox/laravel-formatter) + +A Laravel 4 Formatter Package based on the work done by @dberry37388 with FuelPHP's Formatter class. This package will help you to easily convert between various formats such as XML, JSON, CSV, etc... diff --git a/src/SoapBox/Formatter/Formatter.php b/src/SoapBox/Formatter/Formatter.php index 7a8e13b..bfb0d0f 100644 --- a/src/SoapBox/Formatter/Formatter.php +++ b/src/SoapBox/Formatter/Formatter.php @@ -19,7 +19,9 @@ //namespace Formatter; namespace SoapBox\Formatter; -use Config, Lang; +//use Config, Lang; +use Illuminate\Support\Facades\Config; +use Illuminate\Support\Facades\Lang; /** * The Formatter Class @@ -55,7 +57,6 @@ public static function make($data = null, $from_type = null, $attributes = array return new self($data, $from_type, $attributes); } - /** * Should not be called directly. You should be using Formatter::make() * @@ -369,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)) { @@ -379,6 +379,10 @@ protected function _from_csv($string, $attributes = array()) { } } + if(empty($rows) && !empty($headings) && count($headings) > 0) { + $data = $headings; + } + return $data; } diff --git a/src/config/config.php b/src/config/config.php index 10ae79a..2d4f3af 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -1,5 +1,4 @@ array( 'delimiter' => ',', diff --git a/src/lang/en/formatter.php b/src/lang/en/formatter.php index aacd26c..8749c10 100644 --- a/src/lang/en/formatter.php +++ b/src/lang/en/formatter.php @@ -14,9 +14,8 @@ */ return array( - 'no_data' => 'No data to convert', 'from_type_not_supported' => ':from_type is not a supported type to convert from.', - 'more_data' => 'The line :line_number contains more data fields than the heading.', - 'less_data' => 'The line :line_number contains less data fields than the heading.' + 'more_data' => 'The line :line_number contains more data fields than the heading.', + 'less_data' => 'The line :line_number contains less data fields than the heading.' ); \ No newline at end of file diff --git a/tests/FormatterTest.php b/tests/FormatterTest.php new file mode 100644 index 0000000..4a68d0b --- /dev/null +++ b/tests/FormatterTest.php @@ -0,0 +1,63 @@ +to_array(); + $expected = array('foo'=>'bar', 'bar'=>'foo'); + $this->assertEquals($expected, $result); + } + + /** + * A basic functional test for Array to JSON conversion + * + * @return void + */ + public function testArrayToJson() { + $data = array('foo'=>'bar', 'bar'=>'foo'); + $result = Formatter::make($data)->to_json(); + $expected = '{"foo":"bar","bar":"foo"}'; + $this->assertEquals($expected, $result); + } + + /** + * A basic functional test for testJSONToXMLToArrayToJsonToArray data to array + * + * @return void + */ + 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); + } + + /** + * A basic functional test for CSV data to array + * + * @return void + */ + public function testCSVToArray() { + $data = 'foo,bar,bing,bam,boom'; + $result = Formatter::make($data, 'csv')->to_array(); + $expected = array('foo','bar','bing','bam','boom'); + $this->assertEquals($expected, $result); + } + +} \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..5e74e53 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,3 @@ +