mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 08:36:39 +00:00
Compare commits
417 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
548a9fe551 | ||
|
|
5336800ec6 | ||
|
|
6bd53940c5 | ||
|
|
a9297449b9 | ||
|
|
c12b6ba671 | ||
|
|
b792a6cc7b | ||
|
|
af442cdd0e | ||
|
|
95810ec2fa | ||
|
|
d79160f40a | ||
|
|
7ba2f9b296 | ||
|
|
d7ba892d38 | ||
|
|
38e9fff285 | ||
|
|
174d2e6070 | ||
|
|
d93036ca87 | ||
|
|
62fc367c14 | ||
|
|
044122efb9 | ||
|
|
18cbc3ab87 | ||
|
|
af75206e86 | ||
|
|
e45eb18213 | ||
|
|
5340ab9584 | ||
|
|
f3601dbfb5 | ||
|
|
67c8a0167c | ||
|
|
8b583daa08 | ||
|
|
b6dccf5fa6 | ||
|
|
5ca540daa7 | ||
|
|
f89ed5978d | ||
|
|
f053cdde80 | ||
|
|
62ae4e5fc3 | ||
|
|
a286556c16 | ||
|
|
7326262304 | ||
|
|
b746ea1de0 | ||
|
|
ef5e81f837 | ||
|
|
637f79c2e6 | ||
|
|
e3105d4003 | ||
|
|
5874b768b9 | ||
|
|
5d766adc17 | ||
|
|
67ed765eff | ||
|
|
a7be07cc02 | ||
|
|
b4cef37bb5 | ||
|
|
f97f03a088 | ||
|
|
2e94f4ad41 | ||
|
|
d03f8a2f09 | ||
|
|
14c9254ed4 | ||
|
|
78c288db8c | ||
|
|
531ac26e11 | ||
|
|
65566adcd3 | ||
|
|
2d428ffa93 | ||
|
|
bc9207de09 | ||
|
|
ace09c3c11 | ||
|
|
52ad56c66a | ||
|
|
ab9e8a0af9 | ||
|
|
8aa61914f7 | ||
|
|
3b74f7ce57 | ||
|
|
c7375dea8e | ||
|
|
6ce4550eed | ||
|
|
55890da647 | ||
|
|
21953ab6be | ||
|
|
1d8e6808bc | ||
|
|
063a812732 | ||
|
|
4504f4b158 | ||
|
|
b82ad03bd5 | ||
|
|
23a1b0a4b3 | ||
|
|
51d878a9a1 | ||
|
|
7d39b80fca | ||
|
|
8c7eba6815 | ||
|
|
c41c559ac2 | ||
|
|
e1ecf9302e | ||
|
|
de8e792ef4 | ||
|
|
60d6458080 | ||
|
|
406e1e415f | ||
|
|
2e40a2eb1c | ||
|
|
927e785891 | ||
|
|
e6ba24b47a | ||
|
|
f24e5bf3a3 | ||
|
|
bdd4e5ef25 | ||
|
|
42b5d30d66 | ||
|
|
93f177a396 | ||
|
|
f5ff7332a4 | ||
|
|
07846d33c1 | ||
|
|
4b60d38fb0 | ||
|
|
c8d15557cb | ||
|
|
10895788c8 | ||
|
|
ffed7ffdc0 | ||
|
|
7498a963ac | ||
|
|
49728a74bb | ||
|
|
e4c9fed6cf | ||
|
|
5bc6d09b5e | ||
|
|
b7f2075efc | ||
|
|
3cc20fbb47 | ||
|
|
086cd15b44 | ||
|
|
03bc946b98 | ||
|
|
8f431aa7a4 | ||
|
|
7168cb15e3 | ||
|
|
1142ef2611 | ||
|
|
80a4954a3a | ||
|
|
9c14bc2f30 | ||
|
|
4b6b7ee0b8 | ||
|
|
a80a6f1862 | ||
|
|
da70838698 | ||
|
|
9d7ccab20f | ||
|
|
abecaf1b5d | ||
|
|
3af94399e6 | ||
|
|
499088b3b2 | ||
|
|
fd63ff4a26 | ||
|
|
d250c9034b | ||
|
|
bfc2c0901b | ||
|
|
a0336f02ee | ||
|
|
ea78f38e26 | ||
|
|
c9e7997643 | ||
|
|
68239d6859 | ||
|
|
d05577d303 | ||
|
|
eedbffc375 | ||
|
|
fb9325884d | ||
|
|
11b20a3144 | ||
|
|
7766bf7c3b | ||
|
|
5daa422aca | ||
|
|
48a3cdbc5c | ||
|
|
fbe5263bca | ||
|
|
5b1002a677 | ||
|
|
4bbc928f09 | ||
|
|
951fc68886 | ||
|
|
68b849a37b | ||
|
|
aaefe2a480 | ||
|
|
611b1a92e8 | ||
|
|
e5eccf1fc1 | ||
|
|
9e5c97328d | ||
|
|
b6247c367c | ||
|
|
ba4cc0672a | ||
|
|
c9cc9697ef | ||
|
|
f8fe4cad03 | ||
|
|
95521cde87 | ||
|
|
249e5a24ac | ||
|
|
343c683077 | ||
|
|
cf91bf40ff | ||
|
|
958af1027e | ||
|
|
7a3120dd28 | ||
|
|
a74736d4da | ||
|
|
657cec4b4e | ||
|
|
ef44ea3989 | ||
|
|
2ac9f450f1 | ||
|
|
3968659b9f | ||
|
|
d3a084d973 | ||
|
|
b0355be96a | ||
|
|
b753d9c694 | ||
|
|
2b95ec5aa1 | ||
|
|
3f595dfd3b | ||
|
|
081949339b | ||
|
|
a60aae47a4 | ||
|
|
4cba97ad51 | ||
|
|
2206ec1e7c | ||
|
|
d188921ab6 | ||
|
|
85cc0d9cfc | ||
|
|
a7f2bbb7bc | ||
|
|
d484f3e9e3 | ||
|
|
f8b2fe274a | ||
|
|
17bfd9496a | ||
|
|
ec7b1c1d55 | ||
|
|
ba9eae17d4 | ||
|
|
8f26ce6ea3 | ||
|
|
913dc94e2c | ||
|
|
5ced1f4212 | ||
|
|
ca84469950 | ||
|
|
75902f4a22 | ||
|
|
7470d2b804 | ||
|
|
01f0891cfb | ||
|
|
52a9b61b97 | ||
|
|
5660be3373 | ||
|
|
70b6fe4bd8 | ||
|
|
f1a89127c2 | ||
|
|
31dba0411e | ||
|
|
6da0537197 | ||
|
|
91091ac933 | ||
|
|
2c7c552515 | ||
|
|
52e87953a4 | ||
|
|
2b6e38734c | ||
|
|
af7368378c | ||
|
|
f2fb7c81bd | ||
|
|
92653c99c5 | ||
|
|
d4f9c9f020 | ||
|
|
2b17f01a0a | ||
|
|
9551862a95 | ||
|
|
736bc489e6 | ||
|
|
f702ca93fe | ||
|
|
48cec20f4f | ||
|
|
c266ef9fbc | ||
|
|
76a0406eff | ||
|
|
17ac1dee1a | ||
|
|
f6bd414ae7 | ||
|
|
a7a0bc34a4 | ||
|
|
0e5afb209e | ||
|
|
f8c68a6cd7 | ||
|
|
5babb55879 | ||
|
|
8ec87126dc | ||
|
|
d788135bbe | ||
|
|
301f6da3e6 | ||
|
|
b9721a9aff | ||
|
|
3c31b70724 | ||
|
|
823f51c4f0 | ||
|
|
488b8f9d9b | ||
|
|
b004911218 | ||
|
|
47f691ce33 | ||
|
|
ab6b3d19e0 | ||
|
|
2d7ab4b2f0 | ||
|
|
2bf4a2d0cd | ||
|
|
f41a18b1c0 | ||
|
|
003e98e8ff | ||
|
|
d8aefa792e | ||
|
|
a92506d078 | ||
|
|
54e846ee30 | ||
|
|
63932f67a4 | ||
|
|
44ff705a23 | ||
|
|
6432ba2ced | ||
|
|
8ae09c604b | ||
|
|
5ea1d95f31 | ||
|
|
af549dc47e | ||
|
|
12aec86bf7 | ||
|
|
38944ba221 | ||
|
|
5a9a10aeed | ||
|
|
631fae5191 | ||
|
|
18ddde98c0 | ||
|
|
a07de14bba | ||
|
|
e87a5ae0e6 | ||
|
|
c04fc89c43 | ||
|
|
cc908fd096 | ||
|
|
155c50ad7a | ||
|
|
98edcbfec3 | ||
|
|
378353eb79 | ||
|
|
e4ca0062f6 | ||
|
|
e419663679 | ||
|
|
0ecaa80222 | ||
|
|
0ac5064d58 | ||
|
|
4d011827f5 | ||
|
|
c818fff81a | ||
|
|
ccad5c8360 | ||
|
|
b082849b17 | ||
|
|
7536d55f27 | ||
|
|
db1e242d07 | ||
|
|
fd5fb96bce | ||
|
|
4f08cbb561 | ||
|
|
0d14ca01be | ||
|
|
a4a0dfa2fd | ||
|
|
95ad7c315a | ||
|
|
0e50aec3b9 | ||
|
|
f5851ba9ac | ||
|
|
fad5ebcdc8 | ||
|
|
bd2031582d | ||
|
|
1db4b02e9e | ||
|
|
07f2306a9c | ||
|
|
7eb6daa6fb | ||
|
|
240fa0ce23 | ||
|
|
1321c3b693 | ||
|
|
b9cf7cb0c1 | ||
|
|
4bae0d2bcf | ||
|
|
189746405f | ||
|
|
2ae35c26ff | ||
|
|
2d1b7cebde | ||
|
|
b33cc6c667 | ||
|
|
fa5aea7631 | ||
|
|
4f144f2662 | ||
|
|
ba4c30add1 | ||
| a8b443d48c | |||
|
|
9c6e0d64cf | ||
| 83ec2fbce7 | |||
|
|
e750fcfb07 | ||
|
|
b7d1848363 | ||
|
|
8bb51f8bab | ||
|
|
22fcff2d24 | ||
|
|
5e8a5df988 | ||
| 813ef3849e | |||
| d795ad0e05 | |||
|
|
ac4c4699cb | ||
| e0728a3941 | |||
|
|
2f3732e350 | ||
|
|
01e71adf36 | ||
|
|
0c4a804db1 | ||
|
|
4366201dc0 | ||
|
|
0422d5d9fe | ||
|
|
6f4d150e6f | ||
|
|
494087ab1d | ||
|
|
ce19deec4e | ||
|
|
33825514d9 | ||
|
|
8af4d1e513 | ||
|
|
4d07760933 | ||
|
|
710f22eeb9 | ||
|
|
604872faad | ||
|
|
8e19ecf94f | ||
|
|
31417caafd | ||
|
|
17e060ea33 | ||
|
|
936b6f4a50 | ||
|
|
65061046bd | ||
|
|
4393781abc | ||
|
|
8bbd474e66 | ||
|
|
734b35abc5 | ||
|
|
5cb602be2d | ||
|
|
b8920f99cb | ||
|
|
b8248af92e | ||
|
|
d1f25ee3df | ||
|
|
0d5ae35a34 | ||
|
|
e1c8cae852 | ||
|
|
844a1520f3 | ||
|
|
81746a561f | ||
|
|
1524618293 | ||
|
|
e98cef4846 | ||
|
|
4ed88da7a2 | ||
|
|
18a6479aec | ||
|
|
1c4bcaffa6 | ||
|
|
7527267c68 | ||
|
|
b074f83644 | ||
|
|
7b83653ee2 | ||
|
|
0ed23e7b84 | ||
|
|
4a76ed9ca9 | ||
|
|
68eab2de59 | ||
|
|
32b50efe82 | ||
|
|
372c43fba4 | ||
|
|
7d63a8c894 | ||
|
|
7187bbd68f | ||
|
|
ee784f3180 | ||
|
|
633666060b | ||
|
|
8a5f15bdd8 | ||
|
|
a6f8c708f9 | ||
| cff692d976 | |||
|
|
c3c13258f3 | ||
| 9cf0ba9f97 | |||
|
|
18eb8a8f11 | ||
|
|
44a5507e10 | ||
|
|
3ffcd731ee | ||
|
|
3545dbeb00 | ||
|
|
0235c24b36 | ||
|
|
33b1a0d196 | ||
|
|
f3c70ad17c | ||
|
|
98a146bd38 | ||
|
|
0486a4bf3e | ||
|
|
90042a553c | ||
|
|
73d7acb493 | ||
|
|
2ed5801b33 | ||
|
|
7b6692d7fd | ||
|
|
01d25e0fdd | ||
|
|
c64a4bffd7 | ||
|
|
2d7f18ebc9 | ||
|
|
ee5b34e1c1 | ||
|
|
44929708ca | ||
| c55ab2ec2c | |||
|
|
6784c614ea | ||
|
|
5a70a7bffa | ||
|
|
c1a6946815 | ||
|
|
dbf415bd92 | ||
|
|
c915579955 | ||
|
|
55aaa1eb17 | ||
|
|
ab069e5d2d | ||
|
|
7e7aeba761 | ||
|
|
a3e3582222 | ||
|
|
fff3e73f08 | ||
| f59af53183 | |||
| 629b0c9fd5 | |||
| 698a570a11 | |||
| 2aca09e05d | |||
| 7a9fc101f8 | |||
| a1564a9a99 | |||
| 4b280885f4 | |||
|
|
88ad81f061 | ||
|
|
7e46cc193a | ||
|
|
a274874084 | ||
|
|
dea1b453eb | ||
|
|
9a8bd77eb8 | ||
|
|
89a2817765 | ||
|
|
75d7d51a9c | ||
|
|
9b6dbd9849 | ||
|
|
61d1c27eac | ||
|
|
ecbb0736f1 | ||
|
|
2ab7625bb5 | ||
|
|
0f39cfa75a | ||
|
|
0457fbfba2 | ||
|
|
af5d7cf70d | ||
|
|
531cc66a5e | ||
|
|
178699580e | ||
|
|
8c300568bb | ||
|
|
28967dea20 | ||
|
|
46b42776e4 | ||
|
|
8ba88c582d | ||
|
|
ba1d462044 | ||
|
|
0323d5108b | ||
|
|
2988278f0b | ||
|
|
9301b016c8 | ||
|
|
75038f9d2f | ||
|
|
b72385395e | ||
|
|
eb3c9faa4a | ||
|
|
2f142e7313 | ||
|
|
05dbcaf652 | ||
|
|
4639fa1701 | ||
|
|
8bb8e2d7cf | ||
|
|
c7e1f825a0 | ||
|
|
0638049fb2 | ||
|
|
585a0d472e | ||
|
|
d6a070ce08 | ||
|
|
4454196b4c | ||
|
|
6a6754ca04 | ||
|
|
8dd2e52073 | ||
|
|
e16d818d07 | ||
|
|
9e6fdbb42a | ||
| 85ca7af160 | |||
| 1b91591d32 | |||
|
|
dfe298f4d0 | ||
|
|
fc22bee7ca | ||
|
|
9b64cb07c4 | ||
|
|
2bfa3de220 | ||
|
|
c522cd87a7 | ||
|
|
0395362d66 | ||
|
|
20a6a9d1b1 | ||
|
|
70366e3085 | ||
|
|
2dfd35b988 | ||
|
|
ae00f949f0 | ||
|
|
4e76da5eff | ||
|
|
7762e71316 | ||
|
|
e28b3d0f9d | ||
|
|
9c389ed0c1 | ||
|
|
82049f1701 | ||
|
|
72fde0424d |
18
.editorconfig
Normal file
18
.editorconfig
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# @see http://editorconfig.org/
|
||||||
|
|
||||||
|
# This is the top-most .editorconfig file; do not search in parent directories.
|
||||||
|
root = true
|
||||||
|
|
||||||
|
# All files.
|
||||||
|
[*]
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 4
|
||||||
|
charset = utf-8
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
insert_final_newline = true
|
||||||
|
|
||||||
|
[composer.json]
|
||||||
|
indent_size = 4
|
||||||
|
|
||||||
|
[.travis.yml]
|
||||||
|
indent_size = 2
|
||||||
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
*.bak
|
||||||
|
/.idea
|
||||||
|
/phive.xml
|
||||||
|
/tools
|
||||||
|
composer.lock
|
||||||
|
vendor/
|
||||||
27
.travis.yml
Normal file
27
.travis.yml
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
dist: trusty
|
||||||
|
language: php
|
||||||
|
dist: trusty
|
||||||
|
|
||||||
|
php:
|
||||||
|
- '7.4'
|
||||||
|
- '7.3'
|
||||||
|
- '7.2'
|
||||||
|
- '7.1'
|
||||||
|
- '7.0'
|
||||||
|
- '5.6'
|
||||||
|
- '5.5'
|
||||||
|
|
||||||
|
before_install:
|
||||||
|
- composer update
|
||||||
|
|
||||||
|
script:
|
||||||
|
- composer validate
|
||||||
|
- vendor/bin/phpunit --version
|
||||||
|
- vendor/bin/phpunit --configuration tests/phpunit.xml
|
||||||
|
|
||||||
|
notifications:
|
||||||
|
email:
|
||||||
|
recipients:
|
||||||
|
- will.knauss@gmail.com
|
||||||
|
on_success: never
|
||||||
|
on_failure: always
|
||||||
261
ChangeLog.txt
261
ChangeLog.txt
@@ -1,10 +1,267 @@
|
|||||||
|
ParseCSV 1.2.0
|
||||||
|
-----------------------------------
|
||||||
|
Date: 07-Jan-2020
|
||||||
|
|
||||||
|
Breaking changes: none
|
||||||
|
|
||||||
|
New features:
|
||||||
|
- Compatible with PHP 7.4. Thanks to @andreybolonin
|
||||||
|
@morrislaptop @martijnengler and @fjf2002.
|
||||||
|
- unparse() now also understands $use_mb_convert_encoding.
|
||||||
|
- Verbal condition operators are now allowed to contain
|
||||||
|
upper case letters, for example:
|
||||||
|
$csv->conditions = 'rating IS GREATER THAN 4';
|
||||||
|
|
||||||
|
Bug fixes:
|
||||||
|
- All filter condition operators containing "is" or "equals"
|
||||||
|
were broken.
|
||||||
|
|
||||||
|
Code quality:
|
||||||
|
- Improved test coverage.
|
||||||
|
-----------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
ParseCSV 1.1.1
|
||||||
|
-----------------------------------
|
||||||
|
Date: 2-Feb-2019
|
||||||
|
|
||||||
|
Breaking changes: none
|
||||||
|
|
||||||
|
New features: none
|
||||||
|
|
||||||
|
Bug fixes:
|
||||||
|
- Function load_data: check length of input, prevents E_NOTICE
|
||||||
|
if too long.
|
||||||
|
- Fixed bugs in unparse().
|
||||||
|
|
||||||
|
Code quality:
|
||||||
|
- Improved test coverage.
|
||||||
|
-----------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
ParseCSV 1.1.0
|
||||||
|
-----------------------------------
|
||||||
|
Date: 9-Aug-2018
|
||||||
|
|
||||||
|
Breaking changes:
|
||||||
|
- Ignore entirely empty lines at the end of files
|
||||||
|
See https://github.com/parsecsv/parsecsv-for-php/pull/142
|
||||||
|
- 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.
|
||||||
|
|
||||||
|
New features:
|
||||||
|
- Added support for Laravel-style collections via the
|
||||||
|
new getCollection() function - see
|
||||||
|
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.
|
||||||
|
|
||||||
|
Bug fixes:
|
||||||
|
- Better support for streams.
|
||||||
|
See https://github.com/parsecsv/parsecsv-for-php/pull/147
|
||||||
|
- Fixed output() with custom header.
|
||||||
|
See https://github.com/parsecsv/parsecsv-for-php/issues/132
|
||||||
|
- Fixed bug on _validate_fields_for_unparse() if titles property
|
||||||
|
is used instead of fields parameter for changing the titles for
|
||||||
|
unparsing.
|
||||||
|
- Fixed bug in unparse() that caused incorrect column order
|
||||||
|
(Issue #41).
|
||||||
|
|
||||||
|
|
||||||
|
Code quality:
|
||||||
|
- Improved test coverage.
|
||||||
|
-----------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
ParseCSV 1.0.0
|
||||||
|
-----------------------------------
|
||||||
|
Date: 3-March-2018
|
||||||
|
|
||||||
|
- Renamed class from parseCSV to Csv and added name-
|
||||||
|
space "ParseCsv" for PSR compliance.
|
||||||
|
|
||||||
|
- Added support for MS Excel's "sep=" to detect the
|
||||||
|
delimiter (Issue #60).
|
||||||
|
|
||||||
|
- Added data type detection - function getDatatypes()
|
||||||
|
guesses the type of each column.
|
||||||
|
|
||||||
|
- MIME: output() sends correct MIME type to browser
|
||||||
|
if the separator is a tab char (Issue #79).
|
||||||
|
|
||||||
|
- Added support for mb_convert_encoding() instead of
|
||||||
|
iconv() - see issue #109.
|
||||||
|
|
||||||
|
- A number of minor bug fixes - see GitHub issues.
|
||||||
|
|
||||||
|
- Added many more unit tests.
|
||||||
|
|
||||||
|
-----------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
parseCSV 0.4.3 beta
|
||||||
|
-----------------------------------
|
||||||
|
Date: 1-July-2008
|
||||||
|
|
||||||
|
- Issue #4. Added an option for setting sorting
|
||||||
|
type behavior when sorting data.
|
||||||
|
Simply set $csv->sort_type to "regular", "numeric",
|
||||||
|
or "string".
|
||||||
|
|
||||||
|
- Issue #6. Raw loaded file data is now cleared from
|
||||||
|
file_data property when it has been successfully
|
||||||
|
parsed to keep parseCSV's memory footprint to a
|
||||||
|
minimum. Specifically handy when using multiple
|
||||||
|
instances of parseCSV to process large files.
|
||||||
|
|
||||||
|
-----------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
parseCSV 0.4.2 beta
|
||||||
|
-----------------------------------
|
||||||
|
Date: 31-May-2008
|
||||||
|
|
||||||
|
- IMPORTANT! If you're using the output(),
|
||||||
|
method please note that the first parameter
|
||||||
|
has been completely removed as it was
|
||||||
|
technically just useless. Instead, the second
|
||||||
|
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 :)
|
||||||
|
|
||||||
|
- Small fix to the headers sent by the output()
|
||||||
|
method.
|
||||||
|
|
||||||
|
- Added a download example using the output()
|
||||||
|
method to the examples folder.
|
||||||
|
|
||||||
|
-----------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
parseCSV 0.4.1 beta
|
||||||
|
-----------------------------------
|
||||||
|
Date: 29-May-2008
|
||||||
|
|
||||||
|
- Fixed a small bug in how the output() method
|
||||||
|
handles input data.
|
||||||
|
|
||||||
|
-----------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
parseCSV 0.4 beta
|
||||||
|
-----------------------------------
|
||||||
|
Date: 11-Apr-2008
|
||||||
|
|
||||||
|
- Error reporting for files/data which is corrupt
|
||||||
|
or has formatting errors like using double
|
||||||
|
quotes in a field without enclosing quotes. Or
|
||||||
|
not escaping double quotes with a second one.
|
||||||
|
|
||||||
|
- parse() method does not require input anymore
|
||||||
|
if the "$object->file" property has been set.
|
||||||
|
|
||||||
|
I'm calling this a beta release due to the heavy
|
||||||
|
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.
|
||||||
|
|
||||||
|
The second reason I'm calling it a beta release
|
||||||
|
is cause I'm sure the error reporting code will
|
||||||
|
need more refinements and tweaks to detect more
|
||||||
|
types of errors, as it's only picking two types
|
||||||
|
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.3.2
|
||||||
|
-----------------------------------
|
||||||
|
Date: 1-Apr-2008
|
||||||
|
|
||||||
|
This is primarily a bug-fix release for a critical
|
||||||
|
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
|
||||||
|
situations.
|
||||||
|
|
||||||
|
- Fixed a small code error which would cause PHP to
|
||||||
|
generate a invalid offset notice when zero length
|
||||||
|
values were fed into the unparse() method to
|
||||||
|
generate CSV data from an array.
|
||||||
|
|
||||||
|
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
|
parseCSV 0.2.0 beta
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
Date: 2-Jan-2007
|
Date: 2-Jan-2007
|
||||||
|
|
||||||
- Added auto() function to automatically detect
|
- Added auto() function to automatically detect
|
||||||
delimiter character.
|
delimiter character.
|
||||||
Useful for user upload incase delimiter is
|
Useful for user upload in case delimiter is
|
||||||
comma (,), tab, or semi-colon (;). Some
|
comma (,), tab, or semi-colon (;). Some
|
||||||
versions of MS Excel for Windows use
|
versions of MS Excel for Windows use
|
||||||
semi-colons instead of commas when saving to
|
semi-colons instead of commas when saving to
|
||||||
@@ -15,7 +272,7 @@ Date: 2-Jan-2007
|
|||||||
almost no matter what the delimiter is.
|
almost no matter what the delimiter is.
|
||||||
|
|
||||||
- Generally updated some of the core workings
|
- Generally updated some of the core workings
|
||||||
to increase performance, and offer better
|
to increase performance, and offer better
|
||||||
support for large (1MB and up) files.
|
support for large (1MB and up) files.
|
||||||
|
|
||||||
- Added code examples to header comment.
|
- Added code examples to header comment.
|
||||||
|
|||||||
21
License.txt
Normal file
21
License.txt
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
(The MIT license)
|
||||||
|
|
||||||
|
Copyright (c) 2014 Jim Myhrberg.
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
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
|
||||||
|
SOFTWARE.
|
||||||
15
Makefile
Normal file
15
Makefile
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
COMPOSER_BIN_DIR := vendor/bin
|
||||||
|
PHPUNIT_ARGS = -c tests/phpunit.xml
|
||||||
|
|
||||||
|
test: phpunit-dep
|
||||||
|
${COMPOSER_BIN_DIR}/phpunit ${PHPUNIT_ARGS}
|
||||||
|
|
||||||
|
phpunit-dep:
|
||||||
|
test -f ${COMPOSER_BIN_DIR}/phpunit || ( \
|
||||||
|
echo "phpunit is required to run tests." \
|
||||||
|
"Please run: composer install" >&2 && \
|
||||||
|
exit 1 \
|
||||||
|
)
|
||||||
|
|
||||||
|
.SILENT:
|
||||||
|
.PHONY: test phpunit-dep
|
||||||
233
README.md
Normal file
233
README.md
Normal file
@@ -0,0 +1,233 @@
|
|||||||
|
# ParseCsv
|
||||||
|
[](https://opencollective.com/parsecsv)
|
||||||
|
|
||||||
|
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
|
||||||
|
[Wikipedia article][CSV] (and thus RFC 4180). It has many advanced features which help make your
|
||||||
|
life easier when dealing with CSV data.
|
||||||
|
|
||||||
|
You may not need a library at all: before using ParseCsv, please make sure if PHP's own `str_getcsv()`, ``fgetcvs()`` or `fputcsv()` meets your needs.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
[csv]: http://en.wikipedia.org/wiki/Comma-separated_values
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
* ParseCsv is a complete and fully featured CSV solution for PHP
|
||||||
|
* Supports enclosed values, enclosed commas, double quotes and new lines.
|
||||||
|
* Automatic delimiter character detection.
|
||||||
|
* Sort data by specific fields/columns.
|
||||||
|
* Easy data manipulation.
|
||||||
|
* Basic SQL-like _conditions_, _offset_ and _limit_ options for filtering
|
||||||
|
data.
|
||||||
|
* Error detection for incorrectly formatted input. It attempts to be
|
||||||
|
intelligent, but can not be trusted 100% due to the structure of CSV, and
|
||||||
|
how different programs like Excel for example outputs CSV data.
|
||||||
|
* Support for character encoding conversion using PHP's
|
||||||
|
`iconv()` and `mb_convert_encoding()` functions.
|
||||||
|
* Supports PHP 5.5 and higher.
|
||||||
|
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
|
||||||
|
|
||||||
|
**General**
|
||||||
|
|
||||||
|
```php
|
||||||
|
$csv = new ParseCsv\Csv('data.csv');
|
||||||
|
print_r($csv->data);
|
||||||
|
```
|
||||||
|
|
||||||
|
**Tab delimited, and encoding conversion**
|
||||||
|
|
||||||
|
```php
|
||||||
|
$csv = new ParseCsv\Csv();
|
||||||
|
$csv->encoding('UTF-16', 'UTF-8');
|
||||||
|
$csv->delimiter = "\t";
|
||||||
|
$csv->parse('data.tsv');
|
||||||
|
print_r($csv->data);
|
||||||
|
```
|
||||||
|
|
||||||
|
**Auto-detect delimiter character**
|
||||||
|
|
||||||
|
```php
|
||||||
|
$csv = new ParseCsv\Csv();
|
||||||
|
$csv->auto('data.csv');
|
||||||
|
print_r($csv->data);
|
||||||
|
```
|
||||||
|
|
||||||
|
**Parse data with offset**
|
||||||
|
* ignoring the first X (e.g. two) rows
|
||||||
|
```php
|
||||||
|
$csv = new ParseCsv\Csv();
|
||||||
|
$csv->offset = 2;
|
||||||
|
$csv->parse('data.csv');
|
||||||
|
print_r($csv->data);
|
||||||
|
```
|
||||||
|
|
||||||
|
**Limit the number of returned data rows**
|
||||||
|
```php
|
||||||
|
$csv = new ParseCsv\Csv();
|
||||||
|
$csv->limit = 5;
|
||||||
|
$csv->parse('data.csv');
|
||||||
|
print_r($csv->data);
|
||||||
|
```
|
||||||
|
|
||||||
|
**Get total number of data rows without parsing whole data**
|
||||||
|
* Excluding heading line if present (see $csv->header property)
|
||||||
|
```php
|
||||||
|
$csv = new ParseCsv\Csv();
|
||||||
|
$csv->load_data('data.csv');
|
||||||
|
$count = $csv->getTotalDataRowCount();
|
||||||
|
print_r($count);
|
||||||
|
```
|
||||||
|
|
||||||
|
**Get most common data type for each column (Requires PHP >= 5.5)**
|
||||||
|
|
||||||
|
```php
|
||||||
|
$csv = new ParseCsv\Csv('data.csv');
|
||||||
|
$csv->getDatatypes()
|
||||||
|
print_r($csv->data_types);
|
||||||
|
```
|
||||||
|
|
||||||
|
**Modify data in a CSV file**
|
||||||
|
|
||||||
|
Change data values:
|
||||||
|
```php
|
||||||
|
$csv = new ParseCsv\Csv();
|
||||||
|
$csv->sort_by = 'id';
|
||||||
|
$csv->parse('data.csv');
|
||||||
|
# "4" is the value of the "id" column of the CSV row
|
||||||
|
$csv->data[4] = array('firstname' => 'John', 'lastname' => 'Doe', 'email' => 'john@doe.com');
|
||||||
|
$csv->save();
|
||||||
|
```
|
||||||
|
|
||||||
|
Enclose each data value by quotes:
|
||||||
|
```php
|
||||||
|
$csv = new ParseCsv\Csv();
|
||||||
|
$csv->parse('data.csv');
|
||||||
|
$csv->enclose_all = true;
|
||||||
|
$csv->save();
|
||||||
|
```
|
||||||
|
|
||||||
|
**Replace field names or set ones if missing**
|
||||||
|
|
||||||
|
```php
|
||||||
|
$csv = new ParseCsv\Csv();
|
||||||
|
$csv->fields = ['id', 'name', 'category']
|
||||||
|
$csv->parse('data.csv');
|
||||||
|
```
|
||||||
|
|
||||||
|
**Add row/entry to end of CSV file**
|
||||||
|
|
||||||
|
_Only recommended when you know the exact structure of the file._
|
||||||
|
|
||||||
|
```php
|
||||||
|
$csv = new ParseCsv\Csv();
|
||||||
|
$csv->save('data.csv', array(array('1986', 'Home', 'Nowhere', '')), true);
|
||||||
|
```
|
||||||
|
|
||||||
|
**Convert 2D array to CSV data and send headers to browser to treat output as
|
||||||
|
a file and download it**
|
||||||
|
|
||||||
|
```php
|
||||||
|
$csv = new ParseCsv\Csv();
|
||||||
|
$csv->output('movies.csv', $array, array('field 1', 'field 2'), ',');
|
||||||
|
```
|
||||||
|
|
||||||
|
For more complex examples, see the ``tests`` and `examples` directories.
|
||||||
|
|
||||||
|
## Credits
|
||||||
|
|
||||||
|
* ParseCsv is based on the concept of [Ming Hong Ng][ming]'s [CsvFileParser][]
|
||||||
|
class.
|
||||||
|
|
||||||
|
[ming]: http://minghong.blogspot.com/
|
||||||
|
[CsvFileParser]: http://minghong.blogspot.com/2006/07/csv-parser-for-php.html
|
||||||
|
|
||||||
|
|
||||||
|
## Contributors
|
||||||
|
|
||||||
|
Please find a complete list on the project's [contributors][] page.
|
||||||
|
|
||||||
|
[contributors]: https://github.com/parsecsv/parsecsv-for-php/graphs/contributors
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Contributors
|
||||||
|
|
||||||
|
### Code Contributors
|
||||||
|
|
||||||
|
This project exists thanks to all the people who contribute. [[Contribute](CONTRIBUTING.md)].
|
||||||
|
<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
|
||||||
|
|
||||||
|
(The MIT license)
|
||||||
|
|
||||||
|
Copyright (c) 2014 Jim Myhrberg.
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
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
|
||||||
|
SOFTWARE.
|
||||||
|
|
||||||
|
[](https://travis-ci.org/parsecsv/parsecsv-for-php)
|
||||||
51
composer.json
Normal file
51
composer.json
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
{
|
||||||
|
"name": "parsecsv/php-parsecsv",
|
||||||
|
"description": "CSV data parser for PHP",
|
||||||
|
"license": "MIT",
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Jim Myhrberg",
|
||||||
|
"email": "contact@jimeh.me"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "William Knauss",
|
||||||
|
"email": "will.knauss@gmail.com"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Susann Sgorzaly",
|
||||||
|
"homepage": "https://github.com/susgo"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Christian Bläul",
|
||||||
|
"homepage": "https://github.com/Fonata"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"ParseCsv\\": "src"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"autoload-dev": {
|
||||||
|
"psr-4": {
|
||||||
|
"ParseCsv\\tests\\": "tests"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"php": ">=5.5"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"phpunit/phpunit": "4.1.*"
|
||||||
|
},
|
||||||
|
"suggest": {
|
||||||
|
"illuminate/support": "Fluent array interface for map functions"
|
||||||
|
},
|
||||||
|
"extra": {
|
||||||
|
"branch-alias": {
|
||||||
|
"dev-master": "1.0.x-dev"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"support": {
|
||||||
|
"issues": "https://github.com/parsecsv/parsecsv-for-php/issues",
|
||||||
|
"source": "https://github.com/parsecsv/parsecsv-for-php"
|
||||||
|
}
|
||||||
|
}
|
||||||
15
examples/_books.csv
Normal file
15
examples/_books.csv
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
rating,title,author,type,asin,tags,review
|
||||||
|
0,The Killing Kind,John Connolly,Book,0340771224,,i still haven't had time to read this one...
|
||||||
|
0,The Third Secret,Steve Berry,Book,0340899263,,need to find time to read this book
|
||||||
|
3,The Last Templar,Raymond Khoury,Book,0752880705,,
|
||||||
|
5,The Traveller,John Twelve Hawks,Book,059305430X,,
|
||||||
|
4,Crisis Four,Andy Mcnab,Book,0345428080,,
|
||||||
|
5,Prey,Michael Crichton,Book,0007154534,,
|
||||||
|
3,The Broker (Paperback),John Grisham,Book,0440241588,book johngrisham,"good book, but is slow in the middle"
|
||||||
|
3,Without Blood (Paperback),Alessandro Baricco,Book,1841955744,,
|
||||||
|
5,State of Fear (Paperback),Michael Crichton,Book,0061015733,,
|
||||||
|
4,The Rule of Four (Paperback),Ian Caldwell,Book,0099451956,book bestseller,
|
||||||
|
4,Deception Point (Paperback),Dan Brown,Book,0671027387,book danbrown bestseller,
|
||||||
|
5,Digital Fortress : A Thriller (Mass Market Paperback),Dan Brown,Book,0312995423,book danbrown bestseller,
|
||||||
|
5,Angels & Demons (Mass Market Paperback),Dan Brown,Book,0671027360,book danbrown bestseller,
|
||||||
|
4,The Da Vinci Code (Hardcover),Dan Brown," Book ",0385504209,book movie danbrown bestseller davinci,
|
||||||
|
57
examples/basic.php
Normal file
57
examples/basic.php
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
<pre>
|
||||||
|
<?php
|
||||||
|
|
||||||
|
# include parseCSV class.
|
||||||
|
require __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
|
use ParseCsv\Csv;
|
||||||
|
|
||||||
|
|
||||||
|
# create new parseCSV object.
|
||||||
|
$csv = new Csv();
|
||||||
|
|
||||||
|
|
||||||
|
# Parse '_books.csv' using automatic delimiter detection...
|
||||||
|
$csv->auto('_books.csv');
|
||||||
|
|
||||||
|
# ...or if you know the delimiter, set the delimiter character
|
||||||
|
# if its not the default comma...
|
||||||
|
// $csv->delimiter = "\t"; # tab delimited
|
||||||
|
|
||||||
|
# ...and then use the parse() function.
|
||||||
|
// $csv->parse('_books.csv');
|
||||||
|
|
||||||
|
|
||||||
|
# Output result.
|
||||||
|
// print_r($csv->data);
|
||||||
|
|
||||||
|
|
||||||
|
?>
|
||||||
|
</pre>
|
||||||
|
<style type="text/css" media="screen">
|
||||||
|
table {
|
||||||
|
background-color: #BBB;
|
||||||
|
}
|
||||||
|
|
||||||
|
th {
|
||||||
|
background-color: #EEE;
|
||||||
|
}
|
||||||
|
|
||||||
|
td {
|
||||||
|
background-color: #FFF;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<?php foreach ($csv->titles as $value): ?>
|
||||||
|
<th><?php echo $value; ?></th>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</tr>
|
||||||
|
<?php foreach ($csv->data as $key => $row): ?>
|
||||||
|
<tr>
|
||||||
|
<?php foreach ($row as $value): ?>
|
||||||
|
<td><?php echo $value; ?></td>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</tr>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</table>
|
||||||
59
examples/conditions.php
Normal file
59
examples/conditions.php
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
<pre>
|
||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
# include parseCSV class.
|
||||||
|
require __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
|
use ParseCsv\Csv;
|
||||||
|
|
||||||
|
|
||||||
|
# create new parseCSV object.
|
||||||
|
$csv = new Csv();
|
||||||
|
|
||||||
|
|
||||||
|
# Example conditions:
|
||||||
|
// $csv->conditions = 'title contains paperback OR title contains hardcover';
|
||||||
|
$csv->conditions = 'author does not contain dan brown';
|
||||||
|
// $csv->conditions = 'rating < 4 OR author is John Twelve Hawks';
|
||||||
|
// $csv->conditions = 'rating > 4 AND author is Dan Brown';
|
||||||
|
// $csv->conditions = 'rating is greater than 4';
|
||||||
|
|
||||||
|
|
||||||
|
# Parse '_books.csv' using automatic delimiter detection.
|
||||||
|
$csv->auto('_books.csv');
|
||||||
|
|
||||||
|
|
||||||
|
# Output result.
|
||||||
|
// print_r($csv->data);
|
||||||
|
|
||||||
|
|
||||||
|
?>
|
||||||
|
</pre>
|
||||||
|
<style type="text/css" media="screen">
|
||||||
|
table {
|
||||||
|
background-color: #BBB;
|
||||||
|
}
|
||||||
|
|
||||||
|
th {
|
||||||
|
background-color: #EEE;
|
||||||
|
}
|
||||||
|
|
||||||
|
td {
|
||||||
|
background-color: #FFF;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<?php foreach ($csv->titles as $value): ?>
|
||||||
|
<th><?php echo $value; ?></th>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</tr>
|
||||||
|
<?php foreach ($csv->data as $key => $row): ?>
|
||||||
|
<tr>
|
||||||
|
<?php foreach ($row as $value): ?>
|
||||||
|
<td><?php echo $value; ?></td>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</tr>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</table>
|
||||||
34
examples/download.php
Normal file
34
examples/download.php
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
# include parseCSV class.
|
||||||
|
require __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
|
use ParseCsv\Csv;
|
||||||
|
|
||||||
|
|
||||||
|
# create new parseCSV object.
|
||||||
|
$csv = new Csv();
|
||||||
|
|
||||||
|
|
||||||
|
# Parse '_books.csv' using automatic delimiter detection...
|
||||||
|
$csv->auto('_books.csv');
|
||||||
|
|
||||||
|
# ...or if you know the delimiter, set the delimiter character
|
||||||
|
# if its not the default comma...
|
||||||
|
// $csv->delimiter = "\t"; # tab delimited
|
||||||
|
|
||||||
|
# ...and then use the parse() function.
|
||||||
|
// $csv->parse('_books.csv');
|
||||||
|
|
||||||
|
# now we have data in $csv->data, at which point we can modify
|
||||||
|
# it to our hearts content, like removing the last item...
|
||||||
|
array_pop($csv->data);
|
||||||
|
|
||||||
|
# then we output the file to the browser as a downloadable file...
|
||||||
|
$csv->output('books.csv');
|
||||||
|
# ...when the first parameter is given and is not null, the
|
||||||
|
# output method will itself send the correct headers and the
|
||||||
|
# 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
|
||||||
|
# output data, and will not output to the browser itself.
|
||||||
71
examples/limit.php
Normal file
71
examples/limit.php
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
<pre>
|
||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
# include parseCSV class.
|
||||||
|
require __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
|
use ParseCsv\Csv;
|
||||||
|
|
||||||
|
|
||||||
|
# create new parseCSV object.
|
||||||
|
$csv = new Csv();
|
||||||
|
|
||||||
|
|
||||||
|
# if sorting is enabled, the whole CSV file
|
||||||
|
# will be processed and sorted and then rows
|
||||||
|
# are extracted based on offset and limit.
|
||||||
|
#
|
||||||
|
# if sorting is not enabled, then the least
|
||||||
|
# amount of rows to satisfy offset and limit
|
||||||
|
# settings will be processed. this is useful
|
||||||
|
# with large files when you only need the
|
||||||
|
# first 20 rows for example.
|
||||||
|
$csv->sort_by = 'title';
|
||||||
|
|
||||||
|
|
||||||
|
# offset from the beginning of the file,
|
||||||
|
# ignoring the first X number of rows.
|
||||||
|
$csv->offset = 2;
|
||||||
|
|
||||||
|
# limit the number of returned rows.
|
||||||
|
$csv->limit = 3;
|
||||||
|
|
||||||
|
|
||||||
|
# Parse '_books.csv' using automatic delimiter detection.
|
||||||
|
$csv->auto('_books.csv');
|
||||||
|
|
||||||
|
|
||||||
|
# Output result.
|
||||||
|
// print_r($csv->data);
|
||||||
|
|
||||||
|
|
||||||
|
?>
|
||||||
|
</pre>
|
||||||
|
<style type="text/css" media="screen">
|
||||||
|
table {
|
||||||
|
background-color: #BBB;
|
||||||
|
}
|
||||||
|
|
||||||
|
th {
|
||||||
|
background-color: #EEE;
|
||||||
|
}
|
||||||
|
|
||||||
|
td {
|
||||||
|
background-color: #FFF;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<?php foreach ($csv->titles as $value): ?>
|
||||||
|
<th><?php echo $value; ?></th>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</tr>
|
||||||
|
<?php foreach ($csv->data as $key => $row): ?>
|
||||||
|
<tr>
|
||||||
|
<?php foreach ($row as $value): ?>
|
||||||
|
<td><?php echo $value; ?></td>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</tr>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</table>
|
||||||
27
examples/save_to_file_without_header_row.php
Normal file
27
examples/save_to_file_without_header_row.php
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
# include parseCSV class.
|
||||||
|
require __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
|
use ParseCsv\Csv;
|
||||||
|
|
||||||
|
|
||||||
|
# Create new parseCSV object.
|
||||||
|
$csv = new Csv();
|
||||||
|
|
||||||
|
# When saving, don't write the header row:
|
||||||
|
$csv->heading = false;
|
||||||
|
|
||||||
|
# Specify which columns to write, and in which order.
|
||||||
|
# We won't output the 'Awesome' column this time.
|
||||||
|
$csv->titles = ['Age', 'Name'];
|
||||||
|
|
||||||
|
# Data to write:
|
||||||
|
$csv->data = [
|
||||||
|
0 => ['Name' => 'Anne', 'Age' => 45, 'Awesome' => true],
|
||||||
|
1 => ['Name' => 'John', 'Age' => 44, 'Awesome' => false],
|
||||||
|
];
|
||||||
|
|
||||||
|
# Then we save the file to the file system:
|
||||||
|
$csv->save('people.csv');
|
||||||
574
parsecsv.lib.php
574
parsecsv.lib.php
@@ -1,560 +1,24 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
class parseCSV {
|
// 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.
|
||||||
|
|
||||||
Class: parseCSV v0.2.0 beta
|
// Check if people used Composer to include this project in theirs
|
||||||
http://code.google.com/p/parsecsv-for-php/
|
if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
|
||||||
|
require __DIR__ . '/src/enums/AbstractEnum.php';
|
||||||
Created by Jim Myhrberg (jim@zydev.info).
|
require __DIR__ . '/src/enums/DatatypeEnum.php';
|
||||||
|
require __DIR__ . '/src/enums/FileProcessingModeEnum.php';
|
||||||
Fully conforms to the specifications lined out on wikipedia:
|
require __DIR__ . '/src/enums/SortEnum.php';
|
||||||
- http://en.wikipedia.org/wiki/Comma-separated_values
|
require __DIR__ . '/src/extensions/DatatypeTrait.php';
|
||||||
|
require __DIR__ . '/src/Csv.php';
|
||||||
Based on the concept of this class:
|
} else {
|
||||||
- http://minghong.blogspot.com/2006/07/csv-parser-for-php.html
|
require __DIR__ . '/vendor/autoload.php';
|
||||||
|
|
||||||
|
|
||||||
Code Examples
|
|
||||||
----------------
|
|
||||||
# general usage
|
|
||||||
$csv = new parseCSV('data.csv');
|
|
||||||
print_r($csv->data);
|
|
||||||
----------------
|
|
||||||
# tab delimited, and encoding conversion
|
|
||||||
$csv = new parseCSV();
|
|
||||||
$csv->encoding('UTF-16', 'UTF-8');
|
|
||||||
$csv->delimiter = "\t";
|
|
||||||
$csv->parse('data.tsv');
|
|
||||||
print_r($csv->data);
|
|
||||||
----------------
|
|
||||||
# auto-detect delimiter character
|
|
||||||
$csv = new parseCSV();
|
|
||||||
$csv->auto('data.csv');
|
|
||||||
print_r($csv->data);
|
|
||||||
----------------
|
|
||||||
# modify data in a csv file
|
|
||||||
$csv = new parseCSV();
|
|
||||||
$csv->sort_by = 'id';
|
|
||||||
$csv->parse('data.csv');
|
|
||||||
# "4" is the value of the "id" column of the CSV row
|
|
||||||
$csv->data[4] = array('firstname' => 'John', 'lastname' => 'Doe', 'email' => 'john@doe.com');
|
|
||||||
$csv->save();
|
|
||||||
----------------
|
|
||||||
# add row/entry to end of CSV file
|
|
||||||
# - only recommended when you know the extact sctructure of the file
|
|
||||||
$csv = new parseCSV();
|
|
||||||
$csv->save('data.csv', array('1986', 'Home', 'Nowhere', ''), true);
|
|
||||||
----------------
|
|
||||||
# convert 2D array to csv data and send headers
|
|
||||||
# to browser to treat output as a file and download it
|
|
||||||
$csv = new parseCSV();
|
|
||||||
$csv->output (true, 'movies.csv', $array);
|
|
||||||
----------------
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
----------
|
|
||||||
This program is free software; you can redistributeit and/or modify it
|
|
||||||
under the terms of the GNU General Public License as published by the Free
|
|
||||||
Software Foundation; either version 2 of the License, or (at your option)
|
|
||||||
any later version. http://www.gnu.org/licenses/gpl.txt
|
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
||||||
more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License along
|
|
||||||
with this program; if not, write to the Free Software Foundation, Inc., 59
|
|
||||||
Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
||||||
----------
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Configuration
|
|
||||||
*/
|
|
||||||
|
|
||||||
# use first line/entry as field names
|
|
||||||
var $heading = true;
|
|
||||||
|
|
||||||
# override field names
|
|
||||||
var $fields = array();
|
|
||||||
|
|
||||||
# sort entries by this field
|
|
||||||
var $sort_by = null;
|
|
||||||
var $sort_reverse = false;
|
|
||||||
|
|
||||||
# delimiter (comma) and enclosure (double quote)
|
|
||||||
var $delimiter = ',';
|
|
||||||
var $enclosure = '"';
|
|
||||||
|
|
||||||
# number of rows to analyze when attempting to auto-detect delimiter
|
|
||||||
var $auto_depth = 15;
|
|
||||||
|
|
||||||
# characters to ignore when attempting to auto-detect delimiter
|
|
||||||
var $auto_non_chars = "a-zA-Z0-9\n\r";
|
|
||||||
|
|
||||||
# prefered delimiter characters, only used when all filtering method
|
|
||||||
# returns multiple possible delimiters (happens very rarely)
|
|
||||||
var $auto_prefered = ",;\t.:|";
|
|
||||||
|
|
||||||
# character encoding options
|
|
||||||
var $convert_encoding = false;
|
|
||||||
var $input_encoding = 'ISO-8859-1';
|
|
||||||
var $output_encoding = 'ISO-8859-1';
|
|
||||||
|
|
||||||
# used by unparse(), save(), and output() functions
|
|
||||||
var $linefeed = "\n";
|
|
||||||
|
|
||||||
# only used by output() function
|
|
||||||
var $output_delimiter = ',';
|
|
||||||
var $output_filename = 'data.csv';
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Internal variables
|
|
||||||
*/
|
|
||||||
|
|
||||||
# current file
|
|
||||||
var $file;
|
|
||||||
|
|
||||||
# loaded file contents
|
|
||||||
var $file_data;
|
|
||||||
|
|
||||||
# array of field values in data parsed
|
|
||||||
var $titles = array();
|
|
||||||
|
|
||||||
# two dimentional array of CSV data
|
|
||||||
var $data = array();
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor
|
|
||||||
* @param input CSV file or string
|
|
||||||
* @return nothing
|
|
||||||
*/
|
|
||||||
function parseCSV ($input = null) {
|
|
||||||
if ( !empty($input) ) $this->parse($input);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// ==============================================
|
|
||||||
// ----- [ Main Functions ] ---------------------
|
|
||||||
// ==============================================
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse CSV file or string
|
|
||||||
* @param input CSV file or string
|
|
||||||
* @return nothing
|
|
||||||
*/
|
|
||||||
function parse ($input = null) {
|
|
||||||
if ( !empty($input) ) {
|
|
||||||
if ( is_readable($input) ) {
|
|
||||||
$this->data = $this->parse_file($input);
|
|
||||||
} else {
|
|
||||||
$this->file_data = &$input;
|
|
||||||
$this->data = $this->parse_string();
|
|
||||||
}
|
|
||||||
if ( $this->data === false ) return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Save changes, or new file and/or data
|
|
||||||
* @param file file to save to
|
|
||||||
* @param data 2D array with data
|
|
||||||
* @param append append current data to end of target CSV if exists
|
|
||||||
* @param fields field names
|
|
||||||
* @return true or false
|
|
||||||
*/
|
|
||||||
function save ($file = null, $data = array(), $append = false, $fields = array()) {
|
|
||||||
if ( empty($file) ) $file = &$this->file;
|
|
||||||
$mode = ( $append ) ? 'at' : 'wt' ;
|
|
||||||
$is_php = ( preg_match('/\.php$/i', $file) ) ? true : false ;
|
|
||||||
return $this->wfile($file, $this->unparse($data, $fields, $append, $is_php), $mode);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate CSV based string for output
|
|
||||||
* @param output if true, prints headers and strings to browser
|
|
||||||
* @param filename filename sent to browser in headers if output is true
|
|
||||||
* @param data 2D array with data
|
|
||||||
* @param fields field names
|
|
||||||
* @param delimiter delimiter used to seperate data
|
|
||||||
* @return CSV data using delimiter of choice, or default
|
|
||||||
*/
|
|
||||||
function output ($output = true, $filename = null, $data = array(), $fields = array(), $delimiter = null) {
|
|
||||||
if ( empty($filename) ) $filename = $this->output_filename;
|
|
||||||
if ( $delimiter === null ) $delimiter = $this->output_delimiter;
|
|
||||||
$data = $this->unparse($data, $fields, null, null, $delimiter);
|
|
||||||
if ( $output ) {
|
|
||||||
header('Content-type: application/csv');
|
|
||||||
header('Content-Disposition: inline; filename="'.$filename.'"');
|
|
||||||
echo $data;
|
|
||||||
}
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert character encoding
|
|
||||||
* @param input input character encoding, uses default if left blank
|
|
||||||
* @param output output character encoding, uses default if left blank
|
|
||||||
* @return nothing
|
|
||||||
*/
|
|
||||||
function encoding ($input = null, $output = null) {
|
|
||||||
$this->convert_encoding = true;
|
|
||||||
if ( $input !== null ) $this->input_encoding = $input;
|
|
||||||
if ( $output !== null ) $this->output_encoding = $output;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Auto-Detect Delimiter: Find delimiter by analyzing a specific number of
|
|
||||||
* rows to determin most probable delimiter character
|
|
||||||
* @param file local CSV file
|
|
||||||
* @param parse true/false parse file directly
|
|
||||||
* @param search_depth number of rows to analyze
|
|
||||||
* @param prefered prefered delimiter characters
|
|
||||||
* @param enclosure enclosure character, default is double quote (").
|
|
||||||
* @return delimiter character
|
|
||||||
*/
|
|
||||||
function auto ($file = null, $parse = true, $search_depth = null, $prefered = null, $enclosure = null) {
|
|
||||||
|
|
||||||
if ( $file === null ) $file = $this->file;
|
|
||||||
if ( empty($search_depth) ) $search_depth = $this->auto_depth;
|
|
||||||
if ( $enclosure === null ) $enclosure = $this->enclosure;
|
|
||||||
|
|
||||||
if ( $prefered === null ) $prefered = $this->auto_prefered;
|
|
||||||
|
|
||||||
if ( empty($data) ) {
|
|
||||||
if ( $this->check_data() ) {
|
|
||||||
$data = &$this->file_data;
|
|
||||||
} else return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$chars = array();
|
|
||||||
$strlen = strlen($data);
|
|
||||||
$enclosed = false;
|
|
||||||
$n = 1;
|
|
||||||
$to_end = true;
|
|
||||||
|
|
||||||
// walk specific depth finding posssible delimiter characters
|
|
||||||
for ( $i=0; $i < $strlen; $i++ ) {
|
|
||||||
$ch = $data[$i];
|
|
||||||
$nch = ( isset($data[$i+1]) ) ? $data[$i+1] : false ;
|
|
||||||
$pch = ( isset($data[$i-1]) ) ? $data[$i-1] : false ;
|
|
||||||
|
|
||||||
// open and closing quotes
|
|
||||||
if ( $ch == $enclosure && (!$enclosed || $nch != $enclosure) ) {
|
|
||||||
$enclosed = ( $enclosed ) ? false : true ;
|
|
||||||
|
|
||||||
// inline quotes
|
|
||||||
} elseif ( $ch == $enclosure && $enclosed ) {
|
|
||||||
$i++;
|
|
||||||
|
|
||||||
// end of row
|
|
||||||
} elseif ( ($ch == "\n" && $pch != "\r" || $ch == "\r") && !$enclosed ) {
|
|
||||||
if ( $n >= $search_depth ) {
|
|
||||||
$strlen = 0;
|
|
||||||
$to_end = false;
|
|
||||||
} else {
|
|
||||||
$n++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// count character
|
|
||||||
} elseif (!$enclosed) {
|
|
||||||
if ( !preg_match('/['.preg_quote($this->auto_non_chars, '/').']/i', $ch) ) {
|
|
||||||
if ( !isset($chars[$ch][$n]) ) {
|
|
||||||
$chars[$ch][$n] = 1;
|
|
||||||
} else {
|
|
||||||
$chars[$ch][$n]++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// filtering
|
|
||||||
$depth = ( $to_end ) ? $n-1 : $n ;
|
|
||||||
$filtered = array();
|
|
||||||
foreach( $chars as $char => $value ) {
|
|
||||||
if ( $match = $this->check_count($char, $value, $depth, $prefered) ) {
|
|
||||||
$filtered[$match] = $char;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// capture most probable delimiter
|
|
||||||
ksort($filtered);
|
|
||||||
$delimiter = reset($filtered);
|
|
||||||
$this->delimiter = $delimiter;
|
|
||||||
|
|
||||||
// parse data
|
|
||||||
if ( $parse ) $this->data = $this->parse_string();
|
|
||||||
|
|
||||||
return $delimiter;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// ==============================================
|
|
||||||
// ----- [ Core Functions ] ---------------------
|
|
||||||
// ==============================================
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Load local file or string
|
|
||||||
* @param input local CSV file
|
|
||||||
* @return true or false
|
|
||||||
*/
|
|
||||||
function load_data ($input = null) {
|
|
||||||
$data = null;
|
|
||||||
$file = null;
|
|
||||||
if ( $input === null ) {
|
|
||||||
$file = $this->file;
|
|
||||||
} elseif ( file_exists($input) ) {
|
|
||||||
$file = $input;
|
|
||||||
} else {
|
|
||||||
$data = $input;
|
|
||||||
}
|
|
||||||
if ( !empty($data) || $data = $this->rfile($file) ) {
|
|
||||||
if ( $this->file != $file ) $this->file = $file;
|
|
||||||
if ( preg_match('/\.php$/i', $file) && preg_match('/<\?.*?\?>(.*)/ims', $data, $strip) ) {
|
|
||||||
$data = ltrim($strip[1]);
|
|
||||||
}
|
|
||||||
if ( $this->convert_encoding ) $data = iconv($this->input_encoding, $this->output_encoding, $data);
|
|
||||||
if ( $data[strlen($data)-1] != "\n" ) $data .= "\n";
|
|
||||||
$this->file_data = &$data;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read file to string and call parse_string()
|
|
||||||
* @param file local CSV file
|
|
||||||
* @return 2D array with CSV data, or false on failure
|
|
||||||
*/
|
|
||||||
function parse_file ($file = null) {
|
|
||||||
if ( $file === null ) $file = $this->file;
|
|
||||||
if ( empty($this->file_data) ) $this->load_data($file);
|
|
||||||
return ( !empty($this->file_data) ) ? $this->parse_string() : false ;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse CSV strings to arrays
|
|
||||||
* @param data CSV string
|
|
||||||
* @return 2D array with CSV data, or false on failure
|
|
||||||
*/
|
|
||||||
function parse_string ($data = null) {
|
|
||||||
if ( empty($data) ) {
|
|
||||||
if ( $this->check_data() ) {
|
|
||||||
$data = &$this->file_data;
|
|
||||||
} else return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$rows = array();
|
|
||||||
$row = array();
|
|
||||||
$row_count = 0;
|
|
||||||
$current = '';
|
|
||||||
$head = ( !empty($this->fields) ) ? $this->fields : array() ;
|
|
||||||
$col = 0;
|
|
||||||
$enclosed = false;
|
|
||||||
$strlen = strlen($data);
|
|
||||||
|
|
||||||
// walk through each character
|
|
||||||
for ( $i=0; $i < $strlen; $i++ ) {
|
|
||||||
$ch = $data[$i];
|
|
||||||
$nch = ( isset($data[$i+1]) ) ? $data[$i+1] : false ;
|
|
||||||
$pch = ( isset($data[$i-1]) ) ? $data[$i-1] : false ;
|
|
||||||
|
|
||||||
// open and closing quotes
|
|
||||||
if ( $ch == $this->enclosure && (!$enclosed || $nch != $this->enclosure) ) {
|
|
||||||
$enclosed = ( $enclosed ) ? false : true ;
|
|
||||||
|
|
||||||
// inline quotes
|
|
||||||
} elseif ( $ch == $this->enclosure && $enclosed ) {
|
|
||||||
$current .= $ch;
|
|
||||||
$i++;
|
|
||||||
|
|
||||||
// end of field/row
|
|
||||||
} elseif ( ($ch == $this->delimiter || ($ch == "\n" && $pch != "\r") || $ch == "\r") && !$enclosed ) {
|
|
||||||
$current = trim($current);
|
|
||||||
$key = ( !empty($head[$col]) ) ? $head[$col] : $col ;
|
|
||||||
$row[$key] = $current;
|
|
||||||
$current = '';
|
|
||||||
$col++;
|
|
||||||
|
|
||||||
// end of row
|
|
||||||
if ( $ch == "\n" || $ch == "\r" ) {
|
|
||||||
if ( $this->heading && empty($head) ) {
|
|
||||||
$head = $row;
|
|
||||||
} elseif ( empty($this->fields) || (!empty($this->fields) && (($this->heading && $row_count > 0) || !$this->heading)) ) {
|
|
||||||
if ( !empty($this->sort_by) && !empty($row[$this->sort_by]) ) {
|
|
||||||
if ( isset($rows[$row[$this->sort_by]]) ) {
|
|
||||||
$rows[$row[$this->sort_by].'_0'] = &$rows[$row[$this->sort_by]];
|
|
||||||
unset($rows[$row[$this->sort_by]]);
|
|
||||||
for ( $sn=1; isset($rows[$row[$this->sort_by].'_'.$sn]); $sn++ ) {}
|
|
||||||
$rows[$row[$this->sort_by].'_'.$sn] = $row;
|
|
||||||
} else $rows[$row[$this->sort_by]] = $row;
|
|
||||||
} else $rows[] = $row;
|
|
||||||
}
|
|
||||||
$row = array();
|
|
||||||
$col = 0;
|
|
||||||
$row_count++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// append character to current field
|
|
||||||
} else {
|
|
||||||
$current .= $ch;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$this->titles = $head;
|
|
||||||
if ( !empty($this->sort_by) ) {
|
|
||||||
( $this->sort_reverse ) ? krsort($rows) : ksort($rows) ;
|
|
||||||
}
|
|
||||||
return $rows;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create CSV data from array
|
|
||||||
* @param data 2D array with data
|
|
||||||
* @param fields field names
|
|
||||||
* @param append if true, field names will not be output
|
|
||||||
* @param is_php if a php die() call should be put on the first
|
|
||||||
* line of the file, this is later ignored when read.
|
|
||||||
* @param delimiter field delimiter to use
|
|
||||||
* @return CSV data (text string)
|
|
||||||
*/
|
|
||||||
function unparse ( $data = array(), $fields = array(), $append = false , $is_php = false, $delimiter = null) {
|
|
||||||
if ( !is_array($data) || empty($data) ) $data = &$this->data;
|
|
||||||
if ( !is_array($fields) || empty($fields) ) $fields = &$this->titles;
|
|
||||||
if ( $delimiter === null ) $delimiter = $this->delimiter;
|
|
||||||
|
|
||||||
$string = ( $is_php ) ? "<?php header('Status: 403'); die(' '); ?>".$this->linefeed : '' ;
|
|
||||||
$entry = array();
|
|
||||||
|
|
||||||
// create heading
|
|
||||||
if ( $this->heading && !$append ) {
|
|
||||||
foreach( $fields as $key => $value ) {
|
|
||||||
$entry[] = $this->enclose_value($value);
|
|
||||||
}
|
|
||||||
$string .= implode($delimiter, $entry).$this->linefeed;
|
|
||||||
$entry = array();
|
|
||||||
}
|
|
||||||
|
|
||||||
// create data
|
|
||||||
foreach( $data as $key => $row ) {
|
|
||||||
foreach( $row as $field => $value ) {
|
|
||||||
$entry[] = $this->enclose_value($value);
|
|
||||||
}
|
|
||||||
$string .= implode($delimiter, $entry).$this->linefeed;
|
|
||||||
$entry = array();
|
|
||||||
}
|
|
||||||
|
|
||||||
return $string;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// ==============================================
|
|
||||||
// ----- [ Internal Functions ] -----------------
|
|
||||||
// ==============================================
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Enclose values if needed
|
|
||||||
* - only used by unparse()
|
|
||||||
* @param value string to process
|
|
||||||
* @return Processed value
|
|
||||||
*/
|
|
||||||
function enclose_value ($value = null) {
|
|
||||||
$delimiter = preg_quote($this->delimiter, '/');
|
|
||||||
$enclosure = preg_quote($this->enclosure, '/');
|
|
||||||
if ( preg_match("/".$delimiter."|".$enclosure."|\n|\r/i", $value) ) {
|
|
||||||
$value = str_replace($this->enclosure, $this->enclosure.$this->enclosure, $value);
|
|
||||||
$value = $this->enclosure.$value.$this->enclosure;
|
|
||||||
}
|
|
||||||
return $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check file data
|
|
||||||
* @param file local filename
|
|
||||||
* @return true or false
|
|
||||||
*/
|
|
||||||
function check_data ($file = null) {
|
|
||||||
if ( empty($this->file_data) ) {
|
|
||||||
if ( $file === null ) $file = $this->file;
|
|
||||||
return $this->load_data($file);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if passed info might be delimiter
|
|
||||||
* - only used by find_delimiter()
|
|
||||||
* @return special string used for delimiter selection, or false
|
|
||||||
*/
|
|
||||||
function check_count ($char, $array, $depth, $prefered) {
|
|
||||||
if ( $depth == count($array) ) {
|
|
||||||
$first = null;
|
|
||||||
$equal = null;
|
|
||||||
$almost = false;
|
|
||||||
foreach( $array as $key => $value ) {
|
|
||||||
if ( $first == null ) {
|
|
||||||
$first = $value;
|
|
||||||
} elseif ( $value == $first && $equal !== false) {
|
|
||||||
$equal = true;
|
|
||||||
} elseif ( $value == $first+1 && $equal !== false ) {
|
|
||||||
$equal = true;
|
|
||||||
$almost = true;
|
|
||||||
} else {
|
|
||||||
$equal = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ( $equal ) {
|
|
||||||
$match = ( $almost ) ? 2 : 1 ;
|
|
||||||
$pref = strpos($prefered, $char);
|
|
||||||
$pref = ( $pref !== false ) ? str_pad($pref, 3, '0', STR_PAD_LEFT) : '999' ;
|
|
||||||
return $pref.$match.'.'.(99999 - str_pad($first, 5, '0', STR_PAD_LEFT));
|
|
||||||
} else return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read local file
|
|
||||||
* @param file local filename
|
|
||||||
* @return Data from file, or false on failure
|
|
||||||
*/
|
|
||||||
function rfile ($file = null){
|
|
||||||
if ( is_readable($file) ) {
|
|
||||||
if ( !($fh = fopen($file, 'r')) ) return false;
|
|
||||||
$data = fread($fh, filesize($file));
|
|
||||||
fclose($fh);
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Write to local file
|
|
||||||
* @param file local filename
|
|
||||||
* @param string data to write to file
|
|
||||||
* @param mode fopen() mode
|
|
||||||
* @param lock flock() mode
|
|
||||||
* @return true or false
|
|
||||||
*/
|
|
||||||
function wfile($file, $string = '', $mode = 'wb', $lock = 2){
|
|
||||||
if ( $fp = fopen($file, $mode) ) {
|
|
||||||
flock($fp, $lock);
|
|
||||||
$re = fwrite($fp, $string);
|
|
||||||
$re2 = fclose($fp);
|
|
||||||
if ( $re != false && $re2 != false ) return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
|
||||||
|
// 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 {
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
1399
src/Csv.php
Normal file
1399
src/Csv.php
Normal file
File diff suppressed because it is too large
Load Diff
38
src/enums/AbstractEnum.php
Normal file
38
src/enums/AbstractEnum.php
Normal 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
120
src/enums/DatatypeEnum.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
28
src/enums/FileProcessingModeEnum.php
Normal file
28
src/enums/FileProcessingModeEnum.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ParseCsv\enums;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class FileProcessingEnum
|
||||||
|
*
|
||||||
|
* @package ParseCsv\enums
|
||||||
|
*
|
||||||
|
* todo extends a basic enum class after merging #121
|
||||||
|
*/
|
||||||
|
class FileProcessingModeEnum {
|
||||||
|
|
||||||
|
const __default = self::MODE_FILE_OVERWRITE;
|
||||||
|
|
||||||
|
const MODE_FILE_APPEND = true;
|
||||||
|
|
||||||
|
const MODE_FILE_OVERWRITE = false;
|
||||||
|
|
||||||
|
public static function getAppendMode($mode) {
|
||||||
|
if ($mode == self::MODE_FILE_APPEND) {
|
||||||
|
return 'ab';
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'wb';
|
||||||
|
}
|
||||||
|
}
|
||||||
30
src/enums/SortEnum.php
Normal file
30
src/enums/SortEnum.php
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
<?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];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
103
src/extensions/DatatypeTrait.php
Normal file
103
src/extensions/DatatypeTrait.php
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
<?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;
|
||||||
|
}
|
||||||
|
}
|
||||||
13
tests/Bootstrap.php
Normal file
13
tests/Bootstrap.php
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
chdir(__DIR__ . '/..');
|
||||||
|
if (!file_exists('vendor/autoload.php')) {
|
||||||
|
`composer dump-autoload`;
|
||||||
|
}
|
||||||
|
|
||||||
|
require __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
|
if (!class_exists('PHPUnit\Framework\TestCase') && class_exists('PHPUnit_Framework_TestCase')) {
|
||||||
|
// we run on an older PHPUnit version without namespaces.
|
||||||
|
require_once __DIR__ . '/PHPUnit_Framework_TestCase.inc.php';
|
||||||
|
}
|
||||||
|
|
||||||
11
tests/PHPUnit_Framework_TestCase.inc.php
Normal file
11
tests/PHPUnit_Framework_TestCase.inc.php
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<?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 {
|
||||||
|
|
||||||
|
}
|
||||||
BIN
tests/example_files/Piwik_API_download.csv
Normal file
BIN
tests/example_files/Piwik_API_download.csv
Normal file
Binary file not shown.
|
BIN
tests/example_files/UTF-16LE_with_BOM_and_sep_row.csv
Normal file
BIN
tests/example_files/UTF-16LE_with_BOM_and_sep_row.csv
Normal file
Binary file not shown.
|
5
tests/example_files/UTF-8_sep_row_but_no_BOM.csv
Normal file
5
tests/example_files/UTF-8_sep_row_but_no_BOM.csv
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
sep=;
|
||||||
|
title;isbn;publishedAt
|
||||||
|
Красивая кулинария;5454-5587-3210;21.05.2011
|
||||||
|
The Wine Connoisseurs;2547-8548-2541;12.12.2011
|
||||||
|
Weißwein;1313-4545-8875;23.02.2012
|
||||||
|
5
tests/example_files/UTF-8_with_BOM_and_sep_row.csv
Normal file
5
tests/example_files/UTF-8_with_BOM_and_sep_row.csv
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
sep=;
|
||||||
|
title;isbn;publishedAt
|
||||||
|
Красивая кулинария;5454-5587-3210;21.05.2011
|
||||||
|
The Wine Connoisseurs;2547-8548-2541;12.12.2011
|
||||||
|
Weißwein;1313-4545-8875;23.02.2012
|
||||||
|
22
tests/example_files/multiple_empty_lines.csv
Normal file
22
tests/example_files/multiple_empty_lines.csv
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
ID,dID,createdAt,else1,else2,else3,else4,else5,user,else6,else7,else8,else9,else10,else11,else12,else13,else14,else15,else16,else17,else18,else19,else20,else21,else22,else23,else24,else25,else26,else27,else28,else29,else30,else31,else32,else33,else34,else35,else36,else37,else38
|
||||||
|
418,0,2017-05-16,,,2018-01-22,22.01.2018 10:00:09,,admin,Ja,,10001,Abweichung,,10001,v1,1,ddd,100,1000,,HH,,v1,0,401,1,2,H1,,-1,10,1,111,Ja,2017-01-01,,12,0,11109,HH-100,default
|
||||||
|
419,0,2017-05-16,,,2017-05-16,14.07.2017 09:58:09,,admin,Ja,,,Abweichung,,10002,v2,1,ddd,200,500,DD,DD,,v2,1,402,2,4,H2,,-2,100,1,1111,Ja,2017-01-01,1,13,1,11109,DD-200,default
|
||||||
|
438,0,2017-05-16,,,2017-05-16,14.07.2017 09:58:29,,admin,Ja,,10021,Abweichung,,10021,v3,4,ddd,300,400,DD,DD,,v3,0,421,1,,H3,,-1,106,1,111,Ja,2017-05-08,,2,1,11109,DD-300,default
|
||||||
|
440,0,2017-05-16,,,,14.07.2017 09:58:53,,admin,Ja,,,Alt,,10023,v4,3,,400,500,BE,DD,,v4,1,423,3,,H4,,-3,143,1,1111,Ja,2017-01-01,1,33,1,11108,BE-400,default
|
||||||
|
441,0,2017-05-16,,,,14.07.2017 09:59:19,,admin,Ja,,,Fehlt,,10024,v5,3,,500,,BE,,,v5,2,424,4,,H5,,0,1435,0,111,Ja,2017-01-01,,12,1,0,BE-500,default
|
||||||
|
442,0,2017-05-16,,,,14.07.2017 10:00:46,,admin,Ja,,,Neu,,10025,v6,3,,100,,DD,,,v6,435,425,1,,H6,,0,10,0,1111,Ja,2017-01-01,,102,1,0,DD-100,default
|
||||||
|
443,0,2017-05-16,,,2017-07-04,14.07.2017 10:01:12,,admin,Ja,,,OK,,10026,v7,3,,200,200,DD,DD,,v7,32,426,2,2,H7,,0,100,0,111,Ja,2017-01-01,,77,1,0,DD-200,default
|
||||||
|
444,0,2017-05-16,,,,14.07.2017 10:02:13,,admin,Ja,,,Fehlt,,10027,v8,3,,300,,BE,,,v8,45,427,3,,H8,,0,200,0,1111,Ja,2017-01-01,,44,3,0,BE-300,default
|
||||||
|
445,0,2017-05-16,,,,14.07.2017 10:02:38,,admin,Ja,,,Fehlt,,10028,v9,3,,100,,BE,,,v9,45,428,4,,H9,,0,400,0,111,Ja,2017-01-01,,44,1,0,BE-100,default
|
||||||
|
446,0,2017-05-16,,,,14.07.2017 10:03:01,,admin,Ja,,,Fehlt,,10029,v10,3,,,400,,DD,,v10,45,429,,,H10,,0,1124,0,1111,Ja,2017-01-01,,89,1,0,,default
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
5
tests/example_files/single_column.csv
Normal file
5
tests/example_files/single_column.csv
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
SMS
|
||||||
|
0444
|
||||||
|
5555
|
||||||
|
6606
|
||||||
|
7777
|
||||||
|
6
tests/example_files/waiver_field_separator.csv
Normal file
6
tests/example_files/waiver_field_separator.csv
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
keyword
|
||||||
|
liability waiver
|
||||||
|
release of liability form
|
||||||
|
release of liability
|
||||||
|
sample waiver
|
||||||
|
sample waiver form
|
||||||
|
70
tests/methods/ConstructTest.php
Normal file
70
tests/methods/ConstructTest.php
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ParseCsv\tests\methods;
|
||||||
|
|
||||||
|
use ParseCsv\Csv;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class ConstructTest extends TestCase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Csv object
|
||||||
|
*/
|
||||||
|
protected $csv = null;
|
||||||
|
|
||||||
|
public function test_offset_param() {
|
||||||
|
$offset = 10;
|
||||||
|
$this->csv = new Csv(null, $offset);
|
||||||
|
$this->assertTrue(is_numeric($this->csv->offset));
|
||||||
|
$this->assertEquals($offset, $this->csv->offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_limit_param() {
|
||||||
|
$limit = 10;
|
||||||
|
$this->csv = new Csv(null, null, $limit);
|
||||||
|
$this->assertTrue(is_numeric($this->csv->limit));
|
||||||
|
$this->assertEquals($limit, $this->csv->limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_conditions_param() {
|
||||||
|
$conditions = 'some column NOT value';
|
||||||
|
$this->csv = new Csv(null, null, null, $conditions);
|
||||||
|
$this->assertTrue(is_string($this->csv->conditions));
|
||||||
|
$this->assertEquals($conditions, $this->csv->conditions);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_keep_file_data_param() {
|
||||||
|
$keep = true;
|
||||||
|
$this->csv = new Csv(null, null, null, null, $keep);
|
||||||
|
$this->assertTrue(is_bool($this->csv->keep_file_data));
|
||||||
|
$this->assertEquals($keep, $this->csv->keep_file_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_input_param() {
|
||||||
|
$csv = "col1,col2,col3\r\nval1,val2,val3\r\nval1A,val2A,val3A\r\n";
|
||||||
|
$this->csv = new Csv($csv, null, null, null, true);
|
||||||
|
$this->assertTrue(is_string($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('..');
|
||||||
|
}
|
||||||
|
}
|
||||||
82
tests/methods/DataRowCountTest.php
Normal file
82
tests/methods/DataRowCountTest.php
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ParseCsv\tests\methods;
|
||||||
|
|
||||||
|
use ParseCsv\Csv;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
|
||||||
|
class DataRowCountTest extends TestCase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CSV
|
||||||
|
* The CSV object
|
||||||
|
*
|
||||||
|
* @access protected
|
||||||
|
* @var Csv
|
||||||
|
*/
|
||||||
|
protected $csv;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup
|
||||||
|
* Setup our test environment objects
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
public function setUp() {
|
||||||
|
$this->csv = new Csv();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function countRowsProvider() {
|
||||||
|
return [
|
||||||
|
'auto-double-enclosure' => [
|
||||||
|
'auto-double-enclosure.csv',
|
||||||
|
2,
|
||||||
|
],
|
||||||
|
'auto-single-enclosure' => [
|
||||||
|
'auto-single-enclosure.csv',
|
||||||
|
2,
|
||||||
|
],
|
||||||
|
'UTF-8_sep_row' => [
|
||||||
|
'datatype.csv',
|
||||||
|
3,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider countRowsProvider
|
||||||
|
*
|
||||||
|
* @param string $file
|
||||||
|
* @param int $expectedRows
|
||||||
|
*/
|
||||||
|
public function testGetTotalRowCountFromFile($file, $expectedRows) {
|
||||||
|
$this->csv->heading = true;
|
||||||
|
$this->csv->load_data(__DIR__ . '/fixtures/' . $file);
|
||||||
|
$this->assertEquals($expectedRows, $this->csv->getTotalDataRowCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetTotalRowCountMissingEndingLineBreak() {
|
||||||
|
$this->csv->heading = false;
|
||||||
|
$this->csv->enclosure = '"';
|
||||||
|
$sInput = "86545235689,a\r\n34365587654,b\r\n13469874576,\"c\r\nd\"";
|
||||||
|
$this->csv->load_data($sInput);
|
||||||
|
$this->assertEquals(3, $this->csv->getTotalDataRowCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetTotalRowCountSingleEnclosure() {
|
||||||
|
$this->csv->heading = false;
|
||||||
|
$this->csv->enclosure = "'";
|
||||||
|
$sInput = "86545235689,a\r\n34365587654,b\r\n13469874576,\'c\r\nd\'";
|
||||||
|
$this->csv->load_data($sInput);
|
||||||
|
$this->assertEquals(3, $this->csv->getTotalDataRowCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetTotalRowCountSingleRow() {
|
||||||
|
$this->csv->heading = false;
|
||||||
|
$this->csv->enclosure = "'";
|
||||||
|
$sInput = "86545235689";
|
||||||
|
$this->csv->load_data($sInput);
|
||||||
|
$this->assertEquals(1, $this->csv->getTotalDataRowCount());
|
||||||
|
}
|
||||||
|
}
|
||||||
69
tests/methods/DatatypeTest.php
Normal file
69
tests/methods/DatatypeTest.php
Normal 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'));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
101
tests/methods/ExampleStream.php
Normal file
101
tests/methods/ExampleStream.php
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
<?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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
13
tests/methods/ObjectThatHasToStringMethod.php
Normal file
13
tests/methods/ObjectThatHasToStringMethod.php
Normal 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';
|
||||||
|
}
|
||||||
|
}
|
||||||
45
tests/methods/OldRequireTest.php
Normal file
45
tests/methods/OldRequireTest.php
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
<?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() {
|
||||||
|
rename('vendor/autoload.php', '__autoload');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function tearDown() {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
21
tests/methods/OutputTest.php
Normal file
21
tests/methods/OutputTest.php
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ParseCsv\tests\methods;
|
||||||
|
|
||||||
|
use ParseCsv\Csv;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class OutputTest extends TestCase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess because download.php uses header()
|
||||||
|
*/
|
||||||
|
public function testOutputWithFourParameters() {
|
||||||
|
$csv = new Csv();
|
||||||
|
$data = [0 => ['a', 'b', 'c'], 1 => ['d', 'e', 'f']];
|
||||||
|
$fields = ['col1', 'col2', 'col3'];
|
||||||
|
$output = $csv->output('test.csv', $data, $fields, ',');
|
||||||
|
$expected = "col1,col2,col3\ra,b,c\rd,e,f\r";
|
||||||
|
self::assertEquals($expected, $output);
|
||||||
|
}
|
||||||
|
}
|
||||||
299
tests/methods/ParseTest.php
Normal file
299
tests/methods/ParseTest.php
Normal file
@@ -0,0 +1,299 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ParseCsv\tests\methods;
|
||||||
|
|
||||||
|
use ParseCsv\Csv;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class ParseTest extends TestCase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Csv object
|
||||||
|
*/
|
||||||
|
protected $csv;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup
|
||||||
|
* Setup our test environment objects
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
public function setUp() {
|
||||||
|
$this->csv = new Csv();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testParse() {
|
||||||
|
// can we trick 'is_readable' into whining? See #67.
|
||||||
|
$this->parseRepetitiveString('c:/looks/like/a/path');
|
||||||
|
$this->parseRepetitiveString('http://looks/like/an/url');
|
||||||
|
}
|
||||||
|
|
||||||
|
private function parseRepetitiveString($content) {
|
||||||
|
$this->csv->delimiter = ';';
|
||||||
|
$this->csv->heading = false;
|
||||||
|
$success = $this->csv->parse(str_repeat($content . ';', 500));
|
||||||
|
$this->assertEquals(true, $success);
|
||||||
|
|
||||||
|
$row = array_pop($this->csv->data);
|
||||||
|
$expected_data = array_fill(0, 500, $content);
|
||||||
|
$expected_data [] = '';
|
||||||
|
$this->assertEquals($expected_data, $row);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testParse
|
||||||
|
*
|
||||||
|
* @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);
|
||||||
|
$this->assertEquals($this->_get_magazines_data(), $this->csv->data);
|
||||||
|
}
|
||||||
|
|
||||||
|
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() {
|
||||||
|
$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");
|
||||||
|
$row = array_pop($this->csv->data);
|
||||||
|
$expected_data = ['URL' => 'http://www.amazon.com/ROX-Ice-Ball-Maker-Original/dp/B00MX59NMQ/ref=sr_1_1?ie=UTF8&qid=1435604374&sr=8-1&keywords=rox,+ice+molds'];
|
||||||
|
$this->assertEquals($expected_data, $row);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAllNumericalCsv() {
|
||||||
|
$this->csv->heading = false;
|
||||||
|
$sInput = "86545235689\r\n34365587654\r\n13469874576";
|
||||||
|
$this->assertEquals(false, $this->csv->auto($sInput));
|
||||||
|
$this->assertEquals(null, $this->csv->delimiter);
|
||||||
|
$expected_data = explode("\r\n", $sInput);
|
||||||
|
$actual_data = array_map('reset', $this->csv->data);
|
||||||
|
$this->assertEquals($expected_data, $actual_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMissingEndingLineBreak() {
|
||||||
|
$this->csv->heading = false;
|
||||||
|
$this->csv->enclosure = '"';
|
||||||
|
$sInput = "86545235689,a\r\n34365587654,b\r\n13469874576,\"c\r\nd\"";
|
||||||
|
$expected_data = [86545235689, 34365587654, 13469874576];
|
||||||
|
|
||||||
|
$actual_data = $this->invokeMethod($this->csv, '_parse_string', array($sInput));
|
||||||
|
$actual_column = array_map('reset', $actual_data);
|
||||||
|
$this->assertEquals($expected_data, $actual_column);
|
||||||
|
$this->assertEquals([
|
||||||
|
'a',
|
||||||
|
'b',
|
||||||
|
"c\r\nd",
|
||||||
|
], array_map('next', $actual_data));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSingleColumn() {
|
||||||
|
$this->csv->auto(__DIR__ . '/../example_files/single_column.csv');
|
||||||
|
$expected = [
|
||||||
|
['SMS' => '0444'],
|
||||||
|
['SMS' => '5555'],
|
||||||
|
['SMS' => '6606'],
|
||||||
|
['SMS' => '7777'],
|
||||||
|
];
|
||||||
|
$this->assertEquals($expected, $this->csv->data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMatomoData() {
|
||||||
|
// Matomo (Piwik) export cannot be read with
|
||||||
|
$this->csv->use_mb_convert_encoding = true;
|
||||||
|
$this->csv->output_encoding = 'UTF-8';
|
||||||
|
$this->csv->auto(__DIR__ . '/../example_files/Piwik_API_download.csv');
|
||||||
|
$aAction27 = array_column($this->csv->data, 'url (actionDetails 27)');
|
||||||
|
$this->assertEquals([
|
||||||
|
'http://application/_Main/_GraphicMeanSTD_MDI/btnConfBandOptions',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
], $aAction27);
|
||||||
|
|
||||||
|
$aCity = array_column($this->csv->data, 'city');
|
||||||
|
$this->assertEquals([
|
||||||
|
'São Paulo',
|
||||||
|
'Johannesburg',
|
||||||
|
'',
|
||||||
|
], $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->load_data($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',
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $this->csv->data_types);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testSepRowAutoDetection
|
||||||
|
*/
|
||||||
|
public function testAutoDetectFileHasHeading() {
|
||||||
|
$this->csv->auto(__DIR__ . '/fixtures/datatype.csv');
|
||||||
|
$this->assertTrue($this->csv->autoDetectFileHasHeading());
|
||||||
|
|
||||||
|
$this->csv->heading = false;
|
||||||
|
$this->csv->auto(__DIR__ . '/fixtures/datatype.csv');
|
||||||
|
$this->assertTrue($this->csv->autoDetectFileHasHeading());
|
||||||
|
|
||||||
|
$this->csv->heading = false;
|
||||||
|
$sInput = "86545235689\r\n34365587654\r\n13469874576";
|
||||||
|
$this->csv->auto($sInput);
|
||||||
|
$this->assertFalse($this->csv->autoDetectFileHasHeading());
|
||||||
|
|
||||||
|
$this->csv->heading = true;
|
||||||
|
$sInput = "86545235689\r\n34365587654\r\n13469874576";
|
||||||
|
$this->csv->auto($sInput);
|
||||||
|
$this->assertFalse($this->csv->autoDetectFileHasHeading());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testVeryLongNonExistingFile() {
|
||||||
|
$this->csv->parse(str_repeat('long_string', PHP_MAXPATHLEN));
|
||||||
|
$this->csv->auto(str_repeat('long_string', PHP_MAXPATHLEN));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function _get_magazines_data() {
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
'title' => 'Красивая кулинария',
|
||||||
|
'isbn' => '5454-5587-3210',
|
||||||
|
'publishedAt' => '21.05.2011',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'title' => 'The Wine Connoisseurs',
|
||||||
|
'isbn' => '2547-8548-2541',
|
||||||
|
'publishedAt' => '12.12.2011',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'title' => 'Weißwein',
|
||||||
|
'isbn' => '1313-4545-8875',
|
||||||
|
'publishedAt' => '23.02.2012',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function autoQuotesDataProvider() {
|
||||||
|
return array(
|
||||||
|
array('auto-double-enclosure.csv', '"'),
|
||||||
|
array('auto-single-enclosure.csv', "'"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testSepRowAutoDetection
|
||||||
|
*
|
||||||
|
* @dataProvider autoQuotesDataProvider
|
||||||
|
*
|
||||||
|
* @param string $file
|
||||||
|
* @param string $enclosure
|
||||||
|
*/
|
||||||
|
public function testAutoQuotes($file, $enclosure) {
|
||||||
|
$csv = new Csv();
|
||||||
|
$csv->auto(__DIR__ . '/fixtures/' . $file, true, null, null, $enclosure);
|
||||||
|
$this->assertArrayHasKey('column1', $csv->data[0], 'Data parsed incorrectly with enclosure ' . $enclosure);
|
||||||
|
$this->assertEquals('value1', $csv->data[0]['column1'], 'Data parsed incorrectly with enclosure ' . $enclosure);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Call protected/private method of a class.
|
||||||
|
*
|
||||||
|
* @param object &$object Instantiated object that we will run method on.
|
||||||
|
* @param string $methodName Method name to call
|
||||||
|
* @param array $parameters Array of parameters to pass into method.
|
||||||
|
*
|
||||||
|
* @return mixed Method return.
|
||||||
|
*/
|
||||||
|
private function invokeMethod(&$object, $methodName, array $parameters = array()) {
|
||||||
|
$reflection = new \ReflectionClass(get_class($object));
|
||||||
|
$method = $reflection->getMethod($methodName);
|
||||||
|
$method->setAccessible(true);
|
||||||
|
|
||||||
|
return $method->invokeArgs($object, $parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testWaiverFieldSeparator() {
|
||||||
|
$this->assertSame(false, $this->csv->auto(__DIR__ . '/../example_files/waiver_field_separator.csv'));
|
||||||
|
$expected = [
|
||||||
|
'liability waiver',
|
||||||
|
'release of liability form',
|
||||||
|
'release of liability',
|
||||||
|
'sample waiver',
|
||||||
|
'sample waiver form',
|
||||||
|
];
|
||||||
|
$actual = array_column($this->csv->data, 'keyword');
|
||||||
|
$this->assertSame($expected, $actual);
|
||||||
|
}
|
||||||
|
}
|
||||||
77
tests/methods/SaveTest.php
Normal file
77
tests/methods/SaveTest.php
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ParseCsv\tests\methods;
|
||||||
|
|
||||||
|
use ParseCsv\Csv;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class SaveTest extends TestCase {
|
||||||
|
|
||||||
|
/** @var Csv */
|
||||||
|
private $csv;
|
||||||
|
|
||||||
|
private $temp_filename;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup our test environment objects; will be called before each test.
|
||||||
|
*/
|
||||||
|
public function setUp() {
|
||||||
|
$this->csv = new Csv();
|
||||||
|
$this->csv->auto(__DIR__ . '/../example_files/single_column.csv');
|
||||||
|
|
||||||
|
// Remove last 2 lines to simplify comparison
|
||||||
|
unset($this->csv->data[2], $this->csv->data[3]);
|
||||||
|
|
||||||
|
$temp_dir = str_replace("\\", '/', sys_get_temp_dir());
|
||||||
|
if (substr($temp_dir, -1) != '/') {
|
||||||
|
// From the PHP.net documentation:
|
||||||
|
// This function does not always add trailing slash. This behaviour
|
||||||
|
// is inconsistent across systems.
|
||||||
|
$temp_dir .= '/';
|
||||||
|
}
|
||||||
|
$this->temp_filename = $temp_dir . 'parsecsv_test_file.csv';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSaveWithDefaultSettings() {
|
||||||
|
$expected = "SMS\r0444\r5555\r";
|
||||||
|
$this->saveAndCompare($expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSaveWithDosLineEnding() {
|
||||||
|
$this->csv->linefeed = "\r\n";
|
||||||
|
$expected = "SMS\r\n0444\r\n5555\r\n";
|
||||||
|
$this->saveAndCompare($expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSaveWithUnixLineEnding() {
|
||||||
|
$this->csv->linefeed = "\n";
|
||||||
|
$expected = "SMS\n0444\n5555\n";
|
||||||
|
$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() {
|
||||||
|
$this->csv->linefeed = "\n";
|
||||||
|
$this->csv->heading = false;
|
||||||
|
$expected = "0444\n5555\n";
|
||||||
|
$this->saveAndCompare($expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAllQuotes() {
|
||||||
|
$this->csv->enclose_all = true;
|
||||||
|
$expected = "\"SMS\"\r\"0444\"\r\"5555\"\r";
|
||||||
|
$this->saveAndCompare($expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function saveAndCompare($expected) {
|
||||||
|
$this->csv->save($this->temp_filename);
|
||||||
|
$content = file_get_contents($this->temp_filename);
|
||||||
|
$this->assertEquals($expected, $content);
|
||||||
|
}
|
||||||
|
}
|
||||||
71
tests/methods/StreamTest.php
Normal file
71
tests/methods/StreamTest.php
Normal 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 {
|
||||||
|
|
||||||
|
public function setUp() {
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
108
tests/methods/UnparseTest.php
Normal file
108
tests/methods/UnparseTest.php
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ParseCsv\tests\methods;
|
||||||
|
|
||||||
|
use ParseCsv\Csv;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
|
||||||
|
class UnparseTest extends Testcase {
|
||||||
|
|
||||||
|
/** @var Csv */
|
||||||
|
private $csv;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup our test environment objects; will be called before each test.
|
||||||
|
*/
|
||||||
|
public function setUp() {
|
||||||
|
$this->csv = new Csv();
|
||||||
|
$this->csv->auto(__DIR__ . '/fixtures/auto-double-enclosure.csv');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUnparseWithParameters() {
|
||||||
|
$fields = array('a' => 'AA', 'b' => 'BB');
|
||||||
|
$data = [['a' => 'value1', 'b' => 'value2']];
|
||||||
|
$csv_object = new Csv();
|
||||||
|
$csv_string = $csv_object->unparse($data, $fields);
|
||||||
|
$this->assertEquals("AA,BB\rvalue1,value2\r", $csv_string);
|
||||||
|
|
||||||
|
$csv_object = new Csv();
|
||||||
|
$csv_object->linefeed = "\n";
|
||||||
|
$csv_string = $csv_object->unparse([[55, 66]]);
|
||||||
|
$this->assertEquals("55,66\n", $csv_string);
|
||||||
|
|
||||||
|
$csv_object = new Csv();
|
||||||
|
$data2 = [['a' => "multi\rline", 'b' => 'value2']];
|
||||||
|
$csv_object->enclosure = "'";
|
||||||
|
$csv_string = $csv_object->unparse($data2, $fields);
|
||||||
|
$this->assertEquals("AA,BB\r'multi\rline',value2\r", $csv_string);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUnparseDefault() {
|
||||||
|
$expected = "column1,column2\rvalue1,value2\rvalue3,value4\r";
|
||||||
|
$this->unparseAndCompare($expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUnparseDefaultWithoutHeading() {
|
||||||
|
$this->csv->heading = false;
|
||||||
|
$this->csv->auto(__DIR__ . '/fixtures/auto-double-enclosure.csv');
|
||||||
|
$expected = "column1,column2\rvalue1,value2\rvalue3,value4\r";
|
||||||
|
$this->unparseAndCompare($expected);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUnparseRenameFields() {
|
||||||
|
$expected = "C1,C2\rvalue1,value2\rvalue3,value4\r";
|
||||||
|
$this->unparseAndCompare($expected, array("C1", "C2"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testReorderFields() {
|
||||||
|
$expected = "column2,column1\rvalue2,value1\rvalue4,value3\r";
|
||||||
|
$this->unparseAndCompare($expected, array("column2", "column1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSubsetFields() {
|
||||||
|
$expected = "column1\rvalue1\rvalue3\r";
|
||||||
|
$this->unparseAndCompare($expected, array("column1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testReorderAndRenameFields() {
|
||||||
|
$fields = array(
|
||||||
|
'column2' => 'C2',
|
||||||
|
'column1' => 'C1',
|
||||||
|
);
|
||||||
|
$expected = "C2,C1\rvalue2,value1\rvalue4,value3\r";
|
||||||
|
$this->unparseAndCompare($expected, $fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUnparseDefaultFirstRowMissing() {
|
||||||
|
unset($this->csv->data[0]);
|
||||||
|
$expected = "column1,column2\rvalue3,value4\r";
|
||||||
|
$this->unparseAndCompare($expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUnparseDefaultWithoutData() {
|
||||||
|
unset($this->csv->data[0]);
|
||||||
|
unset($this->csv->data[1]);
|
||||||
|
$expected = "column1,column2\r";
|
||||||
|
$this->unparseAndCompare($expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
3
tests/methods/fixtures/auto-double-enclosure.csv
Normal file
3
tests/methods/fixtures/auto-double-enclosure.csv
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
"column1","column2"
|
||||||
|
"value1","value2"
|
||||||
|
"value3","value4"
|
||||||
|
3
tests/methods/fixtures/auto-single-enclosure.csv
Normal file
3
tests/methods/fixtures/auto-single-enclosure.csv
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
'column1','column2'
|
||||||
|
'value1','value2'
|
||||||
|
'value3','value4'
|
||||||
|
5
tests/methods/fixtures/datatype.csv
Normal file
5
tests/methods/fixtures/datatype.csv
Normal 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
|
||||||
|
18
tests/phpunit.xml
Normal file
18
tests/phpunit.xml
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<phpunit
|
||||||
|
bootstrap="Bootstrap.php"
|
||||||
|
colors="false"
|
||||||
|
convertNoticesToExceptions="true"
|
||||||
|
convertWarningsToExceptions="true"
|
||||||
|
stopOnError="true"
|
||||||
|
stopOnFailure="true"
|
||||||
|
stopOnIncomplete="true"
|
||||||
|
stopOnSkipped="false">
|
||||||
|
<testsuites>
|
||||||
|
<testsuite name="parseCSV Test Suite">
|
||||||
|
<directory suffix="est.php">properties/</directory>
|
||||||
|
<directory suffix="est.php">methods/</directory>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>
|
||||||
|
</phpunit>
|
||||||
33
tests/properties/BaseClass.php
Normal file
33
tests/properties/BaseClass.php
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ParseCsv\tests\properties;
|
||||||
|
|
||||||
|
use ParseCsv\Csv;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class BaseClass extends TestCase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CSV
|
||||||
|
* The parseCSV object
|
||||||
|
*
|
||||||
|
* @var Csv
|
||||||
|
*/
|
||||||
|
protected $csv;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup
|
||||||
|
* Setup our test environment objects
|
||||||
|
*/
|
||||||
|
protected function setUp() {
|
||||||
|
$this->csv = new Csv();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function _compareWithExpected($expected) {
|
||||||
|
$this->csv->auto(__DIR__ . '/../../examples/_books.csv');
|
||||||
|
$actual = array_map(function ($row) {
|
||||||
|
return $row['title'];
|
||||||
|
}, $this->csv->data);
|
||||||
|
$this->assertEquals($expected, array_values($actual));
|
||||||
|
}
|
||||||
|
}
|
||||||
121
tests/properties/ConditionsTest.php
Normal file
121
tests/properties/ConditionsTest.php
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ParseCsv\tests\properties;
|
||||||
|
|
||||||
|
class ConditionsTest extends BaseClass {
|
||||||
|
|
||||||
|
public function testNotDanBrown() {
|
||||||
|
$this->csv->conditions = 'author does not contain dan brown';
|
||||||
|
|
||||||
|
$this->_compareWithExpected([
|
||||||
|
'The Killing Kind',
|
||||||
|
'The Third Secret',
|
||||||
|
'The Last Templar',
|
||||||
|
'The Traveller',
|
||||||
|
'Crisis Four',
|
||||||
|
'Prey',
|
||||||
|
'The Broker (Paperback)',
|
||||||
|
'Without Blood (Paperback)',
|
||||||
|
'State of Fear (Paperback)',
|
||||||
|
'The Rule of Four (Paperback)',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRatingEquals() {
|
||||||
|
$rating_of_3 = [
|
||||||
|
'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 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRatingLessThan() {
|
||||||
|
$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',
|
||||||
|
'Prey',
|
||||||
|
'State of Fear (Paperback)',
|
||||||
|
'Digital Fortress : A Thriller (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() {
|
||||||
|
$this->csv->conditions = 'title contains code OR title contains SECRET';
|
||||||
|
|
||||||
|
$this->_compareWithExpected([
|
||||||
|
'The Third Secret',
|
||||||
|
'The Da Vinci Code (Hardcover)',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
167
tests/properties/DefaultValuesPropertiesTest.php
Normal file
167
tests/properties/DefaultValuesPropertiesTest.php
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
<?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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup
|
||||||
|
* Setup our test environment objects
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
public function setUp() {
|
||||||
|
//setup parse CSV
|
||||||
|
$this->csv = new Csv();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tear down
|
||||||
|
* Tear down our test environment objects
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
public function tearDown() {
|
||||||
|
$this->csv = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_heading_default() {
|
||||||
|
$this->assertTrue(is_bool($this->csv->heading));
|
||||||
|
$this->assertTrue($this->csv->heading);
|
||||||
|
}
|
||||||
|
|
||||||
|
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->assertEquals('regular', $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);
|
||||||
|
}
|
||||||
|
}
|
||||||
79
tests/properties/OffsetTest.php
Normal file
79
tests/properties/OffsetTest.php
Normal 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 integer $offset
|
||||||
|
*/
|
||||||
|
public function testOffsetOfOneNoHeader($offset) {
|
||||||
|
$this->csv->offset = $offset;
|
||||||
|
$this->csv->heading = false;
|
||||||
|
$this->csv->auto(__DIR__ . '/../methods/fixtures/datatype.csv');
|
||||||
|
$this->assertCount(4 - $offset, $this->csv->data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDataArrayKeysWhenSettingOffsetWithHeading() {
|
||||||
|
$this->csv->offset = 2;
|
||||||
|
$this->csv->auto(__DIR__ . '/../methods/fixtures/datatype.csv');
|
||||||
|
$expected = [
|
||||||
|
[
|
||||||
|
'title' => 'The Wine Connoisseurs',
|
||||||
|
'isbn' => '2547-8548-2541',
|
||||||
|
'publishedAt' => '12.12.2011',
|
||||||
|
'published' => 'TRUE',
|
||||||
|
'count' => '',
|
||||||
|
'price' => 20.33,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'title' => 'Weißwein',
|
||||||
|
'isbn' => '1313-4545-8875',
|
||||||
|
'publishedAt' => '23.02.2012',
|
||||||
|
'published' => 'false',
|
||||||
|
'count' => 10,
|
||||||
|
'price' => 10,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $this->csv->data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDataArrayKeysWhenSettingOffsetWithoutHeading() {
|
||||||
|
$this->csv->heading = false;
|
||||||
|
$this->csv->offset = 2;
|
||||||
|
$this->csv->auto(__DIR__ . '/../methods/fixtures/datatype.csv');
|
||||||
|
$expected = range(0, 5, 1);
|
||||||
|
|
||||||
|
$this->assertEquals($expected, array_keys($this->csv->data[0]));
|
||||||
|
}
|
||||||
|
}
|
||||||
171
tests/properties/PublicPropertiesTest.php
Normal file
171
tests/properties/PublicPropertiesTest.php
Normal file
@@ -0,0 +1,171 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
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
|
||||||
|
* @var Csv
|
||||||
|
*/
|
||||||
|
protected $csv = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reflection Object
|
||||||
|
* The reflection class object
|
||||||
|
*
|
||||||
|
* @access protected
|
||||||
|
* @var \ReflectionClass
|
||||||
|
*/
|
||||||
|
protected $reflection = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reflection Properties
|
||||||
|
* The reflected class properties
|
||||||
|
*
|
||||||
|
* @var \ReflectionProperty[]
|
||||||
|
* @access protected
|
||||||
|
*/
|
||||||
|
protected $properties = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup
|
||||||
|
* Setup our test environment objects
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
public function setUp() {
|
||||||
|
//setup parse CSV
|
||||||
|
$this->csv = new Csv();
|
||||||
|
|
||||||
|
//setup the reflection class
|
||||||
|
$this->reflection = new \ReflectionClass($this->csv);
|
||||||
|
|
||||||
|
//setup the reflected class properties
|
||||||
|
$this->properties = $this->reflection->getProperties();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tear down
|
||||||
|
* Tear down our test environment objects
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
public function tearDown() {
|
||||||
|
$this->csv = null;
|
||||||
|
$this->reflection = null;
|
||||||
|
$this->properties = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test_propertiesCount
|
||||||
|
* Counts the number of properties to make sure we didn't add or
|
||||||
|
* subtract any without thinking
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
public function test_propertiesCount() {
|
||||||
|
$this->assertCount(29, $this->properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test_property_names
|
||||||
|
* We have an expected set of properties that should exists
|
||||||
|
* Make sure our expected number of properties matches the real
|
||||||
|
* count of properties and also check to make sure our expected
|
||||||
|
* properties exists within the class
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
public function test_property_names() {
|
||||||
|
//set our expected properties name(s)
|
||||||
|
$expected_names = array(
|
||||||
|
'heading',
|
||||||
|
'fields',
|
||||||
|
'sort_by',
|
||||||
|
'sort_reverse',
|
||||||
|
'sort_type',
|
||||||
|
'delimiter',
|
||||||
|
'enclosure',
|
||||||
|
'enclose_all',
|
||||||
|
'conditions',
|
||||||
|
'offset',
|
||||||
|
'limit',
|
||||||
|
'auto_depth',
|
||||||
|
'auto_non_chars',
|
||||||
|
'auto_preferred',
|
||||||
|
'convert_encoding',
|
||||||
|
'input_encoding',
|
||||||
|
'output_encoding',
|
||||||
|
'use_mb_convert_encoding',
|
||||||
|
'linefeed',
|
||||||
|
'output_delimiter',
|
||||||
|
'output_filename',
|
||||||
|
'keep_file_data',
|
||||||
|
'file',
|
||||||
|
'file_data',
|
||||||
|
'error',
|
||||||
|
'error_info',
|
||||||
|
'titles',
|
||||||
|
'data',
|
||||||
|
'data_types',
|
||||||
|
);
|
||||||
|
|
||||||
|
// Find our real properties
|
||||||
|
$real_properties = array_map(function (\ReflectionProperty $property) {
|
||||||
|
return $property->getName();
|
||||||
|
}, $this->properties);
|
||||||
|
|
||||||
|
// Lets make sure our expected matches the number of real properties
|
||||||
|
$this->assertEquals($expected_names, $real_properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test_count_public_properties
|
||||||
|
* We at this point only have public properties so
|
||||||
|
* lets verify all properties are public
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
public function test_count_public_properties() {
|
||||||
|
$counter = 0;
|
||||||
|
|
||||||
|
$propertiesCount = count($this->properties);
|
||||||
|
for ($a = 0; $a < $propertiesCount; $a++) {
|
||||||
|
if ($this->properties[$a]->isPublic() === true) {
|
||||||
|
$counter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$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);
|
||||||
|
}
|
||||||
|
}
|
||||||
60
tests/properties/SortByTest.php
Normal file
60
tests/properties/SortByTest.php
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ParseCsv\tests\properties;
|
||||||
|
|
||||||
|
class SortByTest extends BaseClass {
|
||||||
|
|
||||||
|
public function testSortByRating() {
|
||||||
|
$this->csv->sort_by = 'rating';
|
||||||
|
$this->csv->conditions = 'title does not contain Blood';
|
||||||
|
$this->_compareWithExpected([
|
||||||
|
// Rating 0
|
||||||
|
'The Killing Kind',
|
||||||
|
'The Third Secret',
|
||||||
|
|
||||||
|
// Rating 3
|
||||||
|
'The Last Templar',
|
||||||
|
'The Broker (Paperback)',
|
||||||
|
|
||||||
|
// Rating 4
|
||||||
|
'Deception Point (Paperback)',
|
||||||
|
'The Rule of Four (Paperback)',
|
||||||
|
'The Da Vinci Code (Hardcover)',
|
||||||
|
|
||||||
|
// Rating 5
|
||||||
|
'State of Fear (Paperback)',
|
||||||
|
'Prey',
|
||||||
|
'Digital Fortress : A Thriller (Mass Market Paperback)',
|
||||||
|
'Angels & Demons (Mass Market Paperback)',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testReverseSortByRating() {
|
||||||
|
$this->csv->sort_by = 'rating';
|
||||||
|
$this->csv->conditions =
|
||||||
|
'title does not contain Prey AND ' .
|
||||||
|
'title does not contain Fortress AND ' .
|
||||||
|
'title does not contain Blood AND ' .
|
||||||
|
'title does not contain Fear';
|
||||||
|
$this->csv->sort_reverse = true;
|
||||||
|
$this->_compareWithExpected([
|
||||||
|
|
||||||
|
// Rating 5
|
||||||
|
'Angels & Demons (Mass Market Paperback)',
|
||||||
|
'The Traveller',
|
||||||
|
|
||||||
|
// Rating 4
|
||||||
|
'The Da Vinci Code (Hardcover)',
|
||||||
|
'The Rule of Four (Paperback)',
|
||||||
|
'Deception Point (Paperback)',
|
||||||
|
|
||||||
|
// Rating 3
|
||||||
|
'The Broker (Paperback)',
|
||||||
|
'The Last Templar',
|
||||||
|
|
||||||
|
// Rating 0
|
||||||
|
'The Third Secret',
|
||||||
|
'The Killing Kind',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user