mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 08:36:39 +00:00
Compare commits
38 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6c0e74c89e | ||
|
|
c47ba7934c | ||
|
|
653d5c0307 | ||
|
|
ff0e0c77fe | ||
|
|
f9b08cb0ac | ||
|
|
71e5f223a4 | ||
|
|
24e229ddfa | ||
|
|
259e9a6c31 | ||
|
|
27ab8a3e05 | ||
|
|
fe27ca6a7d | ||
|
|
33f0b6aa7c | ||
|
|
260de6126c | ||
|
|
1465973860 | ||
|
|
2c46b3fd14 | ||
|
|
bb3abe97de | ||
|
|
ac6b2665cd | ||
|
|
919528bb2f | ||
|
|
c4a90941e2 | ||
|
|
e967067a46 | ||
|
|
11dbbcb40e | ||
|
|
67163568ee | ||
|
|
6648dc99d5 | ||
|
|
2d6236cae0 | ||
|
|
f2b0aecd12 | ||
|
|
5d4643b201 | ||
| d5606f8b2a | |||
| 8cebcbd9bb | |||
| 518f5081fb | |||
|
|
a28fc6ab0a | ||
|
|
009820d190 | ||
|
|
facdf1c06c | ||
|
|
ee13c17157 | ||
|
|
99daaa7235 | ||
|
|
05826c2bbf | ||
|
|
731900effe | ||
|
|
913c3b1b94 | ||
|
|
96b2784d3c | ||
|
|
be01bc9ae4 |
@@ -14,5 +14,8 @@ insert_final_newline = true
|
|||||||
[composer.json]
|
[composer.json]
|
||||||
indent_size = 4
|
indent_size = 4
|
||||||
|
|
||||||
[.travis.yml]
|
[Makefile]
|
||||||
|
indent_style = tab
|
||||||
|
|
||||||
|
[*.yml,*.yaml]
|
||||||
indent_size = 2
|
indent_size = 2
|
||||||
|
|||||||
82
.github/workflows/phpunit.yml
vendored
Normal file
82
.github/workflows/phpunit.yml
vendored
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
---
|
||||||
|
name: PHPUnit
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test_php_82_and_newer:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
php_version:
|
||||||
|
- "8.3"
|
||||||
|
- "8.2"
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- 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@v4
|
||||||
|
with:
|
||||||
|
path: ${{ steps.composer-cache.outputs.dir }}
|
||||||
|
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
|
||||||
|
restore-keys: ${{ runner.os }}-composer-
|
||||||
|
- name: Update PHPUnit
|
||||||
|
run: composer require phpunit/phpunit --dev -W
|
||||||
|
- name: Install dependencies
|
||||||
|
run: composer update
|
||||||
|
- name: Validate dependencies
|
||||||
|
run: composer validate
|
||||||
|
- name: install Rector
|
||||||
|
run: composer require rector/rector --dev -W
|
||||||
|
- name: run Rector
|
||||||
|
run: cd tests && ../vendor/bin/phpunit --migrate-configuration
|
||||||
|
shell: bash
|
||||||
|
- name: run Rector
|
||||||
|
run: cd tests && ../vendor/bin/rector process .
|
||||||
|
shell: bash
|
||||||
|
- name: Run tests
|
||||||
|
run: vendor/bin/phpunit --configuration tests/phpunit.xml
|
||||||
|
test_php_81_and_lower:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
php_version:
|
||||||
|
- "8.1"
|
||||||
|
- "8.0"
|
||||||
|
- "7.4"
|
||||||
|
- "7.3"
|
||||||
|
- "7.2"
|
||||||
|
- "7.1"
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- 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@v4
|
||||||
|
with:
|
||||||
|
path: ${{ steps.composer-cache.outputs.dir }}
|
||||||
|
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
|
||||||
|
restore-keys: ${{ runner.os }}-composer-
|
||||||
|
- name: Update PHPUnit
|
||||||
|
run: composer require phpunit/phpunit --dev -W
|
||||||
|
- name: Install dependencies
|
||||||
|
run: composer update
|
||||||
|
- name: Validate dependencies
|
||||||
|
run: composer validate
|
||||||
|
- name: Run tests
|
||||||
|
run: vendor/bin/phpunit --configuration tests/phpunit.xml
|
||||||
7
.gitignore
vendored
7
.gitignore
vendored
@@ -1,7 +1,14 @@
|
|||||||
*.bak
|
*.bak
|
||||||
|
.env
|
||||||
|
.envrc
|
||||||
/.idea
|
/.idea
|
||||||
|
/.vscode
|
||||||
|
/composer-setup.php
|
||||||
/composer.lock
|
/composer.lock
|
||||||
/coverage_clover.xml
|
/coverage_clover.xml
|
||||||
|
/docker-compose.yml
|
||||||
|
/examples/people.csv
|
||||||
/phive.xml
|
/phive.xml
|
||||||
|
/tests/.phpunit.result.cache
|
||||||
/tools
|
/tools
|
||||||
/vendor/
|
/vendor/
|
||||||
|
|||||||
24
.travis.yml
24
.travis.yml
@@ -1,24 +0,0 @@
|
|||||||
dist: trusty
|
|
||||||
language: php
|
|
||||||
dist: trusty
|
|
||||||
|
|
||||||
php:
|
|
||||||
- '7.4'
|
|
||||||
- '7.3'
|
|
||||||
- '7.2'
|
|
||||||
- '7.1'
|
|
||||||
|
|
||||||
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,29 @@
|
|||||||
|
ParseCSV 1.3.2
|
||||||
|
-----------------------------------
|
||||||
|
Date: 07-Nov-2021
|
||||||
|
|
||||||
|
Bugfix:
|
||||||
|
- Allow _guess_delimiter to work with a single
|
||||||
|
row of data. As a consequence, `$csv->auto()`
|
||||||
|
now works for files with just one row of data.
|
||||||
|
See issue #206.
|
||||||
|
|
||||||
|
-----------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
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
|
ParseCSV 1.3.0
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
Date: 14-Apr-2021
|
Date: 14-Apr-2021
|
||||||
|
|||||||
18
Makefile
18
Makefile
@@ -11,5 +11,21 @@ phpunit-dep:
|
|||||||
exit 1 \
|
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:
|
.SILENT:
|
||||||
.PHONY: test phpunit-dep
|
.PHONY: test phpunit-dep local-ci
|
||||||
|
|||||||
57
README.md
57
README.md
@@ -1,8 +1,8 @@
|
|||||||
# ParseCsv
|
# ParseCsv
|
||||||
[](https://opencollective.com/parsecsv)
|

|
||||||
|
|
||||||
ParseCsv is an easy-to-use PHP class that reads and writes CSV data properly. It
|
ParseCsv is an easy-to-use PHP class that reads and writes CSV data properly. It
|
||||||
fully conforms to the specifications outlined on the on the
|
fully conforms to the specifications outlined on the
|
||||||
[Wikipedia article][CSV] (and thus RFC 4180). It has many advanced features which help make your
|
[Wikipedia article][CSV] (and thus RFC 4180). It has many advanced features which help make your
|
||||||
life easier when dealing with CSV data.
|
life easier when dealing with CSV data.
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ and third-party support for handling CSV data in PHP.
|
|||||||
* 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.5 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 8.3 and all versions in between.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@@ -53,14 +53,7 @@ To use ParseCSV, you then have to add a `require 'parsecsv.lib.php';` line.
|
|||||||
|
|
||||||
## Example Usage
|
## Example Usage
|
||||||
|
|
||||||
**General parsing**
|
**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
|
```php
|
||||||
$csv = new \ParseCsv\Csv();
|
$csv = new \ParseCsv\Csv();
|
||||||
@@ -70,7 +63,7 @@ $csv->parseFile('data.tsv');
|
|||||||
print_r($csv->data);
|
print_r($csv->data);
|
||||||
```
|
```
|
||||||
|
|
||||||
**Auto-detect delimiter character**
|
**Auto-detect field delimiter character**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$csv = new \ParseCsv\Csv();
|
$csv = new \ParseCsv\Csv();
|
||||||
@@ -152,6 +145,8 @@ $csv->save('data.csv', array(array('1986', 'Home', 'Nowhere', '')), /* append */
|
|||||||
**Convert 2D array to CSV data and send headers to browser to treat output as
|
**Convert 2D array to CSV data and send headers to browser to treat output as
|
||||||
a file and download it**
|
a file and download it**
|
||||||
|
|
||||||
|
Your web app users would call this an export.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$csv = new \ParseCsv\Csv();
|
$csv = new \ParseCsv\Csv();
|
||||||
$csv->linefeed = "\n";
|
$csv->linefeed = "\n";
|
||||||
@@ -170,8 +165,18 @@ composer install
|
|||||||
composer run test
|
composer run test
|
||||||
````
|
````
|
||||||
|
|
||||||
When pushing code to GitHub, tests will be executed using Travis CI. The relevant configuration is in the
|
Note that PHP 8.2 and newer allow PHPUnit versions that deprecate `@annotations`. The GitHub actions use Rector to
|
||||||
file `.travis.yml`.
|
convert them to `#[attributes]`.
|
||||||
|
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
|
## Credits
|
||||||
|
|
||||||
@@ -191,30 +196,6 @@ This project exists thanks to all the people who contribute.
|
|||||||
Please find a complete list on the project's [contributors][] page.
|
Please find a complete list on the project's [contributors][] page.
|
||||||
|
|
||||||
[contributors]: https://github.com/parsecsv/parsecsv-for-php/graphs/contributors
|
[contributors]: https://github.com/parsecsv/parsecsv-for-php/graphs/contributors
|
||||||
<a href="https://github.com/parsecsv/parsecsv-for-php/graphs/contributors"><img src="https://opencollective.com/parsecsv/contributors.svg?width=890&button=false" /></a>
|
|
||||||
|
|
||||||
### Financial Contributors
|
|
||||||
|
|
||||||
Become a financial contributor and help us sustain our community. [[Contribute](https://opencollective.com/parsecsv/contribute)]
|
|
||||||
|
|
||||||
#### Individuals
|
|
||||||
|
|
||||||
<a href="https://opencollective.com/parsecsv"><img src="https://opencollective.com/parsecsv/individuals.svg?width=890"></a>
|
|
||||||
|
|
||||||
#### Organizations
|
|
||||||
|
|
||||||
Support this project with your organization. Your logo will show up here with a link to your website. [[Contribute](https://opencollective.com/parsecsv/contribute)]
|
|
||||||
|
|
||||||
<a href="https://opencollective.com/parsecsv/organization/0/website"><img src="https://opencollective.com/parsecsv/organization/0/avatar.svg"></a>
|
|
||||||
<a href="https://opencollective.com/parsecsv/organization/1/website"><img src="https://opencollective.com/parsecsv/organization/1/avatar.svg"></a>
|
|
||||||
<a href="https://opencollective.com/parsecsv/organization/2/website"><img src="https://opencollective.com/parsecsv/organization/2/avatar.svg"></a>
|
|
||||||
<a href="https://opencollective.com/parsecsv/organization/3/website"><img src="https://opencollective.com/parsecsv/organization/3/avatar.svg"></a>
|
|
||||||
<a href="https://opencollective.com/parsecsv/organization/4/website"><img src="https://opencollective.com/parsecsv/organization/4/avatar.svg"></a>
|
|
||||||
<a href="https://opencollective.com/parsecsv/organization/5/website"><img src="https://opencollective.com/parsecsv/organization/5/avatar.svg"></a>
|
|
||||||
<a href="https://opencollective.com/parsecsv/organization/6/website"><img src="https://opencollective.com/parsecsv/organization/6/avatar.svg"></a>
|
|
||||||
<a href="https://opencollective.com/parsecsv/organization/7/website"><img src="https://opencollective.com/parsecsv/organization/7/avatar.svg"></a>
|
|
||||||
<a href="https://opencollective.com/parsecsv/organization/8/website"><img src="https://opencollective.com/parsecsv/organization/8/avatar.svg"></a>
|
|
||||||
<a href="https://opencollective.com/parsecsv/organization/9/website"><img src="https://opencollective.com/parsecsv/organization/9/avatar.svg"></a>
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@
|
|||||||
"php": ">=5.5"
|
"php": ">=5.5"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^6",
|
"phpunit/phpunit": "8.5.52",
|
||||||
"squizlabs/php_codesniffer": "^3.5"
|
"squizlabs/php_codesniffer": "^3.5"
|
||||||
},
|
},
|
||||||
"suggest": {
|
"suggest": {
|
||||||
|
|||||||
101
src/Csv.php
101
src/Csv.php
@@ -49,7 +49,7 @@ class Csv {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Heading
|
* Header row:
|
||||||
* Use first line/entry as field names
|
* Use first line/entry as field names
|
||||||
*
|
*
|
||||||
* @var bool
|
* @var bool
|
||||||
@@ -57,7 +57,6 @@ class Csv {
|
|||||||
public $heading = true;
|
public $heading = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fields
|
|
||||||
* Override field names
|
* Override field names
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
@@ -65,7 +64,6 @@ class Csv {
|
|||||||
public $fields = array();
|
public $fields = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sort By
|
|
||||||
* Sort CSV by this field
|
* Sort CSV by this field
|
||||||
*
|
*
|
||||||
* @var string|null
|
* @var string|null
|
||||||
@@ -73,15 +71,13 @@ class Csv {
|
|||||||
public $sort_by = null;
|
public $sort_by = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sort Reverse
|
* Reverse the sort direction
|
||||||
* Reverse the sort function
|
|
||||||
*
|
*
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
public $sort_reverse = false;
|
public $sort_reverse = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sort Type
|
|
||||||
* Sort behavior passed to sort methods
|
* Sort behavior passed to sort methods
|
||||||
*
|
*
|
||||||
* regular = SORT_REGULAR
|
* regular = SORT_REGULAR
|
||||||
@@ -93,31 +89,34 @@ class Csv {
|
|||||||
public $sort_type = SortEnum::SORT_TYPE_REGULAR;
|
public $sort_type = SortEnum::SORT_TYPE_REGULAR;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delimiter
|
* Field delimiter character
|
||||||
* Delimiter character
|
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public $delimiter = ',';
|
public $delimiter = ',';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enclosure
|
|
||||||
* Enclosure character
|
* Enclosure character
|
||||||
*
|
*
|
||||||
|
* This is useful for cell values that are either multi-line
|
||||||
|
* or contain the field delimiter character.
|
||||||
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public $enclosure = '"';
|
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
|
* @var bool
|
||||||
*/
|
*/
|
||||||
public $enclose_all = false;
|
public $enclose_all = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Conditions
|
|
||||||
* Basic SQL-Like conditions for row matching
|
* Basic SQL-Like conditions for row matching
|
||||||
*
|
*
|
||||||
* @var string|null
|
* @var string|null
|
||||||
@@ -125,7 +124,6 @@ class Csv {
|
|||||||
public $conditions = null;
|
public $conditions = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Offset
|
|
||||||
* Number of rows to ignore from beginning of data. If present, the heading
|
* Number of rows to ignore from beginning of data. If present, the heading
|
||||||
* row is also counted (if $this->heading == true). In other words,
|
* row is also counted (if $this->heading == true). In other words,
|
||||||
* $offset == 1 and $offset == 0 have the same meaning in that situation.
|
* $offset == 1 and $offset == 0 have the same meaning in that situation.
|
||||||
@@ -135,7 +133,6 @@ class Csv {
|
|||||||
public $offset = null;
|
public $offset = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Limit
|
|
||||||
* Limits the number of returned rows to the specified amount
|
* Limits the number of returned rows to the specified amount
|
||||||
*
|
*
|
||||||
* @var int|null
|
* @var int|null
|
||||||
@@ -143,7 +140,6 @@ class Csv {
|
|||||||
public $limit = null;
|
public $limit = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Auto Depth
|
|
||||||
* Number of rows to analyze when attempting to auto-detect delimiter
|
* Number of rows to analyze when attempting to auto-detect delimiter
|
||||||
*
|
*
|
||||||
* @var int
|
* @var int
|
||||||
@@ -151,7 +147,6 @@ class Csv {
|
|||||||
public $auto_depth = 15;
|
public $auto_depth = 15;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Auto Non Chars
|
|
||||||
* Characters that should be ignored when attempting to auto-detect delimiter
|
* Characters that should be ignored when attempting to auto-detect delimiter
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
@@ -159,7 +154,6 @@ class Csv {
|
|||||||
public $auto_non_chars = "a-zA-Z0-9\n\r";
|
public $auto_non_chars = "a-zA-Z0-9\n\r";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Auto Preferred
|
|
||||||
* preferred delimiter characters, only used when all filtering method
|
* preferred delimiter characters, only used when all filtering method
|
||||||
* returns multiple possible delimiters (happens very rarely)
|
* returns multiple possible delimiters (happens very rarely)
|
||||||
*
|
*
|
||||||
@@ -168,15 +162,14 @@ class Csv {
|
|||||||
public $auto_preferred = ",;\t.:|";
|
public $auto_preferred = ",;\t.:|";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert Encoding
|
|
||||||
* Should we convert the CSV character encoding?
|
* Should we convert the CSV character encoding?
|
||||||
|
* Used for both parse and unparse operations.
|
||||||
*
|
*
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
public $convert_encoding = false;
|
public $convert_encoding = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Input Encoding
|
|
||||||
* Set the input encoding
|
* Set the input encoding
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
@@ -184,7 +177,6 @@ class Csv {
|
|||||||
public $input_encoding = 'ISO-8859-1';
|
public $input_encoding = 'ISO-8859-1';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Output Encoding
|
|
||||||
* Set the output encoding
|
* Set the output encoding
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
@@ -202,15 +194,14 @@ class Csv {
|
|||||||
public $use_mb_convert_encoding = false;
|
public $use_mb_convert_encoding = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Linefeed
|
|
||||||
* Line feed characters used by unparse, save, and output methods
|
* Line feed characters used by unparse, save, and output methods
|
||||||
|
* Popular choices are "\r\n" and "\n".
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public $linefeed = "\r";
|
public $linefeed = "\r";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Output Delimiter
|
|
||||||
* Sets the output delimiter used by the output method
|
* Sets the output delimiter used by the output method
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
@@ -218,7 +209,6 @@ class Csv {
|
|||||||
public $output_delimiter = ',';
|
public $output_delimiter = ',';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Output filename
|
|
||||||
* Sets the output filename
|
* Sets the output filename
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
@@ -226,7 +216,6 @@ class Csv {
|
|||||||
public $output_filename = 'data.csv';
|
public $output_filename = 'data.csv';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Keep File Data
|
|
||||||
* keep raw file data in memory after successful parsing (useful for debugging)
|
* keep raw file data in memory after successful parsing (useful for debugging)
|
||||||
*
|
*
|
||||||
* @var bool
|
* @var bool
|
||||||
@@ -270,7 +259,6 @@ class Csv {
|
|||||||
public $error = 0;
|
public $error = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Error Information
|
|
||||||
* Detailed error information
|
* Detailed error information
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
@@ -298,17 +286,16 @@ class Csv {
|
|||||||
public $titles = array();
|
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();
|
public $data = array();
|
||||||
|
|
||||||
use DatatypeTrait;
|
use DatatypeTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
|
||||||
* Class constructor
|
* Class constructor
|
||||||
*
|
*
|
||||||
* @param string|null $data The CSV string or a direct file path.
|
* @param string|null $data The CSV string or a direct file path.
|
||||||
@@ -368,7 +355,6 @@ class Csv {
|
|||||||
// ==============================================
|
// ==============================================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse
|
|
||||||
* Parse a CSV file or string
|
* Parse a CSV file or string
|
||||||
*
|
*
|
||||||
* @param string|null $dataString The CSV string or a direct file path
|
* @param string|null $dataString The CSV string or a direct file path
|
||||||
@@ -415,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 string $file File location to save to
|
||||||
* @param array $data 2D array of data
|
* @param array $data 2D array of data
|
||||||
@@ -434,14 +419,15 @@ class Csv {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$mode = FileProcessingModeEnum::getAppendMode($append);
|
$mode = FileProcessingModeEnum::getAppendMode($append);
|
||||||
$is_php = preg_match('/\.php$/i', $file) ? true : false;
|
$is_php = (bool) preg_match('/\.php$/i', $file);
|
||||||
|
|
||||||
return $this->_wfile($file, $this->unparse($data, $fields, $append, $is_php), $mode);
|
return $this->_wfile($file, $this->unparse($data, $fields, $append, $is_php), $mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
* @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
|
||||||
@@ -485,11 +471,15 @@ class Csv {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encoding
|
|
||||||
* Convert character 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
|
* @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
|
* @return void
|
||||||
*/
|
*/
|
||||||
@@ -505,8 +495,7 @@ 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
|
* rows to determine most probable delimiter character
|
||||||
*
|
*
|
||||||
* @param string|null $file Local CSV file
|
* @param string|null $file Local CSV file
|
||||||
@@ -609,7 +598,6 @@ class Csv {
|
|||||||
// ==============================================
|
// ==============================================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse File
|
|
||||||
* Read file to string and call _parse_string()
|
* Read file to string and call _parse_string()
|
||||||
*
|
*
|
||||||
* @param string|null $file Path to a CSV file.
|
* @param string|null $file Path to a CSV file.
|
||||||
@@ -617,23 +605,26 @@ class Csv {
|
|||||||
* the path may also contain a protocol:
|
* the path may also contain a protocol:
|
||||||
* https://example.org/some/file.csv
|
* https://example.org/some/file.csv
|
||||||
*
|
*
|
||||||
* @return array|bool
|
* @return array<array>|false
|
||||||
*/
|
*/
|
||||||
public function parseFile($file = null) {
|
public function parseFile($file = null) {
|
||||||
if (is_null($file)) {
|
if (is_null($file)) {
|
||||||
$file = $this->file;
|
$file = $this->file;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($this->file_data)) {
|
|
||||||
/**
|
/**
|
||||||
* @see self::keep_file_data
|
* @see self::keep_file_data
|
||||||
* Usually, _parse_string will clean this
|
* Usually, _parse_string will clean this
|
||||||
* Instead of leaving stale data for the next parseFile call behind.
|
* Instead of leaving stale data for the next parseFile call behind.
|
||||||
*/
|
*/
|
||||||
$this->load_data($file);
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -647,7 +638,8 @@ class Csv {
|
|||||||
*
|
*
|
||||||
* @param string|null $data CSV data
|
* @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) {
|
protected function _parse_string($data = null) {
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
@@ -863,9 +855,8 @@ class Csv {
|
|||||||
$string .= implode($delimiter, $entry) . $this->linefeed;
|
$string .= implode($delimiter, $entry) . $this->linefeed;
|
||||||
$entry = array();
|
$entry = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
// create data
|
// create data
|
||||||
foreach ($data as $key => $row) {
|
foreach ($data as $row) {
|
||||||
foreach (array_keys($fieldOrder) as $index) {
|
foreach (array_keys($fieldOrder) as $index) {
|
||||||
$cell_value = $row[$index];
|
$cell_value = $row[$index];
|
||||||
$entry[] = $this->_enclose_value($cell_value, $delimiter);
|
$entry[] = $this->_enclose_value($cell_value, $delimiter);
|
||||||
@@ -1117,6 +1108,8 @@ class Csv {
|
|||||||
'is greater than or equals',
|
'is greater than or equals',
|
||||||
'contains',
|
'contains',
|
||||||
'does not contain',
|
'does not contain',
|
||||||
|
'is number',
|
||||||
|
'is not number',
|
||||||
);
|
);
|
||||||
|
|
||||||
$operators_regex = array();
|
$operators_regex = array();
|
||||||
@@ -1158,6 +1151,10 @@ class Csv {
|
|||||||
$op_equals = in_array($op, ['=', 'equals', 'is'], true);
|
$op_equals = in_array($op, ['=', 'equals', 'is'], true);
|
||||||
if ($op_equals && $row[$field] == $value) {
|
if ($op_equals && $row[$field] == $value) {
|
||||||
return '1';
|
return '1';
|
||||||
|
} elseif ($op_equals && $value == 'number' && is_numeric($row[$field])) {
|
||||||
|
return '1';
|
||||||
|
} elseif (($op == '!=' || $op == 'is not') && $value == 'number' && !is_numeric($row[$field])) {
|
||||||
|
return '1';
|
||||||
} elseif (($op == '!=' || $op == 'is not') && $row[$field] != $value) {
|
} elseif (($op == '!=' || $op == 'is not') && $row[$field] != $value) {
|
||||||
return '1';
|
return '1';
|
||||||
} elseif (($op == '<' || $op == 'is less than') && $row[$field] < $value) {
|
} elseif (($op == '<' || $op == 'is less than') && $row[$field] < $value) {
|
||||||
@@ -1234,14 +1231,14 @@ class Csv {
|
|||||||
$file = $this->file;
|
$file = $this->file;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->load_data($file);
|
return $this->loadFile($file);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if passed info might be delimiter
|
* Check if passed info might be delimiter.
|
||||||
* Only used by find_delimiter
|
* Only used by find_delimiter
|
||||||
*
|
*
|
||||||
* @param string $char Potential field separating character
|
* @param string $char Potential field separating character
|
||||||
@@ -1256,7 +1253,7 @@ class Csv {
|
|||||||
$first = null;
|
$first = null;
|
||||||
$equal = null;
|
$equal = null;
|
||||||
$almost = false;
|
$almost = false;
|
||||||
foreach ($array as $key => $value) {
|
foreach ($array as $value) {
|
||||||
if ($first == null) {
|
if ($first == null) {
|
||||||
$first = $value;
|
$first = $value;
|
||||||
} elseif ($value == $first && $equal !== false) {
|
} elseif ($value == $first && $equal !== false) {
|
||||||
@@ -1269,7 +1266,7 @@ class Csv {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($equal) {
|
if ($equal || $depth === 1) {
|
||||||
$match = $almost ? 2 : 1;
|
$match = $almost ? 2 : 1;
|
||||||
$pref = strpos($preferred, $char);
|
$pref = strpos($preferred, $char);
|
||||||
$pref = ($pref !== false) ? str_pad($pref, 3, '0', STR_PAD_LEFT) : '999';
|
$pref = ($pref !== false) ? str_pad($pref, 3, '0', STR_PAD_LEFT) : '999';
|
||||||
@@ -1413,7 +1410,7 @@ class Csv {
|
|||||||
$is_newline = ($ch == "\n" && $pch != "\r") || $ch == "\r";
|
$is_newline = ($ch == "\n" && $pch != "\r") || $ch == "\r";
|
||||||
if ($ch == $enclosure) {
|
if ($ch == $enclosure) {
|
||||||
if (!$enclosed || $nch != $enclosure) {
|
if (!$enclosed || $nch != $enclosure) {
|
||||||
$enclosed = $enclosed ? false : true;
|
$enclosed = !$enclosed;
|
||||||
} elseif ($enclosed) {
|
} elseif ($enclosed) {
|
||||||
$i++;
|
$i++;
|
||||||
}
|
}
|
||||||
|
|||||||
1
tests/example_files/single_row.csv
Normal file
1
tests/example_files/single_row.csv
Normal file
@@ -0,0 +1 @@
|
|||||||
|
C1,C2,C3
|
||||||
|
@@ -61,10 +61,15 @@ class ConstructTest extends TestCase {
|
|||||||
$ob_get_clean = ob_get_clean();
|
$ob_get_clean = ob_get_clean();
|
||||||
$verb = strtok($script_file, '_.');
|
$verb = strtok($script_file, '_.');
|
||||||
|
|
||||||
if (!in_array($verb, ['download', 'save'], true)) {
|
if (!in_array($verb, ['download', 'save'], TRUE)) {
|
||||||
|
if (method_exists($this, 'assertStringContainsString')) {
|
||||||
|
$this->assertStringContainsString('<td>', $ob_get_clean);
|
||||||
|
}
|
||||||
|
else {
|
||||||
$this->assertContains('<td>', $ob_get_clean);
|
$this->assertContains('<td>', $ob_get_clean);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
chdir('..');
|
chdir('..');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ class DataRowCountTest extends TestCase {
|
|||||||
$this->csv = new Csv();
|
$this->csv = new Csv();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function countRowsProvider() {
|
public static function countRowsProvider() {
|
||||||
return [
|
return [
|
||||||
'auto-double-enclosure' => [
|
'auto-double-enclosure' => [
|
||||||
'auto-double-enclosure.csv',
|
'auto-double-enclosure.csv',
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ class ParseTest extends TestCase {
|
|||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function autoDetectionProvider() {
|
public static function autoDetectionProvider() {
|
||||||
return [
|
return [
|
||||||
'UTF8_no_BOM' => [__DIR__ . '/../example_files/UTF-8_sep_row_but_no_BOM.csv'],
|
'UTF8_no_BOM' => [__DIR__ . '/../example_files/UTF-8_sep_row_but_no_BOM.csv'],
|
||||||
'UTF8' => [__DIR__ . '/../example_files/UTF-8_with_BOM_and_sep_row.csv'],
|
'UTF8' => [__DIR__ . '/../example_files/UTF-8_with_BOM_and_sep_row.csv'],
|
||||||
@@ -87,7 +87,9 @@ class ParseTest extends TestCase {
|
|||||||
self::assertEquals(false, $this->csv->autoDetectionForDataString($sInput));
|
self::assertEquals(false, $this->csv->autoDetectionForDataString($sInput));
|
||||||
self::assertEquals(null, $this->csv->delimiter);
|
self::assertEquals(null, $this->csv->delimiter);
|
||||||
$expected_data = explode("\r\n", $sInput);
|
$expected_data = explode("\r\n", $sInput);
|
||||||
$actual_data = array_map('reset', $this->csv->data);
|
$actual_data = array_map(function ($k) {
|
||||||
|
return reset($k);
|
||||||
|
}, $this->csv->data);
|
||||||
self::assertEquals($expected_data, $actual_data);
|
self::assertEquals($expected_data, $actual_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,7 +100,9 @@ class ParseTest extends TestCase {
|
|||||||
$expected_data = [86545235689, 34365587654, 13469874576];
|
$expected_data = [86545235689, 34365587654, 13469874576];
|
||||||
|
|
||||||
$actual_data = $this->invokeMethod($this->csv, '_parse_string', [$sInput]);
|
$actual_data = $this->invokeMethod($this->csv, '_parse_string', [$sInput]);
|
||||||
$actual_column = array_map('reset', $actual_data);
|
$actual_column = array_map(function ($k) {
|
||||||
|
return reset($k);
|
||||||
|
}, $actual_data);
|
||||||
self::assertEquals($expected_data, $actual_column);
|
self::assertEquals($expected_data, $actual_column);
|
||||||
self::assertEquals(
|
self::assertEquals(
|
||||||
[
|
[
|
||||||
@@ -106,7 +110,9 @@ class ParseTest extends TestCase {
|
|||||||
'b',
|
'b',
|
||||||
"c\r\nd",
|
"c\r\nd",
|
||||||
],
|
],
|
||||||
array_map('next', $actual_data)
|
array_map(function ($el) {
|
||||||
|
return next($el);
|
||||||
|
}, $actual_data)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,6 +127,15 @@ class ParseTest extends TestCase {
|
|||||||
self::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() {
|
public function testMatomoData() {
|
||||||
// Matomo (Piwik) export cannot be read with
|
// Matomo (Piwik) export cannot be read with
|
||||||
$this->csv->use_mb_convert_encoding = true;
|
$this->csv->use_mb_convert_encoding = true;
|
||||||
@@ -166,18 +181,22 @@ class ParseTest extends TestCase {
|
|||||||
|
|
||||||
// This also tests if ::load_data removed the BOM from the data;
|
// This also tests if ::load_data removed the BOM from the data;
|
||||||
// otherwise the 'title' column would have 3 extra bytes.
|
// otherwise the 'title' column would have 3 extra bytes.
|
||||||
$this->assertEquals([
|
$this->assertEquals(
|
||||||
|
[
|
||||||
'title',
|
'title',
|
||||||
'isbn',
|
'isbn',
|
||||||
'publishedAt',
|
'publishedAt',
|
||||||
], array_keys(reset($this->csv->data)));
|
],
|
||||||
|
array_keys(reset($this->csv->data)));
|
||||||
|
|
||||||
$titles = array_column($this->csv->data, 'title');
|
$titles = array_column($this->csv->data, 'title');
|
||||||
$this->assertEquals([
|
$this->assertEquals(
|
||||||
|
[
|
||||||
'Красивая кулинария',
|
'Красивая кулинария',
|
||||||
'The Wine Connoisseurs',
|
'The Wine Connoisseurs',
|
||||||
'Weißwein',
|
'Weißwein',
|
||||||
], $titles);
|
],
|
||||||
|
$titles);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWithMultipleNewlines() {
|
public function testWithMultipleNewlines() {
|
||||||
@@ -185,7 +204,8 @@ class ParseTest extends TestCase {
|
|||||||
$aElse9 = array_column($this->csv->data, 'else9');
|
$aElse9 = array_column($this->csv->data, 'else9');
|
||||||
|
|
||||||
/** @noinspection SpellCheckingInspection */
|
/** @noinspection SpellCheckingInspection */
|
||||||
$this->assertEquals([
|
$this->assertEquals(
|
||||||
|
[
|
||||||
'Abweichung',
|
'Abweichung',
|
||||||
'Abweichung',
|
'Abweichung',
|
||||||
'Abweichung',
|
'Abweichung',
|
||||||
@@ -196,7 +216,8 @@ class ParseTest extends TestCase {
|
|||||||
'Fehlt',
|
'Fehlt',
|
||||||
'Fehlt',
|
'Fehlt',
|
||||||
'Fehlt',
|
'Fehlt',
|
||||||
], $aElse9);
|
],
|
||||||
|
$aElse9);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -273,7 +294,7 @@ class ParseTest extends TestCase {
|
|||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function autoQuotesDataProvider(): array {
|
public static function autoQuotesDataProvider(): array {
|
||||||
return array(
|
return array(
|
||||||
array('auto-double-enclosure.csv', '"'),
|
array('auto-double-enclosure.csv', '"'),
|
||||||
array('auto-single-enclosure.csv', "'"),
|
array('auto-single-enclosure.csv', "'"),
|
||||||
@@ -331,4 +352,10 @@ class ParseTest extends TestCase {
|
|||||||
self::assertFalse($this->csv->parseFile(''));
|
self::assertFalse($this->csv->parseFile(''));
|
||||||
self::assertFalse($this->csv->parseFile(null));
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ class SaveTest extends TestCase {
|
|||||||
private $temp_filename;
|
private $temp_filename;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup our test environment objects; will be called before each test.
|
* Set up our test environment objects; will be called before each test.
|
||||||
*/
|
*/
|
||||||
protected function setUp(): void {
|
protected function setUp(): void {
|
||||||
$this->csv = new Csv();
|
$this->csv = new Csv();
|
||||||
@@ -51,11 +51,39 @@ class SaveTest extends TestCase {
|
|||||||
|
|
||||||
public function testSaveWithNewHeader() {
|
public function testSaveWithNewHeader() {
|
||||||
$this->csv->linefeed = "\n";
|
$this->csv->linefeed = "\n";
|
||||||
$this->csv->titles = array("NewTitle");
|
$this->csv->titles = ["NewTitle"];
|
||||||
$expected = "NewTitle\n0444\n5555\n";
|
$expected = "NewTitle\n0444\n5555\n";
|
||||||
$this->saveAndCompare($expected);
|
$this->saveAndCompare($expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSaveWithDelimiterOfComma() {
|
||||||
|
$this->csv = new Csv();
|
||||||
|
$this->csv->heading = false;
|
||||||
|
$this->csv->delimiter = ",";
|
||||||
|
$this->csv->linefeed = "\n";
|
||||||
|
$this->csv->data = [
|
||||||
|
[
|
||||||
|
'3,21',
|
||||||
|
'Twitter',
|
||||||
|
'Monsieur',
|
||||||
|
'eat more vegan food',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'"9,72"',
|
||||||
|
'newsletter',
|
||||||
|
'Madame',
|
||||||
|
'"free travel"',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
// Yep, these double quotes are what Excel and Open Office understand.
|
||||||
|
$expected =
|
||||||
|
'"3,21",Twitter,Monsieur,eat more vegan food' . "\n" .
|
||||||
|
'"""9,72""",newsletter,Madame,"""free travel"""' . "\n";
|
||||||
|
$actual = $this->csv->unparse();
|
||||||
|
self::assertSame($expected, $actual);
|
||||||
|
}
|
||||||
|
|
||||||
public function testSaveWithoutHeader() {
|
public function testSaveWithoutHeader() {
|
||||||
$this->csv->linefeed = "\n";
|
$this->csv->linefeed = "\n";
|
||||||
$this->csv->heading = false;
|
$this->csv->heading = false;
|
||||||
@@ -63,9 +91,9 @@ class SaveTest extends TestCase {
|
|||||||
$this->saveAndCompare($expected);
|
$this->saveAndCompare($expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAllQuotes() {
|
public function testEncloseAllWithQuotes() {
|
||||||
$this->csv->enclose_all = true;
|
$this->csv->enclose_all = true;
|
||||||
$expected = "\"SMS\"\r\"0444\"\r\"5555\"\r";
|
$expected = '"SMS"'."\r".'"0444"'."\r".'"5555"'."\r";
|
||||||
$this->saveAndCompare($expected);
|
$this->saveAndCompare($expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ class UnparseTest extends Testcase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function testUnparseWithParameters() {
|
public function testUnparseWithParameters() {
|
||||||
$fields = array('a' => 'AA', 'b' => 'BB');
|
$fields = ['a' => 'AA', 'b' => 'BB'];
|
||||||
$data = [['a' => 'value1', 'b' => 'value2']];
|
$data = [['a' => 'value1', 'b' => 'value2']];
|
||||||
$csv_object = new Csv();
|
$csv_object = new Csv();
|
||||||
$csv_string = $csv_object->unparse($data, $fields);
|
$csv_string = $csv_object->unparse($data, $fields);
|
||||||
@@ -43,6 +43,9 @@ class UnparseTest extends Testcase {
|
|||||||
$this->unparseAndCompare($expected);
|
$this->unparseAndCompare($expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @doesNotPerformAssertions
|
||||||
|
*/
|
||||||
public function testUnparseDefaultWithoutHeading() {
|
public function testUnparseDefaultWithoutHeading() {
|
||||||
$this->csv->heading = false;
|
$this->csv->heading = false;
|
||||||
$this->csv->auto(__DIR__ . '/fixtures/auto-double-enclosure.csv');
|
$this->csv->auto(__DIR__ . '/fixtures/auto-double-enclosure.csv');
|
||||||
@@ -52,24 +55,21 @@ class UnparseTest extends Testcase {
|
|||||||
|
|
||||||
public function testUnparseRenameFields() {
|
public function testUnparseRenameFields() {
|
||||||
$expected = "C1,C2\rvalue1,value2\rvalue3,value4\r";
|
$expected = "C1,C2\rvalue1,value2\rvalue3,value4\r";
|
||||||
$this->unparseAndCompare($expected, array("C1", "C2"));
|
$this->unparseAndCompare($expected, ["C1", "C2"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testReorderFields() {
|
public function testReorderFields() {
|
||||||
$expected = "column2,column1\rvalue2,value1\rvalue4,value3\r";
|
$expected = "column2,column1\rvalue2,value1\rvalue4,value3\r";
|
||||||
$this->unparseAndCompare($expected, array("column2", "column1"));
|
$this->unparseAndCompare($expected, ["column2", "column1"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSubsetFields() {
|
public function testSubsetFields() {
|
||||||
$expected = "column1\rvalue1\rvalue3\r";
|
$expected = "column1\rvalue1\rvalue3\r";
|
||||||
$this->unparseAndCompare($expected, array("column1"));
|
$this->unparseAndCompare($expected, ["column1"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testReorderAndRenameFields() {
|
public function testReorderAndRenameFields() {
|
||||||
$fields = array(
|
$fields = ['column2' => 'C2', 'column1' => 'C1'];
|
||||||
'column2' => 'C2',
|
|
||||||
'column1' => 'C1',
|
|
||||||
);
|
|
||||||
$expected = "C2,C1\rvalue2,value1\rvalue4,value3\r";
|
$expected = "C2,C1\rvalue2,value1\rvalue4,value3\r";
|
||||||
$this->unparseAndCompare($expected, $fields);
|
$this->unparseAndCompare($expected, $fields);
|
||||||
}
|
}
|
||||||
@@ -99,7 +99,7 @@ class UnparseTest extends Testcase {
|
|||||||
$this->unparseAndCompare($expected);
|
$this->unparseAndCompare($expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function unparseAndCompare($expected, $fields = array()) {
|
private function unparseAndCompare($expected, $fields = []) {
|
||||||
$str = $this->csv->unparse($this->csv->data, $fields);
|
$str = $this->csv->unparse($this->csv->data, $fields);
|
||||||
$this->assertEquals($expected, $str);
|
$this->assertEquals($expected, $str);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ class OffsetTest extends BaseClass {
|
|||||||
$this->assertEquals($expected, $actual);
|
$this->assertEquals($expected, $actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function numberRangeZeroToFourProvider() {
|
public static function numberRangeZeroToFourProvider() {
|
||||||
return array_map(function ($number) {
|
return array_map(function ($number) {
|
||||||
return [$number];
|
return [$number];
|
||||||
}, range(0, 4));
|
}, range(0, 4));
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ class SortByTest extends BaseClass {
|
|||||||
public function testSortByRating() {
|
public function testSortByRating() {
|
||||||
$this->csv->sort_by = 'rating';
|
$this->csv->sort_by = 'rating';
|
||||||
$this->csv->conditions = 'title does not contain Blood';
|
$this->csv->conditions = 'title does not contain Blood';
|
||||||
|
if (!preg_match('/^8\.2\./', phpversion()) && !preg_match('/^8\.3\./', phpversion()))
|
||||||
$this->_compareWithExpected([
|
$this->_compareWithExpected([
|
||||||
// Rating 0
|
// Rating 0
|
||||||
'The Killing Kind',
|
'The Killing Kind',
|
||||||
@@ -27,6 +28,27 @@ class SortByTest extends BaseClass {
|
|||||||
'Digital Fortress : A Thriller (Mass Market Paperback)',
|
'Digital Fortress : A Thriller (Mass Market Paperback)',
|
||||||
'Angels & Demons (Mass Market Paperback)',
|
'Angels & Demons (Mass Market Paperback)',
|
||||||
]);
|
]);
|
||||||
|
else
|
||||||
|
$this->_compareWithExpected([
|
||||||
|
// Rating 0
|
||||||
|
'The Killing Kind',
|
||||||
|
'The Third Secret',
|
||||||
|
|
||||||
|
// Rating 3
|
||||||
|
'The Last Templar',
|
||||||
|
'The Broker (Paperback)',
|
||||||
|
|
||||||
|
// Rating 4
|
||||||
|
'Deception Point (Paperback)',
|
||||||
|
'The Rule of Four (Paperback)',
|
||||||
|
'The Da Vinci Code (Hardcover)',
|
||||||
|
|
||||||
|
// Rating 5
|
||||||
|
'Angels & Demons (Mass Market Paperback)',
|
||||||
|
'State of Fear (Paperback)',
|
||||||
|
'Prey',
|
||||||
|
'Digital Fortress : A Thriller (Mass Market Paperback)',
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testReverseSortByRating() {
|
public function testReverseSortByRating() {
|
||||||
|
|||||||
18
tests/rector.php
Normal file
18
tests/rector.php
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Rector\Config\RectorConfig;
|
||||||
|
use Rector\PHPUnit\AnnotationsToAttributes\Rector\ClassMethod\DataProviderAnnotationToAttributeRector;
|
||||||
|
use Rector\PHPUnit\AnnotationsToAttributes\Rector\ClassMethod\DependsAnnotationWithValueToAttributeRector;
|
||||||
|
use Rector\PHPUnit\CodeQuality\Rector\Class_\AddCoversClassAttributeRector;
|
||||||
|
use Rector\PHPUnit\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector;
|
||||||
|
|
||||||
|
return static function(RectorConfig $rectorConfig): void {
|
||||||
|
if (class_exists(AddCoversClassAttributeRector::class)) {
|
||||||
|
$rectorConfig->rule(AddCoversClassAttributeRector::class);
|
||||||
|
}
|
||||||
|
$rectorConfig->rule(DataProviderAnnotationToAttributeRector::class);
|
||||||
|
$rectorConfig->rule(AddDoesNotPerformAssertionToNonAssertingTestRector::class);
|
||||||
|
$rectorConfig->rule(DependsAnnotationWithValueToAttributeRector::class);
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user