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/ 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.*" } diff --git a/src/enums/DatatypeEnum.php b/src/enums/DatatypeEnum.php index 83ad1bc..8fea47d 100644 --- a/src/enums/DatatypeEnum.php +++ b/src/enums/DatatypeEnum.php @@ -1,4 +1,5 @@ null, self::TYPE_INT => 'isValidInteger', self::TYPE_BOOL => 'isValidBoolean', self::TYPE_FLOAT => 'isValidFloat', - self::TYPE_DATE => 'isValidDate' + self::TYPE_DATE => 'isValidDate', ); /** @@ -49,22 +54,20 @@ 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; } - if (method_exists(__CLASS__, $validator)){ - if (get_class()::$validator($value)) { - return $type; - } + if (method_exists(__CLASS__, $validator) && self::$validator($value)) { + return $type; } } diff --git a/src/extensions/DatatypeTrait.php b/src/extensions/DatatypeTrait.php index 37012f1..ce380c9 100644 --- a/src/extensions/DatatypeTrait.php +++ b/src/extensions/DatatypeTrait.php @@ -1,4 +1,5 @@ = 5.5 + * * @access public * - * @uses getDatatypeFromString + * @uses getDatatypeFromString * * @return array|bool */ @@ -58,16 +56,15 @@ 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 = []; foreach ($this->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; 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 = [