From f8b2fe274ae71716cc81945c8b25e8d59b9726f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Tue, 20 Feb 2018 21:22:39 +0100 Subject: [PATCH 1/8] Added missing letter in function name --- src/extensions/DatatypeTrait.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/extensions/DatatypeTrait.php b/src/extensions/DatatypeTrait.php index 37012f1..b4550cb 100644 --- a/src/extensions/DatatypeTrait.php +++ b/src/extensions/DatatypeTrait.php @@ -1,4 +1,5 @@ titles as $cName) { $column = array_column($this->data, $cName); - $cDatatypes = array_map('ParseCsv\enums\DatatypeEnum::getValidTypeFromSample', $column); - $result[$cName] = $this->getMostFrequentDataypeForColumn($cDatatypes); + $result[$cName] = $this->getMostFrequentDatatypeForColumn($cDatatypes); } $this->data_types = $result; From d484f3e9e34b275836c62a9ba3bb2835a87f15eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Tue, 20 Feb 2018 21:37:39 +0100 Subject: [PATCH 2/8] The workaround for array_count_values was wrong and unnecessary. --- src/extensions/DatatypeTrait.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/extensions/DatatypeTrait.php b/src/extensions/DatatypeTrait.php index b4550cb..49797f9 100644 --- a/src/extensions/DatatypeTrait.php +++ b/src/extensions/DatatypeTrait.php @@ -23,20 +23,15 @@ trait DatatypeTrait { * * @return string|false */ - array_filter($datatypes); private function getMostFrequentDatatypeForColumn($datatypes) { + // remove 'false' from array (can happen if CSV cell is empty) + $types_filtered = array_filter($datatypes); - if (empty($datatypes)){ + if (empty($types_filtered)) { return false; } - // workaround because array_count_values($datatypes) does not work anymore :-( - foreach ($datatypes as $type) { - $ids = array_keys($datatypes, $type); - $typesFreq[$type] = count($ids); - - $datatypes = array_diff_key($datatypes, array_flip($ids)); - } + $typesFreq = array_count_values($types_filtered); arsort($typesFreq); reset($typesFreq); From a7f2bbb7bca51bc63ee13106f0673af25a605c47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Tue, 20 Feb 2018 21:39:38 +0100 Subject: [PATCH 3/8] Throw more specific exception --- src/extensions/DatatypeTrait.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/extensions/DatatypeTrait.php b/src/extensions/DatatypeTrait.php index 49797f9..9323a2f 100644 --- a/src/extensions/DatatypeTrait.php +++ b/src/extensions/DatatypeTrait.php @@ -45,7 +45,7 @@ trait DatatypeTrait { * * @access public * - * @uses getDatatypeFromString + * @uses getDatatypeFromString * * @return array|bool */ @@ -54,7 +54,7 @@ trait DatatypeTrait { $this->data = $this->parse_string(); } if (!is_array($this->data)) { - throw new \Exception('No data set yet.'); + throw new \UnexpectedValueException('No data set yet.'); } $result = []; From 85cc0d9cfca45e312e00d5dd111c1d1efabf2bcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Tue, 20 Feb 2018 21:50:09 +0100 Subject: [PATCH 4/8] Updated README.md to new name & namespace --- README.md | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index b4bde72..f35eb2a 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ -# parseCSV +# ParseCsv -parseCSV is an easy to use PHP class that reads and writes CSV data properly. It +ParseCsv is an easy to use PHP class that reads and writes CSV data properly. It fully conforms to the specifications outlined on the on the [Wikipedia article][CSV] (and thus RFC 4180). It has many advanced features which help make your life easier when dealing with CSV data. -You may not need a library at all: before using parseCSV, please make sure if PHP's own `str_getcsv()`, ``fgetcvs()`` or `fputcsv()` meets your needs. +You may not need a library at all: before using ParseCsv, please make sure if PHP's own `str_getcsv()`, ``fgetcvs()`` or `fputcsv()` meets your needs. This library was originally created in early 2007 by [jimeh](https://github.com/jimeh) due to the lack of built-in and third-party support for handling CSV data in PHP. @@ -25,8 +25,7 @@ require_once 'parsecsv.lib.php'; ## Features -* parseCSV is the only complete and fully featured CSV solution for PHP (as - far as I know). +* ParseCsv is a complete and fully featured CSV solution for PHP * Supports enclosed values, enclosed commas, double quotes and new lines. * Automatic delimiter character detection. * Sort data by specific fields/columns. @@ -46,14 +45,14 @@ require_once 'parsecsv.lib.php'; **General** ```php -$csv = new parseCSV('data.csv'); +$csv = new ParseCsv\Csv('data.csv'); print_r($csv->data); ``` **Tab delimited, and encoding conversion** ```php -$csv = new parseCSV(); +$csv = new ParseCsv\Csv(); $csv->encoding('UTF-16', 'UTF-8'); $csv->delimiter = "\t"; $csv->parse('data.tsv'); @@ -63,7 +62,7 @@ print_r($csv->data); **Auto-detect delimiter character** ```php -$csv = new parseCSV(); +$csv = new ParseCsv\Csv(); $csv->auto('data.csv'); print_r($csv->data); ``` @@ -71,7 +70,7 @@ print_r($csv->data); **Modify data in a CSV file** ```php -$csv = new parseCSV(); +$csv = new ParseCsv\Csv(); $csv->sort_by = 'id'; $csv->parse('data.csv'); # "4" is the value of the "id" column of the CSV row @@ -82,7 +81,7 @@ $csv->save(); **Replace field names or set ones if missing** ```php -$csv = new parseCSV(); +$csv = new ParseCsv\Csv(); $csv->fields = ['id', 'name', 'category'] $csv->parse('data.csv'); ``` @@ -92,7 +91,7 @@ $csv->parse('data.csv'); _Only recommended when you know the exact structure of the file._ ```php -$csv = new parseCSV(); +$csv = new ParseCsv\Csv(); $csv->save('data.csv', array(array('1986', 'Home', 'Nowhere', '')), true); ``` @@ -100,7 +99,7 @@ $csv->save('data.csv', array(array('1986', 'Home', 'Nowhere', '')), true); a file and download it** ```php -$csv = new parseCSV(); +$csv = new ParseCsv\Csv(); $csv->output('movies.csv', $array, array('field 1', 'field 2'), ','); ``` @@ -108,7 +107,7 @@ For more complex examples, see the ``tests`` and `examples` directories. ## Credits -* parseCSV is based on the concept of [Ming Hong Ng][ming]'s [CsvFileParser][] +* ParseCsv is based on the concept of [Ming Hong Ng][ming]'s [CsvFileParser][] class. [ming]: http://minghong.blogspot.com/ From d188921ab67df3af447fde1854128b90315d8a51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Tue, 20 Feb 2018 21:50:52 +0100 Subject: [PATCH 5/8] composer.json: Only allow to load tests in dev environment --- composer.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/composer.json b/composer.json index ec01fc3..2af8b63 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,11 @@ "ParseCsv\\tests\\": "tests" } }, + "autoload-dev":{ + "psr-4":{ + "ParseCsv\\tests\\": "tests" + } + }, "require-dev": { "phpunit/phpunit": "4.1.*" } From 2206ec1e7ce5e0a8ff0f5a1b4ca0fb994217cebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Tue, 20 Feb 2018 22:06:06 +0100 Subject: [PATCH 6/8] only reformatted source code to match the rest of the project --- src/enums/DatatypeEnum.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/enums/DatatypeEnum.php b/src/enums/DatatypeEnum.php index 83ad1bc..5813132 100644 --- a/src/enums/DatatypeEnum.php +++ b/src/enums/DatatypeEnum.php @@ -1,4 +1,5 @@ 'isValidInteger', self::TYPE_BOOL => 'isValidBoolean', self::TYPE_FLOAT => 'isValidFloat', - self::TYPE_DATE => 'isValidDate' + self::TYPE_DATE => 'isValidDate', ); /** @@ -49,15 +49,15 @@ class DatatypeEnum * * @return bool|string */ - public static function getValidTypeFromSample($value){ + public static function getValidTypeFromSample($value) { $value = trim((string) $value); - if (empty($value)){ + if (empty($value)) { return false; } - foreach (self::$validators as $type => $validator){ - if ($validator === null){ + foreach (self::$validators as $type => $validator) { + if ($validator === null) { continue; } From 4cba97ad516b3d918bdd4f6895acb6c79cba3669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Tue, 20 Feb 2018 22:07:08 +0100 Subject: [PATCH 7/8] Allow PhpStorm to understand the code better (I followed suggestions from PhpStorm's inspections) --- src/enums/DatatypeEnum.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/enums/DatatypeEnum.php b/src/enums/DatatypeEnum.php index 5813132..8fea47d 100644 --- a/src/enums/DatatypeEnum.php +++ b/src/enums/DatatypeEnum.php @@ -33,6 +33,11 @@ class DatatypeEnum { * Define validator functions here. * * @var array + * + * @uses isValidFloat + * @uses isValidInteger + * @uses isValidBoolean + * @uses isValidDate */ private static $validators = array( self::TYPE_STRING => null, @@ -61,10 +66,8 @@ class DatatypeEnum { continue; } - if (method_exists(__CLASS__, $validator)){ - if (get_class()::$validator($value)) { - return $type; - } + if (method_exists(__CLASS__, $validator) && self::$validator($value)) { + return $type; } } From a60aae47a40649abea2eb29b52d93a9782af9c9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Tue, 20 Feb 2018 22:16:49 +0100 Subject: [PATCH 8/8] getDatatypes: Documented the need for PHP 5.5 or higher --- src/extensions/DatatypeTrait.php | 2 ++ tests/methods/ParseTest.php | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/src/extensions/DatatypeTrait.php b/src/extensions/DatatypeTrait.php index 9323a2f..ce380c9 100644 --- a/src/extensions/DatatypeTrait.php +++ b/src/extensions/DatatypeTrait.php @@ -43,6 +43,8 @@ trait DatatypeTrait { * Check data type foreach Column * Check data type for each column and returns the most commonly. * + * Requires PHP >= 5.5 + * * @access public * * @uses getDatatypeFromString diff --git a/tests/methods/ParseTest.php b/tests/methods/ParseTest.php index 07eb3b7..d48435f 100644 --- a/tests/methods/ParseTest.php +++ b/tests/methods/ParseTest.php @@ -137,6 +137,12 @@ class ParseTest extends TestCase * @depends testSepRowAutoDetection */ public function testGetColumnDatatypes() { + if (!function_exists('array_column')) { + // getDatatypes requires array_column, but that + // function is only available in PHP >= 5.5 + return; + } + $this->csv->auto(__DIR__ . '/fixtures/datatype.csv'); $this->csv->getDatatypes(); $expected = [