mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 08:36:39 +00:00
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
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
class parseCSV {
|
class ParseCsvForPhp {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Class: parseCSV v0.4.3 beta
|
Class: parseCSV v1.0.0
|
||||||
https://github.com/parsecsv/parsecsv-for-php
|
https://github.com/parsecsv/parsecsv-for-php
|
||||||
|
|
||||||
Fully conforms to the specifications lined out on Wikipedia:
|
Fully conforms to the specifications lined out on Wikipedia:
|
||||||
@@ -39,23 +39,23 @@ class parseCSV {
|
|||||||
Code Examples
|
Code Examples
|
||||||
----------------
|
----------------
|
||||||
# general usage
|
# general usage
|
||||||
$csv = new parseCSV('data.csv');
|
$csv = new ParseCsvForPhp('data.csv');
|
||||||
print_r($csv->data);
|
print_r($csv->data);
|
||||||
----------------
|
----------------
|
||||||
# tab delimited, and encoding conversion
|
# tab delimited, and encoding conversion
|
||||||
$csv = new parseCSV();
|
$csv = new ParseCsvForPhp();
|
||||||
$csv->encoding('UTF-16', 'UTF-8');
|
$csv->encoding('UTF-16', 'UTF-8');
|
||||||
$csv->delimiter = "\t";
|
$csv->delimiter = "\t";
|
||||||
$csv->parse('data.tsv');
|
$csv->parse('data.tsv');
|
||||||
print_r($csv->data);
|
print_r($csv->data);
|
||||||
----------------
|
----------------
|
||||||
# auto-detect delimiter character
|
# auto-detect delimiter character
|
||||||
$csv = new parseCSV();
|
$csv = new ParseCsvForPhp();
|
||||||
$csv->auto('data.csv');
|
$csv->auto('data.csv');
|
||||||
print_r($csv->data);
|
print_r($csv->data);
|
||||||
----------------
|
----------------
|
||||||
# modify data in a csv file
|
# modify data in a csv file
|
||||||
$csv = new parseCSV();
|
$csv = new ParseCsvForPhp();
|
||||||
$csv->sort_by = 'id';
|
$csv->sort_by = 'id';
|
||||||
$csv->parse('data.csv');
|
$csv->parse('data.csv');
|
||||||
# "4" is the value of the "id" column of the CSV row
|
# "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
|
# add row/entry to end of CSV file
|
||||||
# - only recommended when you know the exact structure of the 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);
|
$csv->save('data.csv', array(array('1986', 'Home', 'Nowhere', '')), true);
|
||||||
----------------
|
----------------
|
||||||
# convert 2D array to csv data and send headers
|
# convert 2D array to csv data and send headers
|
||||||
# to browser to treat output as a file and download it
|
# 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'), ',');
|
$csv->output('movies.csv', $array, array('field 1', 'field 2'), ',');
|
||||||
----------------
|
----------------
|
||||||
*/
|
*/
|
||||||
18
README.md
18
README.md
@@ -18,9 +18,9 @@ Installation is easy using Composer. Include the following in your composer.json
|
|||||||
"parsecsv/php-parsecsv": "0.4.5"
|
"parsecsv/php-parsecsv": "0.4.5"
|
||||||
```
|
```
|
||||||
|
|
||||||
You may also manually include the parsecsv.lib.php file
|
You may also manually include the ParseCsvForPhp.php file
|
||||||
```php
|
```php
|
||||||
require_once 'parsecsv.lib.php';
|
require_once 'ParseCsvForPhp.php';
|
||||||
```
|
```
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
@@ -46,14 +46,14 @@ require_once 'parsecsv.lib.php';
|
|||||||
**General**
|
**General**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$csv = new parseCSV('data.csv');
|
$csv = new ParseCsvForPhp('data.csv');
|
||||||
print_r($csv->data);
|
print_r($csv->data);
|
||||||
```
|
```
|
||||||
|
|
||||||
**Tab delimited, and encoding conversion**
|
**Tab delimited, and encoding conversion**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$csv = new parseCSV();
|
$csv = new ParseCsvForPhp();
|
||||||
$csv->encoding('UTF-16', 'UTF-8');
|
$csv->encoding('UTF-16', 'UTF-8');
|
||||||
$csv->delimiter = "\t";
|
$csv->delimiter = "\t";
|
||||||
$csv->parse('data.tsv');
|
$csv->parse('data.tsv');
|
||||||
@@ -63,7 +63,7 @@ print_r($csv->data);
|
|||||||
**Auto-detect delimiter character**
|
**Auto-detect delimiter character**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$csv = new parseCSV();
|
$csv = new ParseCsvForPhp();
|
||||||
$csv->auto('data.csv');
|
$csv->auto('data.csv');
|
||||||
print_r($csv->data);
|
print_r($csv->data);
|
||||||
```
|
```
|
||||||
@@ -71,7 +71,7 @@ print_r($csv->data);
|
|||||||
**Modify data in a CSV file**
|
**Modify data in a CSV file**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$csv = new parseCSV();
|
$csv = new ParseCsvForPhp();
|
||||||
$csv->sort_by = 'id';
|
$csv->sort_by = 'id';
|
||||||
$csv->parse('data.csv');
|
$csv->parse('data.csv');
|
||||||
# "4" is the value of the "id" column of the CSV row
|
# "4" is the value of the "id" column of the CSV row
|
||||||
@@ -82,7 +82,7 @@ $csv->save();
|
|||||||
**Replace field names or set ones if missing**
|
**Replace field names or set ones if missing**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$csv = new parseCSV();
|
$csv = new ParseCsvForPhp();
|
||||||
$csv->fields = ['id', 'name', 'category']
|
$csv->fields = ['id', 'name', 'category']
|
||||||
$csv->parse('data.csv');
|
$csv->parse('data.csv');
|
||||||
```
|
```
|
||||||
@@ -92,7 +92,7 @@ $csv->parse('data.csv');
|
|||||||
_Only recommended when you know the exact structure of the file._
|
_Only recommended when you know the exact structure of the file._
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$csv = new parseCSV();
|
$csv = new ParseCsvForPhp();
|
||||||
$csv->save('data.csv', array(array('1986', 'Home', 'Nowhere', '')), true);
|
$csv->save('data.csv', array(array('1986', 'Home', 'Nowhere', '')), true);
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -100,7 +100,7 @@ $csv->save('data.csv', array(array('1986', 'Home', 'Nowhere', '')), true);
|
|||||||
a file and download it**
|
a file and download it**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$csv = new parseCSV();
|
$csv = new ParseCsvForPhp();
|
||||||
$csv->output('movies.csv', $array, array('field 1', 'field 2'), ',');
|
$csv->output('movies.csv', $array, array('field 1', 'field 2'), ',');
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -3,11 +3,11 @@
|
|||||||
|
|
||||||
|
|
||||||
# include parseCSV class.
|
# include parseCSV class.
|
||||||
require_once('../parsecsv.lib.php');
|
require_once('../ParseCsvForPhp.php');
|
||||||
|
|
||||||
|
|
||||||
# create new parseCSV object.
|
# create new parseCSV object.
|
||||||
$csv = new parseCSV();
|
$csv = new ParseCsvForPhp();
|
||||||
|
|
||||||
|
|
||||||
# Parse '_books.csv' using automatic delimiter detection...
|
# Parse '_books.csv' using automatic delimiter detection...
|
||||||
|
|||||||
@@ -3,11 +3,11 @@
|
|||||||
|
|
||||||
|
|
||||||
# include parseCSV class.
|
# include parseCSV class.
|
||||||
require_once('../parsecsv.lib.php');
|
require_once('../ParseCsvForPhp.php');
|
||||||
|
|
||||||
|
|
||||||
# create new parseCSV object.
|
# create new parseCSV object.
|
||||||
$csv = new parseCSV();
|
$csv = new ParseCsvForPhp();
|
||||||
|
|
||||||
|
|
||||||
# Example conditions:
|
# Example conditions:
|
||||||
|
|||||||
@@ -2,11 +2,11 @@
|
|||||||
|
|
||||||
|
|
||||||
# include parseCSV class.
|
# include parseCSV class.
|
||||||
require_once('../parsecsv.lib.php');
|
require_once('../ParseCsvForPhp.php');
|
||||||
|
|
||||||
|
|
||||||
# create new parseCSV object.
|
# create new parseCSV object.
|
||||||
$csv = new parseCSV();
|
$csv = new ParseCsvForPhp();
|
||||||
|
|
||||||
|
|
||||||
# Parse '_books.csv' using automatic delimiter detection...
|
# 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
|
# or is set to null, output will only return the generated CSV
|
||||||
# output data, and will not output to the browser itself.
|
# output data, and will not output to the browser itself.
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -3,11 +3,11 @@
|
|||||||
|
|
||||||
|
|
||||||
# include parseCSV class.
|
# include parseCSV class.
|
||||||
require_once('../parsecsv.lib.php');
|
require_once('../ParseCsvForPhp.php');
|
||||||
|
|
||||||
|
|
||||||
# create new parseCSV object.
|
# create new parseCSV object.
|
||||||
$csv = new parseCSV();
|
$csv = new ParseCsvForPhp();
|
||||||
|
|
||||||
|
|
||||||
# if sorting is enabled, the whole CSV file
|
# if sorting is enabled, the whole CSV file
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
$dir = realpath(__DIR__);
|
$dir = realpath(__DIR__);
|
||||||
defined('BASE') OR define('BASE', dirname($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')) {
|
if (!class_exists('PHPUnit\Framework\TestCase')) {
|
||||||
// we run on an older PHPUnit version without namespaces.
|
// we run on an older PHPUnit version without namespaces.
|
||||||
|
|||||||
@@ -11,17 +11,6 @@ class ConstructTest extends PHPUnit\Framework\TestCase {
|
|||||||
*/
|
*/
|
||||||
protected $csv = null;
|
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
|
||||||
* Tear down our test environment objects
|
* Tear down our test environment objects
|
||||||
@@ -34,35 +23,35 @@ class ConstructTest extends PHPUnit\Framework\TestCase {
|
|||||||
|
|
||||||
public function test_offset_param() {
|
public function test_offset_param() {
|
||||||
$offset = 10;
|
$offset = 10;
|
||||||
$this->csv = new parseCSV(null, $offset);
|
$this->csv = new ParseCsvForPhp(null, $offset);
|
||||||
$this->assertTrue(is_numeric($this->csv->offset));
|
$this->assertTrue(is_numeric($this->csv->offset));
|
||||||
$this->assertEquals($offset, $this->csv->offset);
|
$this->assertEquals($offset, $this->csv->offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_limit_param() {
|
public function test_limit_param() {
|
||||||
$limit = 10;
|
$limit = 10;
|
||||||
$this->csv = new parseCSV(null, null, $limit);
|
$this->csv = new ParseCsvForPhp(null, null, $limit);
|
||||||
$this->assertTrue(is_numeric($this->csv->limit));
|
$this->assertTrue(is_numeric($this->csv->limit));
|
||||||
$this->assertEquals($limit, $this->csv->limit);
|
$this->assertEquals($limit, $this->csv->limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_conditions_param() {
|
public function test_conditions_param() {
|
||||||
$conditions = 'some column NOT value';
|
$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->assertTrue(is_string($this->csv->conditions));
|
||||||
$this->assertEquals($conditions, $this->csv->conditions);
|
$this->assertEquals($conditions, $this->csv->conditions);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_keep_file_data_param() {
|
public function test_keep_file_data_param() {
|
||||||
$keep = true;
|
$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->assertTrue(is_bool($this->csv->keep_file_data));
|
||||||
$this->assertEquals($keep, $this->csv->keep_file_data);
|
$this->assertEquals($keep, $this->csv->keep_file_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_input_param() {
|
public function test_input_param() {
|
||||||
$csv = "col1,col2,col3\r\nval1,val2,val3\r\nval1A,val2A,val3A\r\n";
|
$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->assertTrue(is_string($this->csv->file_data));
|
||||||
$this->assertEquals($csv, $this->csv->file_data);
|
$this->assertEquals($csv, $this->csv->file_data);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ class ParseTest extends PHPUnit\Framework\TestCase {
|
|||||||
* The parseCSV object
|
* The parseCSV object
|
||||||
*
|
*
|
||||||
* @access protected
|
* @access protected
|
||||||
* @var parseCSV
|
* @var ParseCsvForPhp
|
||||||
*/
|
*/
|
||||||
protected $csv;
|
protected $csv;
|
||||||
|
|
||||||
@@ -18,7 +18,7 @@ class ParseTest extends PHPUnit\Framework\TestCase {
|
|||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
public function setUp() {
|
public function setUp() {
|
||||||
$this->csv = new parseCSV();
|
$this->csv = new ParseCsvForPhp();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_parse() {
|
public function test_parse() {
|
||||||
@@ -164,7 +164,7 @@ class ParseTest extends PHPUnit\Framework\TestCase {
|
|||||||
* @param string $enclosure
|
* @param string $enclosure
|
||||||
*/
|
*/
|
||||||
public function testAutoQuotes($file, $enclosure) {
|
public function testAutoQuotes($file, $enclosure) {
|
||||||
$csv = new parseCSV();
|
$csv = new ParseCsvForPhp();
|
||||||
$csv->auto(__DIR__ . '/../example_files/' . $file, true, null, null, $enclosure);
|
$csv->auto(__DIR__ . '/../example_files/' . $file, true, null, null, $enclosure);
|
||||||
$this->assertArrayHasKey('column1', $csv->data[0], 'Data parsed incorrectly with enclosure ' . $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);
|
$this->assertEquals('value1', $csv->data[0]['column1'], 'Data parsed incorrectly with enclosure ' . $enclosure);
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
class SaveTest extends PHPUnit\Framework\TestCase {
|
class SaveTest extends PHPUnit\Framework\TestCase {
|
||||||
|
|
||||||
/** @var parseCSV */
|
/** @var ParseCsvForPhp */
|
||||||
private $csv;
|
private $csv;
|
||||||
|
|
||||||
private $temp_filename;
|
private $temp_filename;
|
||||||
@@ -11,7 +11,7 @@ class SaveTest extends PHPUnit\Framework\TestCase {
|
|||||||
* Setup our test environment objects; will be called before each test.
|
* Setup our test environment objects; will be called before each test.
|
||||||
*/
|
*/
|
||||||
public function setUp() {
|
public function setUp() {
|
||||||
$this->csv = new parseCSV();
|
$this->csv = new ParseCsvForPhp();
|
||||||
$this->csv->auto(__DIR__ . '/../example_files/single_column.csv');
|
$this->csv->auto(__DIR__ . '/../example_files/single_column.csv');
|
||||||
|
|
||||||
// Remove last 2 lines to simplify comparison
|
// Remove last 2 lines to simplify comparison
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ class BaseClass extends PHPUnit\Framework\TestCase {
|
|||||||
* The parseCSV object
|
* The parseCSV object
|
||||||
*
|
*
|
||||||
* @access protected
|
* @access protected
|
||||||
* @var parseCSV
|
* @var ParseCsvForPhp
|
||||||
*/
|
*/
|
||||||
protected $csv;
|
protected $csv;
|
||||||
|
|
||||||
@@ -18,7 +18,7 @@ class BaseClass extends PHPUnit\Framework\TestCase {
|
|||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
public function setUp() {
|
public function setUp() {
|
||||||
$this->csv = new parseCSV();
|
$this->csv = new ParseCsvForPhp();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function _compareWithExpected($expected) {
|
protected function _compareWithExpected($expected) {
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ class default_values_properties_Test extends PHPUnit\Framework\TestCase {
|
|||||||
* The parseCSV object
|
* The parseCSV object
|
||||||
*
|
*
|
||||||
* @access protected
|
* @access protected
|
||||||
* @var [parseCSV]
|
* @var ParseCsvForPhp
|
||||||
*/
|
*/
|
||||||
protected $csv = null;
|
protected $csv = null;
|
||||||
|
|
||||||
@@ -19,7 +19,7 @@ class default_values_properties_Test extends PHPUnit\Framework\TestCase {
|
|||||||
*/
|
*/
|
||||||
public function setUp() {
|
public function setUp() {
|
||||||
//setup parse CSV
|
//setup parse CSV
|
||||||
$this->csv = new parseCSV();
|
$this->csv = new ParseCsvForPhp();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase {
|
|||||||
* The parseCSV object
|
* The parseCSV object
|
||||||
*
|
*
|
||||||
* @access protected
|
* @access protected
|
||||||
* @var [parseCSV]
|
* @var ParseCsvForPhp
|
||||||
*/
|
*/
|
||||||
protected $csv = null;
|
protected $csv = null;
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase {
|
|||||||
* The reflection class object
|
* The reflection class object
|
||||||
*
|
*
|
||||||
* @access protected
|
* @access protected
|
||||||
* @var [ReflectionClass]
|
* @var ReflectionClass
|
||||||
*/
|
*/
|
||||||
protected $reflection = null;
|
protected $reflection = null;
|
||||||
|
|
||||||
@@ -25,6 +25,7 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase {
|
|||||||
* The reflected class properties
|
* The reflected class properties
|
||||||
*
|
*
|
||||||
* @access protected
|
* @access protected
|
||||||
|
* @var ReflectionProperty[]
|
||||||
*/
|
*/
|
||||||
protected $properties = null;
|
protected $properties = null;
|
||||||
|
|
||||||
@@ -36,7 +37,7 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase {
|
|||||||
*/
|
*/
|
||||||
public function setUp() {
|
public function setUp() {
|
||||||
//setup parse CSV
|
//setup parse CSV
|
||||||
$this->csv = new parseCSV();
|
$this->csv = new ParseCsvForPhp();
|
||||||
|
|
||||||
//setup the reflection class
|
//setup the reflection class
|
||||||
$this->reflection = new ReflectionClass($this->csv);
|
$this->reflection = new ReflectionClass($this->csv);
|
||||||
@@ -107,7 +108,7 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase {
|
|||||||
'error',
|
'error',
|
||||||
'error_info',
|
'error_info',
|
||||||
'titles',
|
'titles',
|
||||||
'data'
|
'data',
|
||||||
);
|
);
|
||||||
|
|
||||||
// Find our real properties
|
// Find our real properties
|
||||||
@@ -129,7 +130,7 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase {
|
|||||||
public function test_count_public_properties() {
|
public function test_count_public_properties() {
|
||||||
$counter = 0;
|
$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) {
|
if ($this->properties[$a]->isPublic() === true) {
|
||||||
$counter++;
|
$counter++;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user