mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 08:36:39 +00:00
Compare commits
40 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f2b0aecd12 | ||
|
|
5d4643b201 | ||
| d5606f8b2a | |||
| 8cebcbd9bb | |||
| 518f5081fb | |||
|
|
a28fc6ab0a | ||
|
|
009820d190 | ||
|
|
facdf1c06c | ||
|
|
ee13c17157 | ||
|
|
99daaa7235 | ||
|
|
05826c2bbf | ||
|
|
731900effe | ||
|
|
913c3b1b94 | ||
|
|
96b2784d3c | ||
|
|
be01bc9ae4 | ||
|
|
b444afae2f | ||
|
|
bd6befe01e | ||
|
|
05ed8ec667 | ||
|
|
d21233c0f8 | ||
|
|
7a066f9d5e | ||
|
|
2425c1bbda | ||
|
|
eb502f9dad | ||
|
|
0af6e98670 | ||
|
|
b38d0de800 | ||
|
|
e15d144e27 | ||
|
|
4518e944c4 | ||
|
|
3ce6a82052 | ||
|
|
abae91a1a2 | ||
|
|
45f079b5dd | ||
|
|
fba67b84ff | ||
|
|
2a154869b4 | ||
|
|
d1186fcafa | ||
|
|
9c5eee23f6 | ||
|
|
6a54a6a509 | ||
|
|
8277b9682d | ||
|
|
0e54a2b9fa | ||
|
|
9c35237b1b | ||
|
|
1528a1f6ae | ||
|
|
aadb654edf | ||
|
|
8069409c73 |
@@ -14,5 +14,8 @@ insert_final_newline = true
|
||||
[composer.json]
|
||||
indent_size = 4
|
||||
|
||||
[.travis.yml]
|
||||
[Makefile]
|
||||
indent_style = tab
|
||||
|
||||
[*.yml,*.yaml]
|
||||
indent_size = 2
|
||||
|
||||
9
.gitattributes
vendored
Normal file
9
.gitattributes
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
/.editorconfig export-ignore
|
||||
/.gitattributes export-ignore
|
||||
/.gitignore export-ignore
|
||||
/.travis.yml export-ignore
|
||||
/ChangeLog.txt export-ignore
|
||||
/examples export-ignore
|
||||
/Makefile export-ignore
|
||||
/ruleset.xml export-ignore
|
||||
/tests export-ignore
|
||||
38
.github/workflows/ci.yml
vendored
Normal file
38
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
---
|
||||
name: CI
|
||||
on:
|
||||
push:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
php_version:
|
||||
- "7.4"
|
||||
- "7.3"
|
||||
- "7.2"
|
||||
- "7.1"
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: ${{ matrix.php_version }}
|
||||
env:
|
||||
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
- name: Get composer cache directory
|
||||
id: composer-cache
|
||||
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
|
||||
- name: Cache composer dependencies
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: ${{ steps.composer-cache.outputs.dir }}
|
||||
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
|
||||
restore-keys: ${{ runner.os }}-composer-
|
||||
- name: Install dependencies
|
||||
run: composer update
|
||||
- name: Validate dependencies
|
||||
run: composer validate
|
||||
- name: Run tests
|
||||
run: vendor/bin/phpunit --configuration tests/phpunit.xml
|
||||
5
.gitignore
vendored
5
.gitignore
vendored
@@ -1,6 +1,7 @@
|
||||
*.bak
|
||||
/.idea
|
||||
/composer.lock
|
||||
/coverage_clover.xml
|
||||
/phive.xml
|
||||
/tools
|
||||
composer.lock
|
||||
vendor/
|
||||
/vendor/
|
||||
|
||||
25
.travis.yml
25
.travis.yml
@@ -1,25 +0,0 @@
|
||||
dist: trusty
|
||||
language: php
|
||||
dist: trusty
|
||||
|
||||
php:
|
||||
- '7.4'
|
||||
- '7.3'
|
||||
- '7.2'
|
||||
- '7.1'
|
||||
- '7.0'
|
||||
|
||||
before_install:
|
||||
- composer update
|
||||
|
||||
script:
|
||||
- composer validate
|
||||
- vendor/bin/phpunit --version
|
||||
- vendor/bin/phpunit --configuration tests/phpunit.xml
|
||||
|
||||
notifications:
|
||||
email:
|
||||
recipients:
|
||||
- will.knauss@gmail.com
|
||||
on_success: never
|
||||
on_failure: always
|
||||
@@ -1,3 +1,54 @@
|
||||
ParseCSV 1.3.1
|
||||
-----------------------------------
|
||||
Date: 20-Jun-2021
|
||||
|
||||
Bugfix:
|
||||
- `parseFile()` will now set `$csv->data`.
|
||||
Until now, the parsed data was only returned.
|
||||
This adds consistency with `$csv->parse()`
|
||||
for the following operations on the object.
|
||||
|
||||
-----------------------------------
|
||||
|
||||
|
||||
ParseCSV 1.3.0
|
||||
-----------------------------------
|
||||
Date: 14-Apr-2021
|
||||
|
||||
Breaking changes:
|
||||
- Passing file paths to parse() or new Csv() is now deprecated
|
||||
and will be removed in v2.0.0. Use ->parseFile() instead.
|
||||
|
||||
WARNING: It will call trigger_error() for now.
|
||||
This may still break your application, depending on your
|
||||
php.ini config. You can add E_USER_DEPRECATED to the
|
||||
error_reporting mask to avoid breaking your application:
|
||||
|
||||
Example:
|
||||
|
||||
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_USER_DEPRECATED
|
||||
|
||||
This change is to avoid security issues: see issue #198.
|
||||
|
||||
Non-breaking deprecations:
|
||||
- The function load_data() is deprecated.
|
||||
Call loadFile() or loadDataString() instead.
|
||||
- Supplying CSV data (file content) to auto() is deprecated.
|
||||
Please use autoDetectionForDataString().
|
||||
|
||||
The motivation is to make subtle gotchas less likely: when the
|
||||
file cannot be found or read by PHP (permissions), its file path
|
||||
would be treated like CSV data.
|
||||
- Currently, there is code to parse .php: <?...?> tags are removed
|
||||
before the remaining file content is treated like a .csv file.
|
||||
This rarely-used functionality will be removed in v2.0.0.
|
||||
You are only affected if you use ParseCsv to parse .php files.
|
||||
|
||||
New features: none
|
||||
Bug fixes: none
|
||||
-----------------------------------
|
||||
|
||||
|
||||
ParseCSV 1.2.1
|
||||
-----------------------------------
|
||||
Date: 25-Apr-2020
|
||||
|
||||
18
Makefile
18
Makefile
@@ -11,5 +11,21 @@ phpunit-dep:
|
||||
exit 1 \
|
||||
)
|
||||
|
||||
# Requires:
|
||||
# - Docker: https://docker.com
|
||||
# - act: https://github.com/nektos/act
|
||||
local-ci:
|
||||
ifeq (, $(shell which act))
|
||||
define ACT_ERROR
|
||||
Consider running the following to install 'act':
|
||||
|
||||
curl https://raw.githubusercontent.com/nektos/act/master/install.sh | sudo bash
|
||||
|
||||
The dependency 'act' was not found
|
||||
endef
|
||||
$(error ${ACT_ERROR})
|
||||
endif
|
||||
act -P ubuntu-latest=shivammathur/node:latest -W .github/workflows/ci.yml
|
||||
|
||||
.SILENT:
|
||||
.PHONY: test phpunit-dep
|
||||
.PHONY: test phpunit-dep local-ci
|
||||
|
||||
79
README.md
79
README.md
@@ -6,7 +6,7 @@ fully conforms to the specifications outlined on the on the
|
||||
[Wikipedia article][CSV] (and thus RFC 4180). It has many advanced features which help make your
|
||||
life easier when dealing with CSV data.
|
||||
|
||||
You may not need a library at all: before using ParseCsv, please make sure if PHP's own `str_getcsv()`, ``fgetcvs()`` or `fputcsv()` meets your needs.
|
||||
You may not need a library at all: before using ParseCsv, please make sure if PHP's own `str_getcsv()`, ``fgetcsv()`` or `fputcsv()` meets your needs.
|
||||
|
||||
This library was originally created in early 2007 by [jimeh](https://github.com/jimeh) due to the lack of built-in
|
||||
and third-party support for handling CSV data in PHP.
|
||||
@@ -53,27 +53,20 @@ To use ParseCSV, you then have to add a `require 'parsecsv.lib.php';` line.
|
||||
|
||||
## Example Usage
|
||||
|
||||
**General**
|
||||
**Parse a tab-delimited CSV file with encoding conversion**
|
||||
|
||||
```php
|
||||
$csv = new ParseCsv\Csv('data.csv');
|
||||
print_r($csv->data);
|
||||
```
|
||||
|
||||
**Tab delimited, and encoding conversion**
|
||||
|
||||
```php
|
||||
$csv = new ParseCsv\Csv();
|
||||
$csv = new \ParseCsv\Csv();
|
||||
$csv->encoding('UTF-16', 'UTF-8');
|
||||
$csv->delimiter = "\t";
|
||||
$csv->parse('data.tsv');
|
||||
$csv->parseFile('data.tsv');
|
||||
print_r($csv->data);
|
||||
```
|
||||
|
||||
**Auto-detect delimiter character**
|
||||
**Auto-detect field delimiter character**
|
||||
|
||||
```php
|
||||
$csv = new ParseCsv\Csv();
|
||||
$csv = new \ParseCsv\Csv();
|
||||
$csv->auto('data.csv');
|
||||
print_r($csv->data);
|
||||
```
|
||||
@@ -81,25 +74,25 @@ print_r($csv->data);
|
||||
**Parse data with offset**
|
||||
* ignoring the first X (e.g. two) rows
|
||||
```php
|
||||
$csv = new ParseCsv\Csv();
|
||||
$csv = new \ParseCsv\Csv();
|
||||
$csv->offset = 2;
|
||||
$csv->parse('data.csv');
|
||||
$csv->parseFile('data.csv');
|
||||
print_r($csv->data);
|
||||
```
|
||||
|
||||
**Limit the number of returned data rows**
|
||||
```php
|
||||
$csv = new ParseCsv\Csv();
|
||||
$csv = new \ParseCsv\Csv();
|
||||
$csv->limit = 5;
|
||||
$csv->parse('data.csv');
|
||||
$csv->parseFile('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');
|
||||
$csv = new \ParseCsv\Csv();
|
||||
$csv->loadFile('data.csv');
|
||||
$count = $csv->getTotalDataRowCount();
|
||||
print_r($count);
|
||||
```
|
||||
@@ -107,8 +100,8 @@ print_r($count);
|
||||
**Get most common data type for each column**
|
||||
|
||||
```php
|
||||
$csv = new ParseCsv\Csv('data.csv');
|
||||
$csv->getDatatypes()
|
||||
$csv = new \ParseCsv\Csv('data.csv');
|
||||
$csv->getDatatypes();
|
||||
print_r($csv->data_types);
|
||||
```
|
||||
|
||||
@@ -116,9 +109,9 @@ print_r($csv->data_types);
|
||||
|
||||
Change data values:
|
||||
```php
|
||||
$csv = new ParseCsv\Csv();
|
||||
$csv = new \ParseCsv\Csv();
|
||||
$csv->sort_by = 'id';
|
||||
$csv->parse('data.csv');
|
||||
$csv->parseFile('data.csv');
|
||||
# "4" is the value of the "id" column of the CSV row
|
||||
$csv->data[4] = array('firstname' => 'John', 'lastname' => 'Doe', 'email' => 'john@doe.com');
|
||||
$csv->save();
|
||||
@@ -126,8 +119,8 @@ $csv->save();
|
||||
|
||||
Enclose each data value by quotes:
|
||||
```php
|
||||
$csv = new ParseCsv\Csv();
|
||||
$csv->parse('data.csv');
|
||||
$csv = new \ParseCsv\Csv();
|
||||
$csv->parseFile('data.csv');
|
||||
$csv->enclose_all = true;
|
||||
$csv->save();
|
||||
```
|
||||
@@ -135,9 +128,9 @@ $csv->save();
|
||||
**Replace field names or set ones if missing**
|
||||
|
||||
```php
|
||||
$csv = new ParseCsv\Csv();
|
||||
$csv->fields = ['id', 'name', 'category']
|
||||
$csv->parse('data.csv');
|
||||
$csv = new \ParseCsv\Csv();
|
||||
$csv->fields = ['id', 'name', 'category'];
|
||||
$csv->parseFile('data.csv');
|
||||
```
|
||||
|
||||
**Add row/entry to end of CSV file**
|
||||
@@ -145,20 +138,44 @@ $csv->parse('data.csv');
|
||||
_Only recommended when you know the exact structure of the file._
|
||||
|
||||
```php
|
||||
$csv = new ParseCsv\Csv();
|
||||
$csv = new \ParseCsv\Csv();
|
||||
$csv->save('data.csv', array(array('1986', 'Home', 'Nowhere', '')), /* append */ true);
|
||||
```
|
||||
|
||||
**Convert 2D array to CSV data and send headers to browser to treat output as
|
||||
a file and download it**
|
||||
|
||||
Your web app users would call this an export.
|
||||
|
||||
```php
|
||||
$csv = new ParseCsv\Csv();
|
||||
$csv->output('movies.csv', $array, array('field 1', 'field 2'), ',');
|
||||
$csv = new \ParseCsv\Csv();
|
||||
$csv->linefeed = "\n";
|
||||
$header = array('field 1', 'field 2');
|
||||
$csv->output('movies.csv', $data_array, $header, ',');
|
||||
```
|
||||
|
||||
For more complex examples, see the ``tests`` and `examples` directories.
|
||||
|
||||
## Test coverage
|
||||
|
||||
All tests are located in the `tests` directory. To execute tests, run the following commands:
|
||||
|
||||
````bash
|
||||
composer install
|
||||
composer run test
|
||||
````
|
||||
|
||||
When pushing code to GitHub, tests will be executed using GitHub Actions. The relevant configuration is in the
|
||||
file `.github/workflows/ci.yml`. To run the `test` action locally, you can execute the following command:
|
||||
|
||||
````bash
|
||||
make local-ci
|
||||
````
|
||||
|
||||
## Security
|
||||
|
||||
If you discover any security related issues, please email ParseCsv@blaeul.de instead of using GitHub issues.
|
||||
|
||||
## Credits
|
||||
|
||||
* ParseCsv is based on the concept of [Ming Hong Ng][ming]'s [CsvFileParser][]
|
||||
|
||||
@@ -45,6 +45,11 @@
|
||||
"dev-master": "1.0.x-dev"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"test": [
|
||||
"vendor/bin/phpunit -c tests tests --disallow-test-output --coverage-clover coverage_clover.xml --whitelist src"
|
||||
]
|
||||
},
|
||||
"support": {
|
||||
"issues": "https://github.com/parsecsv/parsecsv-for-php/issues",
|
||||
"source": "https://github.com/parsecsv/parsecsv-for-php"
|
||||
|
||||
@@ -18,8 +18,8 @@ $csv->auto('_books.csv');
|
||||
# if its not the default comma...
|
||||
// $csv->delimiter = "\t"; # tab delimited
|
||||
|
||||
# ...and then use the parse() function.
|
||||
// $csv->parse('_books.csv');
|
||||
# ...and then use the parseFile() function.
|
||||
// $csv->parseFile('_books.csv');
|
||||
|
||||
|
||||
# Output result.
|
||||
|
||||
243
src/Csv.php
243
src/Csv.php
@@ -49,7 +49,7 @@ class Csv {
|
||||
*/
|
||||
|
||||
/**
|
||||
* Heading
|
||||
* Header row:
|
||||
* Use first line/entry as field names
|
||||
*
|
||||
* @var bool
|
||||
@@ -57,7 +57,6 @@ class Csv {
|
||||
public $heading = true;
|
||||
|
||||
/**
|
||||
* Fields
|
||||
* Override field names
|
||||
*
|
||||
* @var array
|
||||
@@ -65,7 +64,6 @@ class Csv {
|
||||
public $fields = array();
|
||||
|
||||
/**
|
||||
* Sort By
|
||||
* Sort CSV by this field
|
||||
*
|
||||
* @var string|null
|
||||
@@ -73,15 +71,13 @@ class Csv {
|
||||
public $sort_by = null;
|
||||
|
||||
/**
|
||||
* Sort Reverse
|
||||
* Reverse the sort function
|
||||
* Reverse the sort direction
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $sort_reverse = false;
|
||||
|
||||
/**
|
||||
* Sort Type
|
||||
* Sort behavior passed to sort methods
|
||||
*
|
||||
* regular = SORT_REGULAR
|
||||
@@ -93,31 +89,34 @@ class Csv {
|
||||
public $sort_type = SortEnum::SORT_TYPE_REGULAR;
|
||||
|
||||
/**
|
||||
* Delimiter
|
||||
* Delimiter character
|
||||
* Field delimiter character
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $delimiter = ',';
|
||||
|
||||
/**
|
||||
* Enclosure
|
||||
* Enclosure character
|
||||
*
|
||||
* This is useful for cell values that are either multi-line
|
||||
* or contain the field delimiter character.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $enclosure = '"';
|
||||
|
||||
/**
|
||||
* Enclose All
|
||||
* Force enclosing all columns
|
||||
* Force enclosing all columns.
|
||||
*
|
||||
* If false, only cells that are either multi-line or
|
||||
* contain the field delimiter character are enclosed
|
||||
* in the $enclosure char.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $enclose_all = false;
|
||||
|
||||
/**
|
||||
* Conditions
|
||||
* Basic SQL-Like conditions for row matching
|
||||
*
|
||||
* @var string|null
|
||||
@@ -125,7 +124,6 @@ class Csv {
|
||||
public $conditions = null;
|
||||
|
||||
/**
|
||||
* Offset
|
||||
* 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.
|
||||
@@ -135,7 +133,6 @@ class Csv {
|
||||
public $offset = null;
|
||||
|
||||
/**
|
||||
* Limit
|
||||
* Limits the number of returned rows to the specified amount
|
||||
*
|
||||
* @var int|null
|
||||
@@ -143,7 +140,6 @@ class Csv {
|
||||
public $limit = null;
|
||||
|
||||
/**
|
||||
* Auto Depth
|
||||
* Number of rows to analyze when attempting to auto-detect delimiter
|
||||
*
|
||||
* @var int
|
||||
@@ -151,7 +147,6 @@ class Csv {
|
||||
public $auto_depth = 15;
|
||||
|
||||
/**
|
||||
* Auto Non Chars
|
||||
* Characters that should be ignored when attempting to auto-detect delimiter
|
||||
*
|
||||
* @var string
|
||||
@@ -159,7 +154,6 @@ class Csv {
|
||||
public $auto_non_chars = "a-zA-Z0-9\n\r";
|
||||
|
||||
/**
|
||||
* Auto Preferred
|
||||
* preferred delimiter characters, only used when all filtering method
|
||||
* returns multiple possible delimiters (happens very rarely)
|
||||
*
|
||||
@@ -168,15 +162,14 @@ class Csv {
|
||||
public $auto_preferred = ",;\t.:|";
|
||||
|
||||
/**
|
||||
* Convert Encoding
|
||||
* Should we convert the CSV character encoding?
|
||||
* Used for both parse and unparse operations.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $convert_encoding = false;
|
||||
|
||||
/**
|
||||
* Input Encoding
|
||||
* Set the input encoding
|
||||
*
|
||||
* @var string
|
||||
@@ -184,7 +177,6 @@ class Csv {
|
||||
public $input_encoding = 'ISO-8859-1';
|
||||
|
||||
/**
|
||||
* Output Encoding
|
||||
* Set the output encoding
|
||||
*
|
||||
* @var string
|
||||
@@ -202,15 +194,14 @@ class Csv {
|
||||
public $use_mb_convert_encoding = false;
|
||||
|
||||
/**
|
||||
* Linefeed
|
||||
* Line feed characters used by unparse, save, and output methods
|
||||
* Popular choices are "\r\n" and "\n".
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $linefeed = "\r";
|
||||
|
||||
/**
|
||||
* Output Delimiter
|
||||
* Sets the output delimiter used by the output method
|
||||
*
|
||||
* @var string
|
||||
@@ -218,7 +209,6 @@ class Csv {
|
||||
public $output_delimiter = ',';
|
||||
|
||||
/**
|
||||
* Output filename
|
||||
* Sets the output filename
|
||||
*
|
||||
* @var string
|
||||
@@ -226,7 +216,6 @@ class Csv {
|
||||
public $output_filename = 'data.csv';
|
||||
|
||||
/**
|
||||
* Keep File Data
|
||||
* keep raw file data in memory after successful parsing (useful for debugging)
|
||||
*
|
||||
* @var bool
|
||||
@@ -270,7 +259,6 @@ class Csv {
|
||||
public $error = 0;
|
||||
|
||||
/**
|
||||
* Error Information
|
||||
* Detailed error information
|
||||
*
|
||||
* @var array
|
||||
@@ -298,20 +286,23 @@ class Csv {
|
||||
public $titles = array();
|
||||
|
||||
/**
|
||||
* Data
|
||||
* Two-dimensional array of CSV data
|
||||
* Two-dimensional array of CSV data.
|
||||
* The first dimension are the line numbers. Each line is represented as an array with field names as keys.
|
||||
*
|
||||
* @var array
|
||||
* @var array<array>
|
||||
*/
|
||||
public $data = array();
|
||||
|
||||
use DatatypeTrait;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Class constructor
|
||||
*
|
||||
* @param string|null $input The CSV string or a direct file path
|
||||
* @param string|null $data The CSV string or a direct file path.
|
||||
*
|
||||
* WARNING: Supplying file paths here is
|
||||
* deprecated. Use parseFile() instead.
|
||||
*
|
||||
* @param int|null $offset Number of rows to ignore from the
|
||||
* beginning of the data
|
||||
* @param int|null $limit Limits the number of returned rows
|
||||
@@ -322,11 +313,11 @@ class Csv {
|
||||
* successful parsing
|
||||
* (useful for debugging)
|
||||
*/
|
||||
public function __construct($input = null, $offset = null, $limit = null, $conditions = null, $keep_file_data = null) {
|
||||
public function __construct($data = 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);
|
||||
if (!empty($data)) {
|
||||
$this->parse($data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -364,10 +355,12 @@ class Csv {
|
||||
// ==============================================
|
||||
|
||||
/**
|
||||
* Parse
|
||||
* Parse a CSV file or string
|
||||
*
|
||||
* @param string|null $input The CSV string or a direct file path
|
||||
* @param string|null $dataString The CSV string or a direct file path
|
||||
* WARNING: Supplying file paths here is
|
||||
* deprecated and will trigger an
|
||||
* E_USER_DEPRECATED error.
|
||||
* @param int|null $offset Number of rows to ignore from the
|
||||
* beginning of the data
|
||||
* @param int|null $limit Limits the number of returned rows to
|
||||
@@ -377,23 +370,30 @@ class Csv {
|
||||
*
|
||||
* @return bool True on success
|
||||
*/
|
||||
public function parse($input = null, $offset = null, $limit = null, $conditions = null) {
|
||||
if (is_null($input)) {
|
||||
$input = $this->file;
|
||||
public function parse($dataString = null, $offset = null, $limit = null, $conditions = null) {
|
||||
if (is_null($dataString)) {
|
||||
$this->data = $this->parseFile();
|
||||
return $this->data !== false;
|
||||
}
|
||||
|
||||
if (empty($input)) {
|
||||
if (empty($dataString)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->init($offset, $limit, $conditions);
|
||||
|
||||
if (strlen($input) <= PHP_MAXPATHLEN && is_readable($input)) {
|
||||
$this->file = $input;
|
||||
$this->data = $this->_parse_file();
|
||||
if (strlen($dataString) <= PHP_MAXPATHLEN && is_readable($dataString)) {
|
||||
$this->file = $dataString;
|
||||
$this->data = $this->parseFile();
|
||||
trigger_error(
|
||||
'Supplying file paths to parse() will no longer ' .
|
||||
'be supported in a future version of ParseCsv. ' .
|
||||
'Use ->parseFile() instead.',
|
||||
E_USER_DEPRECATED
|
||||
);
|
||||
} else {
|
||||
$this->file = null;
|
||||
$this->file_data = &$input;
|
||||
$this->file_data = &$dataString;
|
||||
$this->data = $this->_parse_string();
|
||||
}
|
||||
|
||||
@@ -401,8 +401,7 @@ class Csv {
|
||||
}
|
||||
|
||||
/**
|
||||
* Save
|
||||
* Save changes, or write a new file and/or data
|
||||
* Save changes, or write a new file and/or data.
|
||||
*
|
||||
* @param string $file File location to save to
|
||||
* @param array $data 2D array of data
|
||||
@@ -426,8 +425,9 @@ class Csv {
|
||||
}
|
||||
|
||||
/**
|
||||
* Output
|
||||
* Generate a CSV based string for output.
|
||||
* Generate a CSV-based string for output.
|
||||
*
|
||||
* Useful for exports in web applications.
|
||||
*
|
||||
* @param string|null $filename If a filename is specified here or in the
|
||||
* object, headers and data will be output
|
||||
@@ -471,11 +471,15 @@ class Csv {
|
||||
}
|
||||
|
||||
/**
|
||||
* Encoding
|
||||
* Convert character encoding
|
||||
*
|
||||
* @param string|null $input Input character encoding, uses default if left blank
|
||||
* Specify the encoding to use for the next parsing or unparsing.
|
||||
* Calling this function will not change the data held in the object immediately.
|
||||
*
|
||||
* @param string|null $input Input character encoding
|
||||
* If the value null is passed, the existing input encoding remains set (default: ISO-8859-1).
|
||||
* @param string|null $output Output character encoding, uses default if left blank
|
||||
* If the value null is passed, the existing input encoding remains set (default: ISO-8859-1).
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
@@ -491,11 +495,13 @@ class Csv {
|
||||
}
|
||||
|
||||
/**
|
||||
* Auto
|
||||
* Auto-Detect Delimiter: Find delimiter by analyzing a specific number of
|
||||
* Auto-detect delimiter: Find delimiter by analyzing a specific number of
|
||||
* rows to determine most probable delimiter character
|
||||
*
|
||||
* @param string|null $file Local CSV file
|
||||
* Supplying CSV data (file content) here is deprecated.
|
||||
* For CSV data, please use autoDetectionForDataString().
|
||||
* Support for CSV data will be removed in v2.0.0.
|
||||
* @param bool $parse True/false parse file directly
|
||||
* @param int|null $search_depth Number of rows to analyze
|
||||
* @param string|null $preferred Preferred delimiter characters
|
||||
@@ -532,6 +538,13 @@ class Csv {
|
||||
$data = &$this->file_data;
|
||||
}
|
||||
|
||||
$this->autoDetectionForDataString($data, $parse, $search_depth, $preferred, $enclosure);
|
||||
|
||||
return $this->delimiter;
|
||||
}
|
||||
|
||||
public function autoDetectionForDataString($data, $parse = true, $search_depth = null, $preferred = null, $enclosure = null) {
|
||||
$this->file_data = &$data;
|
||||
if (!$this->_detect_and_remove_sep_row_from_data($data)) {
|
||||
$this->_guess_delimiter($search_depth, $preferred, $enclosure, $data);
|
||||
}
|
||||
@@ -585,23 +598,33 @@ class Csv {
|
||||
// ==============================================
|
||||
|
||||
/**
|
||||
* Parse File
|
||||
* Read file to string and call _parse_string()
|
||||
*
|
||||
* @param string|null $file Local CSV file
|
||||
* @param string|null $file Path to a CSV file.
|
||||
* If configured in files such as php.ini,
|
||||
* the path may also contain a protocol:
|
||||
* https://example.org/some/file.csv
|
||||
*
|
||||
* @return array|bool
|
||||
* @return array<array>|false
|
||||
*/
|
||||
protected function _parse_file($file = null) {
|
||||
public function parseFile($file = null) {
|
||||
if (is_null($file)) {
|
||||
$file = $this->file;
|
||||
}
|
||||
|
||||
if (empty($this->file_data)) {
|
||||
$this->load_data($file);
|
||||
/**
|
||||
* @see self::keep_file_data
|
||||
* Usually, _parse_string will clean this
|
||||
* Instead of leaving stale data for the next parseFile call behind.
|
||||
*/
|
||||
if (empty($this->file_data) && !$this->loadFile($file)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !empty($this->file_data) ? $this->_parse_string() : false;
|
||||
if (empty($this->file_data)) {
|
||||
return false;
|
||||
}
|
||||
return $this->data = $this->_parse_string();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -615,7 +638,8 @@ class Csv {
|
||||
*
|
||||
* @param string|null $data CSV data
|
||||
*
|
||||
* @return array|false - 2D array with CSV data, or false on failure
|
||||
* @return array<array>|false
|
||||
* 2D array with CSV data, or false on failure
|
||||
*/
|
||||
protected function _parse_string($data = null) {
|
||||
if (empty($data)) {
|
||||
@@ -712,9 +736,8 @@ class Csv {
|
||||
} else {
|
||||
$enclosed = false;
|
||||
}
|
||||
|
||||
// end of field/row/csv
|
||||
} elseif (($ch === $this->delimiter || $ch == "\n" || $ch == "\r" || $ch === false) && !$enclosed) {
|
||||
} elseif ((in_array($ch, [$this->delimiter, "\n", "\r", false], true)) && !$enclosed) {
|
||||
$key = !empty($head[$col]) ? $head[$col] : $col;
|
||||
$row[$key] = $was_enclosed ? $current : trim($current);
|
||||
$current = '';
|
||||
@@ -914,32 +937,71 @@ class Csv {
|
||||
* This function load_data() is able to handle BOMs and encodings. The data
|
||||
* is stored within the $this->file_data class field.
|
||||
*
|
||||
* @param string|null $input local CSV file or CSV data as a string
|
||||
* @param string|null $input CSV file path or CSV data as a string
|
||||
*
|
||||
* Supplying CSV data (file content) here is deprecated.
|
||||
* For CSV data, please use loadDataString().
|
||||
* Support for CSV data will be removed in v2.0.0.
|
||||
*
|
||||
* @return bool True on success
|
||||
* @deprecated Use loadDataString() or loadFile() instead.
|
||||
*/
|
||||
public function load_data($input = null) {
|
||||
return $this->loadFile($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a file, but don't parse it.
|
||||
*
|
||||
* Only use this function if auto() and parseFile() don't handle your data well.
|
||||
*
|
||||
* This function is able to handle BOMs and encodings. The data
|
||||
* is stored within the $this->file_data class field.
|
||||
*
|
||||
* @param string|null $file CSV file path
|
||||
*
|
||||
* @return bool True on success
|
||||
*/
|
||||
public function load_data($input = null) {
|
||||
public function loadFile($file = null) {
|
||||
$data = null;
|
||||
$file = null;
|
||||
|
||||
if (is_null($input)) {
|
||||
$file = $this->file;
|
||||
} elseif (\strlen($input) <= PHP_MAXPATHLEN && file_exists($input)) {
|
||||
$file = $input;
|
||||
} else {
|
||||
// It is CSV data as a string.
|
||||
$data = $input;
|
||||
}
|
||||
|
||||
if (!empty($data) || $data = $this->_rfile($file)) {
|
||||
if (is_null($file)) {
|
||||
$data = $this->_rfile($this->file);
|
||||
} elseif (\strlen($file) <= PHP_MAXPATHLEN && file_exists($file)) {
|
||||
$data = $this->_rfile($file);
|
||||
if ($this->file != $file) {
|
||||
$this->file = $file;
|
||||
}
|
||||
} else {
|
||||
// It is CSV data as a string.
|
||||
|
||||
if (preg_match('/\.php$/i', $file) && preg_match('/<\?.*?\?>(.*)/ms', $data, $strip)) {
|
||||
$data = ltrim($strip[1]);
|
||||
// WARNING:
|
||||
// Supplying CSV data to load_data() will no longer
|
||||
// be supported in a future version of ParseCsv.
|
||||
// This function will return false for invalid paths from v2.0.0 onwards.
|
||||
|
||||
// Use ->loadDataString() instead.
|
||||
|
||||
$data = $file;
|
||||
}
|
||||
|
||||
return $this->loadDataString($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a data string, but don't parse it.
|
||||
*
|
||||
* Only use this function if autoDetectionForDataString() and parse() don't handle your data well.
|
||||
*
|
||||
* This function is able to handle BOMs and encodings. The data
|
||||
* is stored within the $this->file_data class field.
|
||||
*
|
||||
* @param string|null $file_path CSV file path
|
||||
*
|
||||
* @return bool True on success
|
||||
*/
|
||||
public function loadDataString($data) {
|
||||
if (!empty($data)) {
|
||||
if (strpos($data, "\xef\xbb\xbf") === 0) {
|
||||
// strip off BOM (UTF-8)
|
||||
$data = substr($data, 3);
|
||||
@@ -989,7 +1051,7 @@ class Csv {
|
||||
* @param array $row array with values from a row
|
||||
* @param string|null $conditions specified conditions that the row must match
|
||||
*
|
||||
* @return true of false
|
||||
* @return bool
|
||||
*/
|
||||
protected function _validate_row_conditions($row = array(), $conditions = null) {
|
||||
if (!empty($row)) {
|
||||
@@ -1116,7 +1178,7 @@ class Csv {
|
||||
*
|
||||
* @param int $current_row the current row number being processed
|
||||
*
|
||||
* @return true of false
|
||||
* @return bool
|
||||
*/
|
||||
protected function _validate_offset($current_row) {
|
||||
return
|
||||
@@ -1164,14 +1226,14 @@ class Csv {
|
||||
$file = $this->file;
|
||||
}
|
||||
|
||||
return $this->load_data($file);
|
||||
return $this->loadFile($file);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if passed info might be delimiter
|
||||
* Check if passed info might be delimiter.
|
||||
* Only used by find_delimiter
|
||||
*
|
||||
* @param string $char Potential field separating character
|
||||
@@ -1186,7 +1248,7 @@ class Csv {
|
||||
$first = null;
|
||||
$equal = null;
|
||||
$almost = false;
|
||||
foreach ($array as $key => $value) {
|
||||
foreach ($array as $value) {
|
||||
if ($first == null) {
|
||||
$first = $value;
|
||||
} elseif ($value == $first && $equal !== false) {
|
||||
@@ -1199,7 +1261,7 @@ class Csv {
|
||||
}
|
||||
}
|
||||
|
||||
if ($equal) {
|
||||
if ($equal || $depth === 1) {
|
||||
$match = $almost ? 2 : 1;
|
||||
$pref = strpos($preferred, $char);
|
||||
$pref = ($pref !== false) ? str_pad($pref, 3, '0', STR_PAD_LEFT) : '999';
|
||||
@@ -1215,16 +1277,23 @@ class Csv {
|
||||
/**
|
||||
* Read local file.
|
||||
*
|
||||
* @param string $file local filename
|
||||
* @param string $filePath local filename
|
||||
*
|
||||
* @return string|false Data from file, or false on failure
|
||||
*/
|
||||
protected function _rfile($file) {
|
||||
if (is_readable($file)) {
|
||||
$data = file_get_contents($file);
|
||||
protected function _rfile($filePath) {
|
||||
if (is_readable($filePath)) {
|
||||
$data = file_get_contents($filePath);
|
||||
if ($data === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (preg_match('/\.php$/i', $filePath) && preg_match('/<\?.*?\?>(.*)/ms', $data, $strip)) {
|
||||
// Return section behind closing tags.
|
||||
// This parsing is deprecated and will be removed in v2.0.0.
|
||||
$data = ltrim($strip[1]);
|
||||
}
|
||||
|
||||
return rtrim($data, "\r\n");
|
||||
}
|
||||
|
||||
@@ -1336,7 +1405,7 @@ class Csv {
|
||||
$is_newline = ($ch == "\n" && $pch != "\r") || $ch == "\r";
|
||||
if ($ch == $enclosure) {
|
||||
if (!$enclosed || $nch != $enclosure) {
|
||||
$enclosed = $enclosed ? false : true;
|
||||
$enclosed = !$enclosed;
|
||||
} elseif ($enclosed) {
|
||||
$i++;
|
||||
}
|
||||
|
||||
@@ -36,7 +36,6 @@ trait DatatypeTrait {
|
||||
reset($typesFreq);
|
||||
|
||||
return key($typesFreq);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
chdir(__DIR__ . '/..');
|
||||
if (!file_exists('vendor/autoload.php')) {
|
||||
`composer dump-autoload`;
|
||||
|
||||
1
tests/example_files/single_row.csv
Normal file
1
tests/example_files/single_row.csv
Normal file
@@ -0,0 +1 @@
|
||||
C1,C2,C3
|
||||
|
@@ -17,13 +17,7 @@ class DataRowCountTest extends TestCase {
|
||||
*/
|
||||
protected $csv;
|
||||
|
||||
/**
|
||||
* Setup
|
||||
* Setup our test environment objects
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function setUp() {
|
||||
protected function setUp(): void {
|
||||
$this->csv = new Csv();
|
||||
}
|
||||
|
||||
@@ -53,22 +47,22 @@ class DataRowCountTest extends TestCase {
|
||||
public function testGetTotalRowCountFromFile($file, $expectedRows) {
|
||||
$this->csv->heading = true;
|
||||
$this->csv->load_data(__DIR__ . '/fixtures/' . $file);
|
||||
$this->assertEquals($expectedRows, $this->csv->getTotalDataRowCount());
|
||||
self::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());
|
||||
$this->csv->loadDataString($sInput);
|
||||
self::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->csv->loadDataString($sInput);
|
||||
$this->assertEquals(3, $this->csv->getTotalDataRowCount());
|
||||
}
|
||||
|
||||
@@ -76,7 +70,11 @@ class DataRowCountTest extends TestCase {
|
||||
$this->csv->heading = false;
|
||||
$this->csv->enclosure = "'";
|
||||
$sInput = "86545235689";
|
||||
$this->csv->load_data($sInput);
|
||||
$this->csv->loadDataString($sInput);
|
||||
$this->assertEquals(1, $this->csv->getTotalDataRowCount());
|
||||
}
|
||||
|
||||
public function testGetTotalRowCountNoData() {
|
||||
self::assertFalse($this->csv->getTotalDataRowCount());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,4 +98,3 @@ class ExampleStream {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,11 +11,11 @@ use PHPUnit\Framework\TestCase;
|
||||
*/
|
||||
class OldRequireTest extends TestCase {
|
||||
|
||||
protected function setUp() {
|
||||
protected function setUp(): void {
|
||||
rename('vendor/autoload.php', '__autoload');
|
||||
}
|
||||
|
||||
protected function tearDown() {
|
||||
protected function tearDown(): void {
|
||||
rename('__autoload', 'vendor/autoload.php');
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ class OldRequireTest extends TestCase {
|
||||
* @runInSeparateProcess so that disabled autoloading has an effect
|
||||
*/
|
||||
public function testOldLibWithoutComposer() {
|
||||
|
||||
file_put_contents('__eval.php', '<?php require "parsecsv.lib.php"; new \ParseCsv\Csv;');
|
||||
exec("php __eval.php", $output, $return_var);
|
||||
unlink('__eval.php');
|
||||
@@ -35,7 +34,6 @@ class OldRequireTest extends TestCase {
|
||||
* @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');
|
||||
|
||||
@@ -14,8 +14,10 @@ class OutputTest extends TestCase {
|
||||
$csv = new Csv();
|
||||
$data = [0 => ['a', 'b', 'c'], 1 => ['d', 'e', 'f']];
|
||||
$fields = ['col1', 'col2', 'col3'];
|
||||
ob_start();
|
||||
$output = $csv->output('test.csv', $data, $fields, ',');
|
||||
$expected = "col1,col2,col3\ra,b,c\rd,e,f\r";
|
||||
self::assertEquals($expected, ob_get_clean());
|
||||
self::assertEquals($expected, $output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ class ParseTest extends TestCase {
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function setUp() {
|
||||
protected function setUp(): void {
|
||||
$this->csv = new Csv();
|
||||
}
|
||||
|
||||
@@ -36,12 +36,12 @@ class ParseTest extends TestCase {
|
||||
$this->csv->delimiter = ';';
|
||||
$this->csv->heading = false;
|
||||
$success = $this->csv->parse(str_repeat($content . ';', 500));
|
||||
$this->assertEquals(true, $success);
|
||||
self::assertEquals(true, $success);
|
||||
|
||||
$row = array_pop($this->csv->data);
|
||||
$expected_data = array_fill(0, 500, $content);
|
||||
$expected_data [] = '';
|
||||
$this->assertEquals($expected_data, $row);
|
||||
$expected_data[] = '';
|
||||
self::assertEquals($expected_data, $row);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,7 +57,7 @@ class ParseTest extends TestCase {
|
||||
$this->csv->output_encoding = 'UTF-8';
|
||||
|
||||
$this->csv->auto($file);
|
||||
$this->assertEquals($this->_get_magazines_data(), $this->csv->data);
|
||||
self::assertEquals($this->_get_magazines_data(), $this->csv->data);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,17 +78,17 @@ class ParseTest extends TestCase {
|
||||
);
|
||||
$row = array_pop($this->csv->data);
|
||||
$expected_data = ['URL' => 'http://www.amazon.com/ROX-Ice-Ball-Maker-Original/dp/B00MX59NMQ/ref=sr_1_1?ie=UTF8&qid=1435604374&sr=8-1&keywords=rox,+ice+molds'];
|
||||
$this->assertEquals($expected_data, $row);
|
||||
self::assertEquals($expected_data, $row);
|
||||
}
|
||||
|
||||
public function testAllNumericalCsv() {
|
||||
$this->csv->heading = false;
|
||||
$sInput = "86545235689\r\n34365587654\r\n13469874576";
|
||||
$this->assertEquals(false, $this->csv->auto($sInput));
|
||||
$this->assertEquals(null, $this->csv->delimiter);
|
||||
self::assertEquals(false, $this->csv->autoDetectionForDataString($sInput));
|
||||
self::assertEquals(null, $this->csv->delimiter);
|
||||
$expected_data = explode("\r\n", $sInput);
|
||||
$actual_data = array_map('reset', $this->csv->data);
|
||||
$this->assertEquals($expected_data, $actual_data);
|
||||
self::assertEquals($expected_data, $actual_data);
|
||||
}
|
||||
|
||||
public function testMissingEndingLineBreak() {
|
||||
@@ -99,8 +99,8 @@ class ParseTest extends TestCase {
|
||||
|
||||
$actual_data = $this->invokeMethod($this->csv, '_parse_string', [$sInput]);
|
||||
$actual_column = array_map('reset', $actual_data);
|
||||
$this->assertEquals($expected_data, $actual_column);
|
||||
$this->assertEquals(
|
||||
self::assertEquals($expected_data, $actual_column);
|
||||
self::assertEquals(
|
||||
[
|
||||
'a',
|
||||
'b',
|
||||
@@ -118,7 +118,16 @@ class ParseTest extends TestCase {
|
||||
['SMS' => '6606'],
|
||||
['SMS' => '7777'],
|
||||
];
|
||||
$this->assertEquals($expected, $this->csv->data);
|
||||
self::assertEquals($expected, $this->csv->data);
|
||||
}
|
||||
|
||||
public function testSingleRow() {
|
||||
$this->csv->auto(__DIR__ . '/../example_files/single_row.csv');
|
||||
self::assertEquals([], $this->csv->data, 'Single row is detected as header');
|
||||
$this->csv->heading = false;
|
||||
$this->csv->auto(__DIR__ . '/../example_files/single_row.csv');
|
||||
$expected = [['C1', 'C2', 'C3']];
|
||||
self::assertEquals($expected, $this->csv->data);
|
||||
}
|
||||
|
||||
public function testMatomoData() {
|
||||
@@ -127,7 +136,7 @@ class ParseTest extends TestCase {
|
||||
$this->csv->output_encoding = 'UTF-8';
|
||||
$this->csv->auto(__DIR__ . '/../example_files/Piwik_API_download.csv');
|
||||
$aAction27 = array_column($this->csv->data, 'url (actionDetails 27)');
|
||||
$this->assertEquals(
|
||||
self::assertEquals(
|
||||
[
|
||||
'http://application/_Main/_GraphicMeanSTD_MDI/btnConfBandOptions',
|
||||
'',
|
||||
@@ -137,7 +146,7 @@ class ParseTest extends TestCase {
|
||||
);
|
||||
|
||||
$aCity = array_column($this->csv->data, 'city');
|
||||
$this->assertEquals(
|
||||
self::assertEquals(
|
||||
[
|
||||
'São Paulo',
|
||||
'Johannesburg',
|
||||
@@ -161,23 +170,27 @@ class ParseTest extends TestCase {
|
||||
|
||||
$this->csv->output_encoding = 'UTF-8';
|
||||
$this->csv->delimiter = ';';
|
||||
self::assertTrue($this->csv->load_data($string_with_bom));
|
||||
self::assertTrue($this->csv->loadDataString($string_with_bom));
|
||||
self::assertTrue($this->csv->parse($this->csv->file_data));
|
||||
|
||||
// This also tests if ::load_data removed the BOM from the data;
|
||||
// otherwise the 'title' column would have 3 extra bytes.
|
||||
$this->assertEquals([
|
||||
$this->assertEquals(
|
||||
[
|
||||
'title',
|
||||
'isbn',
|
||||
'publishedAt',
|
||||
], array_keys(reset($this->csv->data)));
|
||||
],
|
||||
array_keys(reset($this->csv->data)));
|
||||
|
||||
$titles = array_column($this->csv->data, 'title');
|
||||
$this->assertEquals([
|
||||
$this->assertEquals(
|
||||
[
|
||||
'Красивая кулинария',
|
||||
'The Wine Connoisseurs',
|
||||
'Weißwein',
|
||||
], $titles);
|
||||
],
|
||||
$titles);
|
||||
}
|
||||
|
||||
public function testWithMultipleNewlines() {
|
||||
@@ -185,7 +198,8 @@ class ParseTest extends TestCase {
|
||||
$aElse9 = array_column($this->csv->data, 'else9');
|
||||
|
||||
/** @noinspection SpellCheckingInspection */
|
||||
$this->assertEquals([
|
||||
$this->assertEquals(
|
||||
[
|
||||
'Abweichung',
|
||||
'Abweichung',
|
||||
'Abweichung',
|
||||
@@ -196,7 +210,8 @@ class ParseTest extends TestCase {
|
||||
'Fehlt',
|
||||
'Fehlt',
|
||||
'Fehlt',
|
||||
], $aElse9);
|
||||
],
|
||||
$aElse9);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -214,7 +229,7 @@ class ParseTest extends TestCase {
|
||||
'price' => 'float',
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, $this->csv->data_types);
|
||||
self::assertEquals($expected, $this->csv->data_types);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -222,21 +237,21 @@ class ParseTest extends TestCase {
|
||||
*/
|
||||
public function testAutoDetectFileHasHeading() {
|
||||
$this->csv->auto(__DIR__ . '/fixtures/datatype.csv');
|
||||
$this->assertTrue($this->csv->autoDetectFileHasHeading());
|
||||
self::assertTrue($this->csv->autoDetectFileHasHeading());
|
||||
|
||||
$this->csv->heading = false;
|
||||
$this->csv->auto(__DIR__ . '/fixtures/datatype.csv');
|
||||
$this->assertTrue($this->csv->autoDetectFileHasHeading());
|
||||
self::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->autoDetectionForDataString($sInput);
|
||||
self::assertFalse($this->csv->autoDetectFileHasHeading());
|
||||
|
||||
$this->csv->heading = true;
|
||||
$sInput = "86545235689\r\n34365587654\r\n13469874576";
|
||||
$this->csv->auto($sInput);
|
||||
$this->assertFalse($this->csv->autoDetectFileHasHeading());
|
||||
$this->csv->autoDetectionForDataString($sInput);
|
||||
self::assertFalse($this->csv->autoDetectFileHasHeading());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -273,8 +288,7 @@ class ParseTest extends TestCase {
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function autoQuotesDataProvider(): array
|
||||
{
|
||||
public function autoQuotesDataProvider(): array {
|
||||
return array(
|
||||
array('auto-double-enclosure.csv', '"'),
|
||||
array('auto-single-enclosure.csv', "'"),
|
||||
@@ -292,8 +306,8 @@ class ParseTest extends TestCase {
|
||||
public function testAutoQuotes($file, $enclosure) {
|
||||
$csv = new Csv();
|
||||
$csv->auto(__DIR__ . '/fixtures/' . $file, true, null, null, $enclosure);
|
||||
$this->assertArrayHasKey('column1', $csv->data[0], 'Data parsed incorrectly with enclosure ' . $enclosure);
|
||||
$this->assertEquals('value1', $csv->data[0]['column1'], 'Data parsed incorrectly with enclosure ' . $enclosure);
|
||||
self::assertArrayHasKey('column1', $csv->data[0], 'Data parsed incorrectly with enclosure ' . $enclosure);
|
||||
self::assertEquals('value1', $csv->data[0]['column1'], 'Data parsed incorrectly with enclosure ' . $enclosure);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -314,7 +328,7 @@ class ParseTest extends TestCase {
|
||||
}
|
||||
|
||||
public function testWaiverFieldSeparator() {
|
||||
$this->assertFalse($this->csv->auto(__DIR__ . '/../example_files/waiver_field_separator.csv'));
|
||||
self::assertFalse($this->csv->auto(__DIR__ . '/../example_files/waiver_field_separator.csv'));
|
||||
$expected = [
|
||||
'liability waiver',
|
||||
'release of liability form',
|
||||
@@ -323,6 +337,19 @@ class ParseTest extends TestCase {
|
||||
'sample waiver form',
|
||||
];
|
||||
$actual = array_column($this->csv->data, 'keyword');
|
||||
$this->assertSame($expected, $actual);
|
||||
self::assertSame($expected, $actual);
|
||||
}
|
||||
|
||||
public function testEmptyInput() {
|
||||
self::assertFalse($this->csv->parse(''));
|
||||
self::assertFalse($this->csv->parse(null));
|
||||
self::assertFalse($this->csv->parseFile(''));
|
||||
self::assertFalse($this->csv->parseFile(null));
|
||||
}
|
||||
|
||||
public function testParseFile() {
|
||||
$data = $this->csv->parseFile(__DIR__ . '/fixtures/auto-double-enclosure.csv');
|
||||
self::assertCount(2, $data);
|
||||
self::assertEquals($data, $this->csv->data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ class SaveTest extends TestCase {
|
||||
/**
|
||||
* Setup our test environment objects; will be called before each test.
|
||||
*/
|
||||
public function setUp() {
|
||||
protected function setUp(): void {
|
||||
$this->csv = new Csv();
|
||||
$this->csv->auto(__DIR__ . '/../example_files/single_column.csv');
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ use PHPUnit\Framework\TestCase;
|
||||
*/
|
||||
class StreamTest extends TestCase {
|
||||
|
||||
public function setUp() {
|
||||
protected function setUp(): void {
|
||||
static $done;
|
||||
if ($done) {
|
||||
// Make sure we register the stream just once - or PHP will scream.
|
||||
|
||||
@@ -14,7 +14,7 @@ class UnparseTest extends Testcase {
|
||||
/**
|
||||
* Setup our test environment objects; will be called before each test.
|
||||
*/
|
||||
public function setUp() {
|
||||
protected function setUp(): void {
|
||||
$this->csv = new Csv();
|
||||
$this->csv->auto(__DIR__ . '/fixtures/auto-double-enclosure.csv');
|
||||
}
|
||||
@@ -48,7 +48,6 @@ class UnparseTest extends Testcase {
|
||||
$this->csv->auto(__DIR__ . '/fixtures/auto-double-enclosure.csv');
|
||||
$expected = "column1,column2\rvalue1,value2\rvalue3,value4\r";
|
||||
$this->unparseAndCompare($expected);
|
||||
|
||||
}
|
||||
|
||||
public function testUnparseRenameFields() {
|
||||
|
||||
@@ -19,7 +19,7 @@ class BaseClass extends TestCase {
|
||||
* Setup
|
||||
* Setup our test environment objects
|
||||
*/
|
||||
protected function setUp() {
|
||||
protected function setUp(): void {
|
||||
$this->csv = new Csv();
|
||||
}
|
||||
|
||||
|
||||
@@ -16,152 +16,135 @@ class DefaultValuesPropertiesTest extends TestCase {
|
||||
*/
|
||||
protected $csv = null;
|
||||
|
||||
/**
|
||||
* Setup
|
||||
* Setup our test environment objects
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function setUp() {
|
||||
//setup parse CSV
|
||||
protected function setUp(): void {
|
||||
$this->csv = new Csv();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tear down
|
||||
* Tear down our test environment objects
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function tearDown() {
|
||||
$this->csv = null;
|
||||
}
|
||||
|
||||
public function test_heading_default() {
|
||||
$this->assertTrue(is_bool($this->csv->heading));
|
||||
$this->assertTrue($this->csv->heading);
|
||||
self::assertTrue(is_bool($this->csv->heading));
|
||||
self::assertTrue($this->csv->heading);
|
||||
}
|
||||
|
||||
public function test_fields_default() {
|
||||
$this->assertTrue(is_array($this->csv->fields));
|
||||
$this->assertCount(0, $this->csv->fields);
|
||||
self::assertTrue(is_array($this->csv->fields));
|
||||
self::assertCount(0, $this->csv->fields);
|
||||
}
|
||||
|
||||
public function test_sort_by_default() {
|
||||
$this->assertNull($this->csv->sort_by);
|
||||
self::assertNull($this->csv->sort_by);
|
||||
}
|
||||
|
||||
public function test_sort_reverse_default() {
|
||||
$this->assertTrue(is_bool($this->csv->sort_reverse));
|
||||
$this->assertFalse($this->csv->sort_reverse);
|
||||
self::assertTrue(is_bool($this->csv->sort_reverse));
|
||||
self::assertFalse($this->csv->sort_reverse);
|
||||
}
|
||||
|
||||
public function test_sort_type_default() {
|
||||
$this->assertEquals('regular', $this->csv->sort_type);
|
||||
self::assertEquals('regular', $this->csv->sort_type);
|
||||
}
|
||||
|
||||
public function test_delimiter_default() {
|
||||
$this->assertTrue(is_string($this->csv->delimiter));
|
||||
$this->assertEquals(',', $this->csv->delimiter);
|
||||
self::assertTrue(is_string($this->csv->delimiter));
|
||||
self::assertEquals(',', $this->csv->delimiter);
|
||||
}
|
||||
|
||||
public function test_enclosure_default() {
|
||||
$this->assertTrue(is_string($this->csv->enclosure));
|
||||
$this->assertEquals('"', $this->csv->enclosure);
|
||||
self::assertTrue(is_string($this->csv->enclosure));
|
||||
self::assertEquals('"', $this->csv->enclosure);
|
||||
}
|
||||
|
||||
public function test_enclose_all_default() {
|
||||
$this->assertTrue(is_bool($this->csv->enclose_all));
|
||||
$this->assertFalse($this->csv->enclose_all);
|
||||
self::assertTrue(is_bool($this->csv->enclose_all));
|
||||
self::assertFalse($this->csv->enclose_all);
|
||||
}
|
||||
|
||||
public function test_conditions_default() {
|
||||
$this->assertNull($this->csv->conditions);
|
||||
self::assertNull($this->csv->conditions);
|
||||
}
|
||||
|
||||
public function test_offset_default() {
|
||||
$this->assertNull($this->csv->offset);
|
||||
self::assertNull($this->csv->offset);
|
||||
}
|
||||
|
||||
public function test_limit_default() {
|
||||
$this->assertNull($this->csv->limit);
|
||||
self::assertNull($this->csv->limit);
|
||||
}
|
||||
|
||||
public function test_auto_depth_default() {
|
||||
$this->assertTrue(is_numeric($this->csv->auto_depth));
|
||||
$this->assertEquals(15, $this->csv->auto_depth);
|
||||
self::assertTrue(is_numeric($this->csv->auto_depth));
|
||||
self::assertEquals(15, $this->csv->auto_depth);
|
||||
}
|
||||
|
||||
public function test_auto_non_chars_default() {
|
||||
$this->assertTrue(is_string($this->csv->auto_non_chars));
|
||||
$this->assertEquals("a-zA-Z0-9\n\r", $this->csv->auto_non_chars);
|
||||
self::assertTrue(is_string($this->csv->auto_non_chars));
|
||||
self::assertEquals("a-zA-Z0-9\n\r", $this->csv->auto_non_chars);
|
||||
}
|
||||
|
||||
public function test_auto_preferred_default() {
|
||||
$this->assertTrue(is_string($this->csv->auto_preferred));
|
||||
$this->assertEquals(",;\t.:|", $this->csv->auto_preferred);
|
||||
self::assertTrue(is_string($this->csv->auto_preferred));
|
||||
self::assertEquals(",;\t.:|", $this->csv->auto_preferred);
|
||||
}
|
||||
|
||||
public function test_convert_encoding_default() {
|
||||
$this->assertTrue(is_bool($this->csv->convert_encoding));
|
||||
$this->assertFalse($this->csv->convert_encoding);
|
||||
self::assertTrue(is_bool($this->csv->convert_encoding));
|
||||
self::assertFalse($this->csv->convert_encoding);
|
||||
}
|
||||
|
||||
public function test_input_encoding_default() {
|
||||
$this->assertTrue(is_string($this->csv->input_encoding));
|
||||
$this->assertEquals('ISO-8859-1', $this->csv->input_encoding);
|
||||
self::assertTrue(is_string($this->csv->input_encoding));
|
||||
self::assertEquals('ISO-8859-1', $this->csv->input_encoding);
|
||||
}
|
||||
|
||||
public function test_output_encoding_default() {
|
||||
$this->assertTrue(is_string($this->csv->output_encoding));
|
||||
$this->assertEquals('ISO-8859-1', $this->csv->output_encoding);
|
||||
self::assertTrue(is_string($this->csv->output_encoding));
|
||||
self::assertEquals('ISO-8859-1', $this->csv->output_encoding);
|
||||
}
|
||||
|
||||
public function test_linefeed_default() {
|
||||
$this->assertTrue(is_string($this->csv->linefeed));
|
||||
$this->assertEquals("\r", $this->csv->linefeed);
|
||||
self::assertTrue(is_string($this->csv->linefeed));
|
||||
self::assertEquals("\r", $this->csv->linefeed);
|
||||
}
|
||||
|
||||
public function test_output_delimiter_default() {
|
||||
$this->assertTrue(is_string($this->csv->output_delimiter));
|
||||
$this->assertEquals(',', $this->csv->output_delimiter);
|
||||
self::assertTrue(is_string($this->csv->output_delimiter));
|
||||
self::assertEquals(',', $this->csv->output_delimiter);
|
||||
}
|
||||
|
||||
public function test_output_filename_default() {
|
||||
$this->assertTrue(is_string($this->csv->output_filename));
|
||||
$this->assertEquals('data.csv', $this->csv->output_filename);
|
||||
self::assertTrue(is_string($this->csv->output_filename));
|
||||
self::assertEquals('data.csv', $this->csv->output_filename);
|
||||
}
|
||||
|
||||
public function test_keep_file_data_default() {
|
||||
$this->assertTrue(is_bool($this->csv->keep_file_data));
|
||||
$this->assertFalse($this->csv->keep_file_data);
|
||||
self::assertTrue(is_bool($this->csv->keep_file_data));
|
||||
self::assertFalse($this->csv->keep_file_data);
|
||||
}
|
||||
|
||||
public function test_file_default() {
|
||||
$this->assertNull($this->csv->file);
|
||||
self::assertNull($this->csv->file);
|
||||
}
|
||||
|
||||
public function test_file_data_default() {
|
||||
$this->assertNull($this->csv->file_data);
|
||||
self::assertNull($this->csv->file_data);
|
||||
}
|
||||
|
||||
public function test_error_default() {
|
||||
$this->assertTrue(is_numeric($this->csv->error));
|
||||
$this->assertEquals(0, $this->csv->error);
|
||||
self::assertTrue(is_numeric($this->csv->error));
|
||||
self::assertEquals(0, $this->csv->error);
|
||||
}
|
||||
|
||||
public function test_error_info_default() {
|
||||
$this->assertTrue(is_array($this->csv->error_info));
|
||||
$this->assertCount(0, $this->csv->error_info);
|
||||
self::assertTrue(is_array($this->csv->error_info));
|
||||
self::assertCount(0, $this->csv->error_info);
|
||||
}
|
||||
|
||||
public function test_titles_default() {
|
||||
$this->assertTrue(is_array($this->csv->titles));
|
||||
$this->assertCount(0, $this->csv->titles);
|
||||
self::assertTrue(is_array($this->csv->titles));
|
||||
self::assertCount(0, $this->csv->titles);
|
||||
}
|
||||
|
||||
public function test_data_default() {
|
||||
$this->assertTrue(is_array($this->csv->data));
|
||||
$this->assertCount(0, $this->csv->data);
|
||||
self::assertTrue(is_array($this->csv->data));
|
||||
self::assertCount(0, $this->csv->data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ class OffsetTest extends BaseClass {
|
||||
/**
|
||||
* @dataProvider numberRangeZeroToFourProvider
|
||||
*
|
||||
* @param integer $offset
|
||||
* @param int $offset
|
||||
*/
|
||||
public function testOffsetOfOneNoHeader($offset) {
|
||||
$this->csv->offset = $offset;
|
||||
|
||||
@@ -41,7 +41,7 @@ class PublicPropertiesTest extends TestCase {
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function setUp() {
|
||||
protected function setUp(): void {
|
||||
//setup parse CSV
|
||||
$this->csv = new Csv();
|
||||
|
||||
@@ -58,7 +58,7 @@ class PublicPropertiesTest extends TestCase {
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function tearDown() {
|
||||
protected function tearDown(): void {
|
||||
$this->csv = null;
|
||||
$this->reflection = null;
|
||||
$this->properties = null;
|
||||
|
||||
Reference in New Issue
Block a user