239 Commits

Author SHA1 Message Date
Fonata
f2b0aecd12 docs: remove Travis from README; make running act locally easier 2021-11-07 13:36:38 +01:00
Fonata
5d4643b201 Allow _guess_delimiter to work with a single row of data
Fix #206
2021-11-07 13:36:38 +01:00
d5606f8b2a ci(travis): remove .travis.yml config file
If we're switching to GitHub Actions, there's no need to keep the
Travis-CI config file around.
2021-11-07 13:36:38 +01:00
8cebcbd9bb ci(github): add GitHub Actions CI workflow
Can be tested locally via the local-ci make target, which requires
Docker and act (https://github.com/nektos/act).
2021-11-07 13:36:38 +01:00
518f5081fb chore: improve editorconfig for YAML and Makefile 2021-11-07 13:36:38 +01:00
Fonata
a28fc6ab0a refactor: simplify code without changing behavior 2021-11-07 13:36:38 +01:00
Fonata
009820d190 Explicit email address for security problems 2021-06-20 23:25:35 +02:00
Fonata
facdf1c06c Set release date 2021-06-20 23:25:26 +02:00
Fonata
ee13c17157 Apply PhpStorm source code formatting 2021-06-20 23:21:46 +02:00
Fonata
99daaa7235 Bugfix: $csv->parseFile now sets $this->data
This adds consistency because $csv->parse() does the same.

Fix #200
Fix #201
2021-06-20 23:21:46 +02:00
Fonata
05826c2bbf Make return value of parseFile() explicit as false if parsing failed
In practise this changes nothing because $this->file_data would typically be empty.
The only exception is if the object was reused from a previous parsing operation.
2021-06-20 23:21:46 +02:00
Fonata
731900effe DocBlock types: Use Psalm notation for 2-dimensional array 2021-06-20 23:21:46 +02:00
Fonata
913c3b1b94 Source code comments: add more information to DocBlocks 2021-06-20 23:21:46 +02:00
Fonata
96b2784d3c Source code comments: don’t repeat the field we are describing
The motivation for this change is how the PhpStorm IDE displays help texts.
Because the enter is shown as a space in the tooltips/help hovers, the
previous version was harder to read than after this commit.
2021-06-20 23:21:46 +02:00
Fonata
be01bc9ae4 README.md: Fix incorrect constructor usage 2021-05-03 21:26:56 +02:00
Fonata
b444afae2f Specify release date 2021-04-14 20:14:01 +02:00
Fonata
bd6befe01e Remove low-value comment 2021-04-14 20:12:43 +02:00
Fonata
05ed8ec667 Rename parameter to reduce confusion 2021-04-14 20:12:43 +02:00
Fonata
d21233c0f8 Remove Travis tests for PHP 7.0
It said:
> TypeError: Return value of ParseCsv\tests\properties\BaseClass::setUp() must be an instance of void, none returned
2021-04-14 20:12:43 +02:00
Fonata
7a066f9d5e Add test for empty first parameter 2021-04-14 20:12:43 +02:00
Fonata
2425c1bbda Deprecation: Stronger language in PHP DocBlock 2021-04-14 20:12:43 +02:00
Fonata
eb502f9dad PHP DocBlock: use more common type name 2021-04-14 20:12:43 +02:00
Fonata
0af6e98670 Tests should not produce output - capture it 2021-04-14 20:12:43 +02:00
Fonata
b38d0de800 Add test for getTotalDataRowCount without loading data 2021-04-14 20:12:43 +02:00
Fonata
e15d144e27 Adjust call to static function 2021-04-14 20:12:43 +02:00
Fonata
4518e944c4 Adjust function signatures for newer PHPUnit versions
Will make an update to PHPUnit 8 or 9 easier.
2021-04-14 20:12:43 +02:00
Fonata
3ce6a82052 Make deprecation clearer 2021-04-14 20:12:43 +02:00
Fonata
abae91a1a2 Make condition easier to read; no change in logic 2021-04-14 20:12:43 +02:00
Fonata
45f079b5dd Code comment for future me: I was afraid the code would surprise users 2021-04-14 20:12:43 +02:00
Fonata
fba67b84ff More consistent formatting 2021-04-14 20:12:43 +02:00
Fonata
2a154869b4 Fix PHP DocBlock: some tools thought the function always returns true
(Also, there was a spelling mistake)
2021-04-14 20:12:43 +02:00
Fonata
d1186fcafa Deprecate support for file paths in __construct() and parse()
See https://github.com/parsecsv/parsecsv-for-php/issues/198
2021-04-14 20:12:43 +02:00
Fonata
9c5eee23f6 Fix examples in README.md: add missing semicolons 2021-04-14 20:12:43 +02:00
Fonata
6a54a6a509 Move parsing of .php files (sic!) to _rfile for easier refactoring
Note that this arcane code comes from v0.2.0 beta:
5f0e6b03d4
2021-04-14 20:12:43 +02:00
Fonata
8277b9682d Ignore files on the root directory only 2021-04-14 20:12:43 +02:00
Fonata
0e54a2b9fa Make testing easier 2021-04-14 20:12:43 +02:00
Fonata
9c35237b1b README: make sure examples can be used in namespaced files 2020-12-15 16:20:14 +01:00
Fonata
1528a1f6ae README.md: fix the function name fix from my last commit 2020-12-07 18:13:09 +01:00
Fonata
aadb654edf Fix incorrect function name in README.md
Closes #195
2020-11-12 06:40:58 +01:00
Sergei Morozov
8069409c73 Add .gitattributes 2020-05-10 14:57:29 +02:00
Fonata
947b1c0866 Removed PHPUnit <6 compat wrapper 2020-04-25 19:17:04 +02:00
Petri Haikonen
a3981edb84 Added some annotations to tests 2020-04-25 19:17:04 +02:00
Petri Haikonen
b4e4c14b42 PHPUnit 6 instead of 4, to get rid of wrapper
The TestCase wrapper caused PhpStorm to show warnings about
multiple implementations

Fixes #188

Improved some type hints in DocBlocks
2020-04-25 19:17:04 +02:00
Fonata
89429ebd60 Cleaned up README 2020-04-25 19:17:04 +02:00
Fonata
548a9fe551 ChangeLog: Added release date 2020-01-07 09:32:12 +01:00
susgo
5336800ec6 Merge pull request #186 from parsecsv/bu-object-test
Added test for the case of objects as cells
2020-01-06 19:35:23 +01:00
susgo
6bd53940c5 Merge pull request #185 from parsecsv/release-1.2.0
Release candidate 1.2.0
2020-01-06 19:32:41 +01:00
Fonata
a9297449b9 Autoloading: No need to explicitly mention ParseCsv\extensions
It is a sub-directory of src anyway.
2020-01-06 19:04:58 +01:00
Fonata
c12b6ba671 Added test for the case of objects as cells
I encountered this in Drupal 8, where cells implemented the
MarkupInterface. It was objects that, when cast to a string,
returned the translated string.

Before 5ca540daa7, this new test
would fail.
2020-01-06 18:12:37 +01:00
Fonata
b792a6cc7b Code quality: Removed superfluous brackets; should not change anything 2020-01-06 18:04:45 +01:00
Fonata
af442cdd0e PHP closing tags are no longer common practise 2019-12-25 15:09:31 +01:00
Fonata
95810ec2fa Updated ChangeLog.txt 2019-12-25 15:08:37 +01:00
Fonata
d79160f40a Examples: Removed deprecated HTTML attributes from <table> tag 2019-12-25 15:06:18 +01:00
Fonata
7ba2f9b296 Added an example for a verbal comparison 2019-12-25 15:04:07 +01:00
Fonata
d7ba892d38 Bug fix for $csv->conditions = 'rating is LESS than 4'
The $op was wrong because of upper case
2019-12-25 14:54:50 +01:00
Jess
38e9fff285 Added financial contributors to the README 2019-12-25 07:57:32 -05:00
Fonata
174d2e6070 Merge branch 'andreybolonin-patch-1' 2019-12-25 13:49:08 +01:00
Andrey Bolonin
d93036ca87 Update .travis.yml 2019-12-25 13:47:59 +01:00
Craig Morris
62fc367c14 Run https://gist.github.com/theodorejb/763b83a43522b0fc1755a537663b1863 2019-12-25 07:45:00 -05:00
susgo
044122efb9 Merge pull request #180 from parsecsv/more-conditions-bu
Bugfix for verbal conditions
2019-11-04 21:20:58 +01:00
susgo
18cbc3ab87 Merge pull request #174 from parsecsv/code-quality-and-documentation
Code quality and documentation
2019-11-04 21:19:35 +01:00
Fonata
af75206e86 Travis dist implicitly switched to Ubuntu Xenial, which didn't have PHP 5.5 2019-11-03 23:19:51 +01:00
Fonata
e45eb18213 Bugfix: All operators containing "is" or "equals" were broken
Fixes #179
2019-11-03 23:09:23 +01:00
Fonata
5340ab9584 New feature: Verbal condition operators don't have to be lower case 2019-11-03 23:08:08 +01:00
Fonata
f3601dbfb5 PHP 5.5 was not available; attempt to use Ubuntu Trusty 2019-09-14 09:01:10 +02:00
Fonata
67c8a0167c Added test for 1 MB stream data 2019-09-14 04:17:58 +02:00
Fonata
8b583daa08 Improved documentation comments 2019-09-14 04:10:58 +02:00
Fonata
b6dccf5fa6 Use in_array() to make code more readable 2019-09-14 03:56:00 +02:00
Fonata
5ca540daa7 Avoid PhpStorm inspection for $value{0} on empty string (prevent E_NOTICE)
Here, $value is not empty, but the PhpStorm inspection doesn't get that.
I want to have zero warnings for this library.
2019-09-14 01:19:40 +02:00
Fonata
f89ed5978d Simplified RegEx: {1} is the default and can be dropped 2019-09-14 01:17:09 +02:00
Fonata
f053cdde80 New feature: unparse now also understands $use_mb_convert_encoding 2019-09-14 01:14:41 +02:00
Fonata
62ae4e5fc3 Formated code using PhpStorm 2019.2.1 - only DocBlocks changed 2019-09-14 01:05:21 +02:00
susgo
a286556c16 Merge pull request #171 from parsecsv/travis-conf-bu
Travis conf didn't match schema
2019-07-27 12:11:55 +02:00
Fonata
7326262304 Travis conf didn't match schema
- PHP versions need to be strings
- https://docs.travis-ci.com/user/notifications/#configuring-email-notifications

Also: tell editors like PhpStorm how the YAML needs to be formatted.
2019-07-26 21:55:06 +02:00
susgo
b746ea1de0 Merge pull request #166 from parsecsv/parse_string_internal
End users should not be tempted to call parse_string.
2019-05-11 14:19:52 +02:00
Fonata
ef5e81f837 Added test for our code example in issue #165
Closes #165
2019-03-22 08:17:55 +01:00
Fonata
637f79c2e6 Improved documentation and parameter declaration
No functional changes.
2019-03-22 08:17:55 +01:00
Fonata
e3105d4003 Renamed protected functions: no end user should be tempted to call them 2019-03-22 08:17:55 +01:00
susgo
5874b768b9 Merge pull request #164 from parsecsv/composer-validate
Quality improvement, but no bug fixes or new features
2019-02-24 05:47:39 +01:00
Fonata
5d766adc17 PHPUnit versions newer than 8 don't like our setUp function
See https://travis-ci.org/parsecsv/parsecsv-for-php/jobs/497446669
2019-02-23 13:21:54 +01:00
Fonata
67ed765eff Added PHP 7.3 to test coverage 2019-02-23 13:09:09 +01:00
Fonata
a7be07cc02 PHP 7.2.15 doesn't seem to like public setUp functions 2019-02-23 13:06:28 +01:00
Fonata
b4cef37bb5 Simplyfied BaseClass.php: @access annotations are not needed 2019-02-23 13:05:55 +01:00
Fonata
f97f03a088 Only improved code formatting 2019-02-23 12:57:48 +01:00
Fonata
2e94f4ad41 Add composer validation to CI;
Also: add branch alias for `dev-master`

Fix #159
2019-02-23 12:53:58 +01:00
Fonata
d03f8a2f09 Simplified code; shouldn't change results 2019-02-23 12:48:52 +01:00
Fonata
14c9254ed4 Removed @access annotations because they don't add value
I believe they come from the time when PHP didn't universally
support the language keywords. phpDocumentor doesn't need the
@access annotations.
2019-02-23 12:45:28 +01:00
Fonata
78c288db8c Improved documentation comment 2019-02-23 12:45:07 +01:00
Fonata
531ac26e11 Improved spelling in comments 2019-02-23 12:44:31 +01:00
susgo
65566adcd3 Merge pull request #162 from parsecsv/add-save-example
Added example for writing CSV files to better document heading property
2019-02-02 20:40:45 +01:00
Fonata
2d428ffa93 Updated test: no output in save_to_file_without_header_row.php
Thus, effectively, we just test for valid PHP syntax.
2019-02-02 18:47:55 +01:00
Fonata
bc9207de09 Added example for writing CSV files to better document heading property
Fixes #161
2019-02-02 18:39:21 +01:00
susgo
ace09c3c11 Update README.md
Update examples for #155
2018-11-14 18:30:03 +01:00
Susann Sgorzaly
52ad56c66a fixes unparse bug if no data for unparsing remain (comments #150) 2018-11-14 08:26:39 +01:00
Susann Sgorzaly
ab9e8a0af9 fixes unparse bug if array ids doesn't begin on zero (comments#149) 2018-11-14 08:26:39 +01:00
Fonata
8aa61914f7 Function load_data: check length of input, prevents E_NOTICE if too long
Fixes #151
2018-09-25 00:35:03 +02:00
Fonata
3b74f7ce57 Added PHPUnit test to make sure very long text doesn't cause E_NOTICE 2018-09-25 00:27:04 +02:00
Fonata
c7375dea8e Added authors and web links to source and issues 2018-08-09 13:48:06 +02:00
Fonata
6ce4550eed Merge pull request #147 from parsecsv/file_get_contents
Replace fread with file_get_contents to avoid the 8192 byte limit
2018-08-08 21:02:26 +02:00
Fonata
55890da647 Replace fread with file_get_contents to avoid the 8192 byte limit
This can happen when using streams.

Specific problem occurred with a file uploaded with Drupal's ``managed_file``
Form API element.

See https://secure.php.net/manual/en/function.fread.php
2018-07-31 21:59:04 +02:00
Fonata
21953ab6be Merge pull request #146 from parsecsv/waiver-test
Added test for issue 145, couldn't reproduce it
2018-05-22 09:01:01 +02:00
Fonata
1d8e6808bc Added test for issue #145, couldn't reproduce it 2018-05-21 21:56:54 +02:00
Fonata
063a812732 Merge pull request #142 from parsecsv/issue-141-multiple-empty-lines
Ignore entirely empty lines at the end of files
2018-05-11 09:03:36 +02:00
Fonata
4504f4b158 Converted function names to camelCase for PSR karma points 2018-04-29 11:21:00 +02:00
Fonata
b82ad03bd5 Ignore entirely empty lines at the end of files
Closes #141
2018-04-29 11:08:40 +02:00
Fonata
23a1b0a4b3 Merge pull request #138 from geminorum/master
min php on composer.json
2018-04-11 08:47:06 +02:00
Fonata
51d878a9a1 Merge pull request #137 from parsecsv/new-unparse-test
Added new test for unparse with parameters
2018-04-11 08:31:33 +02:00
Fonata
7d39b80fca Merge pull request #140 from susgo/patch-1
Update README.md: corrected function name getTotalDataRowCount()
2018-04-11 08:30:21 +02:00
susgo
8c7eba6815 Update README.md 2018-04-05 16:32:44 +02:00
Nasser Rafie
c41c559ac2 min php on composer.json 2018-04-02 18:36:16 +04:30
Fonata
e1ecf9302e Advertised getCollection in ChangeLog.txt 2018-04-02 15:11:17 +02:00
Fonata
de8e792ef4 Merge branch 'feature-datacollections'
Conflicts:
	src/Csv.php
2018-04-02 14:40:32 +02:00
Fonata
60d6458080 PHPUnit: prevent output from download.php to leak 2018-04-02 14:38:03 +02:00
William Knauss
406e1e415f added getCollection method that returns a Illuminate\Support\Collection object - useful for using the mapping functions 2018-04-02 14:32:17 +02:00
Fonata
2e40a2eb1c Added new test for unparse with parameters 2018-03-28 15:45:44 +02:00
Fonata
927e785891 Merge pull request #133 from parsecsv/fixing-132-output-with-fields
Bug fix: calling ->output() with $fields parameter set
2018-03-28 13:25:05 +02:00
Fonata
e6ba24b47a Dropping PHP 5.4 support (#131)
Also removed special treatment of PHP 5.4, as we don't support it anymore
2018-03-20 11:12:22 +01:00
Fonata
f24e5bf3a3 Merge branch 'offset-comment-and-tests' 2018-03-17 12:44:02 +01:00
Fonata
bdd4e5ef25 Merge branch 'master' into offset-comment-and-tests
Conflicts:
	tests/methods/ParseTest.php
2018-03-17 12:42:18 +01:00
Fonata
42b5d30d66 Improved OldRequireTest: explanation and new test added (#128) 2018-03-17 12:37:54 +01:00
Fonata
93f177a396 Merged two ifs in _validate_row_condition to prevent
This is to a sane count of zero PhpStorm inspections. In particular,
this is NestedPositiveIfStatementsInspection from EA Extended.
2018-03-17 12:17:25 +01:00
Fonata
f5ff7332a4 Set $data field on unparse. This fixes issue #132. 2018-03-17 12:16:39 +01:00
Fonata
07846d33c1 Added OutputTest to verify issue #132 is a bug. 2018-03-17 12:07:12 +01:00
Fonata
4b60d38fb0 Tell PhpStorm not to complain about reference mismatch
http://blog.jpauli.tech/2014/06/27/references-mismatch.html#what-is-a-reference-mismatch
2018-03-17 12:05:01 +01:00
Fonata
c8d15557cb Fixed parse()'s return value: return true only if $data is useful 2018-03-17 12:05:01 +01:00
Fonata
10895788c8 Slightly simplified getTotalDataRowCount, added test
The brackets () in the pattern were not needed, as only
$matches[0] was accessed, not $matches[1].

The @var is useful for PhpStorm's Php Inspections (EA Extended).
More details here: https://github.com/kalessil/phpinspectionsea/blob/master/docs/types-compatibility.md#foreach-source-to-iterate-over
2018-03-17 12:05:01 +01:00
Fonata
ffed7ffdc0 Only improved PHPDoc blocks, including sentance for output() 2018-03-17 12:05:01 +01:00
Fonata
7498a963ac _validate_fields_for_unparse: more info in UnexpectedValueException 2018-03-17 11:42:44 +01:00
Fonata
49728a74bb Only reformatted the source code 2018-03-17 10:31:09 +01:00
susgo
e4c9fed6cf Update README.md for old and new functions (#129)
* Update README.md
2018-03-12 08:28:25 +01:00
Susann Sgorzaly
5bc6d09b5e fixes bug on _validate_fields_for_unparse() if titles property is used instead of fields parameter for changing the titles for unparsing 2018-03-12 08:25:42 +01:00
susgo
b7f2075efc Fix #41: output order and subset (#126)
* init unparse tests for ordering and subseting by fields
* added one test for heading=false
* implements functionality of this issue
2018-03-11 11:14:53 +01:00
Christian Bläul
3cc20fbb47 Merge remote-tracking branch 'itexia/auto-detect-file-has-heading'
# Conflicts:
#	tests/methods/ParseTest.php
2018-03-10 12:46:36 +01:00
Christian Bläul
086cd15b44 Added requires to keep Composer-free environments working 2018-03-07 09:51:50 +01:00
Christian Bläul
03bc946b98 Sorted uses alphabetically, removed version
The repo is no longer on version 1.0.0. The ChangeLog.txt file is a
more obvious place for people to see which version includes what.
2018-03-07 09:45:48 +01:00
Christian Bläul
8f431aa7a4 Merge remote-tracking branch 'itexia/refactoring-constants' 2018-03-07 09:43:22 +01:00
Christian Bläul
7168cb15e3 ChangeLog.txt: Added new feature 2018-03-07 09:40:43 +01:00
Fonata
1142ef2611 Merge pull request #124 from itexia/new-enums-for-docu
New Enum class for file processing (save + unparse) + Documentation
2018-03-07 09:35:01 +01:00
Fonata
80a4954a3a Merge pull request #122 from itexia/get-total-row-count
New feature: get total row count - useful if $limit is set.
2018-03-07 09:30:36 +01:00
Christian Bläul
9c14bc2f30 Updated ChangeLog.txt with new features 2018-03-03 12:01:45 +01:00
Christian Bläul
4b6b7ee0b8 Removed -rc.2 from version, as it is officially released now 2018-03-03 11:55:57 +01:00
Christian Bläul
a80a6f1862 Moved features section up because library selection comes before install 2018-03-03 11:55:06 +01:00
Christian Bläul
da70838698 Only improved ChangeLog to include the output() change 2018-03-02 17:52:26 +01:00
Christian Bläul
9d7ccab20f Improved comment and test data to show how the offset is counted. 2018-03-02 17:50:01 +01:00
Christian Bläul
abecaf1b5d Only re-formated source code slightly 2018-03-02 17:25:38 +01:00
Christian Bläul
3af94399e6 Semantic versioning: Marked the current state of the repo as 1.0.0-rc.2 2018-03-02 17:15:31 +01:00
Christian Bläul
499088b3b2 Slightly improved README.md 2018-03-02 17:15:31 +01:00
Christian Bläul
fd63ff4a26 Removed examples from class file as they are also in the README.md 2018-03-02 17:15:31 +01:00
Christian Bläul
d250c9034b Further improved ChangeLog.txt and README.txt 2018-03-02 17:15:31 +01:00
Christian Bläul
bfc2c0901b Improved installation section of README.md; added Travid badge. 2018-03-02 17:15:31 +01:00
Christian Bläul
a0336f02ee Prepared new release in ChangeLog.txt 2018-03-02 17:15:31 +01:00
Christian Bläul
ea78f38e26 ConstructTest: undo directory change to be on the safe side. 2018-03-02 17:15:31 +01:00
Christian Bläul
c9e7997643 Don't break people's code just because they don't have Composer 2018-03-02 17:15:31 +01:00
Christian Bläul
68239d6859 No code changes, just cleaned up comments a bit 2018-03-02 17:15:31 +01:00
Christian Bläul
d05577d303 output(): Use better mime type for \t separator.
Fixes #79
2018-03-02 17:13:45 +01:00
Fonata
eedbffc375 Merge pull request #125 from itexia/42-Heading-and-offset
Fixed issue 42: heading and offset; added tests
2018-03-02 17:06:22 +01:00
Susann Sgorzaly
fb9325884d small code improvement 2018-02-28 13:47:38 +01:00
Susann Sgorzaly
11b20a3144 fixes #42. If csv has heading and offset is set, first row will always be parsed 2018-02-27 15:10:55 +01:00
Susann Sgorzaly
7766bf7c3b added two tests for reproducing the heading and offset bug. test with heading fails at the moment. if this one is green, bug would be solved 2018-02-27 15:00:14 +01:00
Susann Sgorzaly
5daa422aca new test for setting new headers before save (comments #82) 2018-02-27 14:33:26 +01:00
Susann Sgorzaly
48a3cdbc5c new enum for file processing mode. extended documentation (comments #112) 2018-02-27 14:18:00 +01:00
Susann Sgorzaly
fbe5263bca only code improvements 2018-02-27 13:22:11 +01:00
Susann Sgorzaly
5b1002a677 test correction 2018-02-26 10:06:59 +01:00
Susann Sgorzaly
4bbc928f09 added dependency for test 2018-02-26 10:00:52 +01:00
Susann Sgorzaly
951fc68886 new feature: auto detect if parsed file has heading 2018-02-26 09:46:44 +01:00
Susann Sgorzaly
68b849a37b corrected regex to fit all given enclosures. Added test for single enclosure 2018-02-26 08:55:51 +01:00
Susann Sgorzaly
aaefe2a480 introduces new local variable that holds the data 2018-02-26 08:50:35 +01:00
Susann Sgorzaly
611b1a92e8 use strpos instead of preg_match 2018-02-26 08:46:41 +01:00
Susann Sgorzaly
e5eccf1fc1 put tests into new file 2018-02-26 08:38:08 +01:00
Susann Sgorzaly
9e5c97328d renamed function to getTotalDataRowCount 2018-02-26 08:33:09 +01:00
Susann Sgorzaly
b6247c367c reformat code; only extended comment for new function 2018-02-26 08:31:46 +01:00
Susann Sgorzaly
ba4cc0672a reformat code 2018-02-26 06:57:40 +01:00
Susann Sgorzaly
c9cc9697ef new feature: getting total data row count without parsing all data 2018-02-24 16:55:45 +01:00
Susann Sgorzaly
f8fe4cad03 change accessibility of parse_file and parse_string 2018-02-23 10:37:12 +01:00
Susann Sgorzaly
95521cde87 reset file property if input is string 2018-02-23 10:17:09 +01:00
Susann Sgorzaly
249e5a24ac readded missing return statement in parse-function 2018-02-23 10:14:01 +01:00
Susann Sgorzaly
343c683077 corrected test for default sort type. Is set to regular now 2018-02-23 10:02:13 +01:00
Susann Sgorzaly
cf91bf40ff now compatible with old sorting values 2018-02-23 08:11:56 +01:00
Susann Sgorzaly
958af1027e small code improvements 2018-02-22 21:34:39 +01:00
Susann Sgorzaly
7a3120dd28 added test for sort enums (todo: handle exception on test) 2018-02-22 21:15:00 +01:00
Susann Sgorzaly
a74736d4da init implementation of abstract enum class 2018-02-22 21:01:06 +01:00
Susann Sgorzaly
657cec4b4e added enum for sort 2018-02-22 20:41:03 +01:00
susgo
ef44ea3989 Merge pull request #7 from parsecsv/master
update
2018-02-22 14:55:40 +01:00
Christian Bläul
2ac9f450f1 Check if output contains HTML table cells
They should if the _books.csv file is found
2018-02-21 19:25:39 +01:00
Christian Bläul
3968659b9f Make sure the code examples run without syntax or runtime errors. 2018-02-21 19:22:45 +01:00
Christian Bläul
d3a084d973 Added namespace support for further tests 2018-02-21 19:04:00 +01:00
Christian Bläul
b0355be96a Re-added the parsecsv.lib.php file for compatibility 2018-02-21 19:00:41 +01:00
Christian Bläul
b753d9c694 Repaired examples: there was a require missing. 2018-02-21 18:45:50 +01:00
Christian Bläul
2b95ec5aa1 Merge remote-tracking branch 'origin/Tests-for-sort-and-cond' into pull-test
# Conflicts:
#	tests/Bootstrap.php
#	tests/methods/ParseTest.php
#	tests/properties/worthless_test.php
2018-02-21 18:41:45 +01:00
Susann Sgorzaly
3f595dfd3b only renamed variable 2018-02-21 12:55:26 +01:00
susgo
081949339b Merge pull request #6 from parsecsv/itexia-fixed-remaining-test
Fixed remaining test
2018-02-21 08:36:24 +01:00
Christian Bläul
a60aae47a4 getDatatypes: Documented the need for PHP 5.5 or higher 2018-02-20 22:16:49 +01:00
Christian Bläul
4cba97ad51 Allow PhpStorm to understand the code better
(I followed suggestions from PhpStorm's inspections)
2018-02-20 22:07:08 +01:00
Christian Bläul
2206ec1e7c only reformatted source code to match the rest of the project 2018-02-20 22:06:06 +01:00
Christian Bläul
d188921ab6 composer.json: Only allow to load tests in dev environment 2018-02-20 21:50:52 +01:00
Christian Bläul
85cc0d9cfc Updated README.md to new name & namespace 2018-02-20 21:50:45 +01:00
Christian Bläul
a7f2bbb7bc Throw more specific exception 2018-02-20 21:40:48 +01:00
Christian Bläul
d484f3e9e3 The workaround for array_count_values was wrong and unnecessary. 2018-02-20 21:37:39 +01:00
Christian Bläul
f8b2fe274a Added missing letter in function name 2018-02-20 21:35:21 +01:00
susgo
17bfd9496a Merge pull request #5 from parsecsv/itexia-repair-tests
Repair tests (somewhat)
2018-02-20 08:22:59 +01:00
Christian Bläul
ec7b1c1d55 PHPUnit: Better class names with modern naming scheme 2018-02-19 23:01:31 +01:00
Christian Bläul
ba9eae17d4 Bootstrap: Improved the conditions under which the test wrapper loads 2018-02-19 23:00:57 +01:00
Christian Bläul
8f26ce6ea3 Force Travis to output which PHPUnit version it uses 2018-02-19 22:49:43 +01:00
Christian Bläul
913dc94e2c Keep sorting keys compatible: Moved array_values call to PHPUnit.
This commit partially reverts adcd258ea2.
2018-02-19 22:08:59 +01:00
Christian Bläul
5ced1f4212 Added conditions to SortByTest because otherwise different PHP versions...
yielded different sort orders (that's sort of ok, as rating is ambiguous).

See https://travis-ci.org/parsecsv/parsecsv-for-php/jobs/336540118
2018-02-19 22:08:59 +01:00
Christian Bläul
ca84469950 Moved ConditionsTest to properties subfolder 2018-02-19 22:08:59 +01:00
Christian Bläul
75902f4a22 Added PHPUnit test for sorting; I could not get the rows in the right...
order without calling array_values - maybe this helps just PHPUnit. I
consider the risk of breaking library users' code unlikely.
2018-02-19 22:08:59 +01:00
Christian Bläul
7470d2b804 PHPUnit: Repaired file paths in autoQuotesDataProvider() 2018-02-19 22:08:59 +01:00
Christian Bläul
01f0891cfb Test some conditions 2018-02-19 22:08:59 +01:00
Susann Sgorzaly
52a9b61b97 added DatatypeEnum tests; repaired the other test for local execution; small bug fixes 2018-02-19 21:36:31 +01:00
susgo
5660be3373 Merge pull request #1 from itexia/introduce-namespaces
init introduce namespaces and enums
2018-02-19 14:22:46 +01:00
Susann Sgorzaly
70b6fe4bd8 Merge branch 'introduce-namespaces' of https://github.com/itexia/parsecsv-for-php into introduce-namespaces 2018-02-19 14:10:10 +01:00
Susann Sgorzaly
f1a89127c2 regex updated according to merge conflict 2018-02-19 14:09:30 +01:00
susgo
31dba0411e Merge pull request #4 from parsecsv/test-helper-for-itexia-with-namespaces
Test helper for ITEXIA fork with namespaces
2018-02-19 14:05:14 +01:00
susgo
6da0537197 Merge branch 'introduce-namespaces' into test-helper-for-itexia-with-namespaces 2018-02-19 14:04:37 +01:00
susgo
91091ac933 Merge pull request #2 from itexia/new-enom-datatype
init enum for data type
2018-02-19 12:08:38 +01:00
susgo
2c7c552515 Update DatatypeTrait.php 2018-02-19 12:07:32 +01:00
Susann Sgorzaly
52e87953a4 corrected datatype count 2018-02-19 12:02:48 +01:00
Christian Bläul
2b6e38734c Merge remote-tracking branch 'origin/test-helper-for-itexia-master' into delete_this_branch 2018-02-18 19:41:38 +01:00
Christian Bläul
af7368378c Changed tests to work now that we have a namespace 2018-02-18 19:40:21 +01:00
Christian Bläul
f2fb7c81bd Absolute path in require do stop PhpStorm from nagging
(PhpStorm couldn't find the file)
2018-02-18 19:34:32 +01:00
Christian Bläul
92653c99c5 Force predictable working dir; create autoloader file if it doesn't exist 2018-02-18 19:32:39 +01:00
Christian Bläul
d4f9c9f020 Updated worthless_test to allow for more fields 2018-02-18 19:04:16 +01:00
Christian Bläul
2b17f01a0a Removed additional column to make expected data fit to fixture 2018-02-18 19:03:23 +01:00
Christian Bläul
9551862a95 Allow current working dir to be different when running tests
...By using an absolute path
2018-02-18 19:02:37 +01:00
Christian Bläul
736bc489e6 getDatatypes: throw more meaningful exception if no data is present 2018-02-18 19:01:47 +01:00
Christian Bläul
f702ca93fe Float regex: Don't allow double dot; do allow 'e' for exponentials 2018-02-18 19:00:24 +01:00
Susann Sgorzaly
48cec20f4f namespace change 2018-02-18 12:45:33 +01:00
Susann Sgorzaly
c266ef9fbc Merge branch 'introduce-namespaces' of https://github.com/itexia/parsecsv-for-php into new-enom-datatype 2018-02-18 12:43:45 +01:00
Susann Sgorzaly
76a0406eff updated examples for using new classname 2018-02-18 12:43:19 +01:00
Susann Sgorzaly
17ac1dee1a renamed namespace 2018-02-18 12:42:53 +01:00
Susann Sgorzaly
f6bd414ae7 init enum for data type 2018-02-18 00:09:22 +01:00
Susann Sgorzaly
a7a0bc34a4 init introduce namespaces 2018-02-17 23:41:03 +01:00
Susann Sgorzaly
0e5afb209e test for method getDatatypes added 2018-02-17 22:24:50 +01:00
Susann Sgorzaly
f8c68a6cd7 test improvements 2018-02-17 22:24:19 +01:00
Susann Sgorzaly
5babb55879 review changes 2018-02-14 14:03:00 +01:00
Susann Sgorzaly
8ec87126dc load files from extensions folder for tests; added new property in worthless_test 2018-02-14 12:12:53 +01:00
Susann Sgorzaly
d788135bbe exclude datatype detection in its own trait 2018-02-14 12:12:01 +01:00
Susann Sgorzaly
301f6da3e6 init new functionality to identify datatypes of columns 2018-02-13 17:18:01 +01:00
49 changed files with 2762 additions and 882 deletions

View File

@@ -13,3 +13,9 @@ insert_final_newline = true
[composer.json] [composer.json]
indent_size = 4 indent_size = 4
[Makefile]
indent_style = tab
[*.yml,*.yaml]
indent_size = 2

9
.gitattributes vendored Normal file
View 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
View 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
View File

@@ -1,6 +1,7 @@
*.bak *.bak
/.idea /.idea
/composer.lock
/coverage_clover.xml
/phive.xml /phive.xml
/tools /tools
composer.lock /vendor/
vendor/

View File

@@ -1,18 +0,0 @@
language: php
php:
- 7.2
- 7.1
- 7.0
- 5.6
- 5.5
- 5.4
script:
- phpunit --configuration tests/phpunit.xml
notifications:
email:
- will.knauss@gmail.com
on_success: never
on_failure: always

View File

@@ -1,266 +1,415 @@
ParseCsvForPhp 1.0.0-rc.1 ParseCSV 1.3.1
----------------------------------- -----------------------------------
Date: unreleased Date: 20-Jun-2021
- Renamed class from parseCSV to ParseCsvForPhp Bugfix:
- `parseFile()` will now set `$csv->data`.
- Added support for MS Excel's "sep=" to detect the Until now, the parsed data was only returned.
delimiter (Issue #60). This adds consistency with `$csv->parse()`
for the following operations on the object.
- MIME: output() sends correct MIME type to browser
if the separator is a tab tab (Issue #79) -----------------------------------
- Added support for mb_convert_encoding() instead of
iconv() (Issue #109) ParseCSV 1.3.0
-----------------------------------
- A number of minor bug fixes - see GitHub issues 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.
parseCSV 0.4.3 beta
----------------------------------- WARNING: It will call trigger_error() for now.
Date: 1-July-2008 This may still break your application, depending on your
php.ini config. You can add E_USER_DEPRECATED to the
- Issue #4. Added an option for setting sorting error_reporting mask to avoid breaking your application:
type behavior when sorting data.
Simply set $csv->sort_type to "regular", "numeric", Example:
or "string".
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_USER_DEPRECATED
- Issue #6. Raw loaded file data is now cleared from
file_data property when it has been successfully This change is to avoid security issues: see issue #198.
parsed to keep parseCSV's memory footprint to a
minimum. Specifically handy when using multiple Non-breaking deprecations:
instances of parseCSV to process large files. - The function load_data() is deprecated.
Call loadFile() or loadDataString() instead.
----------------------------------- - Supplying CSV data (file content) to auto() is deprecated.
Please use autoDetectionForDataString().
parseCSV 0.4.2 beta The motivation is to make subtle gotchas less likely: when the
----------------------------------- file cannot be found or read by PHP (permissions), its file path
Date: 31-May-2008 would be treated like CSV data.
- Currently, there is code to parse .php: <?...?> tags are removed
- IMPORTANT! If you're using the output(), before the remaining file content is treated like a .csv file.
method please note that the first parameter This rarely-used functionality will be removed in v2.0.0.
has been completely removed as it was You are only affected if you use ParseCsv to parse .php files.
technically just useless. Instead, the second
parameter (filename) doubles as its replacement. New features: none
Simply put, if filename is not set or null, the Bug fixes: none
output() method will not output a downloadable -----------------------------------
file. Please update your existing code
when using 0.4.2 and later :)
ParseCSV 1.2.1
- Small fix to the headers sent by the output() -----------------------------------
method. Date: 25-Apr-2020
- Added a download example using the output() Breaking changes: none
method to the examples folder. New features: none
Bug fixes: none
-----------------------------------
Code quality:
- Switched from PHPUnit 4 to 6 to get rid of
parseCSV 0.4.1 beta wrappers. This will reduce friction for PhpStorm users,
----------------------------------- because it gets rid of the "Multiple definitions" error
Date: 29-May-2008 See issue #188. Thanks to @SharkMachine for the hint.
-----------------------------------
- Fixed a small bug in how the output() method
handles input data.
ParseCSV 1.2.0
----------------------------------- -----------------------------------
Date: 07-Jan-2020
parseCSV 0.4 beta Breaking changes: none
-----------------------------------
Date: 11-Apr-2008 New features:
- Compatible with PHP 7.4. Thanks to @andreybolonin
- Error reporting for files/data which is corrupt @morrislaptop @martijnengler and @fjf2002.
or has formatting errors like using double - unparse() now also understands $use_mb_convert_encoding.
quotes in a field without enclosing quotes. Or - Verbal condition operators are now allowed to contain
not escaping double quotes with a second one. upper case letters, for example:
$csv->conditions = 'rating IS GREATER THAN 4';
- parse() method does not require input anymore
if the "$object->file" property has been set. Bug fixes:
- All filter condition operators containing "is" or "equals"
I'm calling this a beta release due to the heavy were broken.
modifications to the core parsing logic required
for error reporting to work. I have tested the Code quality:
new code quite extensively, I'm fairly confident - Improved test coverage.
that it still parses exactly as it always has. -----------------------------------
The second reason I'm calling it a beta release
is cause I'm sure the error reporting code will ParseCSV 1.1.1
need more refinements and tweaks to detect more -----------------------------------
types of errors, as it's only picking two types Date: 2-Feb-2019
or syntax errors right now. However, it seems
these two are the most common errors that you Breaking changes: none
would be likely to come across.
New features: none
-----------------------------------
Bug fixes:
- Function load_data: check length of input, prevents E_NOTICE
parseCSV 0.3.2 if too long.
----------------------------------- - Fixed bugs in unparse().
Date: 1-Apr-2008
Code quality:
This is primarily a bug-fix release for a critical - Improved test coverage.
bug which was brought to my attention. -----------------------------------
- Fixed a critical bug in conditions parsing which
would generate corrupt matching patterns causing ParseCSV 1.1.0
the condition(s) to not work at all in some -----------------------------------
situations. Date: 9-Aug-2018
- Fixed a small code error which would cause PHP to Breaking changes:
generate a invalid offset notice when zero length - Ignore entirely empty lines at the end of files
values were fed into the unparse() method to See https://github.com/parsecsv/parsecsv-for-php/pull/142
generate CSV data from an array. - Dropped support for PHP 5.4. Now, you need at leas PHP 5.5.
- Fixed parse()'s return value: return true only if $data is useful.
Notice: If you have been using the "parsecsv-stable"
branch as an external in any of your projects, New features:
please use the "stable/parsecsv" branch from this - Added support for Laravel-style collections via the
point on as I will eventually remove the former due new getCollection() function - see
to it's stupid naming. https://github.com/parsecsv/parsecsv-for-php/pull/134
- New function getTotalDataRowCount() - useful if
----------------------------------- $limit is set - see pull request #122.
- Added requires to keep Composer-free environments working.
parseCSV 0.3.1 Bug fixes:
----------------------------------- - Better support for streams.
Date: 1-Sep-2007 See https://github.com/parsecsv/parsecsv-for-php/pull/147
- Fixed output() with custom header.
- Small change to default output settings to See https://github.com/parsecsv/parsecsv-for-php/issues/132
conform with RFC 4180 (http://rfc.net/rfc4180.html). - Fixed bug on _validate_fields_for_unparse() if titles property
Only the LF (line feed) character was used is used instead of fields parameter for changing the titles for
by default to separate rows, rather than unparsing.
CRLF (carriage return & line feed). - Fixed bug in unparse() that caused incorrect column order
(Issue #41).
-----------------------------------
Code quality:
parseCSV 0.3.0 - Improved test coverage.
----------------------------------- -----------------------------------
Date: 9-Aug-2007
- Changed to the MIT license. ParseCSV 1.0.0
-----------------------------------
- Added offset and limit options. Date: 3-March-2018
- Added SQL-like conditions for quickly - Renamed class from parseCSV to Csv and added name-
filtering out entries. Documentation on the space "ParseCsv" for PSR compliance.
condition syntax is forthcoming.
- Added support for MS Excel's "sep=" to detect the
- Small parsing modification to comply delimiter (Issue #60).
with some recent changes to the specifications
outlined on Wikipedia's Comma-separated values - Added data type detection - function getDatatypes()
article. guesses the type of each column.
- Minor changes and optimizations, and a few - MIME: output() sends correct MIME type to browser
spelling corrections. Oops :) if the separator is a tab char (Issue #79).
- Included more complex code examples in the - Added support for mb_convert_encoding() instead of
parseCSV download. iconv() - see issue #109.
----------------------------------- - A number of minor bug fixes - see GitHub issues.
- Added many more unit tests.
parseCSV 0.2.1
----------------------------------- -----------------------------------
Date: 8-Aug-2007
- Fixed stupid code which caused auto function parseCSV 0.4.3 beta
to not work in some situations. -----------------------------------
Date: 1-July-2008
-----------------------------------
- Issue #4. Added an option for setting sorting
type behavior when sorting data.
parseCSV 0.2.0 beta Simply set $csv->sort_type to "regular", "numeric",
----------------------------------- or "string".
Date: 2-Jan-2007
- Issue #6. Raw loaded file data is now cleared from
- Added auto() function to automatically detect file_data property when it has been successfully
delimiter character. parsed to keep parseCSV's memory footprint to a
Useful for user upload in case delimiter is minimum. Specifically handy when using multiple
comma (,), tab, or semi-colon (;). Some instances of parseCSV to process large files.
versions of MS Excel for Windows use
semi-colons instead of commas when saving to -----------------------------------
CSV files.
It uses a process of elimination to eliminate
characters that can not be the delimiter, parseCSV 0.4.2 beta
so it should work on all CSV-structured files -----------------------------------
almost no matter what the delimiter is. Date: 31-May-2008
- Generally updated some of the core workings - IMPORTANT! If you're using the output(),
to increase performance, and offer better method please note that the first parameter
support for large (1MB and up) files. has been completely removed as it was
technically just useless. Instead, the second
- Added code examples to header comment. parameter (filename) doubles as its replacement.
Simply put, if filename is not set or null, the
----------------------------------- output() method will not output a downloadable
file. Please update your existing code
when using 0.4.2 and later :)
parseCSV 0.1.6 beta
----------------------------------- - Small fix to the headers sent by the output()
Date: 22-Dec-2006 method.
- Updated output() function. - Added a download example using the output()
method to the examples folder.
-----------------------------------
-----------------------------------
parseCSV 0.1.5 beta
----------------------------------- parseCSV 0.4.1 beta
Date: 22-Dec-2006 -----------------------------------
Date: 29-May-2008
- Added output() function for easy output to
browser, for downloading features for example. - Fixed a small bug in how the output() method
handles input data.
-----------------------------------
-----------------------------------
parseCSV 0.1.4 beta
----------------------------------- parseCSV 0.4 beta
Date: 17-Dec-2006 -----------------------------------
Date: 11-Apr-2008
- Minor changes and fixes
- Error reporting for files/data which is corrupt
----------------------------------- or has formatting errors like using double
quotes in a field without enclosing quotes. Or
not escaping double quotes with a second one.
parseCSV 0.1.3 beta
----------------------------------- - parse() method does not require input anymore
Date: 17-Dec-2006 if the "$object->file" property has been set.
- Added GPL v2.0 license. I'm calling this a beta release due to the heavy
modifications to the core parsing logic required
----------------------------------- for error reporting to work. I have tested the
new code quite extensively, I'm fairly confident
that it still parses exactly as it always has.
parseCSV 0.1.2 beta
----------------------------------- The second reason I'm calling it a beta release
Date: 17-Dec-2006 is cause I'm sure the error reporting code will
need more refinements and tweaks to detect more
- Added encoding() function for easier character types of errors, as it's only picking two types
encoding configuration. or syntax errors right now. However, it seems
these two are the most common errors that you
----------------------------------- would be likely to come across.
-----------------------------------
parseCSV 0.1.1 beta
-----------------------------------
Date: 24-Nov-2006 parseCSV 0.3.2
-----------------------------------
- Added support for a PHP die command on first Date: 1-Apr-2008
line of csv files if they have a .php extension
to protect secure data from being displayed This is primarily a bug-fix release for a critical
directly to the browser. bug which was brought to my attention.
----------------------------------- - Fixed a critical bug in conditions parsing which
would generate corrupt matching patterns causing
the condition(s) to not work at all in some
parseCSV 0.1 beta situations.
-----------------------------------
Date: 23-Nov-2006 - Fixed a small code error which would cause PHP to
generate a invalid offset notice when zero length
- Initial release values were fed into the unparse() method to
generate CSV data from an array.
-----------------------------------
Notice: If you have been using the "parsecsv-stable"
branch as an external in any of your projects,
please use the "stable/parsecsv" branch from this
point on as I will eventually remove the former due
to it's stupid naming.
-----------------------------------
parseCSV 0.3.1
-----------------------------------
Date: 1-Sep-2007
- Small change to default output settings to
conform with RFC 4180 (http://rfc.net/rfc4180.html).
Only the LF (line feed) character was used
by default to separate rows, rather than
CRLF (carriage return & line feed).
-----------------------------------
parseCSV 0.3.0
-----------------------------------
Date: 9-Aug-2007
- Changed to the MIT license.
- Added offset and limit options.
- Added SQL-like conditions for quickly
filtering out entries. Documentation on the
condition syntax is forthcoming.
- Small parsing modification to comply
with some recent changes to the specifications
outlined on Wikipedia's Comma-separated values
article.
- Minor changes and optimizations, and a few
spelling corrections. Oops :)
- Included more complex code examples in the
parseCSV download.
-----------------------------------
parseCSV 0.2.1
-----------------------------------
Date: 8-Aug-2007
- Fixed stupid code which caused auto function
to not work in some situations.
-----------------------------------
parseCSV 0.2.0 beta
-----------------------------------
Date: 2-Jan-2007
- Added auto() function to automatically detect
delimiter character.
Useful for user upload in case delimiter is
comma (,), tab, or semi-colon (;). Some
versions of MS Excel for Windows use
semi-colons instead of commas when saving to
CSV files.
It uses a process of elimination to eliminate
characters that can not be the delimiter,
so it should work on all CSV-structured files
almost no matter what the delimiter is.
- Generally updated some of the core workings
to increase performance, and offer better
support for large (1MB and up) files.
- Added code examples to header comment.
-----------------------------------
parseCSV 0.1.6 beta
-----------------------------------
Date: 22-Dec-2006
- Updated output() function.
-----------------------------------
parseCSV 0.1.5 beta
-----------------------------------
Date: 22-Dec-2006
- Added output() function for easy output to
browser, for downloading features for example.
-----------------------------------
parseCSV 0.1.4 beta
-----------------------------------
Date: 17-Dec-2006
- Minor changes and fixes
-----------------------------------
parseCSV 0.1.3 beta
-----------------------------------
Date: 17-Dec-2006
- Added GPL v2.0 license.
-----------------------------------
parseCSV 0.1.2 beta
-----------------------------------
Date: 17-Dec-2006
- Added encoding() function for easier character
encoding configuration.
-----------------------------------
parseCSV 0.1.1 beta
-----------------------------------
Date: 24-Nov-2006
- Added support for a PHP die command on first
line of csv files if they have a .php extension
to protect secure data from being displayed
directly to the browser.
-----------------------------------
parseCSV 0.1 beta
-----------------------------------
Date: 23-Nov-2006
- Initial release
-----------------------------------

View File

@@ -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

181
README.md
View File

@@ -1,30 +1,21 @@
# ParseCsvForPhp # ParseCsv
[![Financial Contributors on Open Collective](https://opencollective.com/parsecsv/all/badge.svg?label=financial+contributors)](https://opencollective.com/parsecsv)
This 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 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.
You may not need a library at all: before using ParseCsvForPhp, please make sure if PHP's own `str_getcsv()`, ``fgetcvs()`` or `fputcsv()` meets your needs. 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 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. and third-party support for handling CSV data in PHP.
[csv]: http://en.wikipedia.org/wiki/Comma-separated_values [csv]: http://en.wikipedia.org/wiki/Comma-separated_values
## Installation
Installation is easy using Composer. Include the following in your composer.json
```
"parsecsv/php-parsecsv": "1.0.0"
```
You may also manually include the ParseCsvForPhp.php file
```php
require_once 'ParseCsvForPhp.php';
```
## Features ## Features
* ParseCsv is a complete and fully featured CSV solution for PHP
* Supports enclosed values, enclosed commas, double quotes and new lines. * Supports enclosed values, enclosed commas, double quotes and new lines.
* Automatic delimiter character detection. * Automatic delimiter character detection.
* Sort data by specific fields/columns. * Sort data by specific fields/columns.
@@ -34,56 +25,112 @@ require_once 'ParseCsvForPhp.php';
* Error detection for incorrectly formatted input. It attempts to be * Error detection for incorrectly formatted input. It attempts to be
intelligent, but can not be trusted 100% due to the structure of CSV, and intelligent, but can not be trusted 100% due to the structure of CSV, and
how different programs like Excel for example outputs CSV data. how different programs like Excel for example outputs CSV data.
* Support for character encoding conversion using PHP's * Support for character encoding conversion using PHP's
`iconv()` and `mb_convert_encoding()` functions (requires PHP 5). `iconv()` and `mb_convert_encoding()` functions.
* Supports PHP 5.4 and higher. * Supports PHP 5.5 and higher.
It certainly works with PHP 7.2 and all versions in between. It certainly works with PHP 7.2 and all versions in between.
## Installation
Installation is easy using Composer. Just run the following on the
command line:
```
composer require parsecsv/php-parsecsv
```
If you don't use a framework such as Drupal, Laravel, Symfony, Yii etc.,
you may have to manually include Composer's autoloader file in your PHP
script:
```php
require_once __DIR__ . '/vendor/autoload.php';
```
#### Without composer
Not recommended, but technically possible: you can also clone the
repository or extract the
[ZIP](https://github.com/parsecsv/parsecsv-for-php/archive/master.zip).
To use ParseCSV, you then have to add a `require 'parsecsv.lib.php';` line.
## Example Usage ## Example Usage
**General** **Parse a tab-delimited CSV file with encoding conversion**
```php ```php
$csv = new ParseCsvForPhp('data.csv'); $csv = new \ParseCsv\Csv();
print_r($csv->data);
```
**Tab delimited, and encoding conversion**
```php
$csv = new ParseCsvForPhp();
$csv->encoding('UTF-16', 'UTF-8'); $csv->encoding('UTF-16', 'UTF-8');
$csv->delimiter = "\t"; $csv->delimiter = "\t";
$csv->parse('data.tsv'); $csv->parseFile('data.tsv');
print_r($csv->data); print_r($csv->data);
``` ```
**Auto-detect delimiter character** **Auto-detect field delimiter character**
```php ```php
$csv = new ParseCsvForPhp(); $csv = new \ParseCsv\Csv();
$csv->auto('data.csv'); $csv->auto('data.csv');
print_r($csv->data); print_r($csv->data);
``` ```
**Modify data in a CSV file** **Parse data with offset**
* ignoring the first X (e.g. two) rows
```php
$csv = new \ParseCsv\Csv();
$csv->offset = 2;
$csv->parseFile('data.csv');
print_r($csv->data);
```
**Limit the number of returned data rows**
```php
$csv = new \ParseCsv\Csv();
$csv->limit = 5;
$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->loadFile('data.csv');
$count = $csv->getTotalDataRowCount();
print_r($count);
```
**Get most common data type for each column**
```php ```php
$csv = new ParseCsvForPhp(); $csv = new \ParseCsv\Csv('data.csv');
$csv->getDatatypes();
print_r($csv->data_types);
```
**Modify data in a CSV file**
Change data values:
```php
$csv = new \ParseCsv\Csv();
$csv->sort_by = 'id'; $csv->sort_by = 'id';
$csv->parse('data.csv'); $csv->parseFile('data.csv');
# "4" is the value of the "id" column of the CSV row # "4" is the value of the "id" column of the CSV row
$csv->data[4] = array('firstname' => 'John', 'lastname' => 'Doe', 'email' => 'john@doe.com'); $csv->data[4] = array('firstname' => 'John', 'lastname' => 'Doe', 'email' => 'john@doe.com');
$csv->save(); $csv->save();
``` ```
Enclose each data value by quotes:
```php
$csv = new \ParseCsv\Csv();
$csv->parseFile('data.csv');
$csv->enclose_all = true;
$csv->save();
```
**Replace field names or set ones if missing** **Replace field names or set ones if missing**
```php ```php
$csv = new ParseCsvForPhp(); $csv = new \ParseCsv\Csv();
$csv->fields = ['id', 'name', 'category'] $csv->fields = ['id', 'name', 'category'];
$csv->parse('data.csv'); $csv->parseFile('data.csv');
``` ```
**Add row/entry to end of CSV file** **Add row/entry to end of CSV file**
@@ -91,23 +138,47 @@ $csv->parse('data.csv');
_Only recommended when you know the exact structure of the file._ _Only recommended when you know the exact structure of the file._
```php ```php
$csv = new ParseCsvForPhp(); $csv = new \ParseCsv\Csv();
$csv->save('data.csv', array(array('1986', 'Home', 'Nowhere', '')), true); $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 **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 ParseCsvForPhp(); $csv = new \ParseCsv\Csv();
$csv->output('movies.csv', $array, array('field 1', 'field 2'), ','); $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. 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 ## Credits
* ParseCsvForPhp is based on the concept of [Ming Hong Ng][ming]'s [CsvFileParser][] * ParseCsv is based on the concept of [Ming Hong Ng][ming]'s [CsvFileParser][]
class. class.
[ming]: http://minghong.blogspot.com/ [ming]: http://minghong.blogspot.com/
@@ -116,11 +187,37 @@ For more complex examples, see the ``tests`` and `examples` directories.
## Contributors ## Contributors
### Code Contributors
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
@@ -145,3 +242,5 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
[![Build Status](https://travis-ci.org/parsecsv/parsecsv-for-php.svg?branch=master)](https://travis-ci.org/parsecsv/parsecsv-for-php)

View File

@@ -10,14 +10,48 @@
{ {
"name": "William Knauss", "name": "William Knauss",
"email": "will.knauss@gmail.com" "email": "will.knauss@gmail.com"
},
{
"name": "Susann Sgorzaly",
"homepage": "https://github.com/susgo"
},
{
"name": "Christian Bläul",
"homepage": "https://github.com/Fonata"
} }
], ],
"autoload": { "autoload": {
"classmap": [ "psr-4": {
"." "ParseCsv\\": "src"
] }
},
"autoload-dev": {
"psr-4": {
"ParseCsv\\tests\\": "tests"
}
},
"require": {
"php": ">=5.5"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "4.1.*" "phpunit/phpunit": "^6",
"squizlabs/php_codesniffer": "^3.5"
},
"suggest": {
"illuminate/support": "Fluent array interface for map functions"
},
"extra": {
"branch-alias": {
"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"
} }
} }

View File

@@ -1,13 +1,14 @@
<pre> <pre>
<?php <?php
# include parseCSV class.
require __DIR__ . '/../vendor/autoload.php';
# include ParseCsvForPhp class. use ParseCsv\Csv;
require_once('../ParseCsvForPhp.php');
# create new object. # create new parseCSV object.
$csv = new ParseCsvForPhp(); $csv = new Csv();
# Parse '_books.csv' using automatic delimiter detection... # Parse '_books.csv' using automatic delimiter detection...
@@ -17,8 +18,8 @@ $csv->auto('_books.csv');
# if its not the default comma... # if its not the default comma...
// $csv->delimiter = "\t"; # tab delimited // $csv->delimiter = "\t"; # tab delimited
# ...and then use the parse() function. # ...and then use the parseFile() function.
// $csv->parse('_books.csv'); // $csv->parseFile('_books.csv');
# Output result. # Output result.
@@ -40,7 +41,7 @@ $csv->auto('_books.csv');
background-color: #FFF; background-color: #FFF;
} }
</style> </style>
<table border="0" cellspacing="1" cellpadding="3"> <table>
<tr> <tr>
<?php foreach ($csv->titles as $value): ?> <?php foreach ($csv->titles as $value): ?>
<th><?php echo $value; ?></th> <th><?php echo $value; ?></th>

View File

@@ -2,12 +2,14 @@
<?php <?php
# include ParseCsvForPhp class. # include parseCSV class.
require_once('../ParseCsvForPhp.php'); require __DIR__ . '/../vendor/autoload.php';
use ParseCsv\Csv;
# create new object. # create new parseCSV object.
$csv = new ParseCsvForPhp(); $csv = new Csv();
# Example conditions: # Example conditions:
@@ -15,6 +17,7 @@ $csv = new ParseCsvForPhp();
$csv->conditions = 'author does not contain dan brown'; $csv->conditions = 'author does not contain dan brown';
// $csv->conditions = 'rating < 4 OR author is John Twelve Hawks'; // $csv->conditions = 'rating < 4 OR author is John Twelve Hawks';
// $csv->conditions = 'rating > 4 AND author is Dan Brown'; // $csv->conditions = 'rating > 4 AND author is Dan Brown';
// $csv->conditions = 'rating is greater than 4';
# Parse '_books.csv' using automatic delimiter detection. # Parse '_books.csv' using automatic delimiter detection.
@@ -40,7 +43,7 @@ $csv->auto('_books.csv');
background-color: #FFF; background-color: #FFF;
} }
</style> </style>
<table border="0" cellspacing="1" cellpadding="3"> <table>
<tr> <tr>
<?php foreach ($csv->titles as $value): ?> <?php foreach ($csv->titles as $value): ?>
<th><?php echo $value; ?></th> <th><?php echo $value; ?></th>

View File

@@ -1,12 +1,14 @@
<?php <?php
# include ParseCsvForPhp class. # include parseCSV class.
require_once('../ParseCsvForPhp.php'); require __DIR__ . '/../vendor/autoload.php';
use ParseCsv\Csv;
# create new object. # create new parseCSV object.
$csv = new ParseCsvForPhp(); $csv = new Csv();
# Parse '_books.csv' using automatic delimiter detection... # Parse '_books.csv' using automatic delimiter detection...
@@ -30,5 +32,3 @@ $csv->output('books.csv');
# data to download the output as a CSV file. if it's not set # data to download the output as a CSV file. if it's not set
# or is set to null, output will only return the generated CSV # or is set to null, output will only return the generated CSV
# output data, and will not output to the browser itself. # output data, and will not output to the browser itself.
?>

View File

@@ -2,12 +2,14 @@
<?php <?php
# include class. # include parseCSV class.
require_once('../ParseCsvForPhp.php'); require __DIR__ . '/../vendor/autoload.php';
use ParseCsv\Csv;
# create new object. # create new parseCSV object.
$csv = new ParseCsvForPhp(); $csv = new Csv();
# if sorting is enabled, the whole CSV file # if sorting is enabled, the whole CSV file
@@ -53,7 +55,7 @@ $csv->auto('_books.csv');
background-color: #FFF; background-color: #FFF;
} }
</style> </style>
<table border="0" cellspacing="1" cellpadding="3"> <table>
<tr> <tr>
<?php foreach ($csv->titles as $value): ?> <?php foreach ($csv->titles as $value): ?>
<th><?php echo $value; ?></th> <th><?php echo $value; ?></th>

View File

@@ -0,0 +1,27 @@
<?php
# include parseCSV class.
require __DIR__ . '/../vendor/autoload.php';
use ParseCsv\Csv;
# Create new parseCSV object.
$csv = new Csv();
# When saving, don't write the header row:
$csv->heading = false;
# Specify which columns to write, and in which order.
# We won't output the 'Awesome' column this time.
$csv->titles = ['Age', 'Name'];
# Data to write:
$csv->data = [
0 => ['Name' => 'Anne', 'Age' => 45, 'Awesome' => true],
1 => ['Name' => 'John', 'Age' => 44, 'Awesome' => false],
];
# Then we save the file to the file system:
$csv->save('people.csv');

24
parsecsv.lib.php Normal file
View File

@@ -0,0 +1,24 @@
<?php
// This file should not be used at all! Instead, please use Composer's autoload.
// It purely exists to reduce the maintenance burden for existing code using
// this repository.
// Check if people used Composer to include this project in theirs
if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
require __DIR__ . '/src/enums/AbstractEnum.php';
require __DIR__ . '/src/enums/DatatypeEnum.php';
require __DIR__ . '/src/enums/FileProcessingModeEnum.php';
require __DIR__ . '/src/enums/SortEnum.php';
require __DIR__ . '/src/extensions/DatatypeTrait.php';
require __DIR__ . '/src/Csv.php';
} else {
require __DIR__ . '/vendor/autoload.php';
}
// This wrapper class should not be used by new projects. Please look at the
// examples to find the up-to-date way of using this repo.
class parseCSV extends ParseCsv\Csv {
}

3
ruleset.xml Normal file
View File

@@ -0,0 +1,3 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ParseCSV" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
</ruleset>

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,38 @@
<?php
namespace ParseCsv\enums;
abstract class AbstractEnum {
/**
* Creates a new value of some type
*
* @param mixed $value
*
* @throws \UnexpectedValueException if incompatible type is given.
*/
public function __construct($value) {
if (!$this->isValid($value)) {
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . get_called_class());
}
$this->value = $value;
}
public static function getConstants() {
$class = get_called_class();
$reflection = new \ReflectionClass($class);
return $reflection->getConstants();
}
/**
* Check if enum value is valid
*
* @param $value
*
* @return bool
*/
public static function isValid($value) {
return in_array($value, static::getConstants(), true);
}
}

120
src/enums/DatatypeEnum.php Normal file
View File

@@ -0,0 +1,120 @@
<?php
namespace ParseCsv\enums;
/**
* Class DatatypeEnum
*
* @package ParseCsv\enums
*
* todo: needs a basic parent enum class for error handling.
*/
class DatatypeEnum extends AbstractEnum {
const __DEFAULT = self::TYPE_STRING;
const TYPE_STRING = 'string';
const TYPE_FLOAT = 'float';
const TYPE_INT = 'integer';
const TYPE_BOOL = 'boolean';
const TYPE_DATE = 'date';
const REGEX_FLOAT = '/(^[+-]?$)|(^[+-]?[0-9]+([,.][0-9])?[0-9]*(e[+-]?[0-9]+)?$)/';
const REGEX_INT = '/^[-+]?[0-9]\d*$/';
const REGEX_BOOL = '/^(?i:true|false)$/';
/**
* Define validator functions here.
*
* @var array
*
* @uses isValidFloat
* @uses isValidInteger
* @uses isValidBoolean
* @uses isValidDate
*/
private static $validators = array(
self::TYPE_STRING => null,
self::TYPE_INT => 'isValidInteger',
self::TYPE_BOOL => 'isValidBoolean',
self::TYPE_FLOAT => 'isValidFloat',
self::TYPE_DATE => 'isValidDate',
);
/**
* Checks data type for given string.
*
* @param string $value
*
* @return bool|string
*/
public static function getValidTypeFromSample($value) {
$value = trim((string) $value);
if (empty($value)) {
return false;
}
foreach (self::$validators as $type => $validator) {
if ($validator === null) {
continue;
}
if (method_exists(__CLASS__, $validator) && self::$validator($value)) {
return $type;
}
}
return self::__DEFAULT;
}
/**
* Check if string is float value.
*
* @param string $value
*
* @return bool
*/
private static function isValidFloat($value) {
return (bool) preg_match(self::REGEX_FLOAT, $value);
}
/**
* Check if string is integer value.
*
* @param string $value
*
* @return bool
*/
private static function isValidInteger($value) {
return (bool) preg_match(self::REGEX_INT, $value);
}
/**
* Check if string is boolean.
*
* @param string $value
*
* @return bool
*/
private static function isValidBoolean($value) {
return (bool) preg_match(self::REGEX_BOOL, $value);
}
/**
* Check if string is date.
*
* @param string $value
*
* @return bool
*/
private static function isValidDate($value) {
return (bool) strtotime($value);
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace ParseCsv\enums;
/**
* Class FileProcessingEnum
*
* @package ParseCsv\enums
*
* todo extends a basic enum class after merging #121
*/
class FileProcessingModeEnum {
const __default = self::MODE_FILE_OVERWRITE;
const MODE_FILE_APPEND = true;
const MODE_FILE_OVERWRITE = false;
public static function getAppendMode($mode) {
if ($mode == self::MODE_FILE_APPEND) {
return 'ab';
}
return 'wb';
}
}

29
src/enums/SortEnum.php Normal file
View File

@@ -0,0 +1,29 @@
<?php
namespace ParseCsv\enums;
class SortEnum extends AbstractEnum {
const __DEFAULT = self::SORT_TYPE_REGULAR;
const SORT_TYPE_REGULAR = 'regular';
const SORT_TYPE_NUMERIC = 'numeric';
const SORT_TYPE_STRING = 'string';
private static $sorting = array(
self::SORT_TYPE_REGULAR => SORT_REGULAR,
self::SORT_TYPE_STRING => SORT_STRING,
self::SORT_TYPE_NUMERIC => SORT_NUMERIC,
);
public static function getSorting($type) {
if (array_key_exists($type, self::$sorting)) {
return self::$sorting[$type];
}
return self::$sorting[self::__DEFAULT];
}
}

View File

@@ -0,0 +1,102 @@
<?php
namespace ParseCsv\extensions;
use ParseCsv\enums\DatatypeEnum;
trait DatatypeTrait {
/**
* Data Types
* Data types of CSV data-columns, keyed by the column name. Possible values
* are string, float, integer, boolean, date. See DatatypeEnum.
*
* @var array
*/
public $data_types = [];
/**
* Check data type for one column.
* Check for most commonly data type for one column.
*
* @param array $datatypes
*
* @return string|false
*/
private function getMostFrequentDatatypeForColumn($datatypes) {
// remove 'false' from array (can happen if CSV cell is empty)
$typesFiltered = array_filter($datatypes);
if (empty($typesFiltered)) {
return false;
}
$typesFreq = array_count_values($typesFiltered);
arsort($typesFreq);
reset($typesFreq);
return key($typesFreq);
}
/**
* Check data type foreach Column
* Check data type for each column and returns the most commonly.
*
* Requires PHP >= 5.5
*
* @uses DatatypeEnum::getValidTypeFromSample
*
* @return array|bool
*/
public function getDatatypes() {
if (empty($this->data)) {
$this->data = $this->_parse_string();
}
if (!is_array($this->data)) {
throw new \UnexpectedValueException('No data set yet.');
}
$result = [];
foreach ($this->titles as $cName) {
$column = array_column($this->data, $cName);
$cDatatypes = array_map(DatatypeEnum::class . '::getValidTypeFromSample', $column);
$result[$cName] = $this->getMostFrequentDatatypeForColumn($cDatatypes);
}
$this->data_types = $result;
return !empty($this->data_types) ? $this->data_types : [];
}
/**
* Check data type of titles / first row for auto detecting if this could be
* a heading line.
*
* Requires PHP >= 5.5
*
* @uses DatatypeEnum::getValidTypeFromSample
*
* @return bool
*/
public function autoDetectFileHasHeading() {
if (empty($this->data)) {
throw new \UnexpectedValueException('No data set yet.');
}
if ($this->heading) {
$firstRow = $this->titles;
} else {
$firstRow = $this->data[0];
}
$firstRow = array_filter($firstRow);
if (empty($firstRow)) {
return false;
}
$firstRowDatatype = array_map(DatatypeEnum::class . '::getValidTypeFromSample', $firstRow);
return $this->getMostFrequentDatatypeForColumn($firstRowDatatype) === DatatypeEnum::TYPE_STRING;
}
}

View File

@@ -1,13 +1,8 @@
<?php <?php
$dir = realpath(__DIR__); chdir(__DIR__ . '/..');
defined('BASE') OR define('BASE', dirname($dir) . '/'); if (!file_exists('vendor/autoload.php')) {
`composer dump-autoload`;
require_once BASE . 'ParseCsvForPhp.php';
if (!class_exists('PHPUnit\Framework\TestCase')) {
// we run on an older PHPUnit version without namespaces.
require_once __DIR__ . '/PHPUnit_Framework_TestCase.inc.php';
} }
require_once BASE . 'tests/properties/BaseClass.php'; require __DIR__ . '/../vendor/autoload.php';

View File

@@ -1,11 +0,0 @@
<?php
namespace PHPUnit\Framework;
// Only needed to keep unit test code compatible with older PHPUnit versions
/** @noinspection PhpUndefinedClassInspection */
/** @noinspection AutoloadingIssuesInspection */
class TestCase extends \PHPUnit_Framework_TestCase {
}

View File

@@ -0,0 +1,22 @@
ID,dID,createdAt,else1,else2,else3,else4,else5,user,else6,else7,else8,else9,else10,else11,else12,else13,else14,else15,else16,else17,else18,else19,else20,else21,else22,else23,else24,else25,else26,else27,else28,else29,else30,else31,else32,else33,else34,else35,else36,else37,else38
418,0,2017-05-16,,,2018-01-22,22.01.2018 10:00:09,,admin,Ja,,10001,Abweichung,,10001,v1,1,ddd,100,1000,,HH,,v1,0,401,1,2,H1,,-1,10,1,111,Ja,2017-01-01,,12,0,11109,HH-100,default
419,0,2017-05-16,,,2017-05-16,14.07.2017 09:58:09,,admin,Ja,,,Abweichung,,10002,v2,1,ddd,200,500,DD,DD,,v2,1,402,2,4,H2,,-2,100,1,1111,Ja,2017-01-01,1,13,1,11109,DD-200,default
438,0,2017-05-16,,,2017-05-16,14.07.2017 09:58:29,,admin,Ja,,10021,Abweichung,,10021,v3,4,ddd,300,400,DD,DD,,v3,0,421,1,,H3,,-1,106,1,111,Ja,2017-05-08,,2,1,11109,DD-300,default
440,0,2017-05-16,,,,14.07.2017 09:58:53,,admin,Ja,,,Alt,,10023,v4,3,,400,500,BE,DD,,v4,1,423,3,,H4,,-3,143,1,1111,Ja,2017-01-01,1,33,1,11108,BE-400,default
441,0,2017-05-16,,,,14.07.2017 09:59:19,,admin,Ja,,,Fehlt,,10024,v5,3,,500,,BE,,,v5,2,424,4,,H5,,0,1435,0,111,Ja,2017-01-01,,12,1,0,BE-500,default
442,0,2017-05-16,,,,14.07.2017 10:00:46,,admin,Ja,,,Neu,,10025,v6,3,,100,,DD,,,v6,435,425,1,,H6,,0,10,0,1111,Ja,2017-01-01,,102,1,0,DD-100,default
443,0,2017-05-16,,,2017-07-04,14.07.2017 10:01:12,,admin,Ja,,,OK,,10026,v7,3,,200,200,DD,DD,,v7,32,426,2,2,H7,,0,100,0,111,Ja,2017-01-01,,77,1,0,DD-200,default
444,0,2017-05-16,,,,14.07.2017 10:02:13,,admin,Ja,,,Fehlt,,10027,v8,3,,300,,BE,,,v8,45,427,3,,H8,,0,200,0,1111,Ja,2017-01-01,,44,3,0,BE-300,default
445,0,2017-05-16,,,,14.07.2017 10:02:38,,admin,Ja,,,Fehlt,,10028,v9,3,,100,,BE,,,v9,45,428,4,,H9,,0,400,0,111,Ja,2017-01-01,,44,1,0,BE-100,default
446,0,2017-05-16,,,,14.07.2017 10:03:01,,admin,Ja,,,Fehlt,,10029,v10,3,,,400,,DD,,v10,45,429,,,H10,,0,1124,0,1111,Ja,2017-01-01,,89,1,0,,default
1 ID dID createdAt else1 else2 else3 else4 else5 user else6 else7 else8 else9 else10 else11 else12 else13 else14 else15 else16 else17 else18 else19 else20 else21 else22 else23 else24 else25 else26 else27 else28 else29 else30 else31 else32 else33 else34 else35 else36 else37 else38
2 418 0 2017-05-16 2018-01-22 22.01.2018 10:00:09 admin Ja 10001 Abweichung 10001 v1 1 ddd 100 1000 HH v1 0 401 1 2 H1 -1 10 1 111 Ja 2017-01-01 12 0 11109 HH-100 default
3 419 0 2017-05-16 2017-05-16 14.07.2017 09:58:09 admin Ja Abweichung 10002 v2 1 ddd 200 500 DD DD v2 1 402 2 4 H2 -2 100 1 1111 Ja 2017-01-01 1 13 1 11109 DD-200 default
4 438 0 2017-05-16 2017-05-16 14.07.2017 09:58:29 admin Ja 10021 Abweichung 10021 v3 4 ddd 300 400 DD DD v3 0 421 1 H3 -1 106 1 111 Ja 2017-05-08 2 1 11109 DD-300 default
5 440 0 2017-05-16 14.07.2017 09:58:53 admin Ja Alt 10023 v4 3 400 500 BE DD v4 1 423 3 H4 -3 143 1 1111 Ja 2017-01-01 1 33 1 11108 BE-400 default
6 441 0 2017-05-16 14.07.2017 09:59:19 admin Ja Fehlt 10024 v5 3 500 BE v5 2 424 4 H5 0 1435 0 111 Ja 2017-01-01 12 1 0 BE-500 default
7 442 0 2017-05-16 14.07.2017 10:00:46 admin Ja Neu 10025 v6 3 100 DD v6 435 425 1 H6 0 10 0 1111 Ja 2017-01-01 102 1 0 DD-100 default
8 443 0 2017-05-16 2017-07-04 14.07.2017 10:01:12 admin Ja OK 10026 v7 3 200 200 DD DD v7 32 426 2 2 H7 0 100 0 111 Ja 2017-01-01 77 1 0 DD-200 default
9 444 0 2017-05-16 14.07.2017 10:02:13 admin Ja Fehlt 10027 v8 3 300 BE v8 45 427 3 H8 0 200 0 1111 Ja 2017-01-01 44 3 0 BE-300 default
10 445 0 2017-05-16 14.07.2017 10:02:38 admin Ja Fehlt 10028 v9 3 100 BE v9 45 428 4 H9 0 400 0 111 Ja 2017-01-01 44 1 0 BE-100 default
11 446 0 2017-05-16 14.07.2017 10:03:01 admin Ja Fehlt 10029 v10 3 400 DD v10 45 429 H10 0 1124 0 1111 Ja 2017-01-01 89 1 0 default

View File

@@ -0,0 +1 @@
C1,C2,C3
1 C1 C2 C3

View File

@@ -0,0 +1,6 @@
keyword
liability waiver
release of liability form
release of liability
sample waiver
sample waiver form
1 keyword
2 liability waiver
3 release of liability form
4 release of liability
5 sample waiver
6 sample waiver form

View File

@@ -1,45 +1,70 @@
<?php <?php
class ConstructTest extends PHPUnit\Framework\TestCase { namespace ParseCsv\tests\methods;
use ParseCsv\Csv;
use PHPUnit\Framework\TestCase;
class ConstructTest extends TestCase {
/** /**
* @access protected * @var Csv object
* @var ParseCsvForPhp object
*/ */
protected $csv = null; protected $csv = null;
public function test_offset_param() { public function test_offset_param() {
$offset = 10; $offset = 10;
$this->csv = new ParseCsvForPhp(null, $offset); $this->csv = new Csv(null, $offset);
$this->assertTrue(is_numeric($this->csv->offset)); $this->assertTrue(is_numeric($this->csv->offset));
$this->assertEquals($offset, $this->csv->offset); $this->assertEquals($offset, $this->csv->offset);
} }
public function test_limit_param() { public function test_limit_param() {
$limit = 10; $limit = 10;
$this->csv = new ParseCsvForPhp(null, null, $limit); $this->csv = new Csv(null, null, $limit);
$this->assertTrue(is_numeric($this->csv->limit)); $this->assertTrue(is_numeric($this->csv->limit));
$this->assertEquals($limit, $this->csv->limit); $this->assertEquals($limit, $this->csv->limit);
} }
public function test_conditions_param() { public function test_conditions_param() {
$conditions = 'some column NOT value'; $conditions = 'some column NOT value';
$this->csv = new ParseCsvForPhp(null, null, null, $conditions); $this->csv = new Csv(null, null, null, $conditions);
$this->assertTrue(is_string($this->csv->conditions)); $this->assertTrue(is_string($this->csv->conditions));
$this->assertEquals($conditions, $this->csv->conditions); $this->assertEquals($conditions, $this->csv->conditions);
} }
public function test_keep_file_data_param() { public function test_keep_file_data_param() {
$keep = true; $keep = true;
$this->csv = new ParseCsvForPhp(null, null, null, null, $keep); $this->csv = new Csv(null, null, null, null, $keep);
$this->assertTrue(is_bool($this->csv->keep_file_data)); $this->assertTrue(is_bool($this->csv->keep_file_data));
$this->assertEquals($keep, $this->csv->keep_file_data); $this->assertEquals($keep, $this->csv->keep_file_data);
} }
public function test_input_param() { public function test_input_param() {
$csv = "col1,col2,col3\r\nval1,val2,val3\r\nval1A,val2A,val3A\r\n"; $csv = "col1,col2,col3\r\nval1,val2,val3\r\nval1A,val2A,val3A\r\n";
$this->csv = new ParseCsvForPhp($csv, null, null, null, true); $this->csv = new Csv($csv, null, null, null, true);
$this->assertTrue(is_string($this->csv->file_data)); $this->assertTrue(is_string($this->csv->file_data));
$this->assertEquals($csv, $this->csv->file_data); $this->assertEquals($csv, $this->csv->file_data);
} }
/**
* @runInSeparateProcess because download.php uses header()
*
* @see https://github.com/sebastianbergmann/phpunit/issues/720#issuecomment-10421092
*/
public function testCodeExamples() {
chdir('examples');
foreach (glob('*.php') as $script_file) {
ob_start();
/** @noinspection PhpIncludeInspection */
require $script_file;
$ob_get_clean = ob_get_clean();
$verb = strtok($script_file, '_.');
if (!in_array($verb, ['download', 'save'], true)) {
$this->assertContains('<td>', $ob_get_clean);
}
}
chdir('..');
}
} }

View File

@@ -0,0 +1,80 @@
<?php
namespace ParseCsv\tests\methods;
use ParseCsv\Csv;
use PHPUnit\Framework\TestCase;
class DataRowCountTest extends TestCase {
/**
* CSV
* The CSV object
*
* @access protected
* @var Csv
*/
protected $csv;
protected function setUp(): void {
$this->csv = new Csv();
}
public function countRowsProvider() {
return [
'auto-double-enclosure' => [
'auto-double-enclosure.csv',
2,
],
'auto-single-enclosure' => [
'auto-single-enclosure.csv',
2,
],
'UTF-8_sep_row' => [
'datatype.csv',
3,
],
];
}
/**
* @dataProvider countRowsProvider
*
* @param string $file
* @param int $expectedRows
*/
public function testGetTotalRowCountFromFile($file, $expectedRows) {
$this->csv->heading = true;
$this->csv->load_data(__DIR__ . '/fixtures/' . $file);
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->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->loadDataString($sInput);
$this->assertEquals(3, $this->csv->getTotalDataRowCount());
}
public function testGetTotalRowCountSingleRow() {
$this->csv->heading = false;
$this->csv->enclosure = "'";
$sInput = "86545235689";
$this->csv->loadDataString($sInput);
$this->assertEquals(1, $this->csv->getTotalDataRowCount());
}
public function testGetTotalRowCountNoData() {
self::assertFalse($this->csv->getTotalDataRowCount());
}
}

View File

@@ -0,0 +1,69 @@
<?php
/**
* Created by PhpStorm.
* User: sgorzaly
* Date: 19.02.18
* Time: 20:52
*/
namespace ParseCsv\tests\methods;
use ParseCsv\enums\DatatypeEnum;
use PHPUnit\Framework\TestCase;
class DatatypeTest extends TestCase {
public function testSampleIsValidInteger() {
$this->assertEquals(DatatypeEnum::TYPE_INT, DatatypeEnum::getValidTypeFromSample('1'));
$this->assertEquals(DatatypeEnum::TYPE_INT, DatatypeEnum::getValidTypeFromSample('+1'));
$this->assertEquals(DatatypeEnum::TYPE_INT, DatatypeEnum::getValidTypeFromSample('-1'));
$this->assertNotEquals(DatatypeEnum::TYPE_INT, DatatypeEnum::getValidTypeFromSample('--1'));
$this->assertNotEquals(DatatypeEnum::TYPE_INT, DatatypeEnum::getValidTypeFromSample('test'));
$this->assertNotEquals(DatatypeEnum::TYPE_INT, DatatypeEnum::getValidTypeFromSample('1.0'));
$this->assertNotEquals(DatatypeEnum::TYPE_INT, DatatypeEnum::getValidTypeFromSample('1,0'));
$this->assertNotEquals(DatatypeEnum::TYPE_INT, DatatypeEnum::getValidTypeFromSample('1,1'));
$this->assertNotEquals(DatatypeEnum::TYPE_INT, DatatypeEnum::getValidTypeFromSample('0.1'));
$this->assertNotEquals(DatatypeEnum::TYPE_INT, DatatypeEnum::getValidTypeFromSample('true'));
$this->assertNotEquals(DatatypeEnum::TYPE_INT, DatatypeEnum::getValidTypeFromSample('2018-02-19'));
}
public function testSampleIsValidBool() {
$this->assertEquals(DatatypeEnum::TYPE_BOOL, DatatypeEnum::getValidTypeFromSample('true'));
$this->assertEquals(DatatypeEnum::TYPE_BOOL, DatatypeEnum::getValidTypeFromSample('TRUE'));
$this->assertEquals(DatatypeEnum::TYPE_BOOL, DatatypeEnum::getValidTypeFromSample('false'));
$this->assertEquals(DatatypeEnum::TYPE_BOOL, DatatypeEnum::getValidTypeFromSample('FALSE'));
$this->assertNotEquals(DatatypeEnum::TYPE_BOOL, DatatypeEnum::getValidTypeFromSample('FALS'));
$this->assertNotEquals(DatatypeEnum::TYPE_BOOL, DatatypeEnum::getValidTypeFromSample('test'));
$this->assertNotEquals(DatatypeEnum::TYPE_BOOL, DatatypeEnum::getValidTypeFromSample('0'));
$this->assertNotEquals(DatatypeEnum::TYPE_BOOL, DatatypeEnum::getValidTypeFromSample('1'));
$this->assertNotEquals(DatatypeEnum::TYPE_BOOL, DatatypeEnum::getValidTypeFromSample('0.1'));
}
public function testSampleIsValidFloat() {
$this->assertEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('1.0'));
$this->assertEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('-1.1'));
$this->assertEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('+1,1'));
$this->assertEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('1,1'));
$this->assertEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('1,1'));
$this->assertEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('1e-03'));
$this->assertEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('1e+03'));
$this->assertNotEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('1'));
$this->assertNotEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('test'));
$this->assertNotEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('1,,1'));
$this->assertNotEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('1..1'));
$this->assertNotEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('2018-02-19'));
}
public function testSampleIsValidDate() {
$this->assertEquals(DatatypeEnum::TYPE_DATE, DatatypeEnum::getValidTypeFromSample('2018-02-19'));
$this->assertEquals(DatatypeEnum::TYPE_DATE, DatatypeEnum::getValidTypeFromSample('18-2-19'));
$this->assertEquals(DatatypeEnum::TYPE_DATE, DatatypeEnum::getValidTypeFromSample('01.02.2018'));
$this->assertEquals(DatatypeEnum::TYPE_DATE, DatatypeEnum::getValidTypeFromSample('1.2.18'));
$this->assertEquals(DatatypeEnum::TYPE_DATE, DatatypeEnum::getValidTypeFromSample('07/31/2018'));
$this->assertNotEquals(DatatypeEnum::TYPE_DATE, DatatypeEnum::getValidTypeFromSample('31/07/2018'));
$this->assertNotEquals(DatatypeEnum::TYPE_DATE, DatatypeEnum::getValidTypeFromSample('1-2'));
$this->assertEquals(DatatypeEnum::TYPE_DATE, DatatypeEnum::getValidTypeFromSample('1.2.'));
$this->assertNotEquals(DatatypeEnum::TYPE_DATE, DatatypeEnum::getValidTypeFromSample('test'));
}
}

