12 Commits

Author SHA1 Message Date
Christian Bläul
02a8c1995d Semantic versioning: Marked the current state of the repo as 1.0.0-rc.1 2018-02-23 15:03:57 +01:00
Christian Bläul
72a7c29ed6 Updated remaining occurrences for old class/project name. 2018-02-02 17:31:26 +01:00
Christian Bläul
c905c9abfd Updated README.md 2018-02-02 17:26:13 +01:00
Christian Bläul
221c175217 Further improved ChangeLog.txt 2018-02-02 17:22:35 +01:00
Christian Bläul
4376cc8bd6 Added conditions to SortByTest because otherwise different PHP versions...
yielded different sort orders (that's sort of ok, as rating is ambiguous).

See https://travis-ci.org/parsecsv/parsecsv-for-php/jobs/336540118
2018-02-02 14:28:51 +01:00
Christian Bläul
5c157efbc3 output(): Use better mime type for \t separator.
Fixes #79
2018-02-02 13:49:57 +01:00
Christian Bläul
387a0f5761 Renamed class to follow the PHP community guidelines such as:
- https://svn.apache.org/repos/asf/shindig/attic/php/docs/style-guide.html:
  "Acryonyms are treated as normal words."

- https://softwareengineering.stackexchange.com/a/149321/80632
  Overview of class naming conventions of PHP frameworks

- https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md
  No .lib allowed: "The class name corresponds to a file name ending in .php"

See issue #50
2018-02-02 13:22:58 +01:00
Christian Bläul
7a9d55dd1e Prepared new release in ChangeLog.txt 2018-02-02 12:27:12 +01:00
Christian Bläul
979e9ed0cd Moved ConditionsTest to properties subfolder 2018-02-02 12:26:06 +01:00
Christian Bläul
adcd258ea2 Added PHPUnit test for sorting; I could not get the rows in the right...
order without calling array_values - maybe this helps just PHPUnit. I
consider the risk of breaking library users' code unlikely.
2018-02-02 12:22:23 +01:00
Christian Bläul
34d28f7435 PHPUnit: Repaired file paths in autoQuotesDataProvider() 2018-02-02 12:10:25 +01:00
Christian Bläul
5cdec32466 Test some conditions 2018-02-02 11:14:41 +01:00
17 changed files with 232 additions and 114 deletions

View File

@@ -1,3 +1,23 @@
ParseCsvForPhp 1.0.0-rc.1
-----------------------------------
Date: unreleased
- Renamed class from parseCSV to ParseCsvForPhp
- Added support for MS Excel's "sep=" to detect the
delimiter (Issue #60).
- MIME: output() sends correct MIME type to browser
if the separator is a tab tab (Issue #79)
- Added support for mb_convert_encoding() instead of
iconv() (Issue #109)
- A number of minor bug fixes - see GitHub issues
-----------------------------------
parseCSV 0.4.3 beta
-----------------------------------
Date: 1-July-2008
@@ -159,7 +179,7 @@ Date: 2-Jan-2007
- Added auto() function to automatically detect
delimiter character.
Useful for user upload incase delimiter is
Useful for user upload in case delimiter is
comma (,), tab, or semi-colon (;). Some
versions of MS Excel for Windows use
semi-colons instead of commas when saving to
@@ -170,7 +190,7 @@ Date: 2-Jan-2007
almost no matter what the delimiter is.
- Generally updated some of the core workings
to increase performance, and offer better
to increase performance, and offer better
support for large (1MB and up) files.
- Added code examples to header comment.

View File

@@ -1,9 +1,9 @@
<?php
class parseCSV {
class ParseCsvForPhp {
/*
Class: parseCSV v0.4.3 beta
Class: ParseCsvForPhp 1.0.0-rc.1
https://github.com/parsecsv/parsecsv-for-php
Fully conforms to the specifications lined out on Wikipedia:
@@ -39,23 +39,23 @@ class parseCSV {
Code Examples
----------------
# general usage
$csv = new parseCSV('data.csv');
$csv = new ParseCsvForPhp('data.csv');
print_r($csv->data);
----------------
# tab delimited, and encoding conversion
$csv = new parseCSV();
$csv = new ParseCsvForPhp();
$csv->encoding('UTF-16', 'UTF-8');
$csv->delimiter = "\t";
$csv->parse('data.tsv');
print_r($csv->data);
----------------
# auto-detect delimiter character
$csv = new parseCSV();
$csv = new ParseCsvForPhp();
$csv->auto('data.csv');
print_r($csv->data);
----------------
# modify data in a csv file
$csv = new parseCSV();
$csv = new ParseCsvForPhp();
$csv->sort_by = 'id';
$csv->parse('data.csv');
# "4" is the value of the "id" column of the CSV row
@@ -64,12 +64,12 @@ class parseCSV {
----------------
# add row/entry to end of CSV file
# - only recommended when you know the exact structure of the file
$csv = new parseCSV();
$csv = new ParseCsvForPhp();
$csv->save('data.csv', array(array('1986', 'Home', 'Nowhere', '')), true);
----------------
# convert 2D array to csv data and send headers
# to browser to treat output as a file and download it
$csv = new parseCSV();
$csv = new ParseCsvForPhp();
$csv->output('movies.csv', $array, array('field 1', 'field 2'), ',');
----------------
*/
@@ -450,7 +450,10 @@ class parseCSV {
$flat_string = $this->unparse($data, $fields, null, null, $delimiter);
if (!is_null($filename)) {
header('Content-type: application/csv');
$mime = $delimiter === "\t" ?
'text/tab-separated-values' :
'application/csv';
header('Content-type: ' . $mime);
header('Content-Length: ' . strlen($flat_string));
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
@@ -730,6 +733,9 @@ class parseCSV {
$this->sort_reverse ? krsort($rows, $sort_type) : ksort($rows, $sort_type);
// Avoid issues with mixing string and integer keys:
$rows = array_values($rows);
if ($this->offset !== null || $this->limit !== null) {
$rows = array_slice($rows, ($this->offset === null ? 0 : $this->offset), $this->limit, true);
}

View File

@@ -1,11 +1,11 @@
# parseCSV
# ParseCsvForPhp
parseCSV is an easy to use PHP class that reads and writes CSV data properly. It
This 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 ParseCsvForPhp, 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.
@@ -15,18 +15,16 @@ and third-party support for handling CSV data in PHP.
## Installation
Installation is easy using Composer. Include the following in your composer.json
```
"parsecsv/php-parsecsv": "0.4.5"
"parsecsv/php-parsecsv": "1.0.0"
```
You may also manually include the parsecsv.lib.php file
You may also manually include the ParseCsvForPhp.php file
```php
require_once 'parsecsv.lib.php';
require_once 'ParseCsvForPhp.php';
```
## Features
* parseCSV is the only complete and fully featured CSV solution for PHP (as
far as I know).
* Supports enclosed values, enclosed commas, double quotes and new lines.
* Automatic delimiter character detection.
* Sort data by specific fields/columns.
@@ -36,9 +34,10 @@ require_once 'parsecsv.lib.php';
* Error detection for incorrectly formatted input. It attempts to be
intelligent, but can not be trusted 100% due to the structure of CSV, and
how different programs like Excel for example outputs CSV data.
* Support for character encoding conversion using PHP's _iconv_ function
(requires PHP 5).
* Supports PHP 5.4 and higher. It certainly works with PHP 7.2
* Support for character encoding conversion using PHP's
`iconv()` and `mb_convert_encoding()` functions (requires PHP 5).
* Supports PHP 5.4 and higher.
It certainly works with PHP 7.2 and all versions in between.
## Example Usage
@@ -46,14 +45,14 @@ require_once 'parsecsv.lib.php';
**General**
```php
$csv = new parseCSV('data.csv');
$csv = new ParseCsvForPhp('data.csv');
print_r($csv->data);
```
**Tab delimited, and encoding conversion**
```php
$csv = new parseCSV();
$csv = new ParseCsvForPhp();
$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 ParseCsvForPhp();
$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 ParseCsvForPhp();
$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 ParseCsvForPhp();
$csv->fields = ['id', 'name', 'category']
$csv->parse('data.csv');
```
@@ -92,15 +91,15 @@ $csv->parse('data.csv');
_Only recommended when you know the exact structure of the file._
```php
$csv = new parseCSV();
$csv = new ParseCsvForPhp();
$csv->save('data.csv', array(array('1986', 'Home', 'Nowhere', '')), true);
```
**Convert 2D array to csv data and send headers to browser to treat output as
**Convert 2D array to CSV data and send headers to browser to treat output as
a file and download it**
```php
$csv = new parseCSV();
$csv = new ParseCsvForPhp();
$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][]
* ParseCsvForPhp is based on the concept of [Ming Hong Ng][ming]'s [CsvFileParser][]
class.
[ming]: http://minghong.blogspot.com/

View File

@@ -2,12 +2,12 @@
<?php
# include parseCSV class.
require_once('../parsecsv.lib.php');
# include ParseCsvForPhp class.
require_once('../ParseCsvForPhp.php');
# create new parseCSV object.
$csv = new parseCSV();
# create new object.
$csv = new ParseCsvForPhp();
# Parse '_books.csv' using automatic delimiter detection...

View File

@@ -2,12 +2,12 @@
<?php
# include parseCSV class.
require_once('../parsecsv.lib.php');
# include ParseCsvForPhp class.
require_once('../ParseCsvForPhp.php');
# create new parseCSV object.
$csv = new parseCSV();
# create new object.
$csv = new ParseCsvForPhp();
# Example conditions:

View File

@@ -1,12 +1,12 @@
<?php
# include parseCSV class.
require_once('../parsecsv.lib.php');
# include ParseCsvForPhp class.
require_once('../ParseCsvForPhp.php');
# create new parseCSV object.
$csv = new parseCSV();
# create new object.
$csv = new ParseCsvForPhp();
# Parse '_books.csv' using automatic delimiter detection...
@@ -31,4 +31,4 @@ $csv->output('books.csv');
# or is set to null, output will only return the generated CSV
# output data, and will not output to the browser itself.
?>
?>

View File

@@ -2,12 +2,12 @@
<?php
# include parseCSV class.
require_once('../parsecsv.lib.php');
# include class.
require_once('../ParseCsvForPhp.php');
# create new parseCSV object.
$csv = new parseCSV();
# create new object.
$csv = new ParseCsvForPhp();
# if sorting is enabled, the whole CSV file

View File

@@ -3,9 +3,11 @@
$dir = realpath(__DIR__);
defined('BASE') OR define('BASE', dirname($dir) . '/');
require_once BASE . 'parsecsv.lib.php';
require_once BASE . 'ParseCsvForPhp.php';
if (!class_exists('PHPUnit\Framework\TestCase')) {
// we run on an older PHPUnit version without namespaces.
require_once __DIR__ . '/PHPUnit_Framework_TestCase.inc.php';
}
require_once BASE . 'tests/properties/BaseClass.php';

View File

@@ -3,66 +3,42 @@
class ConstructTest extends PHPUnit\Framework\TestCase {
/**
* CSV
* The parseCSV object
*
* @access protected
* @var [parseCSV]
* @var ParseCsvForPhp object
*/
protected $csv = null;
/**
* Setup
* Setup our test environment objects
*
* @access public
*/
public function setUp() {
//setup parse CSV
#$this->csv = new parseCSV();
}
/**
* Tear down
* Tear down our test environment objects
*
* @access public
*/
public function tearDown() {
$this->csv = null;
}
public function test_offset_param() {
$offset = 10;
$this->csv = new parseCSV(null, $offset);
$this->csv = new ParseCsvForPhp(null, $offset);
$this->assertTrue(is_numeric($this->csv->offset));
$this->assertEquals($offset, $this->csv->offset);
}
public function test_limit_param() {
$limit = 10;
$this->csv = new parseCSV(null, null, $limit);
$this->csv = new ParseCsvForPhp(null, null, $limit);
$this->assertTrue(is_numeric($this->csv->limit));
$this->assertEquals($limit, $this->csv->limit);
}
public function test_conditions_param() {
$conditions = 'some column NOT value';
$this->csv = new parseCSV(null, null, null, $conditions);
$this->csv = new ParseCsvForPhp(null, null, null, $conditions);
$this->assertTrue(is_string($this->csv->conditions));
$this->assertEquals($conditions, $this->csv->conditions);
}
public function test_keep_file_data_param() {
$keep = true;
$this->csv = new parseCSV(null, null, null, null, $keep);
$this->csv = new ParseCsvForPhp(null, null, null, null, $keep);
$this->assertTrue(is_bool($this->csv->keep_file_data));
$this->assertEquals($keep, $this->csv->keep_file_data);
}
public function test_input_param() {
$csv = "col1,col2,col3\r\nval1,val2,val3\r\nval1A,val2A,val3A\r\n";
$this->csv = new parseCSV($csv, null, null, null, true);
$this->csv = new ParseCsvForPhp($csv, null, null, null, true);
$this->assertTrue(is_string($this->csv->file_data));
$this->assertEquals($csv, $this->csv->file_data);
}

View File

@@ -3,11 +3,8 @@
class ParseTest extends PHPUnit\Framework\TestCase {
/**
* CSV
* The parseCSV object
*
* @access protected
* @var parseCSV
* @var ParseCsvForPhp
*/
protected $csv;
@@ -18,7 +15,7 @@ class ParseTest extends PHPUnit\Framework\TestCase {
* @access public
*/
public function setUp() {
$this->csv = new parseCSV();
$this->csv = new ParseCsvForPhp();
}
public function test_parse() {
@@ -152,8 +149,8 @@ class ParseTest extends PHPUnit\Framework\TestCase {
public function autoQuotesDataProvider() {
return array(
array('tests/methods/fixtures/auto-double-enclosure.csv', '"'),
array('tests/methods/fixtures/auto-single-enclosure.csv', "'"),
array('auto-double-enclosure.csv', '"'),
array('auto-single-enclosure.csv', "'"),
);
}
@@ -164,8 +161,8 @@ class ParseTest extends PHPUnit\Framework\TestCase {
* @param string $enclosure
*/
public function testAutoQuotes($file, $enclosure) {
$csv = new parseCSV();
$csv->auto($file, true, null, null, $enclosure);
$csv = new ParseCsvForPhp();
$csv->auto(__DIR__ . '/../example_files/' . $file, true, null, null, $enclosure);
$this->assertArrayHasKey('column1', $csv->data[0], 'Data parsed incorrectly with enclosure ' . $enclosure);
$this->assertEquals('value1', $csv->data[0]['column1'], 'Data parsed incorrectly with enclosure ' . $enclosure);
}

View File

@@ -2,7 +2,7 @@
class SaveTest extends PHPUnit\Framework\TestCase {
/** @var parseCSV */
/** @var ParseCsvForPhp */
private $csv;
private $temp_filename;
@@ -11,7 +11,7 @@ class SaveTest extends PHPUnit\Framework\TestCase {
* Setup our test environment objects; will be called before each test.
*/
public function setUp() {
$this->csv = new parseCSV();
$this->csv = new ParseCsvForPhp();
$this->csv->auto(__DIR__ . '/../example_files/single_column.csv');
// Remove last 2 lines to simplify comparison

View File

@@ -10,7 +10,7 @@
stopOnIncomplete="true"
stopOnSkipped="false">
<testsuites>
<testsuite name="parseCSV Test Suite">
<testsuite name="ParseCsvForPhp Test Suite">
<directory suffix="est.php">properties/</directory>
<directory suffix="est.php">methods/</directory>
</testsuite>

View File

@@ -0,0 +1,28 @@
<?php
class BaseClass extends PHPUnit\Framework\TestCase {
/**
* @access protected
* @var ParseCsvForPhp object
*/
protected $csv;
/**
* Setup
* Setup our test environment objects
*
* @access public
*/
public function setUp() {
$this->csv = new ParseCsvForPhp();
}
protected function _compareWithExpected($expected) {
$this->csv->auto(__DIR__ . '/../../examples/_books.csv');
$actual = array_map(function ($row) {
return $row['title'];
}, $this->csv->data);
$this->assertEquals($expected, $actual);
}
}

View File

@@ -0,0 +1,47 @@
<?php
class ConditionsTest extends BaseClass {
public function testNotDanBrown() {
$this->csv->conditions = 'author does not contain dan brown';
$this->_compareWithExpected([
'The Killing Kind',
'The Third Secret',
'The Last Templar',
'The Traveller',
'Crisis Four',
'Prey',
'The Broker (Paperback)',
'Without Blood (Paperback)',
'State of Fear (Paperback)',
'The Rule of Four (Paperback)',
]);
}
public function testRating() {
$this->csv->conditions = 'rating < 3';
$this->_compareWithExpected([
'The Killing Kind',
'The Third Secret',
]);
$this->csv->conditions = 'rating >= 5';
$this->_compareWithExpected([
'The Traveller',
'Prey',
'State of Fear (Paperback)',
'Digital Fortress : A Thriller (Mass Market Paperback)',
'Angels & Demons (Mass Market Paperback)',
]);
}
public function testTitleContainsSecretOrCode() {
$this->csv->conditions = 'title contains code OR title contains SECRET';
$this->_compareWithExpected([
'The Third Secret',
'The Da Vinci Code (Hardcover)',
]);
}
}

View File

@@ -0,0 +1,58 @@
<?php
class SortByTest extends BaseClass {
public function testSortByRating() {
$this->csv->sort_by = 'rating';
$this->csv->conditions = 'title does not contain Blood';
$this->_compareWithExpected([
// Rating 0
'The Killing Kind',
'The Third Secret',
// Rating 3
'The Last Templar',
'The Broker (Paperback)',
// Rating 4
'Deception Point (Paperback)',
'The Rule of Four (Paperback)',
'The Da Vinci Code (Hardcover)',
// Rating 5
'State of Fear (Paperback)',
'Prey',
'Digital Fortress : A Thriller (Mass Market Paperback)',
'Angels & Demons (Mass Market Paperback)',
]);
}
public function testReverseSortByRating() {
$this->csv->sort_by = 'rating';
$this->csv->conditions =
'title does not contain Prey AND ' .
'title does not contain Fortress AND ' .
'title does not contain Blood AND ' .
'title does not contain Fear';
$this->csv->sort_reverse = true;
$this->_compareWithExpected([
// Rating 5
'Angels & Demons (Mass Market Paperback)',
'The Traveller',
// Rating 4
'The Da Vinci Code (Hardcover)',
'The Rule of Four (Paperback)',
'Deception Point (Paperback)',
// Rating 3
'The Broker (Paperback)',
'The Last Templar',
// Rating 0
'The Third Secret',
'The Killing Kind',
]);
}
}

View File

@@ -3,11 +3,8 @@
class default_values_properties_Test extends PHPUnit\Framework\TestCase {
/**
* CSV
* The parseCSV object
*
* @access protected
* @var [parseCSV]
* @var ParseCsvForPhp object
*/
protected $csv = null;
@@ -19,17 +16,7 @@ class default_values_properties_Test extends PHPUnit\Framework\TestCase {
*/
public function setUp() {
//setup parse CSV
$this->csv = new parseCSV();
}
/**
* Tear down
* Tear down our test environment objects
*
* @access public
*/
public function tearDown() {
$this->csv = null;
$this->csv = new ParseCsvForPhp();
}
public function test_heading_default() {

View File

@@ -3,11 +3,8 @@
class worthless_properties_Test extends PHPUnit\Framework\TestCase {
/**
* CSV
* The parseCSV object
*
* @access protected
* @var [parseCSV]
* @var ParseCsvForPhp
*/
protected $csv = null;
@@ -16,7 +13,7 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase {
* The reflection class object
*
* @access protected
* @var [ReflectionClass]
* @var ReflectionClass
*/
protected $reflection = null;
@@ -25,6 +22,7 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase {
* The reflected class properties
*
* @access protected
* @var ReflectionProperty[]
*/
protected $properties = null;
@@ -36,7 +34,7 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase {
*/
public function setUp() {
//setup parse CSV
$this->csv = new parseCSV();
$this->csv = new ParseCsvForPhp();
//setup the reflection class
$this->reflection = new ReflectionClass($this->csv);
@@ -107,7 +105,7 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase {
'error',
'error_info',
'titles',
'data'
'data',
);
// Find our real properties
@@ -129,7 +127,7 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase {
public function test_count_public_properties() {
$counter = 0;
for ($a = 0; $a < count($this->properties); $a++) {
for ($a = count($this->properties) - 1; $a >= 0; $a--) {
if ($this->properties[$a]->isPublic() === true) {
$counter++;
}