mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 08:36:39 +00:00
Compare commits
75 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
65566adcd3 | ||
|
|
2d428ffa93 | ||
|
|
bc9207de09 | ||
|
|
ace09c3c11 | ||
|
|
52ad56c66a | ||
|
|
ab9e8a0af9 | ||
|
|
8aa61914f7 | ||
|
|
3b74f7ce57 | ||
|
|
c7375dea8e | ||
|
|
6ce4550eed | ||
|
|
55890da647 | ||
|
|
21953ab6be | ||
|
|
1d8e6808bc | ||
|
|
063a812732 | ||
|
|
4504f4b158 | ||
|
|
b82ad03bd5 | ||
|
|
23a1b0a4b3 | ||
|
|
51d878a9a1 | ||
|
|
7d39b80fca | ||
|
|
8c7eba6815 | ||
|
|
c41c559ac2 | ||
|
|
e1ecf9302e | ||
|
|
de8e792ef4 | ||
|
|
60d6458080 | ||
|
|
406e1e415f | ||
|
|
2e40a2eb1c | ||
|
|
927e785891 | ||
|
|
e6ba24b47a | ||
|
|
f24e5bf3a3 | ||
|
|
bdd4e5ef25 | ||
|
|
42b5d30d66 | ||
|
|
93f177a396 | ||
|
|
f5ff7332a4 | ||
|
|
07846d33c1 | ||
|
|
4b60d38fb0 | ||
|
|
c8d15557cb | ||
|
|
10895788c8 | ||
|
|
ffed7ffdc0 | ||
|
|
7498a963ac | ||
|
|
49728a74bb | ||
|
|
e4c9fed6cf | ||
|
|
5bc6d09b5e | ||
|
|
b7f2075efc | ||
|
|
3cc20fbb47 | ||
|
|
086cd15b44 | ||
|
|
03bc946b98 | ||
|
|
8f431aa7a4 | ||
|
|
7168cb15e3 | ||
|
|
1142ef2611 | ||
|
|
80a4954a3a | ||
|
|
9d7ccab20f | ||
|
|
fb9325884d | ||
|
|
5daa422aca | ||
|
|
48a3cdbc5c | ||
|
|
fbe5263bca | ||
|
|
5b1002a677 | ||
|
|
4bbc928f09 | ||
|
|
951fc68886 | ||
|
|
68b849a37b | ||
|
|
aaefe2a480 | ||
|
|
611b1a92e8 | ||
|
|
e5eccf1fc1 | ||
|
|
9e5c97328d | ||
|
|
b6247c367c | ||
|
|
ba4cc0672a | ||
|
|
c9cc9697ef | ||
|
|
f8fe4cad03 | ||
|
|
95521cde87 | ||
|
|
249e5a24ac | ||
|
|
343c683077 | ||
|
|
cf91bf40ff | ||
|
|
958af1027e | ||
|
|
7a3120dd28 | ||
|
|
a74736d4da | ||
|
|
657cec4b4e |
@@ -6,7 +6,6 @@ php:
|
|||||||
- 7.0
|
- 7.0
|
||||||
- 5.6
|
- 5.6
|
||||||
- 5.5
|
- 5.5
|
||||||
- 5.4
|
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- phpunit --version
|
- phpunit --version
|
||||||
|
|||||||
560
ChangeLog.txt
560
ChangeLog.txt
@@ -1,272 +1,288 @@
|
|||||||
ParseCSV 1.0.0
|
ParseCSV dev-master
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
Date: 3-March-2018
|
Date: unreleased
|
||||||
|
|
||||||
- Renamed class from parseCSV to Csv and added name-
|
- New function getTotalDataRowCount() - useful if
|
||||||
space "ParseCsv" for PSR compliance.
|
$limit is set - see pull request #122.
|
||||||
|
|
||||||
- Added support for MS Excel's "sep=" to detect the
|
- Dropped support for PHP 5.4
|
||||||
delimiter (Issue #60).
|
|
||||||
|
- Added support for Laravel-style collections via the
|
||||||
- Added data type detection - function getDatatypes()
|
new getCollection() function - see
|
||||||
guesses the type of each column.
|
https://github.com/parsecsv/parsecsv-for-php/pull/134
|
||||||
|
|
||||||
- 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
|
ParseCSV 1.0.0
|
||||||
iconv() - see issue #109.
|
-----------------------------------
|
||||||
|
Date: 3-March-2018
|
||||||
- A number of minor bug fixes - see GitHub issues.
|
|
||||||
|
- Renamed class from parseCSV to Csv and added name-
|
||||||
- Added many more unit tests.
|
space "ParseCsv" for PSR compliance.
|
||||||
|
|
||||||
-----------------------------------
|
- Added support for MS Excel's "sep=" to detect the
|
||||||
|
delimiter (Issue #60).
|
||||||
|
|
||||||
parseCSV 0.4.3 beta
|
- Added data type detection - function getDatatypes()
|
||||||
-----------------------------------
|
guesses the type of each column.
|
||||||
Date: 1-July-2008
|
|
||||||
|
- MIME: output() sends correct MIME type to browser
|
||||||
- Issue #4. Added an option for setting sorting
|
if the separator is a tab char (Issue #79).
|
||||||
type behavior when sorting data.
|
|
||||||
Simply set $csv->sort_type to "regular", "numeric",
|
- Added support for mb_convert_encoding() instead of
|
||||||
or "string".
|
iconv() - see issue #109.
|
||||||
|
|
||||||
- Issue #6. Raw loaded file data is now cleared from
|
- A number of minor bug fixes - see GitHub issues.
|
||||||
file_data property when it has been successfully
|
|
||||||
parsed to keep parseCSV's memory footprint to a
|
- Added many more unit tests.
|
||||||
minimum. Specifically handy when using multiple
|
|
||||||
instances of parseCSV to process large files.
|
-----------------------------------
|
||||||
|
|
||||||
-----------------------------------
|
|
||||||
|
parseCSV 0.4.3 beta
|
||||||
|
-----------------------------------
|
||||||
parseCSV 0.4.2 beta
|
Date: 1-July-2008
|
||||||
-----------------------------------
|
|
||||||
Date: 31-May-2008
|
- Issue #4. Added an option for setting sorting
|
||||||
|
type behavior when sorting data.
|
||||||
- IMPORTANT! If you're using the output(),
|
Simply set $csv->sort_type to "regular", "numeric",
|
||||||
method please note that the first parameter
|
or "string".
|
||||||
has been completely removed as it was
|
|
||||||
technically just useless. Instead, the second
|
- Issue #6. Raw loaded file data is now cleared from
|
||||||
parameter (filename) doubles as its replacement.
|
file_data property when it has been successfully
|
||||||
Simply put, if filename is not set or null, the
|
parsed to keep parseCSV's memory footprint to a
|
||||||
output() method will not output a downloadable
|
minimum. Specifically handy when using multiple
|
||||||
file. Please update your existing code
|
instances of parseCSV to process large files.
|
||||||
when using 0.4.2 and later :)
|
|
||||||
|
-----------------------------------
|
||||||
- Small fix to the headers sent by the output()
|
|
||||||
method.
|
|
||||||
|
parseCSV 0.4.2 beta
|
||||||
- Added a download example using the output()
|
-----------------------------------
|
||||||
method to the examples folder.
|
Date: 31-May-2008
|
||||||
|
|
||||||
-----------------------------------
|
- IMPORTANT! If you're using the output(),
|
||||||
|
method please note that the first parameter
|
||||||
|
has been completely removed as it was
|
||||||
parseCSV 0.4.1 beta
|
technically just useless. Instead, the second
|
||||||
-----------------------------------
|
parameter (filename) doubles as its replacement.
|
||||||
Date: 29-May-2008
|
Simply put, if filename is not set or null, the
|
||||||
|
output() method will not output a downloadable
|
||||||
- Fixed a small bug in how the output() method
|
file. Please update your existing code
|
||||||
handles input data.
|
when using 0.4.2 and later :)
|
||||||
|
|
||||||
-----------------------------------
|
- Small fix to the headers sent by the output()
|
||||||
|
method.
|
||||||
|
|
||||||
parseCSV 0.4 beta
|
- Added a download example using the output()
|
||||||
-----------------------------------
|
method to the examples folder.
|
||||||
Date: 11-Apr-2008
|
|
||||||
|
-----------------------------------
|
||||||
- Error reporting for files/data which is corrupt
|
|
||||||
or has formatting errors like using double
|
|
||||||
quotes in a field without enclosing quotes. Or
|
parseCSV 0.4.1 beta
|
||||||
not escaping double quotes with a second one.
|
-----------------------------------
|
||||||
|
Date: 29-May-2008
|
||||||
- parse() method does not require input anymore
|
|
||||||
if the "$object->file" property has been set.
|
- Fixed a small bug in how the output() method
|
||||||
|
handles input data.
|
||||||
I'm calling this a beta release due to the heavy
|
|
||||||
modifications to the core parsing logic required
|
-----------------------------------
|
||||||
for error reporting to work. I have tested the
|
|
||||||
new code quite extensively, I'm fairly confident
|
|
||||||
that it still parses exactly as it always has.
|
parseCSV 0.4 beta
|
||||||
|
-----------------------------------
|
||||||
The second reason I'm calling it a beta release
|
Date: 11-Apr-2008
|
||||||
is cause I'm sure the error reporting code will
|
|
||||||
need more refinements and tweaks to detect more
|
- Error reporting for files/data which is corrupt
|
||||||
types of errors, as it's only picking two types
|
or has formatting errors like using double
|
||||||
or syntax errors right now. However, it seems
|
quotes in a field without enclosing quotes. Or
|
||||||
these two are the most common errors that you
|
not escaping double quotes with a second one.
|
||||||
would be likely to come across.
|
|
||||||
|
- parse() method does not require input anymore
|
||||||
-----------------------------------
|
if the "$object->file" property has been set.
|
||||||
|
|
||||||
|
I'm calling this a beta release due to the heavy
|
||||||
parseCSV 0.3.2
|
modifications to the core parsing logic required
|
||||||
-----------------------------------
|
for error reporting to work. I have tested the
|
||||||
Date: 1-Apr-2008
|
new code quite extensively, I'm fairly confident
|
||||||
|
that it still parses exactly as it always has.
|
||||||
This is primarily a bug-fix release for a critical
|
|
||||||
bug which was brought to my attention.
|
The second reason I'm calling it a beta release
|
||||||
|
is cause I'm sure the error reporting code will
|
||||||
- Fixed a critical bug in conditions parsing which
|
need more refinements and tweaks to detect more
|
||||||
would generate corrupt matching patterns causing
|
types of errors, as it's only picking two types
|
||||||
the condition(s) to not work at all in some
|
or syntax errors right now. However, it seems
|
||||||
situations.
|
these two are the most common errors that you
|
||||||
|
would be likely to come across.
|
||||||
- Fixed a small code error which would cause PHP to
|
|
||||||
generate a invalid offset notice when zero length
|
-----------------------------------
|
||||||
values were fed into the unparse() method to
|
|
||||||
generate CSV data from an array.
|
|
||||||
|
parseCSV 0.3.2
|
||||||
Notice: If you have been using the "parsecsv-stable"
|
-----------------------------------
|
||||||
branch as an external in any of your projects,
|
Date: 1-Apr-2008
|
||||||
please use the "stable/parsecsv" branch from this
|
|
||||||
point on as I will eventually remove the former due
|
This is primarily a bug-fix release for a critical
|
||||||
to it's stupid naming.
|
bug which was brought to my attention.
|
||||||
|
|
||||||
-----------------------------------
|
- Fixed a critical bug in conditions parsing which
|
||||||
|
would generate corrupt matching patterns causing
|
||||||
|
the condition(s) to not work at all in some
|
||||||
parseCSV 0.3.1
|
situations.
|
||||||
-----------------------------------
|
|
||||||
Date: 1-Sep-2007
|
- Fixed a small code error which would cause PHP to
|
||||||
|
generate a invalid offset notice when zero length
|
||||||
- Small change to default output settings to
|
values were fed into the unparse() method to
|
||||||
conform with RFC 4180 (http://rfc.net/rfc4180.html).
|
generate CSV data from an array.
|
||||||
Only the LF (line feed) character was used
|
|
||||||
by default to separate rows, rather than
|
Notice: If you have been using the "parsecsv-stable"
|
||||||
CRLF (carriage return & line feed).
|
branch as an external in any of your projects,
|
||||||
|
please use the "stable/parsecsv" branch from this
|
||||||
-----------------------------------
|
point on as I will eventually remove the former due
|
||||||
|
to it's stupid naming.
|
||||||
|
|
||||||
parseCSV 0.3.0
|
-----------------------------------
|
||||||
-----------------------------------
|
|
||||||
Date: 9-Aug-2007
|
|
||||||
|
parseCSV 0.3.1
|
||||||
- Changed to the MIT license.
|
-----------------------------------
|
||||||
|
Date: 1-Sep-2007
|
||||||
- Added offset and limit options.
|
|
||||||
|
- Small change to default output settings to
|
||||||
- Added SQL-like conditions for quickly
|
conform with RFC 4180 (http://rfc.net/rfc4180.html).
|
||||||
filtering out entries. Documentation on the
|
Only the LF (line feed) character was used
|
||||||
condition syntax is forthcoming.
|
by default to separate rows, rather than
|
||||||
|
CRLF (carriage return & line feed).
|
||||||
- Small parsing modification to comply
|
|
||||||
with some recent changes to the specifications
|
-----------------------------------
|
||||||
outlined on Wikipedia's Comma-separated values
|
|
||||||
article.
|
|
||||||
|
parseCSV 0.3.0
|
||||||
- Minor changes and optimizations, and a few
|
-----------------------------------
|
||||||
spelling corrections. Oops :)
|
Date: 9-Aug-2007
|
||||||
|
|
||||||
- Included more complex code examples in the
|
- Changed to the MIT license.
|
||||||
parseCSV download.
|
|
||||||
|
- Added offset and limit options.
|
||||||
-----------------------------------
|
|
||||||
|
- Added SQL-like conditions for quickly
|
||||||
|
filtering out entries. Documentation on the
|
||||||
parseCSV 0.2.1
|
condition syntax is forthcoming.
|
||||||
-----------------------------------
|
|
||||||
Date: 8-Aug-2007
|
- Small parsing modification to comply
|
||||||
|
with some recent changes to the specifications
|
||||||
- Fixed stupid code which caused auto function
|
outlined on Wikipedia's Comma-separated values
|
||||||
to not work in some situations.
|
article.
|
||||||
|
|
||||||
-----------------------------------
|
- Minor changes and optimizations, and a few
|
||||||
|
spelling corrections. Oops :)
|
||||||
|
|
||||||
parseCSV 0.2.0 beta
|
- Included more complex code examples in the
|
||||||
-----------------------------------
|
parseCSV download.
|
||||||
Date: 2-Jan-2007
|
|
||||||
|
-----------------------------------
|
||||||
- Added auto() function to automatically detect
|
|
||||||
delimiter character.
|
|
||||||
Useful for user upload in case delimiter is
|
parseCSV 0.2.1
|
||||||
comma (,), tab, or semi-colon (;). Some
|
-----------------------------------
|
||||||
versions of MS Excel for Windows use
|
Date: 8-Aug-2007
|
||||||
semi-colons instead of commas when saving to
|
|
||||||
CSV files.
|
- Fixed stupid code which caused auto function
|
||||||
It uses a process of elimination to eliminate
|
to not work in some situations.
|
||||||
characters that can not be the delimiter,
|
|
||||||
so it should work on all CSV-structured files
|
-----------------------------------
|
||||||
almost no matter what the delimiter is.
|
|
||||||
|
|
||||||
- Generally updated some of the core workings
|
parseCSV 0.2.0 beta
|
||||||
to increase performance, and offer better
|
-----------------------------------
|
||||||
support for large (1MB and up) files.
|
Date: 2-Jan-2007
|
||||||
|
|
||||||
- Added code examples to header comment.
|
- Added auto() function to automatically detect
|
||||||
|
delimiter character.
|
||||||
-----------------------------------
|
Useful for user upload in case delimiter is
|
||||||
|
comma (,), tab, or semi-colon (;). Some
|
||||||
|
versions of MS Excel for Windows use
|
||||||
parseCSV 0.1.6 beta
|
semi-colons instead of commas when saving to
|
||||||
-----------------------------------
|
CSV files.
|
||||||
Date: 22-Dec-2006
|
It uses a process of elimination to eliminate
|
||||||
|
characters that can not be the delimiter,
|
||||||
- Updated output() function.
|
so it should work on all CSV-structured files
|
||||||
|
almost no matter what the delimiter is.
|
||||||
-----------------------------------
|
|
||||||
|
- Generally updated some of the core workings
|
||||||
|
to increase performance, and offer better
|
||||||
parseCSV 0.1.5 beta
|
support for large (1MB and up) files.
|
||||||
-----------------------------------
|
|
||||||
Date: 22-Dec-2006
|
- Added code examples to header comment.
|
||||||
|
|
||||||
- Added output() function for easy output to
|
-----------------------------------
|
||||||
browser, for downloading features for example.
|
|
||||||
|
|
||||||
-----------------------------------
|
parseCSV 0.1.6 beta
|
||||||
|
-----------------------------------
|
||||||
|
Date: 22-Dec-2006
|
||||||
parseCSV 0.1.4 beta
|
|
||||||
-----------------------------------
|
- Updated output() function.
|
||||||
Date: 17-Dec-2006
|
|
||||||
|
-----------------------------------
|
||||||
- Minor changes and fixes
|
|
||||||
|
|
||||||
-----------------------------------
|
parseCSV 0.1.5 beta
|
||||||
|
-----------------------------------
|
||||||
|
Date: 22-Dec-2006
|
||||||
parseCSV 0.1.3 beta
|
|
||||||
-----------------------------------
|
- Added output() function for easy output to
|
||||||
Date: 17-Dec-2006
|
browser, for downloading features for example.
|
||||||
|
|
||||||
- Added GPL v2.0 license.
|
-----------------------------------
|
||||||
|
|
||||||
-----------------------------------
|
|
||||||
|
parseCSV 0.1.4 beta
|
||||||
|
-----------------------------------
|
||||||
parseCSV 0.1.2 beta
|
Date: 17-Dec-2006
|
||||||
-----------------------------------
|
|
||||||
Date: 17-Dec-2006
|
- Minor changes and fixes
|
||||||
|
|
||||||
- Added encoding() function for easier character
|
-----------------------------------
|
||||||
encoding configuration.
|
|
||||||
|
|
||||||
-----------------------------------
|
parseCSV 0.1.3 beta
|
||||||
|
-----------------------------------
|
||||||
|
Date: 17-Dec-2006
|
||||||
parseCSV 0.1.1 beta
|
|
||||||
-----------------------------------
|
- Added GPL v2.0 license.
|
||||||
Date: 24-Nov-2006
|
|
||||||
|
-----------------------------------
|
||||||
- Added support for a PHP die command on first
|
|
||||||
line of csv files if they have a .php extension
|
|
||||||
to protect secure data from being displayed
|
parseCSV 0.1.2 beta
|
||||||
directly to the browser.
|
-----------------------------------
|
||||||
|
Date: 17-Dec-2006
|
||||||
-----------------------------------
|
|
||||||
|
- Added encoding() function for easier character
|
||||||
|
encoding configuration.
|
||||||
parseCSV 0.1 beta
|
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
Date: 23-Nov-2006
|
|
||||||
|
|
||||||
- Initial release
|
parseCSV 0.1.1 beta
|
||||||
|
-----------------------------------
|
||||||
-----------------------------------
|
Date: 24-Nov-2006
|
||||||
|
|
||||||
|
- Added support for a PHP die command on first
|
||||||
|
line of csv files if they have a .php extension
|
||||||
|
to protect secure data from being displayed
|
||||||
|
directly to the browser.
|
||||||
|
|
||||||
|
-----------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
parseCSV 0.1 beta
|
||||||
|
-----------------------------------
|
||||||
|
Date: 23-Nov-2006
|
||||||
|
|
||||||
|
- Initial release
|
||||||
|
|
||||||
|
-----------------------------------
|
||||||
|
|||||||
45
README.md
45
README.md
@@ -26,7 +26,7 @@ and third-party support for handling CSV data in PHP.
|
|||||||
how different programs like Excel for example outputs CSV data.
|
how different programs like Excel for example outputs CSV data.
|
||||||
* Support for character encoding conversion using PHP's
|
* Support for character encoding conversion using PHP's
|
||||||
`iconv()` and `mb_convert_encoding()` functions.
|
`iconv()` and `mb_convert_encoding()` functions.
|
||||||
* Supports PHP 5.4 and higher.
|
* Supports PHP 5.5 and higher.
|
||||||
It certainly works with PHP 7.2 and all versions in between.
|
It certainly works with PHP 7.2 and all versions in between.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
@@ -77,8 +77,43 @@ $csv->auto('data.csv');
|
|||||||
print_r($csv->data);
|
print_r($csv->data);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**Parse data with offset**
|
||||||
|
* ignoring the first X (e.g. two) rows
|
||||||
|
```php
|
||||||
|
$csv = new ParseCsv\Csv();
|
||||||
|
$csv->offset = 2;
|
||||||
|
$csv->parse('data.csv');
|
||||||
|
print_r($csv->data);
|
||||||
|
```
|
||||||
|
|
||||||
|
**Limit the number of returned data rows**
|
||||||
|
```php
|
||||||
|
$csv = new ParseCsv\Csv();
|
||||||
|
$csv->limit = 5;
|
||||||
|
$csv->parse('data.csv');
|
||||||
|
print_r($csv->data);
|
||||||
|
```
|
||||||
|
|
||||||
|
**Get total number of data rows without parsing whole data**
|
||||||
|
* Excluding heading line if present (see $csv->header property)
|
||||||
|
```php
|
||||||
|
$csv = new ParseCsv\Csv();
|
||||||
|
$csv->load_data('data.csv');
|
||||||
|
$count = $csv->getTotalDataRowCount();
|
||||||
|
print_r($count);
|
||||||
|
```
|
||||||
|
|
||||||
|
**Get most common data type for each column (Requires PHP >= 5.5)**
|
||||||
|
|
||||||
|
```php
|
||||||
|
$csv = new ParseCsv\Csv('data.csv');
|
||||||
|
$csv->getDatatypes()
|
||||||
|
print_r($csv->data_types);
|
||||||
|
```
|
||||||
|
|
||||||
**Modify data in a CSV file**
|
**Modify data in a CSV file**
|
||||||
|
|
||||||
|
Change data values:
|
||||||
```php
|
```php
|
||||||
$csv = new ParseCsv\Csv();
|
$csv = new ParseCsv\Csv();
|
||||||
$csv->sort_by = 'id';
|
$csv->sort_by = 'id';
|
||||||
@@ -88,6 +123,14 @@ $csv->data[4] = array('firstname' => 'John', 'lastname' => 'Doe', 'email' => 'jo
|
|||||||
$csv->save();
|
$csv->save();
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Enclose each data value by quotes:
|
||||||
|
```php
|
||||||
|
$csv = new ParseCsv\Csv();
|
||||||
|
$csv->parse('data.csv');
|
||||||
|
$csv->enclose_all = true;
|
||||||
|
$csv->save();
|
||||||
|
```
|
||||||
|
|
||||||
**Replace field names or set ones if missing**
|
**Replace field names or set ones if missing**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
|||||||
@@ -10,13 +10,20 @@
|
|||||||
{
|
{
|
||||||
"name": "William Knauss",
|
"name": "William Knauss",
|
||||||
"email": "will.knauss@gmail.com"
|
"email": "will.knauss@gmail.com"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Susann Sgorzaly",
|
||||||
|
"homepage": "https://github.com/susgo"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Christian Bläul",
|
||||||
|
"homepage": "https://github.com/Fonata"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"autoload":{
|
"autoload":{
|
||||||
"psr-4":{
|
"psr-4":{
|
||||||
"ParseCsv\\": "src",
|
"ParseCsv\\": "src",
|
||||||
"ParseCsv\\extensions\\": "src\\extensions",
|
"ParseCsv\\extensions\\": "src\\extensions"
|
||||||
"ParseCsv\\tests\\": "tests"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"autoload-dev":{
|
"autoload-dev":{
|
||||||
@@ -24,7 +31,17 @@
|
|||||||
"ParseCsv\\tests\\": "tests"
|
"ParseCsv\\tests\\": "tests"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"require": {
|
||||||
|
"php": ">=5.5"
|
||||||
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "4.1.*"
|
"phpunit/phpunit": "4.1.*"
|
||||||
|
},
|
||||||
|
"suggest": {
|
||||||
|
"illuminate/support": "Fluent array interface for map functions"
|
||||||
|
},
|
||||||
|
"support": {
|
||||||
|
"issues": "https://github.com/parsecsv/parsecsv-for-php/issues",
|
||||||
|
"source": "https://github.com/parsecsv/parsecsv-for-php"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
27
examples/save_to_file_without_header_row.php
Normal file
27
examples/save_to_file_without_header_row.php
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
# include parseCSV class.
|
||||||
|
require __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
|
use ParseCsv\Csv;
|
||||||
|
|
||||||
|
|
||||||
|
# Create new parseCSV object.
|
||||||
|
$csv = new Csv();
|
||||||
|
|
||||||
|
# When saving, don't write the header row:
|
||||||
|
$csv->heading = false;
|
||||||
|
|
||||||
|
# Specify which columns to write, and in which order.
|
||||||
|
# We won't output the 'Awesome' column this time.
|
||||||
|
$csv->titles = ['Age', 'Name'];
|
||||||
|
|
||||||
|
# Data to write:
|
||||||
|
$csv->data = [
|
||||||
|
0 => ['Name' => 'Anne', 'Age' => 45, 'Awesome' => true],
|
||||||
|
1 => ['Name' => 'John', 'Age' => 44, 'Awesome' => false],
|
||||||
|
];
|
||||||
|
|
||||||
|
# Then we save the file to the file system:
|
||||||
|
$csv->save('people.csv');
|
||||||
@@ -6,6 +6,10 @@
|
|||||||
|
|
||||||
// Check if people used Composer to include this project in theirs
|
// Check if people used Composer to include this project in theirs
|
||||||
if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
|
if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
|
||||||
|
require __DIR__ . '/src/enums/AbstractEnum.php';
|
||||||
|
require __DIR__ . '/src/enums/DatatypeEnum.php';
|
||||||
|
require __DIR__ . '/src/enums/FileProcessingModeEnum.php';
|
||||||
|
require __DIR__ . '/src/enums/SortEnum.php';
|
||||||
require __DIR__ . '/src/extensions/DatatypeTrait.php';
|
require __DIR__ . '/src/extensions/DatatypeTrait.php';
|
||||||
require __DIR__ . '/src/Csv.php';
|
require __DIR__ . '/src/Csv.php';
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
297
src/Csv.php
297
src/Csv.php
@@ -2,12 +2,14 @@
|
|||||||
|
|
||||||
namespace ParseCsv;
|
namespace ParseCsv;
|
||||||
|
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use ParseCsv\enums\FileProcessingModeEnum;
|
||||||
|
use ParseCsv\enums\SortEnum;
|
||||||
use ParseCsv\extensions\DatatypeTrait;
|
use ParseCsv\extensions\DatatypeTrait;
|
||||||
|
|
||||||
class Csv {
|
class Csv {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Class: ParseCSV 1.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:
|
||||||
@@ -16,7 +18,6 @@ class Csv {
|
|||||||
Based on the concept of Ming Hong Ng's CsvFileParser class:
|
Based on the concept of Ming Hong Ng's CsvFileParser class:
|
||||||
- http://minghong.blogspot.com/2006/07/csv-parser-for-php.html
|
- http://minghong.blogspot.com/2006/07/csv-parser-for-php.html
|
||||||
|
|
||||||
|
|
||||||
(The MIT license)
|
(The MIT license)
|
||||||
|
|
||||||
Copyright (c) 2014 Jim Myhrberg.
|
Copyright (c) 2014 Jim Myhrberg.
|
||||||
@@ -89,7 +90,7 @@ class Csv {
|
|||||||
*
|
*
|
||||||
* @var string|null
|
* @var string|null
|
||||||
*/
|
*/
|
||||||
public $sort_type = null;
|
public $sort_type = SortEnum::SORT_TYPE_REGULAR;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delimiter
|
* Delimiter
|
||||||
@@ -125,7 +126,9 @@ class Csv {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Offset
|
* Offset
|
||||||
* Number of rows to ignore from beginning of data
|
* Number of rows to ignore from beginning of data. If present, the heading
|
||||||
|
* row is also counted (if $this->heading == true). In other words,
|
||||||
|
* $offset == 1 and $offset == 0 have the same meaning in that situation.
|
||||||
*
|
*
|
||||||
* @var int|null
|
* @var int|null
|
||||||
*/
|
*/
|
||||||
@@ -297,12 +300,36 @@ class Csv {
|
|||||||
* Class constructor
|
* Class constructor
|
||||||
*
|
*
|
||||||
* @param string|null $input The CSV string or a direct filepath
|
* @param string|null $input The CSV string or a direct filepath
|
||||||
* @param integer|null $offset Number of rows to ignore from the beginning of the data
|
* @param integer|null $offset Number of rows to ignore from the
|
||||||
* @param integer|null $limit Limits the number of returned rows to specified amount
|
* beginning of the data
|
||||||
* @param string|null $conditions Basic SQL-like conditions for row matching
|
* @param integer|null $limit Limits the number of returned rows
|
||||||
* @param null|true $keep_file_data Keep raw file data in memory after successful parsing (useful for debugging)
|
* to specified amount
|
||||||
|
* @param string|null $conditions Basic SQL-like conditions for row
|
||||||
|
* matching
|
||||||
|
* @param null|true $keep_file_data Keep raw file data in memory after
|
||||||
|
* successful parsing
|
||||||
|
* (useful for debugging)
|
||||||
*/
|
*/
|
||||||
public function __construct($input = null, $offset = null, $limit = null, $conditions = null, $keep_file_data = null) {
|
public function __construct($input = null, $offset = null, $limit = null, $conditions = null, $keep_file_data = null) {
|
||||||
|
$this->init($offset, $limit, $conditions, $keep_file_data);
|
||||||
|
|
||||||
|
if (!empty($input)) {
|
||||||
|
$this->parse($input);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param integer|null $offset Number of rows to ignore from the
|
||||||
|
* beginning of the data
|
||||||
|
* @param integer|null $limit Limits the number of returned rows
|
||||||
|
* to specified amount
|
||||||
|
* @param string|null $conditions Basic SQL-like conditions for row
|
||||||
|
* matching
|
||||||
|
* @param null|true $keep_file_data Keep raw file data in memory after
|
||||||
|
* successful parsing
|
||||||
|
* (useful for debugging)
|
||||||
|
*/
|
||||||
|
public function init($offset = null, $limit = null, $conditions = null, $keep_file_data = null) {
|
||||||
if (!is_null($offset)) {
|
if (!is_null($offset)) {
|
||||||
$this->offset = $offset;
|
$this->offset = $offset;
|
||||||
}
|
}
|
||||||
@@ -318,10 +345,6 @@ class Csv {
|
|||||||
if (!is_null($keep_file_data)) {
|
if (!is_null($keep_file_data)) {
|
||||||
$this->keep_file_data = $keep_file_data;
|
$this->keep_file_data = $keep_file_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($input)) {
|
|
||||||
$this->parse($input);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==============================================
|
// ==============================================
|
||||||
@@ -333,9 +356,12 @@ class Csv {
|
|||||||
* Parse a CSV file or string
|
* Parse a CSV file or string
|
||||||
*
|
*
|
||||||
* @param string|null $input The CSV string or a direct filepath
|
* @param string|null $input The CSV string or a direct filepath
|
||||||
* @param integer $offset Number of rows to ignore from the beginning of the data
|
* @param integer $offset Number of rows to ignore from the
|
||||||
* @param integer $limit Limits the number of returned rows to specified amount
|
* beginning of the data
|
||||||
* @param string $conditions Basic SQL-like conditions for row matching
|
* @param integer $limit Limits the number of returned rows to
|
||||||
|
* specified amount
|
||||||
|
* @param string $conditions Basic SQL-like conditions for row
|
||||||
|
* matching
|
||||||
*
|
*
|
||||||
* @return bool True on success
|
* @return bool True on success
|
||||||
*/
|
*/
|
||||||
@@ -344,32 +370,22 @@ class Csv {
|
|||||||
$input = $this->file;
|
$input = $this->file;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($input)) {
|
if (empty($input)) {
|
||||||
if (!is_null($offset)) {
|
return false;
|
||||||
$this->offset = $offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!is_null($limit)) {
|
|
||||||
$this->limit = $limit;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!is_null($conditions)) {
|
|
||||||
$this->conditions = $conditions;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strlen($input) <= PHP_MAXPATHLEN && is_readable($input)) {
|
|
||||||
$this->data = $this->parse_file($input);
|
|
||||||
} else {
|
|
||||||
$this->file_data = &$input;
|
|
||||||
$this->data = $this->parse_string();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->data === false) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
$this->init($offset, $limit, $conditions);
|
||||||
|
|
||||||
|
if (strlen($input) <= PHP_MAXPATHLEN && is_readable($input)) {
|
||||||
|
$this->file = $input;
|
||||||
|
$this->data = $this->parse_file();
|
||||||
|
} else {
|
||||||
|
$this->file = null;
|
||||||
|
$this->file_data = &$input;
|
||||||
|
$this->data = $this->parse_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->data !== false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -378,17 +394,19 @@ class Csv {
|
|||||||
*
|
*
|
||||||
* @param string $file File location to save to
|
* @param string $file File location to save to
|
||||||
* @param array $data 2D array of data
|
* @param array $data 2D array of data
|
||||||
* @param bool $append Append current data to end of target CSV, if file exists
|
* @param bool $append Append current data to end of target CSV, if file
|
||||||
* @param array $fields Field names
|
* exists
|
||||||
|
* @param array $fields Field names. Sets the header. If it is not set
|
||||||
|
* $this->titles would be used instead.
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function save($file = '', $data = array(), $append = false, $fields = array()) {
|
public function save($file = '', $data = array(), $append = FileProcessingModeEnum::MODE_FILE_OVERWRITE, $fields = array()) {
|
||||||
if (empty($file)) {
|
if (empty($file)) {
|
||||||
$file = &$this->file;
|
$file = &$this->file;
|
||||||
}
|
}
|
||||||
|
|
||||||
$mode = $append ? 'ab' : 'wb';
|
$mode = FileProcessingModeEnum::getAppendMode($append);
|
||||||
$is_php = preg_match('/\.php$/i', $file) ? true : false;
|
$is_php = preg_match('/\.php$/i', $file) ? true : false;
|
||||||
|
|
||||||
return $this->_wfile($file, $this->unparse($data, $fields, $append, $is_php), $mode);
|
return $this->_wfile($file, $this->unparse($data, $fields, $append, $is_php), $mode);
|
||||||
@@ -401,7 +419,10 @@ class Csv {
|
|||||||
* @param string|null $filename If a filename is specified here or in the
|
* @param string|null $filename If a filename is specified here or in the
|
||||||
* object, headers and data will be output
|
* object, headers and data will be output
|
||||||
* directly to browser as a downloadable
|
* directly to browser as a downloadable
|
||||||
* file.
|
* file. This file doesn't have to exist on
|
||||||
|
* the server; the parameter only affects
|
||||||
|
* how the download is called to the
|
||||||
|
* browser.
|
||||||
* @param array[] $data 2D array with data
|
* @param array[] $data 2D array with data
|
||||||
* @param array $fields Field names
|
* @param array $fields Field names
|
||||||
* @param string|null $delimiter character used to separate data
|
* @param string|null $delimiter character used to separate data
|
||||||
@@ -508,6 +529,44 @@ class Csv {
|
|||||||
return $this->delimiter;
|
return $this->delimiter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get total number of data rows (exclusive heading line if present) in CSV
|
||||||
|
* without parsing the whole data string.
|
||||||
|
*
|
||||||
|
* @return bool|int
|
||||||
|
*/
|
||||||
|
public function getTotalDataRowCount() {
|
||||||
|
if (empty($this->file_data)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = $this->file_data;
|
||||||
|
|
||||||
|
$this->_detect_and_remove_sep_row_from_data($data);
|
||||||
|
|
||||||
|
$pattern = sprintf('/%1$s[^%1$s]*%1$s/i', $this->enclosure);
|
||||||
|
preg_match_all($pattern, $data, $matches);
|
||||||
|
|
||||||
|
/** @var array[] $matches */
|
||||||
|
foreach ($matches[0] as $match) {
|
||||||
|
if (empty($match) || (strpos($match, $this->enclosure) === false)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$replace = str_replace(["\r", "\n"], '', $match);
|
||||||
|
$data = str_replace($match, $replace, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
$headingRow = $this->heading ? 1 : 0;
|
||||||
|
|
||||||
|
$count = substr_count($data, "\r")
|
||||||
|
+ substr_count($data, "\n")
|
||||||
|
- substr_count($data, "\r\n")
|
||||||
|
- $headingRow;
|
||||||
|
|
||||||
|
return $count;
|
||||||
|
}
|
||||||
|
|
||||||
// ==============================================
|
// ==============================================
|
||||||
// ----- [ Core Functions ] ---------------------
|
// ----- [ Core Functions ] ---------------------
|
||||||
// ==============================================
|
// ==============================================
|
||||||
@@ -520,7 +579,7 @@ class Csv {
|
|||||||
*
|
*
|
||||||
* @return array|bool
|
* @return array|bool
|
||||||
*/
|
*/
|
||||||
public function parse_file($file = null) {
|
protected function parse_file($file = null) {
|
||||||
if (is_null($file)) {
|
if (is_null($file)) {
|
||||||
$file = $this->file;
|
$file = $this->file;
|
||||||
}
|
}
|
||||||
@@ -543,7 +602,7 @@ class Csv {
|
|||||||
*
|
*
|
||||||
* @return array|false - 2D array with CSV data, or false on failure
|
* @return array|false - 2D array with CSV data, or false on failure
|
||||||
*/
|
*/
|
||||||
public function parse_string($data = null) {
|
protected function parse_string($data = null) {
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
if ($this->_check_data()) {
|
if ($this->_check_data()) {
|
||||||
$data = &$this->file_data;
|
$data = &$this->file_data;
|
||||||
@@ -694,13 +753,7 @@ class Csv {
|
|||||||
|
|
||||||
$this->titles = $head;
|
$this->titles = $head;
|
||||||
if (!empty($this->sort_by)) {
|
if (!empty($this->sort_by)) {
|
||||||
$sort_type = SORT_REGULAR;
|
$sort_type = SortEnum::getSorting($this->sort_type);
|
||||||
if ($this->sort_type == 'numeric') {
|
|
||||||
$sort_type = SORT_NUMERIC;
|
|
||||||
} elseif ($this->sort_type == 'string') {
|
|
||||||
$sort_type = SORT_STRING;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->sort_reverse ? krsort($rows, $sort_type) : ksort($rows, $sort_type);
|
$this->sort_reverse ? krsort($rows, $sort_type) : ksort($rows, $sort_type);
|
||||||
|
|
||||||
if ($this->offset !== null || $this->limit !== null) {
|
if ($this->offset !== null || $this->limit !== null) {
|
||||||
@@ -728,9 +781,12 @@ class Csv {
|
|||||||
*
|
*
|
||||||
* @return string CSV data
|
* @return string CSV data
|
||||||
*/
|
*/
|
||||||
public function unparse($data = array(), $fields = array(), $append = false, $is_php = false, $delimiter = null) {
|
public function unparse($data = array(), $fields = array(), $append = FileProcessingModeEnum::MODE_FILE_OVERWRITE, $is_php = false, $delimiter = null) {
|
||||||
if (!is_array($data) || empty($data)) {
|
if (!is_array($data) || empty($data)) {
|
||||||
$data = &$this->data;
|
$data = &$this->data;
|
||||||
|
} else {
|
||||||
|
/** @noinspection ReferenceMismatchInspection */
|
||||||
|
$this->data = $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_array($fields) || empty($fields)) {
|
if (!is_array($fields) || empty($fields)) {
|
||||||
@@ -745,8 +801,16 @@ class Csv {
|
|||||||
$entry = array();
|
$entry = array();
|
||||||
|
|
||||||
// create heading
|
// create heading
|
||||||
|
/** @noinspection ReferenceMismatchInspection */
|
||||||
|
$fieldOrder = $this->_validate_fields_for_unparse($fields);
|
||||||
|
if (!$fieldOrder && !empty($data)) {
|
||||||
|
$column_count = count($data[0]);
|
||||||
|
$columns = range(0, $column_count - 1, 1);
|
||||||
|
$fieldOrder = array_combine($columns, $columns);
|
||||||
|
}
|
||||||
|
|
||||||
if ($this->heading && !$append && !empty($fields)) {
|
if ($this->heading && !$append && !empty($fields)) {
|
||||||
foreach ($fields as $key => $column_name) {
|
foreach ($fieldOrder as $column_name) {
|
||||||
$entry[] = $this->_enclose_value($column_name, $delimiter);
|
$entry[] = $this->_enclose_value($column_name, $delimiter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -756,7 +820,8 @@ class Csv {
|
|||||||
|
|
||||||
// create data
|
// create data
|
||||||
foreach ($data as $key => $row) {
|
foreach ($data as $key => $row) {
|
||||||
foreach ($row as $cell_value) {
|
foreach (array_keys($fieldOrder) as $index) {
|
||||||
|
$cell_value = $row[$index];
|
||||||
$entry[] = $this->_enclose_value($cell_value, $delimiter);
|
$entry[] = $this->_enclose_value($cell_value, $delimiter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -771,6 +836,46 @@ class Csv {
|
|||||||
return $string;
|
return $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function _validate_fields_for_unparse($fields) {
|
||||||
|
if (empty($fields)) {
|
||||||
|
$fields = $this->titles;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($fields)) {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
|
||||||
|
// this is needed because sometime titles property is overwritten instead of using fields parameter!
|
||||||
|
$titlesOnParse = !empty($this->data) ? array_keys(reset($this->data)) : array();
|
||||||
|
|
||||||
|
// both are identical, also in ordering OR we have no data (only titles)
|
||||||
|
if (empty($titlesOnParse) || array_values($fields) === array_values($titlesOnParse)) {
|
||||||
|
return array_combine($fields, $fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
// if renaming given by: $oldName => $newName (maybe with reorder and / or subset):
|
||||||
|
// todo: this will only work if titles are unique
|
||||||
|
$fieldOrder = array_intersect(array_flip($fields), $titlesOnParse);
|
||||||
|
if (!empty($fieldOrder)) {
|
||||||
|
return array_flip($fieldOrder);
|
||||||
|
}
|
||||||
|
|
||||||
|
$fieldOrder = array_intersect($fields, $titlesOnParse);
|
||||||
|
if (!empty($fieldOrder)) {
|
||||||
|
return array_combine($fieldOrder, $fieldOrder);
|
||||||
|
}
|
||||||
|
|
||||||
|
// original titles are not given in fields. that is okay if count is okay.
|
||||||
|
if (count($fields) != count($titlesOnParse)) {
|
||||||
|
throw new \UnexpectedValueException(
|
||||||
|
"The specified fields do not match any titles and do not match column count.\n" .
|
||||||
|
"\$fields was " . print_r($fields, true) .
|
||||||
|
"\$titlesOnParse was " . print_r($titlesOnParse, true));
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_combine($titlesOnParse, $fields);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load local file or string
|
* Load local file or string
|
||||||
*
|
*
|
||||||
@@ -784,7 +889,7 @@ class Csv {
|
|||||||
|
|
||||||
if (is_null($input)) {
|
if (is_null($input)) {
|
||||||
$file = $this->file;
|
$file = $this->file;
|
||||||
} elseif (file_exists($input)) {
|
} elseif (\strlen($input) <= PHP_MAXPATHLEN && file_exists($input)) {
|
||||||
$file = $input;
|
$file = $input;
|
||||||
} else {
|
} else {
|
||||||
$data = $input;
|
$data = $input;
|
||||||
@@ -883,12 +988,19 @@ class Csv {
|
|||||||
*/
|
*/
|
||||||
protected function _validate_row_condition($row, $condition) {
|
protected function _validate_row_condition($row, $condition) {
|
||||||
$operators = array(
|
$operators = array(
|
||||||
'=', 'equals', 'is',
|
'=',
|
||||||
'!=', 'is not',
|
'equals',
|
||||||
'<', 'is less than',
|
'is',
|
||||||
'>', 'is greater than',
|
'!=',
|
||||||
'<=', 'is less than or equals',
|
'is not',
|
||||||
'>=', 'is greater than or equals',
|
'<',
|
||||||
|
'is less than',
|
||||||
|
'>',
|
||||||
|
'is greater than',
|
||||||
|
'<=',
|
||||||
|
'is less than or equals',
|
||||||
|
'>=',
|
||||||
|
'is greater than or equals',
|
||||||
'contains',
|
'contains',
|
||||||
'does not contain',
|
'does not contain',
|
||||||
);
|
);
|
||||||
@@ -906,16 +1018,14 @@ class Csv {
|
|||||||
$op = $capture[2];
|
$op = $capture[2];
|
||||||
$value = $capture[3];
|
$value = $capture[3];
|
||||||
|
|
||||||
if (preg_match('/^([\'\"]{1})(.*)([\'\"]{1})$/', $value, $capture)) {
|
if (preg_match('/^([\'\"]{1})(.*)([\'\"]{1})$/', $value, $capture) && $capture[1] == $capture[3]) {
|
||||||
if ($capture[1] == $capture[3]) {
|
$value = strtr($capture[2], array(
|
||||||
$value = strtr($capture[2], array(
|
"\\n" => "\n",
|
||||||
"\\n" => "\n",
|
"\\r" => "\r",
|
||||||
"\\r" => "\r",
|
"\\t" => "\t",
|
||||||
"\\t" => "\t",
|
));
|
||||||
));
|
|
||||||
|
|
||||||
$value = stripslashes($value);
|
$value = stripslashes($value);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (array_key_exists($field, $row)) {
|
if (array_key_exists($field, $row)) {
|
||||||
@@ -1046,21 +1156,19 @@ class Csv {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read local file
|
* Read local file.
|
||||||
*
|
*
|
||||||
* @param string|null $file local filename
|
* @param string $file local filename
|
||||||
*
|
*
|
||||||
* @return string|false Data from file, or false on failure
|
* @return string|false Data from file, or false on failure
|
||||||
*/
|
*/
|
||||||
protected function _rfile($file = null) {
|
protected function _rfile($file) {
|
||||||
if (is_readable($file)) {
|
if (is_readable($file)) {
|
||||||
if (!($fh = fopen($file, 'r'))) {
|
$data = file_get_contents($file);
|
||||||
|
if ($data === false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
return rtrim($data, "\r\n");
|
||||||
$data = fread($fh, filesize($file));
|
|
||||||
fclose($fh);
|
|
||||||
return $data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@@ -1095,7 +1203,7 @@ class Csv {
|
|||||||
* first line containing only "sep=;", where the last character is the
|
* first line containing only "sep=;", where the last character is the
|
||||||
* separator. Microsoft Excel is able to open such files.
|
* separator. Microsoft Excel is able to open such files.
|
||||||
*
|
*
|
||||||
* @param string $data file data
|
* @param string $data file data
|
||||||
*
|
*
|
||||||
* @return string|false detected delimiter, or false if none found
|
* @return string|false detected delimiter, or false if none found
|
||||||
*/
|
*/
|
||||||
@@ -1113,7 +1221,7 @@ class Csv {
|
|||||||
/**
|
/**
|
||||||
* Support for Excel-compatible sep=? row.
|
* Support for Excel-compatible sep=? row.
|
||||||
*
|
*
|
||||||
* @param string $data_string file data to be updated
|
* @param string $data_string file data to be updated
|
||||||
*
|
*
|
||||||
* @return bool TRUE if sep= line was found at the very beginning of the file
|
* @return bool TRUE if sep= line was found at the very beginning of the file
|
||||||
*/
|
*/
|
||||||
@@ -1209,4 +1317,29 @@ class Csv {
|
|||||||
ksort($filtered);
|
ksort($filtered);
|
||||||
$this->delimiter = reset($filtered);
|
$this->delimiter = reset($filtered);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getCollection
|
||||||
|
* Returns a Illuminate/Collection object
|
||||||
|
* This may prove to be helpful to people who want to
|
||||||
|
* create macros, and or use map functions
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @link https://laravel.com/docs/5.6/collections
|
||||||
|
*
|
||||||
|
* @throws \ErrorException - If the Illuminate\Support\Collection class is not found
|
||||||
|
*
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function getCollection() {
|
||||||
|
//does the Illuminate\Support\Collection class exists?
|
||||||
|
//this uses the autoloader to try to determine
|
||||||
|
//@see http://php.net/manual/en/function.class-exists.php
|
||||||
|
if (class_exists('Illuminate\Support\Collection', true) == false) {
|
||||||
|
throw new \ErrorException('It would appear you have not installed the illuminate/support package!');
|
||||||
|
}
|
||||||
|
|
||||||
|
//return the collection
|
||||||
|
return new Collection($this->data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
40
src/enums/AbstractEnum.php
Normal file
40
src/enums/AbstractEnum.php
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
namespace ParseCsv\enums;
|
||||||
|
|
||||||
|
use ReflectionClass;
|
||||||
|
|
||||||
|
abstract class AbstractEnum {
|
||||||
|
/**
|
||||||
|
* Creates a new value of some type
|
||||||
|
*
|
||||||
|
* @param mixed $value
|
||||||
|
*
|
||||||
|
* @throws \UnexpectedValueException if incompatible type is given.
|
||||||
|
*/
|
||||||
|
public function __construct($value)
|
||||||
|
{
|
||||||
|
if (!$this->isValid($value)) {
|
||||||
|
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . get_called_class());
|
||||||
|
}
|
||||||
|
$this->value = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getConstants(){
|
||||||
|
$class = get_called_class();
|
||||||
|
$reflection = new \ReflectionClass($class);
|
||||||
|
|
||||||
|
return $reflection->getConstants();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if enum value is valid
|
||||||
|
*
|
||||||
|
* @param $value
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function isValid($value)
|
||||||
|
{
|
||||||
|
return in_array($value, static::getConstants(), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,7 +9,7 @@ namespace ParseCsv\enums;
|
|||||||
*
|
*
|
||||||
* todo: needs a basic parent enum class for error handling.
|
* todo: needs a basic parent enum class for error handling.
|
||||||
*/
|
*/
|
||||||
class DatatypeEnum {
|
class DatatypeEnum extends AbstractEnum {
|
||||||
|
|
||||||
const __DEFAULT = self::TYPE_STRING;
|
const __DEFAULT = self::TYPE_STRING;
|
||||||
|
|
||||||
|
|||||||
28
src/enums/FileProcessingModeEnum.php
Normal file
28
src/enums/FileProcessingModeEnum.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ParseCsv\enums;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class FileProcessingEnum
|
||||||
|
*
|
||||||
|
* @package ParseCsv\enums
|
||||||
|
*
|
||||||
|
* todo extends a basic enum class after merging #121
|
||||||
|
*/
|
||||||
|
class FileProcessingModeEnum {
|
||||||
|
|
||||||
|
const __default = self::MODE_FILE_OVERWRITE;
|
||||||
|
|
||||||
|
const MODE_FILE_APPEND = true;
|
||||||
|
|
||||||
|
const MODE_FILE_OVERWRITE = false;
|
||||||
|
|
||||||
|
public static function getAppendMode($mode) {
|
||||||
|
if ($mode == self::MODE_FILE_APPEND){
|
||||||
|
return 'ab';
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'wb';
|
||||||
|
}
|
||||||
|
}
|
||||||
28
src/enums/SortEnum.php
Normal file
28
src/enums/SortEnum.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
namespace ParseCsv\enums;
|
||||||
|
|
||||||
|
|
||||||
|
class SortEnum extends AbstractEnum {
|
||||||
|
const __DEFAULT = self::SORT_TYPE_REGULAR;
|
||||||
|
|
||||||
|
const SORT_TYPE_REGULAR = 'regular';
|
||||||
|
|
||||||
|
const SORT_TYPE_NUMERIC = 'numeric';
|
||||||
|
|
||||||
|
const SORT_TYPE_STRING = 'string';
|
||||||
|
|
||||||
|
private static $sorting = array(
|
||||||
|
self::SORT_TYPE_REGULAR => SORT_REGULAR,
|
||||||
|
self::SORT_TYPE_STRING => SORT_STRING,
|
||||||
|
self::SORT_TYPE_NUMERIC => SORT_NUMERIC
|
||||||
|
);
|
||||||
|
|
||||||
|
public static function getSorting($type){
|
||||||
|
if (array_key_exists($type, self::$sorting)){
|
||||||
|
return self::$sorting[$type];
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::$sorting[self::__DEFAULT];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace ParseCsv\extensions;
|
namespace ParseCsv\extensions;
|
||||||
|
|
||||||
|
use ParseCsv\enums\DatatypeEnum;
|
||||||
|
|
||||||
trait DatatypeTrait {
|
trait DatatypeTrait {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -47,7 +49,7 @@ trait DatatypeTrait {
|
|||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
*
|
*
|
||||||
* @uses getDatatypeFromString
|
* @uses DatatypeEnum::getValidTypeFromSample
|
||||||
*
|
*
|
||||||
* @return array|bool
|
* @return array|bool
|
||||||
*/
|
*/
|
||||||
@@ -62,7 +64,7 @@ trait DatatypeTrait {
|
|||||||
$result = [];
|
$result = [];
|
||||||
foreach ($this->titles as $cName) {
|
foreach ($this->titles as $cName) {
|
||||||
$column = array_column($this->data, $cName);
|
$column = array_column($this->data, $cName);
|
||||||
$cDatatypes = array_map('ParseCsv\enums\DatatypeEnum::getValidTypeFromSample', $column);
|
$cDatatypes = array_map(DatatypeEnum::class . '::getValidTypeFromSample', $column);
|
||||||
|
|
||||||
$result[$cName] = $this->getMostFrequentDatatypeForColumn($cDatatypes);
|
$result[$cName] = $this->getMostFrequentDatatypeForColumn($cDatatypes);
|
||||||
}
|
}
|
||||||
@@ -71,4 +73,41 @@ trait DatatypeTrait {
|
|||||||
|
|
||||||
return !empty($this->data_types) ? $this->data_types : [];
|
return !empty($this->data_types) ? $this->data_types : [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check data type of titles / first row for auto detecting if this could be
|
||||||
|
* a heading line.
|
||||||
|
*
|
||||||
|
* Requires PHP >= 5.5
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*
|
||||||
|
* @uses DatatypeEnum::getValidTypeFromSample
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function autoDetectFileHasHeading(){
|
||||||
|
if (empty($this->data)){
|
||||||
|
throw new \UnexpectedValueException('No data set yet.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->heading){
|
||||||
|
$firstRow = $this->titles;
|
||||||
|
} else {
|
||||||
|
$firstRow = $this->data[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
$firstRow = array_filter($firstRow);
|
||||||
|
if (empty($firstRow)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$firstRowDatatype = array_map(DatatypeEnum::class . '::getValidTypeFromSample', $firstRow);
|
||||||
|
|
||||||
|
if ($this->getMostFrequentDatatypeForColumn($firstRowDatatype) !== DatatypeEnum::TYPE_STRING){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
22
tests/example_files/multiple_empty_lines.csv
Normal file
22
tests/example_files/multiple_empty_lines.csv
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
ID,dID,createdAt,else1,else2,else3,else4,else5,user,else6,else7,else8,else9,else10,else11,else12,else13,else14,else15,else16,else17,else18,else19,else20,else21,else22,else23,else24,else25,else26,else27,else28,else29,else30,else31,else32,else33,else34,else35,else36,else37,else38
|
||||||
|
418,0,2017-05-16,,,2018-01-22,22.01.2018 10:00:09,,admin,Ja,,10001,Abweichung,,10001,v1,1,ddd,100,1000,,HH,,v1,0,401,1,2,H1,,-1,10,1,111,Ja,2017-01-01,,12,0,11109,HH-100,default
|
||||||
|
419,0,2017-05-16,,,2017-05-16,14.07.2017 09:58:09,,admin,Ja,,,Abweichung,,10002,v2,1,ddd,200,500,DD,DD,,v2,1,402,2,4,H2,,-2,100,1,1111,Ja,2017-01-01,1,13,1,11109,DD-200,default
|
||||||
|
438,0,2017-05-16,,,2017-05-16,14.07.2017 09:58:29,,admin,Ja,,10021,Abweichung,,10021,v3,4,ddd,300,400,DD,DD,,v3,0,421,1,,H3,,-1,106,1,111,Ja,2017-05-08,,2,1,11109,DD-300,default
|
||||||
|
440,0,2017-05-16,,,,14.07.2017 09:58:53,,admin,Ja,,,Alt,,10023,v4,3,,400,500,BE,DD,,v4,1,423,3,,H4,,-3,143,1,1111,Ja,2017-01-01,1,33,1,11108,BE-400,default
|
||||||
|
441,0,2017-05-16,,,,14.07.2017 09:59:19,,admin,Ja,,,Fehlt,,10024,v5,3,,500,,BE,,,v5,2,424,4,,H5,,0,1435,0,111,Ja,2017-01-01,,12,1,0,BE-500,default
|
||||||
|
442,0,2017-05-16,,,,14.07.2017 10:00:46,,admin,Ja,,,Neu,,10025,v6,3,,100,,DD,,,v6,435,425,1,,H6,,0,10,0,1111,Ja,2017-01-01,,102,1,0,DD-100,default
|
||||||
|
443,0,2017-05-16,,,2017-07-04,14.07.2017 10:01:12,,admin,Ja,,,OK,,10026,v7,3,,200,200,DD,DD,,v7,32,426,2,2,H7,,0,100,0,111,Ja,2017-01-01,,77,1,0,DD-200,default
|
||||||
|
444,0,2017-05-16,,,,14.07.2017 10:02:13,,admin,Ja,,,Fehlt,,10027,v8,3,,300,,BE,,,v8,45,427,3,,H8,,0,200,0,1111,Ja,2017-01-01,,44,3,0,BE-300,default
|
||||||
|
445,0,2017-05-16,,,,14.07.2017 10:02:38,,admin,Ja,,,Fehlt,,10028,v9,3,,100,,BE,,,v9,45,428,4,,H9,,0,400,0,111,Ja,2017-01-01,,44,1,0,BE-100,default
|
||||||
|
446,0,2017-05-16,,,,14.07.2017 10:03:01,,admin,Ja,,,Fehlt,,10029,v10,3,,,400,,DD,,v10,45,429,,,H10,,0,1124,0,1111,Ja,2017-01-01,,89,1,0,,default
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
6
tests/example_files/waiver_field_separator.csv
Normal file
6
tests/example_files/waiver_field_separator.csv
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
keyword
|
||||||
|
liability waiver
|
||||||
|
release of liability form
|
||||||
|
release of liability
|
||||||
|
sample waiver
|
||||||
|
sample waiver form
|
||||||
|
@@ -58,8 +58,11 @@ class ConstructTest extends TestCase {
|
|||||||
ob_start();
|
ob_start();
|
||||||
/** @noinspection PhpIncludeInspection */
|
/** @noinspection PhpIncludeInspection */
|
||||||
require $script_file;
|
require $script_file;
|
||||||
if ($script_file != 'download.php') {
|
$ob_get_clean = ob_get_clean();
|
||||||
$this->assertContains('<td>', ob_get_clean());
|
$verb = strtok($script_file, '_.');
|
||||||
|
|
||||||
|
if (!in_array($verb, ['download', 'save'], true)) {
|
||||||
|
$this->assertContains('<td>', $ob_get_clean);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
chdir('..');
|
chdir('..');
|
||||||
|
|||||||
82
tests/methods/DataRowCountTest.php
Normal file
82
tests/methods/DataRowCountTest.php
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ParseCsv\tests\methods;
|
||||||
|
|
||||||
|
use ParseCsv\Csv;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
|
||||||
|
class DataRowCountTest extends TestCase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CSV
|
||||||
|
* The CSV object
|
||||||
|
*
|
||||||
|
* @access protected
|
||||||
|
* @var Csv
|
||||||
|
*/
|
||||||
|
protected $csv;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup
|
||||||
|
* Setup our test environment objects
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
public function setUp() {
|
||||||
|
$this->csv = new Csv();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function countRowsProvider() {
|
||||||
|
return [
|
||||||
|
'auto-double-enclosure' => [
|
||||||
|
'auto-double-enclosure.csv',
|
||||||
|
2,
|
||||||
|
],
|
||||||
|
'auto-single-enclosure' => [
|
||||||
|
'auto-single-enclosure.csv',
|
||||||
|
2,
|
||||||
|
],
|
||||||
|
'UTF-8_sep_row' => [
|
||||||
|
'datatype.csv',
|
||||||
|
3,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider countRowsProvider
|
||||||
|
*
|
||||||
|
* @param string $file
|
||||||
|
* @param int $expectedRows
|
||||||
|
*/
|
||||||
|
public function testGetTotalRowCountFromFile($file, $expectedRows) {
|
||||||
|
$this->csv->heading = true;
|
||||||
|
$this->csv->load_data(__DIR__ . '/fixtures/' . $file);
|
||||||
|
$this->assertEquals($expectedRows, $this->csv->getTotalDataRowCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetTotalRowCountMissingEndingLineBreak() {
|
||||||
|
$this->csv->heading = false;
|
||||||
|
$this->csv->enclosure = '"';
|
||||||
|
$sInput = "86545235689,a\r\n34365587654,b\r\n13469874576,\"c\r\nd\"";
|
||||||
|
$this->csv->load_data($sInput);
|
||||||
|
$this->assertEquals(3, $this->csv->getTotalDataRowCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetTotalRowCountSingleEnclosure() {
|
||||||
|
$this->csv->heading = false;
|
||||||
|
$this->csv->enclosure = "'";
|
||||||
|
$sInput = "86545235689,a\r\n34365587654,b\r\n13469874576,\'c\r\nd\'";
|
||||||
|
$this->csv->load_data($sInput);
|
||||||
|
$this->assertEquals(3, $this->csv->getTotalDataRowCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetTotalRowCountSingleRow() {
|
||||||
|
$this->csv->heading = false;
|
||||||
|
$this->csv->enclosure = "'";
|
||||||
|
$sInput = "86545235689";
|
||||||
|
$this->csv->load_data($sInput);
|
||||||
|
$this->assertEquals(1, $this->csv->getTotalDataRowCount());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,11 @@ namespace ParseCsv\tests\methods;
|
|||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This test checks for backwards compatibility: Does it work to
|
||||||
|
* - require the old "parsecsv.lib.php" instead of composer autoloading?
|
||||||
|
* - use the old class name "parseCSV"?
|
||||||
|
*/
|
||||||
class OldRequireTest extends TestCase {
|
class OldRequireTest extends TestCase {
|
||||||
|
|
||||||
protected function setUp() {
|
protected function setUp() {
|
||||||
@@ -15,7 +20,7 @@ class OldRequireTest extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @runInSeparateProcess because download.php uses header()
|
* @runInSeparateProcess so that disabled autoloading has an effect
|
||||||
*/
|
*/
|
||||||
public function testOldLibWithoutComposer() {
|
public function testOldLibWithoutComposer() {
|
||||||
|
|
||||||
@@ -25,4 +30,16 @@ class OldRequireTest extends TestCase {
|
|||||||
$this->assertEquals($output, []);
|
$this->assertEquals($output, []);
|
||||||
$this->assertEquals(0, $return_var);
|
$this->assertEquals(0, $return_var);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess so that disabled autoloading has an effect
|
||||||
|
*/
|
||||||
|
public function testOldLibWithOldClassName() {
|
||||||
|
|
||||||
|
file_put_contents('__eval.php', '<?php require "parsecsv.lib.php"; new parseCSV;');
|
||||||
|
exec("php __eval.php", $output, $return_var);
|
||||||
|
unlink('__eval.php');
|
||||||
|
$this->assertEquals($output, []);
|
||||||
|
$this->assertEquals(0, $return_var);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
21
tests/methods/OutputTest.php
Normal file
21
tests/methods/OutputTest.php
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ParseCsv\tests\methods;
|
||||||
|
|
||||||
|
use ParseCsv\Csv;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class OutputTest extends TestCase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess because download.php uses header()
|
||||||
|
*/
|
||||||
|
public function testOutputWithFourParameters() {
|
||||||
|
$csv = new Csv();
|
||||||
|
$data = [0 => ['a', 'b', 'c'], 1 => ['d', 'e', 'f']];
|
||||||
|
$fields = ['col1', 'col2', 'col3'];
|
||||||
|
$output = $csv->output('test.csv', $data, $fields, ',');
|
||||||
|
$expected = "col1,col2,col3\ra,b,c\rd,e,f\r";
|
||||||
|
self::assertEquals($expected, $output);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,13 +22,13 @@ class ParseTest extends TestCase {
|
|||||||
$this->csv = new Csv();
|
$this->csv = new Csv();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_parse() {
|
public function testParse() {
|
||||||
// can we trick 'is_readable' into whining? See #67.
|
// can we trick 'is_readable' into whining? See #67.
|
||||||
$this->parse_repetitive_string('c:/looks/like/a/path');
|
$this->parseRepetitiveString('c:/looks/like/a/path');
|
||||||
$this->parse_repetitive_string('http://looks/like/an/url');
|
$this->parseRepetitiveString('http://looks/like/an/url');
|
||||||
}
|
}
|
||||||
|
|
||||||
private function parse_repetitive_string($content) {
|
private function parseRepetitiveString($content) {
|
||||||
$this->csv->delimiter = ';';
|
$this->csv->delimiter = ';';
|
||||||
$this->csv->heading = false;
|
$this->csv->heading = false;
|
||||||
$success = $this->csv->parse(str_repeat($content . ';', 500));
|
$success = $this->csv->parse(str_repeat($content . ';', 500));
|
||||||
@@ -41,9 +41,11 @@ class ParseTest extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @depends test_parse
|
* @depends testParse
|
||||||
*
|
*
|
||||||
* @dataProvider autoDetectionProvider
|
* @dataProvider autoDetectionProvider
|
||||||
|
*
|
||||||
|
* @param string $file
|
||||||
*/
|
*/
|
||||||
public function testSepRowAutoDetection($file) {
|
public function testSepRowAutoDetection($file) {
|
||||||
// This file (parse_test.php) is encoded in UTF-8, hence comparison will
|
// This file (parse_test.php) is encoded in UTF-8, hence comparison will
|
||||||
@@ -85,7 +87,8 @@ class ParseTest extends TestCase {
|
|||||||
$this->csv->enclosure = '"';
|
$this->csv->enclosure = '"';
|
||||||
$sInput = "86545235689,a\r\n34365587654,b\r\n13469874576,\"c\r\nd\"";
|
$sInput = "86545235689,a\r\n34365587654,b\r\n13469874576,\"c\r\nd\"";
|
||||||
$expected_data = [86545235689, 34365587654, 13469874576];
|
$expected_data = [86545235689, 34365587654, 13469874576];
|
||||||
$actual_data = $this->csv->parse_string($sInput);
|
|
||||||
|
$actual_data = $this->invokeMethod($this->csv, 'parse_string', array($sInput));
|
||||||
$actual_column = array_map('reset', $actual_data);
|
$actual_column = array_map('reset', $actual_data);
|
||||||
$this->assertEquals($expected_data, $actual_column);
|
$this->assertEquals($expected_data, $actual_column);
|
||||||
$this->assertEquals([
|
$this->assertEquals([
|
||||||
@@ -95,7 +98,7 @@ class ParseTest extends TestCase {
|
|||||||
], array_map('next', $actual_data));
|
], array_map('next', $actual_data));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_single_column() {
|
public function testSingleColumn() {
|
||||||
$this->csv->auto(__DIR__ . '/../example_files/single_column.csv');
|
$this->csv->auto(__DIR__ . '/../example_files/single_column.csv');
|
||||||
$expected = [
|
$expected = [
|
||||||
['SMS' => '0444'],
|
['SMS' => '0444'],
|
||||||
@@ -106,11 +109,8 @@ class ParseTest extends TestCase {
|
|||||||
$this->assertEquals($expected, $this->csv->data);
|
$this->assertEquals($expected, $this->csv->data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_Piwik_data() {
|
public function testMatomoData() {
|
||||||
if (!function_exists('array_column')) {
|
// Matomo (Piwik) export cannot be read with
|
||||||
// function only available in PHP >= 5.5
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$this->csv->use_mb_convert_encoding = true;
|
$this->csv->use_mb_convert_encoding = true;
|
||||||
$this->csv->output_encoding = 'UTF-8';
|
$this->csv->output_encoding = 'UTF-8';
|
||||||
$this->csv->auto(__DIR__ . '/../example_files/Piwik_API_download.csv');
|
$this->csv->auto(__DIR__ . '/../example_files/Piwik_API_download.csv');
|
||||||
@@ -129,16 +129,29 @@ class ParseTest extends TestCase {
|
|||||||
], $aCity);
|
], $aCity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testWithMultipleNewlines() {
|
||||||
|
$this->csv->auto(__DIR__ . '/../example_files/multiple_empty_lines.csv');
|
||||||
|
$aElse9 = array_column($this->csv->data, 'else9');
|
||||||
|
|
||||||
|
/** @noinspection SpellCheckingInspection */
|
||||||
|
$this->assertEquals([
|
||||||
|
'Abweichung',
|
||||||
|
'Abweichung',
|
||||||
|
'Abweichung',
|
||||||
|
'Alt',
|
||||||
|
'Fehlt',
|
||||||
|
'Neu',
|
||||||
|
'OK',
|
||||||
|
'Fehlt',
|
||||||
|
'Fehlt',
|
||||||
|
'Fehlt',
|
||||||
|
], $aElse9);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @depends testSepRowAutoDetection
|
* @depends testSepRowAutoDetection
|
||||||
*/
|
*/
|
||||||
public function testGetColumnDatatypes() {
|
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->auto(__DIR__ . '/fixtures/datatype.csv');
|
||||||
$this->csv->getDatatypes();
|
$this->csv->getDatatypes();
|
||||||
$expected = [
|
$expected = [
|
||||||
@@ -153,29 +166,31 @@ class ParseTest extends TestCase {
|
|||||||
$this->assertEquals($expected, $this->csv->data_types);
|
$this->assertEquals($expected, $this->csv->data_types);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
public function testDataArrayKeysWhenSettingOffsetWithHeading() {
|
* @depends testSepRowAutoDetection
|
||||||
$this->csv->offset = 2;
|
*/
|
||||||
|
public function testAutoDetectFileHasHeading() {
|
||||||
$this->csv->auto(__DIR__ . '/fixtures/datatype.csv');
|
$this->csv->auto(__DIR__ . '/fixtures/datatype.csv');
|
||||||
$expected = [
|
$this->assertTrue($this->csv->autoDetectFileHasHeading());
|
||||||
'title',
|
|
||||||
'isbn',
|
|
||||||
'publishedAt',
|
|
||||||
'published',
|
|
||||||
'count',
|
|
||||||
'price'
|
|
||||||
];
|
|
||||||
|
|
||||||
$this->assertEquals($expected, array_keys($this->csv->data[0]));
|
$this->csv->heading = false;
|
||||||
|
$this->csv->auto(__DIR__ . '/fixtures/datatype.csv');
|
||||||
|
$this->assertTrue($this->csv->autoDetectFileHasHeading());
|
||||||
|
|
||||||
|
$this->csv->heading = false;
|
||||||
|
$sInput = "86545235689\r\n34365587654\r\n13469874576";
|
||||||
|
$this->csv->auto($sInput);
|
||||||
|
$this->assertFalse($this->csv->autoDetectFileHasHeading());
|
||||||
|
|
||||||
|
$this->csv->heading = true;
|
||||||
|
$sInput = "86545235689\r\n34365587654\r\n13469874576";
|
||||||
|
$this->csv->auto($sInput);
|
||||||
|
$this->assertFalse($this->csv->autoDetectFileHasHeading());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDataArrayKeysWhenSettingOffsetWithoutHeading() {
|
public function testVeryLongNonExistingFile() {
|
||||||
$this->csv->heading = false;
|
$this->csv->parse(str_repeat('long_string', PHP_MAXPATHLEN));
|
||||||
$this->csv->offset = 2;
|
$this->csv->auto(str_repeat('long_string', PHP_MAXPATHLEN));
|
||||||
$this->csv->auto(__DIR__ . '/fixtures/datatype.csv');
|
|
||||||
$expected = range(0, 5, 1);
|
|
||||||
|
|
||||||
$this->assertEquals($expected, array_keys($this->csv->data[0]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function _get_magazines_data() {
|
protected function _get_magazines_data() {
|
||||||
@@ -219,4 +234,34 @@ class ParseTest extends TestCase {
|
|||||||
$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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Call protected/private method of a class.
|
||||||
|
*
|
||||||
|
* @param object &$object Instantiated object that we will run method on.
|
||||||
|
* @param string $methodName Method name to call
|
||||||
|
* @param array $parameters Array of parameters to pass into method.
|
||||||
|
*
|
||||||
|
* @return mixed Method return.
|
||||||
|
*/
|
||||||
|
private function invokeMethod(&$object, $methodName, array $parameters = array()) {
|
||||||
|
$reflection = new \ReflectionClass(get_class($object));
|
||||||
|
$method = $reflection->getMethod($methodName);
|
||||||
|
$method->setAccessible(true);
|
||||||
|
|
||||||
|
return $method->invokeArgs($object, $parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testWaiverFieldSeparator() {
|
||||||
|
$this->assertSame(false, $this->csv->auto(__DIR__ . '/../example_files/waiver_field_separator.csv'));
|
||||||
|
$expected = [
|
||||||
|
'liability waiver',
|
||||||
|
'release of liability form',
|
||||||
|
'release of liability',
|
||||||
|
'sample waiver',
|
||||||
|
'sample waiver form',
|
||||||
|
];
|
||||||
|
$actual = array_column($this->csv->data, 'keyword');
|
||||||
|
$this->assertSame($expected, $actual);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,6 +49,13 @@ class SaveTest extends TestCase
|
|||||||
$this->saveAndCompare($expected);
|
$this->saveAndCompare($expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSaveWithNewHeader() {
|
||||||
|
$this->csv->linefeed = "\n";
|
||||||
|
$this->csv->titles = array("NewTitle");
|
||||||
|
$expected = "NewTitle\n0444\n5555\n";
|
||||||
|
$this->saveAndCompare($expected);
|
||||||
|
}
|
||||||
|
|
||||||
public function testSaveWithoutHeader() {
|
public function testSaveWithoutHeader() {
|
||||||
$this->csv->linefeed = "\n";
|
$this->csv->linefeed = "\n";
|
||||||
$this->csv->heading = false;
|
$this->csv->heading = false;
|
||||||
|
|||||||
96
tests/methods/UnparseTest.php
Normal file
96
tests/methods/UnparseTest.php
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ParseCsv\tests\methods;
|
||||||
|
|
||||||
|
use ParseCsv\Csv;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
|
||||||
|
class UnparseTest extends Testcase {
|
||||||
|
|
||||||
|
/** @var Csv */
|
||||||
|
private $csv;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup our test environment objects; will be called before each test.
|
||||||
|
*/
|
||||||
|
public function setUp() {
|
||||||
|
$this->csv = new Csv();
|
||||||
|
$this->csv->auto(__DIR__ . '/fixtures/auto-double-enclosure.csv');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUnparseWithParameters() {
|
||||||
|
$fields = array('a' => 'AA', 'b' => 'BB');
|
||||||
|
$data = [['a' => 'value1', 'b' => 'value2']];
|
||||||
|
$csv_object = new Csv();
|
||||||
|
$csv_string = $csv_object->unparse($data, $fields);
|
||||||
|
$this->assertEquals("AA,BB\rvalue1,value2\r", $csv_string);
|
||||||
|
|
||||||
|
$csv_object = new Csv();
|
||||||
|
$csv_object->linefeed = "\n";
|
||||||
|
$csv_string = $csv_object->unparse([[55, 66]]);
|
||||||
|
$this->assertEquals("55,66\n", $csv_string);
|
||||||
|
|
||||||
|
$csv_object = new Csv();
|
||||||
|
$data2 = [['a' => "multi\rline", 'b' => 'value2']];
|
||||||
|
$csv_object->enclosure = "'";
|
||||||
|
$csv_string = $csv_object->unparse($data2, $fields);
|
||||||
|
$this->assertEquals("AA,BB\r'multi\rline',value2\r", $csv_string);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUnparseDefault() {
|
||||||
|
$expected = "column1,column2\rvalue1,value2\rvalue3,value4\r";
|
||||||
|
$this->unparseAndCompare($expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUnparseDefaultWithoutHeading() {
|
||||||
|
$this->csv->heading = false;
|
||||||
|
$this->csv->auto(__DIR__ . '/fixtures/auto-double-enclosure.csv');
|
||||||
|
$expected = "column1,column2\rvalue1,value2\rvalue3,value4\r";
|
||||||
|
$this->unparseAndCompare($expected);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUnparseRenameFields() {
|
||||||
|
$expected = "C1,C2\rvalue1,value2\rvalue3,value4\r";
|
||||||
|
$this->unparseAndCompare($expected, array("C1", "C2"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testReorderFields() {
|
||||||
|
$expected = "column2,column1\rvalue2,value1\rvalue4,value3\r";
|
||||||
|
$this->unparseAndCompare($expected, array("column2", "column1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSubsetFields() {
|
||||||
|
$expected = "column1\rvalue1\rvalue3\r";
|
||||||
|
$this->unparseAndCompare($expected, array("column1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testReorderAndRenameFields() {
|
||||||
|
$fields = array(
|
||||||
|
'column2' => 'C2',
|
||||||
|
'column1' => 'C1',
|
||||||
|
);
|
||||||
|
$expected = "C2,C1\rvalue2,value1\rvalue4,value3\r";
|
||||||
|
$this->unparseAndCompare($expected, $fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUnparseDefaultFirstRowMissing(){
|
||||||
|
unset($this->csv->data[0]);
|
||||||
|
$expected = "column1,column2\rvalue3,value4\r";
|
||||||
|
$this->unparseAndCompare($expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUnparseDefaultWithoutData(){
|
||||||
|
unset($this->csv->data[0]);
|
||||||
|
unset($this->csv->data[1]);
|
||||||
|
$expected = "column1,column2\r";
|
||||||
|
$this->unparseAndCompare($expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function unparseAndCompare($expected, $fields = array()) {
|
||||||
|
$str = $this->csv->unparse($this->csv->data, $fields);
|
||||||
|
$this->assertEquals($expected, $str);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -57,7 +57,7 @@ class DefaultValuesPropertiesTest extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function test_sort_type_default() {
|
public function test_sort_type_default() {
|
||||||
$this->assertNull($this->csv->sort_type);
|
$this->assertEquals('regular', $this->csv->sort_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_delimiter_default() {
|
public function test_delimiter_default() {
|
||||||
|
|||||||
78
tests/properties/OffsetTest.php
Normal file
78
tests/properties/OffsetTest.php
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ParseCsv\tests\properties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests related to the $offset property
|
||||||
|
*/
|
||||||
|
class OffsetTest extends BaseClass {
|
||||||
|
public function testOffsetOfOne() {
|
||||||
|
$this->csv->offset = 1;
|
||||||
|
$this->csv->auto(__DIR__ . '/../methods/fixtures/datatype.csv');
|
||||||
|
$this->assertCount(3, $this->csv->data);
|
||||||
|
|
||||||
|
if (!function_exists('array_column')) {
|
||||||
|
// function only available in PHP >= 5.5
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$expected = [
|
||||||
|
'Красивая кулинария',
|
||||||
|
'The Wine Connoisseurs',
|
||||||
|
'Weißwein',
|
||||||
|
];
|
||||||
|
$actual = array_column($this->csv->data, 'title');
|
||||||
|
$this->assertEquals($expected, $actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function numberRangeZeroToFourProvider() {
|
||||||
|
return array_map(function ($number) {
|
||||||
|
return [$number];
|
||||||
|
}, range(0, 4));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider numberRangeZeroToFourProvider
|
||||||
|
*
|
||||||
|
* @param integer $offset
|
||||||
|
*/
|
||||||
|
public function testOffsetOfOneNoHeader($offset) {
|
||||||
|
$this->csv->offset = $offset;
|
||||||
|
$this->csv->heading = false;
|
||||||
|
$this->csv->auto(__DIR__ . '/../methods/fixtures/datatype.csv');
|
||||||
|
$this->assertCount(4 - $offset, $this->csv->data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDataArrayKeysWhenSettingOffsetWithHeading() {
|
||||||
|
$this->csv->offset = 2;
|
||||||
|
$this->csv->auto(__DIR__ . '/../methods/fixtures/datatype.csv');
|
||||||
|
$expected = [
|
||||||
|
[
|
||||||
|
'title' => 'The Wine Connoisseurs',
|
||||||
|
'isbn' => '2547-8548-2541',
|
||||||
|
'publishedAt' => '12.12.2011',
|
||||||
|
'published' => 'TRUE',
|
||||||
|
'count' => '',
|
||||||
|
'price' => 20.33,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'title' => 'Weißwein',
|
||||||
|
'isbn' => '1313-4545-8875',
|
||||||
|
'publishedAt' => '23.02.2012',
|
||||||
|
'published' => 'false',
|
||||||
|
'count' => 10,
|
||||||
|
'price' => 10,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $this->csv->data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDataArrayKeysWhenSettingOffsetWithoutHeading() {
|
||||||
|
$this->csv->heading = false;
|
||||||
|
$this->csv->offset = 2;
|
||||||
|
$this->csv->auto(__DIR__ . '/../methods/fixtures/datatype.csv');
|
||||||
|
$expected = range(0, 5, 1);
|
||||||
|
|
||||||
|
$this->assertEquals($expected, array_keys($this->csv->data[0]));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace ParseCsv\tests\properties;
|
namespace ParseCsv\tests\properties;
|
||||||
|
|
||||||
use ParseCsv\Csv;
|
use ParseCsv\Csv;
|
||||||
|
use ParseCsv\enums\SortEnum;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
class PublicPropertiesTest extends TestCase {
|
class PublicPropertiesTest extends TestCase {
|
||||||
@@ -145,4 +146,26 @@ class PublicPropertiesTest extends TestCase {
|
|||||||
|
|
||||||
$this->assertCount($counter, $this->properties);
|
$this->assertCount($counter, $this->properties);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testDefaultSortTypeIsRegular(){
|
||||||
|
$this->assertEquals(SortEnum::SORT_TYPE_REGULAR, $this->csv->sort_type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSetSortType(){
|
||||||
|
$this->csv->sort_type = 'numeric';
|
||||||
|
$this->assertEquals(SortEnum::SORT_TYPE_NUMERIC, $this->csv->sort_type);
|
||||||
|
|
||||||
|
$this->csv->sort_type = 'string';
|
||||||
|
$this->assertEquals(SortEnum::SORT_TYPE_STRING, $this->csv->sort_type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetSorting(){
|
||||||
|
$this->csv->sort_type = 'numeric';
|
||||||
|
$sorting = SortEnum::getSorting($this->csv->sort_type);
|
||||||
|
$this->assertEquals(SORT_NUMERIC, $sorting);
|
||||||
|
|
||||||
|
$this->csv->sort_type = 'string';
|
||||||
|
$sorting = SortEnum::getSorting($this->csv->sort_type);
|
||||||
|
$this->assertEquals(SORT_STRING, $sorting);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user