View File

@@ -0,0 +1,100 @@
<?php
namespace ParseCsv\tests\methods;
/**
* This is a very simple implementation of a stream wrapper. All URLs are mapped
* to just one buffer. It exists to prove that ParseCsv can read and write
* streams.
*
* @see https://www.php.net/manual/en/class.streamwrapper.php
*/
class ExampleStream {
private static $position = 0;
private static $stream_content;
public function stream_open($uri, $mode) {
if (strpos($mode, 'a') === false) {
self::$position = 0;
}
if (strpos($mode, 'w') !== false) {
self::$stream_content = '';
}
return true;
}
public function stream_read($count) {
$ret = substr(self::$stream_content, self::$position, $count);
self::$position += strlen($ret);
return $ret;
}
public function stream_write($data) {
$left = substr(self::$stream_content, 0, self::$position);
$right = substr(self::$stream_content, self::$position + strlen($data));
self::$stream_content = $left . $data . $right;
self::$position += strlen($data);
return strlen($data);
}
public function stream_stat() {
return ['size' => strlen(self::$stream_content)];
}
public function stream_tell() {
return self::$position;
}
public function stream_eof() {
return self::$position >= strlen(self::$stream_content);
}
public function url_stat() {
return ['size' => strlen(self::$stream_content)];
}
public function stream_seek($offset, $whence) {
switch ($whence) {
case SEEK_SET:
if ($offset < strlen(self::$stream_content) && $offset >= 0) {
self::$position = $offset;
return true;
} else {
return false;
}
break;
case SEEK_CUR:
if ($offset >= 0) {
self::$position += $offset;
return true;
} else {
return false;
}
break;
case SEEK_END:
if (strlen(self::$stream_content) + $offset >= 0) {
self::$position = strlen(self::$stream_content) + $offset;
return true;
} else {
return false;
}
break;
default:
return false;
}
}
public function stream_lock($operation) {
return true;
}
public function stream_metadata() {
return false;
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace ParseCsv\tests\methods;
/**
* Class HasToString is just a helper to test if cells can be objects.
*/
class ObjectThatHasToStringMethod {
public function __toString() {
return 'some value';
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace ParseCsv\tests\methods;
use PHPUnit\Framework\TestCase;
/**
* This test checks for backwards compatibility: Does it work to
* - require the old "parsecsv.lib.php" instead of composer autoloading?
* - use the old class name "parseCSV"?
*/
class OldRequireTest extends TestCase {
protected function setUp(): void {
rename('vendor/autoload.php', '__autoload');
}
protected function tearDown(): void {
rename('__autoload', 'vendor/autoload.php');
}
/**
* @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');
$this->assertEquals($output, []);
$this->assertEquals(0, $return_var);
}
/**
* @runInSeparateProcess so that disabled autoloading has an effect
*/
public function testOldLibWithOldClassName() {
file_put_contents('__eval.php', '<?php require "parsecsv.lib.php"; new parseCSV;');
exec("php __eval.php", $output, $return_var);
unlink('__eval.php');
$this->assertEquals($output, []);
$this->assertEquals(0, $return_var);
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace ParseCsv\tests\methods;
use ParseCsv\Csv;
use PHPUnit\Framework\TestCase;
class OutputTest extends TestCase {
/**
* @runInSeparateProcess because download.php uses header()
*/
public function testOutputWithFourParameters() {
$csv = new Csv();
$data = [0 => ['a', 'b', 'c'], 1 => ['d', 'e', 'f']];
$fields = ['col1', 'col2', 'col3'];
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);
}
}

View File

@@ -1,10 +1,15 @@
<?php <?php
class ParseTest extends PHPUnit\Framework\TestCase { namespace ParseCsv\tests\methods;
use ParseCsv\Csv;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
class ParseTest extends TestCase {
/** /**
* @access protected * @var Csv object
* @var ParseCsvForPhp
*/ */
protected $csv; protected $csv;
@@ -14,49 +19,76 @@ class ParseTest extends PHPUnit\Framework\TestCase {
* *
* @access public * @access public
*/ */
public function setUp() { protected function setUp(): void {
$this->csv = new ParseCsvForPhp(); $this->csv = new Csv();
} }
public function test_parse() { public function testParse() {
// can we trick 'is_readable' into whining? See #67. // can we trick 'is_readable' into whining? See #67.
$this->parse_repetitive_string('c:/looks/like/a/path'); $this->parseRepetitiveString('c:/looks/like/a/path');
$this->parse_repetitive_string('http://looks/like/an/url'); $this->parseRepetitiveString('http://looks/like/an/url');
} }
private function parse_repetitive_string($content) { /**
* @param string $content
*/
private function parseRepetitiveString($content) {
$this->csv->delimiter = ';'; $this->csv->delimiter = ';';
$this->csv->heading = false; $this->csv->heading = false;
$success = $this->csv->parse(str_repeat($content . ';', 500)); $success = $this->csv->parse(str_repeat($content . ';', 500));
$this->assertEquals(true, $success); self::assertEquals(true, $success);
$row = array_pop($this->csv->data); $row = array_pop($this->csv->data);
$expected_data = array_fill(0, 500, $content); $expected_data = array_fill(0, 500, $content);
$expected_data [] = ''; $expected_data[] = '';
$this->assertEquals($expected_data, $row); self::assertEquals($expected_data, $row);
} }
public function test_sep_row_auto_detection_UTF8_no_BOM() { /**
$this->_autoparse_magazine_file( * @depends testParse
__DIR__ . '/../example_files/UTF-8_sep_row_but_no_BOM.csv'); *
* @dataProvider autoDetectionProvider
*
* @param string $file
*/
public function testSepRowAutoDetection($file) {
// This file (parse_test.php) is encoded in UTF-8, hence comparison will
// fail unless we to this:
$this->csv->output_encoding = 'UTF-8';
$this->csv->auto($file);
self::assertEquals($this->_get_magazines_data(), $this->csv->data);
}
/**
* @return array
*/
public function autoDetectionProvider() {
return [
'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'],
'UTF16' => [__DIR__ . '/../example_files/UTF-16LE_with_BOM_and_sep_row.csv'],
];
} }
public function testSingleColumnWithZeros() { public function testSingleColumnWithZeros() {
$this->csv->delimiter = null; $this->csv->delimiter = null;
$this->csv->parse("URL\nhttp://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->csv->parse(
"URL\nhttp://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"
);
$row = array_pop($this->csv->data); $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']; $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() { public function testAllNumericalCsv() {
$this->csv->heading = false; $this->csv->heading = false;
$sInput = "86545235689\r\n34365587654\r\n13469874576"; $sInput = "86545235689\r\n34365587654\r\n13469874576";
$this->assertEquals(false, $this->csv->auto($sInput)); self::assertEquals(false, $this->csv->autoDetectionForDataString($sInput));
$this->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('reset', $this->csv->data);
$this->assertEquals($expected_data, $actual_data); self::assertEquals($expected_data, $actual_data);
} }
public function testMissingEndingLineBreak() { public function testMissingEndingLineBreak() {
@@ -64,36 +96,21 @@ class ParseTest extends PHPUnit\Framework\TestCase {
$this->csv->enclosure = '"'; $this->csv->enclosure = '"';
$sInput = "86545235689,a\r\n34365587654,b\r\n13469874576,\"c\r\nd\""; $sInput = "86545235689,a\r\n34365587654,b\r\n13469874576,\"c\r\nd\"";
$expected_data = [86545235689, 34365587654, 13469874576]; $expected_data = [86545235689, 34365587654, 13469874576];
$actual_data = $this->csv->parse_string($sInput);
$actual_data = $this->invokeMethod($this->csv, '_parse_string', [$sInput]);
$actual_column = array_map('reset', $actual_data); $actual_column = array_map('reset', $actual_data);
$this->assertEquals($expected_data, $actual_column); self::assertEquals($expected_data, $actual_column);
$this->assertEquals([ self::assertEquals(
'a', [
'b', 'a',
"c\r\nd", 'b',
], array_map('next', $actual_data)); "c\r\nd",
],
array_map('next', $actual_data)
);
} }
public function test_sep_row_auto_detection_UTF8() { public function testSingleColumn() {
$this->_autoparse_magazine_file(
__DIR__ . '/../example_files/UTF-8_with_BOM_and_sep_row.csv');
}
public function test_sep_row_auto_detection_UTF16() {
$this->_autoparse_magazine_file(
__DIR__ . '/../example_files/UTF-16LE_with_BOM_and_sep_row.csv');
}
protected function _autoparse_magazine_file($file) {
// This file (parse_test.php) is encoded in UTF-8, hence comparison will
// fail unless we to this:
$this->csv->output_encoding = 'UTF-8';
$this->csv->auto($file);
$this->assertEquals($this->_get_magazines_data(), $this->csv->data);
}
public function test_single_column() {
$this->csv->auto(__DIR__ . '/../example_files/single_column.csv'); $this->csv->auto(__DIR__ . '/../example_files/single_column.csv');
$expected = [ $expected = [
['SMS' => '0444'], ['SMS' => '0444'],
@@ -101,32 +118,153 @@ class ParseTest extends PHPUnit\Framework\TestCase {
['SMS' => '6606'], ['SMS' => '6606'],
['SMS' => '7777'], ['SMS' => '7777'],
]; ];
$this->assertEquals($expected, $this->csv->data); self::assertEquals($expected, $this->csv->data);
} }
public function test_Piwik_data() { public function testSingleRow() {
if (!function_exists('array_column')) { $this->csv->auto(__DIR__ . '/../example_files/single_row.csv');
// function only available in PHP >= 5.5 self::assertEquals([], $this->csv->data, 'Single row is detected as header');
return; $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() {
// Matomo (Piwik) export cannot be read with
$this->csv->use_mb_convert_encoding = true; $this->csv->use_mb_convert_encoding = true;
$this->csv->output_encoding = 'UTF-8'; $this->csv->output_encoding = 'UTF-8';
$this->csv->auto(__DIR__ . '/../example_files/Piwik_API_download.csv'); $this->csv->auto(__DIR__ . '/../example_files/Piwik_API_download.csv');
$aAction27 = array_column($this->csv->data, 'url (actionDetails 27)'); $aAction27 = array_column($this->csv->data, 'url (actionDetails 27)');
$this->assertEquals([ self::assertEquals(
'http://application/_Main/_GraphicMeanSTD_MDI/btnConfBandOptions', [
'', 'http://application/_Main/_GraphicMeanSTD_MDI/btnConfBandOptions',
'', '',
], $aAction27); '',
],
$aAction27
);
$aCity = array_column($this->csv->data, 'city'); $aCity = array_column($this->csv->data, 'city');
$this->assertEquals([ self::assertEquals(
'São Paulo', [
'Johannesburg', 'São Paulo',
'', 'Johannesburg',
], $aCity); '',
],
$aCity
);
} }
/**
* Tests if we can handle BOMs in string data, in contrast to loading files.
*/
public function testStringWithLeadingBOM() {
$string_with_bom = strtr(
file_get_contents(__DIR__ . '/../example_files/UTF-8_with_BOM_and_sep_row.csv'),
["sep=;\n" => '']
);
// Is the BOM still there?
self::assertSame(0xEF, ord($string_with_bom));
$this->csv->output_encoding = 'UTF-8';
$this->csv->delimiter = ';';
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(
[
'title',
'isbn',
'publishedAt',
],
array_keys(reset($this->csv->data)));
$titles = array_column($this->csv->data, 'title');
$this->assertEquals(
[
'Красивая кулинария',
'The Wine Connoisseurs',
'Weißwein',
],
$titles);
}
public function testWithMultipleNewlines() {
$this->csv->auto(__DIR__ . '/../example_files/multiple_empty_lines.csv');
$aElse9 = array_column($this->csv->data, 'else9');
/** @noinspection SpellCheckingInspection */
$this->assertEquals(
[
'Abweichung',
'Abweichung',
'Abweichung',
'Alt',
'Fehlt',
'Neu',
'OK',
'Fehlt',
'Fehlt',
'Fehlt',
],
$aElse9);
}
/**
* @depends testSepRowAutoDetection
*/
public function testGetColumnDatatypes() {
$this->csv->auto(__DIR__ . '/fixtures/datatype.csv');
$this->csv->getDatatypes();
$expected = [
'title' => 'string',
'isbn' => 'string',
'publishedAt' => 'date',
'published' => 'boolean',
'count' => 'integer',
'price' => 'float',
];
self::assertEquals($expected, $this->csv->data_types);
}
/**
* @depends testSepRowAutoDetection
*/
public function testAutoDetectFileHasHeading() {
$this->csv->auto(__DIR__ . '/fixtures/datatype.csv');
self::assertTrue($this->csv->autoDetectFileHasHeading());
$this->csv->heading = false;
$this->csv->auto(__DIR__ . '/fixtures/datatype.csv');
self::assertTrue($this->csv->autoDetectFileHasHeading());
$this->csv->heading = false;
$sInput = "86545235689\r\n34365587654\r\n13469874576";
$this->csv->autoDetectionForDataString($sInput);
self::assertFalse($this->csv->autoDetectFileHasHeading());
$this->csv->heading = true;
$sInput = "86545235689\r\n34365587654\r\n13469874576";
$this->csv->autoDetectionForDataString($sInput);
self::assertFalse($this->csv->autoDetectFileHasHeading());
}
/**
* @doesNotPerformAssertions
*/
public function testVeryLongNonExistingFile() {
$this->csv->parse(str_repeat('long_string', PHP_MAXPATHLEN));
$this->csv->auto(str_repeat('long_string', PHP_MAXPATHLEN));
}
/**
* @return array
*/
protected function _get_magazines_data() { protected function _get_magazines_data() {
return [ return [
[ [
@@ -147,7 +285,10 @@ class ParseTest extends PHPUnit\Framework\TestCase {
]; ];
} }
public function autoQuotesDataProvider() { /**
* @return array
*/
public 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', "'"),
@@ -155,15 +296,60 @@ class ParseTest extends PHPUnit\Framework\TestCase {
} }
/** /**
* @depends testSepRowAutoDetection
*
* @dataProvider autoQuotesDataProvider * @dataProvider autoQuotesDataProvider
* *
* @param string $file * @param string $file
* @param string $enclosure * @param string $enclosure
*/ */
public function testAutoQuotes($file, $enclosure) { public function testAutoQuotes($file, $enclosure) {
$csv = new ParseCsvForPhp(); $csv = new Csv();
$csv->auto(__DIR__ . '/../example_files/' . $file, true, null, null, $enclosure); $csv->auto(__DIR__ . '/fixtures/' . $file, true, null, null, $enclosure);
$this->assertArrayHasKey('column1', $csv->data[0], 'Data parsed incorrectly with enclosure ' . $enclosure); self::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::assertEquals('value1', $csv->data[0]['column1'], 'Data parsed incorrectly with enclosure ' . $enclosure);
}
/**
* Call protected/private method of a class.
*
* @param object $object Instantiated object that we will run method on.
* @param string $methodName Method name to call
* @param array $parameters Array of parameters to pass into method.
*
* @return mixed Method return.
*/
private function invokeMethod($object, $methodName, $parameters = []) {
$reflection = new ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($object, $parameters);
}
public function testWaiverFieldSeparator() {
self::assertFalse($this->csv->auto(__DIR__ . '/../example_files/waiver_field_separator.csv'));
$expected = [
'liability waiver',
'release of liability form',
'release of liability',
'sample waiver',
'sample waiver form',
];
$actual = array_column($this->csv->data, 'keyword');
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);
} }
} }

View File

@@ -1,8 +1,13 @@
<?php <?php
class SaveTest extends PHPUnit\Framework\TestCase { namespace ParseCsv\tests\methods;
/** @var ParseCsvForPhp */ use ParseCsv\Csv;
use PHPUnit\Framework\TestCase;
class SaveTest extends TestCase {
/** @var Csv */
private $csv; private $csv;
private $temp_filename; private $temp_filename;
@@ -10,8 +15,8 @@ class SaveTest extends PHPUnit\Framework\TestCase {
/** /**
* Setup our test environment objects; will be called before each test. * Setup our test environment objects; will be called before each test.
*/ */
public function setUp() { protected function setUp(): void {
$this->csv = new ParseCsvForPhp(); $this->csv = new Csv();
$this->csv->auto(__DIR__ . '/../example_files/single_column.csv'); $this->csv->auto(__DIR__ . '/../example_files/single_column.csv');
// Remove last 2 lines to simplify comparison // Remove last 2 lines to simplify comparison
@@ -44,6 +49,13 @@ class SaveTest extends PHPUnit\Framework\TestCase {
$this->saveAndCompare($expected); $this->saveAndCompare($expected);
} }
public function testSaveWithNewHeader() {
$this->csv->linefeed = "\n";
$this->csv->titles = array("NewTitle");
$expected = "NewTitle\n0444\n5555\n";
$this->saveAndCompare($expected);
}
public function testSaveWithoutHeader() { public function testSaveWithoutHeader() {
$this->csv->linefeed = "\n"; $this->csv->linefeed = "\n";
$this->csv->heading = false; $this->csv->heading = false;

View File

@@ -0,0 +1,71 @@
<?php
namespace ParseCsv\tests\methods;
use ParseCsv\Csv;
use PHPUnit\Framework\TestCase;
/**
* Writes roughly 1 MB of data. This is useful because of a limit of 8 KB
* encountered with stream operations in certain PHP versions.
*/
class StreamTest extends TestCase {
protected function setUp(): void {
static $done;
if ($done) {
// Make sure we register the stream just once - or PHP will scream.
return;
}
stream_wrapper_register("example", ExampleStream::class)
or die("Failed to register protocol");
$done = 1;
}
public function testReadStream() {
$csv = new Csv();
// Write data to our stream:
$filename = 'example://tmp';
copy(__DIR__ . '/fixtures/datatype.csv', $filename);
$many_dots = str_repeat('.', 1000 * 1000) . ";;;;;\n";
file_put_contents($filename, $many_dots, FILE_APPEND);
self::assertSame(';', $csv->auto(file_get_contents($filename)));
self::assertCount(4, $csv->data);
self::assertCount(6, reset($csv->data));
}
public function testWriteStream() {
$csv = new Csv();
$csv->linefeed = "\n";
$many_dots = str_repeat('.', 1000 * 1000);
$csv->data = [
[
'Name' => 'Rudolf',
'Question' => 'Which color is his nose?',
],
[
'Name' => 'Sponge Bob',
'Question' => 'Which shape are his pants?',
],
[
'Name' => $many_dots,
'Question' => 'Can you count one million dots?',
],
];
// Just export the first column, but with a new name
$csv->titles = ['Name' => 'Character'];
// Write data to our stream:
$filename = 'example://data';
copy(__DIR__ . '/fixtures/datatype.csv', $filename);
self::assertSame(true, $csv->save($filename));
$expected = "Character\nRudolf\nSponge Bob\n";
$expected .= $many_dots . "\n";
self::assertSame($expected, file_get_contents($filename));
}
}

View File

@@ -0,0 +1,107 @@
<?php
namespace ParseCsv\tests\methods;
use ParseCsv\Csv;
use PHPUnit\Framework\TestCase;
class UnparseTest extends Testcase {
/** @var Csv */
private $csv;
/**
* Setup our test environment objects; will be called before each test.
*/
protected function setUp(): void {
$this->csv = new Csv();
$this->csv->auto(__DIR__ . '/fixtures/auto-double-enclosure.csv');
}
public function testUnparseWithParameters() {
$fields = array('a' => 'AA', 'b' => 'BB');
$data = [['a' => 'value1', 'b' => 'value2']];
$csv_object = new Csv();
$csv_string = $csv_object->unparse($data, $fields);
$this->assertEquals("AA,BB\rvalue1,value2\r", $csv_string);
$csv_object = new Csv();
$csv_object->linefeed = "\n";
$csv_string = $csv_object->unparse([[55, 66]]);
$this->assertEquals("55,66\n", $csv_string);
$csv_object = new Csv();
$data2 = [['a' => "multi\rline", 'b' => 'value2']];
$csv_object->enclosure = "'";
$csv_string = $csv_object->unparse($data2, $fields);
$this->assertEquals("AA,BB\r'multi\rline',value2\r", $csv_string);
}
public function testUnparseDefault() {
$expected = "column1,column2\rvalue1,value2\rvalue3,value4\r";
$this->unparseAndCompare($expected);
}
public function testUnparseDefaultWithoutHeading() {
$this->csv->heading = false;
$this->csv->auto(__DIR__ . '/fixtures/auto-double-enclosure.csv');
$expected = "column1,column2\rvalue1,value2\rvalue3,value4\r";
$this->unparseAndCompare($expected);
}
public function testUnparseRenameFields() {
$expected = "C1,C2\rvalue1,value2\rvalue3,value4\r";
$this->unparseAndCompare($expected, array("C1", "C2"));
}
public function testReorderFields() {
$expected = "column2,column1\rvalue2,value1\rvalue4,value3\r";
$this->unparseAndCompare($expected, array("column2", "column1"));
}
public function testSubsetFields() {
$expected = "column1\rvalue1\rvalue3\r";
$this->unparseAndCompare($expected, array("column1"));
}
public function testReorderAndRenameFields() {
$fields = array(
'column2' => 'C2',
'column1' => 'C1',
);
$expected = "C2,C1\rvalue2,value1\rvalue4,value3\r";
$this->unparseAndCompare($expected, $fields);
}
public function testUnparseDefaultFirstRowMissing() {
unset($this->csv->data[0]);
$expected = "column1,column2\rvalue3,value4\r";
$this->unparseAndCompare($expected);
}
public function testUnparseDefaultWithoutData() {
unset($this->csv->data[0]);
unset($this->csv->data[1]);
$expected = "column1,column2\r";
$this->unparseAndCompare($expected);
}
public function testObjectCells() {
$this->csv->data = [
[
'column1' => new ObjectThatHasToStringMethod(),
'column2' => 'boring',
],
];
$this->csv->linefeed = "\n";
$expected = "column1,column2\nsome value,boring\n";
$this->unparseAndCompare($expected);
}
private function unparseAndCompare($expected, $fields = array()) {
$str = $this->csv->unparse($this->csv->data, $fields);
$this->assertEquals($expected, $str);
}
}

View File

@@ -0,0 +1,5 @@
sep=;
title;isbn;publishedAt;published;count;price
Красивая кулинария;5454-5587-3210;21.05.2011;true;1;10.99
The Wine Connoisseurs;2547-8548-2541;12.12.2011;TRUE;;20.33
Weißwein;1313-4545-8875;23.02.2012;false;10;10
1 sep=;
2 title;isbn;publishedAt;published;count;price
3 Красивая кулинария;5454-5587-3210;21.05.2011;true;1;10.99
4 The Wine Connoisseurs;2547-8548-2541;12.12.2011;TRUE;;20.33
5 Weißwein;1313-4545-8875;23.02.2012;false;10;10

View File

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

View File

@@ -1,21 +1,26 @@
<?php <?php
class BaseClass extends PHPUnit\Framework\TestCase { namespace ParseCsv\tests\properties;
use ParseCsv\Csv;
use PHPUnit\Framework\TestCase;
class BaseClass extends TestCase {
/** /**
* @access protected * CSV
* @var ParseCsvForPhp object * The parseCSV object
*
* @var Csv
*/ */
protected $csv; protected $csv;
/** /**
* Setup * Setup
* Setup our test environment objects * Setup our test environment objects
*
* @access public
*/ */
public function setUp() { protected function setUp(): void {
$this->csv = new ParseCsvForPhp(); $this->csv = new Csv();
} }
protected function _compareWithExpected($expected) { protected function _compareWithExpected($expected) {
@@ -23,6 +28,6 @@ class BaseClass extends PHPUnit\Framework\TestCase {
$actual = array_map(function ($row) { $actual = array_map(function ($row) {
return $row['title']; return $row['title'];
}, $this->csv->data); }, $this->csv->data);
$this->assertEquals($expected, $actual); $this->assertEquals($expected, array_values($actual));
} }
} }

View File

@@ -1,5 +1,7 @@
<?php <?php
namespace ParseCsv\tests\properties;
class ConditionsTest extends BaseClass { class ConditionsTest extends BaseClass {
public function testNotDanBrown() { public function testNotDanBrown() {
@@ -19,21 +21,93 @@ class ConditionsTest extends BaseClass {
]); ]);
} }
public function testRating() { public function testRatingEquals() {
$this->csv->conditions = 'rating < 3'; $rating_of_3 = [
$this->_compareWithExpected([ 'The Last Templar',
'The Broker (Paperback)',
'Without Blood (Paperback)',
];
$this->csv->conditions = 'rating = 3';
$this->_compareWithExpected($rating_of_3);
$this->csv->conditions = 'rating is 3';
$this->_compareWithExpected($rating_of_3);
$this->csv->conditions = 'rating equals 3';
$this->_compareWithExpected($rating_of_3);
}
public function testRatingNotEquals() {
$rating_not_4 = [
'The Killing Kind', 'The Killing Kind',
'The Third Secret', 'The Third Secret',
]); 'The Last Templar',
'The Traveller',
'Prey',
'The Broker (Paperback)',
'Without Blood (Paperback)',
'State of Fear (Paperback)',
'Digital Fortress : A Thriller (Mass Market Paperback)',
'Angels & Demons (Mass Market Paperback)',
];
// $this->csv->conditions = 'rating != 4';
// $this->_compareWithExpected($rating_not_4);
$this->csv->conditions = 'rating is not 4';
$this->_compareWithExpected($rating_not_4);
// $this->csv->conditions = 'rating does not contain 4';
// $this->_compareWithExpected($rating_not_4);
}
$this->csv->conditions = 'rating >= 5'; public function testRatingLessThan() {
$this->_compareWithExpected([ $less_than_1 = [
'The Killing Kind',
'The Third Secret',
];
$this->csv->conditions = 'rating < 1';
$this->_compareWithExpected($less_than_1);
$this->csv->conditions = 'rating is less than 1';
$this->_compareWithExpected($less_than_1);
}
public function testRatingLessOrEquals() {
$less_or_equals_1 = [
'The Killing Kind',
'The Third Secret',
];
$this->csv->conditions = 'rating <= 1';
$this->_compareWithExpected($less_or_equals_1);
$this->csv->conditions = 'rating is less than or equals 1';
$this->_compareWithExpected($less_or_equals_1);
}
public function testRatingGreaterThan() {
$greater_4 = [
'The Traveller', 'The Traveller',
'Prey', 'Prey',
'State of Fear (Paperback)', 'State of Fear (Paperback)',
'Digital Fortress : A Thriller (Mass Market Paperback)', 'Digital Fortress : A Thriller (Mass Market Paperback)',
'Angels & Demons (Mass Market Paperback)', 'Angels & Demons (Mass Market Paperback)',
]); ];
$this->csv->conditions = 'rating > 4';
$this->_compareWithExpected($greater_4);
$this->csv->conditions = 'rating is greater than 4';
$this->_compareWithExpected($greater_4);
}
public function testRatingGreaterOrEquals() {
$greater_or_equal_4 = [
'The Traveller',
'Crisis Four',
'Prey',
'State of Fear (Paperback)',
'The Rule of Four (Paperback)',
'Deception Point (Paperback)',
'Digital Fortress : A Thriller (Mass Market Paperback)',
'Angels & Demons (Mass Market Paperback)',
'The Da Vinci Code (Hardcover)',
];
$this->csv->conditions = 'rating >= 4';
$this->_compareWithExpected($greater_or_equal_4);
$this->csv->conditions = 'rating is greater than or equals 4';
$this->_compareWithExpected($greater_or_equal_4);
} }
public function testTitleContainsSecretOrCode() { public function testTitleContainsSecretOrCode() {

View File

@@ -0,0 +1,150 @@
<?php
namespace ParseCsv\tests\properties;
use ParseCsv\Csv;
use PHPUnit\Framework\TestCase;
class DefaultValuesPropertiesTest extends TestCase {
/**
* CSV
* The parseCSV object
*
* @access protected
* @var Csv
*/
protected $csv = null;
protected function setUp(): void {
$this->csv = new Csv();
}
public function test_heading_default() {
self::assertTrue(is_bool($this->csv->heading));
self::assertTrue($this->csv->heading);
}
public function test_fields_default() {
self::assertTrue(is_array($this->csv->fields));
self::assertCount(0, $this->csv->fields);
}
public function test_sort_by_default() {
self::assertNull($this->csv->sort_by);
}
public function test_sort_reverse_default() {
self::assertTrue(is_bool($this->csv->sort_reverse));
self::assertFalse($this->csv->sort_reverse);
}
public function test_sort_type_default() {
self::assertEquals('regular', $this->csv->sort_type);
}
public function test_delimiter_default() {
self::assertTrue(is_string($this->csv->delimiter));
self::assertEquals(',', $this->csv->delimiter);
}
public function test_enclosure_default() {
self::assertTrue(is_string($this->csv->enclosure));
self::assertEquals('"', $this->csv->enclosure);
}
public function test_enclose_all_default() {
self::assertTrue(is_bool($this->csv->enclose_all));
self::assertFalse($this->csv->enclose_all);
}
public function test_conditions_default() {
self::assertNull($this->csv->conditions);
}
public function test_offset_default() {
self::assertNull($this->csv->offset);
}
public function test_limit_default() {
self::assertNull($this->csv->limit);
}
public function test_auto_depth_default() {
self::assertTrue(is_numeric($this->csv->auto_depth));
self::assertEquals(15, $this->csv->auto_depth);
}
public function test_auto_non_chars_default() {
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() {
self::assertTrue(is_string($this->csv->auto_preferred));
self::assertEquals(",;\t.:|", $this->csv->auto_preferred);
}
public function test_convert_encoding_default() {
self::assertTrue(is_bool($this->csv->convert_encoding));
self::assertFalse($this->csv->convert_encoding);
}
public function test_input_encoding_default() {
self::assertTrue(is_string($this->csv->input_encoding));
self::assertEquals('ISO-8859-1', $this->csv->input_encoding);
}
public function test_output_encoding_default() {
self::assertTrue(is_string($this->csv->output_encoding));
self::assertEquals('ISO-8859-1', $this->csv->output_encoding);
}
public function test_linefeed_default() {
self::assertTrue(is_string($this->csv->linefeed));
self::assertEquals("\r", $this->csv->linefeed);
}
public function test_output_delimiter_default() {
self::assertTrue(is_string($this->csv->output_delimiter));
self::assertEquals(',', $this->csv->output_delimiter);
}
public function test_output_filename_default() {
self::assertTrue(is_string($this->csv->output_filename));
self::assertEquals('data.csv', $this->csv->output_filename);
}
public function test_keep_file_data_default() {
self::assertTrue(is_bool($this->csv->keep_file_data));
self::assertFalse($this->csv->keep_file_data);
}
public function test_file_default() {
self::assertNull($this->csv->file);
}
public function test_file_data_default() {
self::assertNull($this->csv->file_data);
}
public function test_error_default() {
self::assertTrue(is_numeric($this->csv->error));
self::assertEquals(0, $this->csv->error);
}
public function test_error_info_default() {
self::assertTrue(is_array($this->csv->error_info));
self::assertCount(0, $this->csv->error_info);
}
public function test_titles_default() {
self::assertTrue(is_array($this->csv->titles));
self::assertCount(0, $this->csv->titles);
}
public function test_data_default() {
self::assertTrue(is_array($this->csv->data));
self::assertCount(0, $this->csv->data);
}
}

View File

@@ -0,0 +1,79 @@
<?php
namespace ParseCsv\tests\properties;
/**
* Tests related to the $offset property
*/
class OffsetTest extends BaseClass {
public function testOffsetOfOne() {
$this->csv->offset = 1;
$this->csv->auto(__DIR__ . '/../methods/fixtures/datatype.csv');
$this->assertCount(3, $this->csv->data);
if (!function_exists('array_column')) {
// function only available in PHP >= 5.5
return;
}
$expected = [
'Красивая кулинария',
'The Wine Connoisseurs',
'Weißwein',
];
$actual = array_column($this->csv->data, 'title');
$this->assertEquals($expected, $actual);
}
public function numberRangeZeroToFourProvider() {
return array_map(function ($number) {
return [$number];
}, range(0, 4));
}
/**
* @dataProvider numberRangeZeroToFourProvider
*
* @param int $offset
*/
public function testOffsetOfOneNoHeader($offset) {
$this->csv->offset = $offset;
$this->csv->heading = false;
$this->csv->auto(__DIR__ . '/../methods/fixtures/datatype.csv');
$this->assertCount(4 - $offset, $this->csv->data);
}
public function testDataArrayKeysWhenSettingOffsetWithHeading() {
$this->csv->offset = 2;
$this->csv->auto(__DIR__ . '/../methods/fixtures/datatype.csv');
$expected = [
[
'title' => 'The Wine Connoisseurs',
'isbn' => '2547-8548-2541',
'publishedAt' => '12.12.2011',
'published' => 'TRUE',
'count' => '',
'price' => 20.33,
],
[
'title' => 'Weißwein',
'isbn' => '1313-4545-8875',
'publishedAt' => '23.02.2012',
'published' => 'false',
'count' => 10,
'price' => 10,
],
];
$this->assertEquals($expected, $this->csv->data);
}
public function testDataArrayKeysWhenSettingOffsetWithoutHeading() {
$this->csv->heading = false;
$this->csv->offset = 2;
$this->csv->auto(__DIR__ . '/../methods/fixtures/datatype.csv');
$expected = range(0, 5, 1);
$this->assertEquals($expected, array_keys($this->csv->data[0]));
}
}

View File

@@ -1,10 +1,19 @@
<?php <?php
class worthless_properties_Test extends PHPUnit\Framework\TestCase { namespace ParseCsv\tests\properties;
use ParseCsv\Csv;
use ParseCsv\enums\SortEnum;
use PHPUnit\Framework\TestCase;
class PublicPropertiesTest extends TestCase {
/** /**
* CSV
* The parseCSV object
*
* @access protected * @access protected
* @var ParseCsvForPhp * @var Csv
*/ */
protected $csv = null; protected $csv = null;
@@ -13,7 +22,7 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase {
* The reflection class object * The reflection class object
* *
* @access protected * @access protected
* @var ReflectionClass * @var \ReflectionClass
*/ */
protected $reflection = null; protected $reflection = null;
@@ -21,8 +30,8 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase {
* Reflection Properties * Reflection Properties
* The reflected class properties * The reflected class properties
* *
* @var \ReflectionProperty[]
* @access protected * @access protected
* @var ReflectionProperty[]
*/ */
protected $properties = null; protected $properties = null;
@@ -32,12 +41,12 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase {
* *
* @access public * @access public
*/ */
public function setUp() { protected function setUp(): void {
//setup parse CSV //setup parse CSV
$this->csv = new ParseCsvForPhp(); $this->csv = new Csv();
//setup the reflection class //setup the reflection class
$this->reflection = new ReflectionClass($this->csv); $this->reflection = new \ReflectionClass($this->csv);
//setup the reflected class properties //setup the reflected class properties
$this->properties = $this->reflection->getProperties(); $this->properties = $this->reflection->getProperties();
@@ -49,7 +58,7 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase {
* *
* @access public * @access public
*/ */
public function tearDown() { protected function tearDown(): void {
$this->csv = null; $this->csv = null;
$this->reflection = null; $this->reflection = null;
$this->properties = null; $this->properties = null;
@@ -63,7 +72,7 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase {
* @access public * @access public
*/ */
public function test_propertiesCount() { public function test_propertiesCount() {
$this->assertCount(28, $this->properties); $this->assertCount(29, $this->properties);
} }
/** /**
@@ -106,10 +115,11 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase {
'error_info', 'error_info',
'titles', 'titles',
'data', 'data',
'data_types',
); );
// Find our real properties // Find our real properties
$real_properties = array_map(function (ReflectionProperty $property) { $real_properties = array_map(function (\ReflectionProperty $property) {
return $property->getName(); return $property->getName();
}, $this->properties); }, $this->properties);
@@ -127,7 +137,8 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase {
public function test_count_public_properties() { public function test_count_public_properties() {
$counter = 0; $counter = 0;
for ($a = count($this->properties) - 1; $a >= 0; $a--) { $propertiesCount = count($this->properties);
for ($a = 0; $a < $propertiesCount; $a++) {
if ($this->properties[$a]->isPublic() === true) { if ($this->properties[$a]->isPublic() === true) {
$counter++; $counter++;
} }
@@ -135,4 +146,26 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase {
$this->assertCount($counter, $this->properties); $this->assertCount($counter, $this->properties);
} }
public function testDefaultSortTypeIsRegular() {
$this->assertEquals(SortEnum::SORT_TYPE_REGULAR, $this->csv->sort_type);
}
public function testSetSortType() {
$this->csv->sort_type = 'numeric';
$this->assertEquals(SortEnum::SORT_TYPE_NUMERIC, $this->csv->sort_type);
$this->csv->sort_type = 'string';
$this->assertEquals(SortEnum::SORT_TYPE_STRING, $this->csv->sort_type);
}
public function testGetSorting() {
$this->csv->sort_type = 'numeric';
$sorting = SortEnum::getSorting($this->csv->sort_type);
$this->assertEquals(SORT_NUMERIC, $sorting);
$this->csv->sort_type = 'string';
$sorting = SortEnum::getSorting($this->csv->sort_type);
$this->assertEquals(SORT_STRING, $sorting);
}
} }

View File

@@ -1,5 +1,7 @@
<?php <?php
namespace ParseCsv\tests\properties;
class SortByTest extends BaseClass { class SortByTest extends BaseClass {
public function testSortByRating() { public function testSortByRating() {

View File

@@ -1,149 +0,0 @@
<?php
class default_values_properties_Test extends PHPUnit\Framework\TestCase {
/**
* @access protected
* @var ParseCsvForPhp object
*/
protected $csv = null;
/**
* Setup
* Setup our test environment objects
*
* @access public
*/
public function setUp() {
//setup parse CSV
$this->csv = new ParseCsvForPhp();
}
public function test_heading_default() {
$this->assertTrue(is_bool($this->csv->heading));
$this->assertTrue($this->csv->heading);
}
public function test_fields_default() {
$this->assertTrue(is_array($this->csv->fields));
$this->assertCount(0, $this->csv->fields);
}
public function test_sort_by_default() {
$this->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);
}
public function test_sort_type_default() {
$this->assertNull($this->csv->sort_type);
}
public function test_delimiter_default() {
$this->assertTrue(is_string($this->csv->delimiter));
$this->assertEquals(',', $this->csv->delimiter);
}
public function test_enclosure_default() {
$this->assertTrue(is_string($this->csv->enclosure));
$this->assertEquals('"', $this->csv->enclosure);
}
public function test_enclose_all_default() {
$this->assertTrue(is_bool($this->csv->enclose_all));
$this->assertFalse($this->csv->enclose_all);
}
public function test_conditions_default() {
$this->assertNull($this->csv->conditions);
}
public function test_offset_default() {
$this->assertNull($this->csv->offset);
}
public function test_limit_default() {
$this->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);
}
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);
}
public function test_auto_preferred_default() {
$this->assertTrue(is_string($this->csv->auto_preferred));
$this->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);
}
public function test_input_encoding_default() {
$this->assertTrue(is_string($this->csv->input_encoding));
$this->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);
}
public function test_linefeed_default() {
$this->assertTrue(is_string($this->csv->linefeed));
$this->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);
}
public function test_output_filename_default() {
$this->assertTrue(is_string($this->csv->output_filename));
$this->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);
}
public function test_file_default() {
$this->assertNull($this->csv->file);
}
public function test_file_data_default() {
$this->assertNull($this->csv->file_data);
}
public function test_error_default() {
$this->assertTrue(is_numeric($this->csv->error));
$this->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);
}
public function test_titles_default() {
$this->assertTrue(is_array($this->csv->titles));
$this->assertCount(0, $this->csv->titles);
}
public function test_data_default() {
$this->assertTrue(is_array($this->csv->data));
$this->assertCount(0, $this->csv->data);
}
}