8 Commits
1.1.0 ... 1.1.1

Author SHA1 Message Date
susgo
65566adcd3 Merge pull request #162 from parsecsv/add-save-example
Added example for writing CSV files to better document heading property
2019-02-02 20:40:45 +01:00
Fonata
2d428ffa93 Updated test: no output in save_to_file_without_header_row.php
Thus, effectively, we just test for valid PHP syntax.
2019-02-02 18:47:55 +01:00
Fonata
bc9207de09 Added example for writing CSV files to better document heading property
Fixes #161
2019-02-02 18:39:21 +01:00
susgo
ace09c3c11 Update README.md
Update examples for #155
2018-11-14 18:30:03 +01:00
Susann Sgorzaly
52ad56c66a fixes unparse bug if no data for unparsing remain (comments #150) 2018-11-14 08:26:39 +01:00
Susann Sgorzaly
ab9e8a0af9 fixes unparse bug if array ids doesn't begin on zero (comments#149) 2018-11-14 08:26:39 +01:00
Fonata
8aa61914f7 Function load_data: check length of input, prevents E_NOTICE if too long
Fixes #151
2018-09-25 00:35:03 +02:00
Fonata
3b74f7ce57 Added PHPUnit test to make sure very long text doesn't cause E_NOTICE 2018-09-25 00:27:04 +02:00
6 changed files with 61 additions and 5 deletions

View File

@@ -113,6 +113,7 @@ print_r($csv->data_types);
**Modify data in a CSV file**
Change data values:
```php
$csv = new ParseCsv\Csv();
$csv->sort_by = 'id';
@@ -122,6 +123,14 @@ $csv->data[4] = array('firstname' => 'John', 'lastname' => 'Doe', 'email' => 'jo
$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

View File

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

View File

@@ -846,10 +846,10 @@ class Csv {
}
// this is needed because sometime titles property is overwritten instead of using fields parameter!
$titlesOnParse = !empty($this->data) ? array_keys($this->data[0]) : array();
$titlesOnParse = !empty($this->data) ? array_keys(reset($this->data)) : array();
// both are identical, also in ordering
if (array_values($fields) === array_values($titlesOnParse)) {
// both are identical, also in ordering OR we have no data (only titles)
if (empty($titlesOnParse) || array_values($fields) === array_values($titlesOnParse)) {
return array_combine($fields, $fields);
}
@@ -889,7 +889,7 @@ class Csv {
if (is_null($input)) {
$file = $this->file;
} elseif (file_exists($input)) {
} elseif (\strlen($input) <= PHP_MAXPATHLEN && file_exists($input)) {
$file = $input;
} else {
$data = $input;

View File

@@ -59,7 +59,9 @@ class ConstructTest extends TestCase {
/** @noinspection PhpIncludeInspection */
require $script_file;
$ob_get_clean = ob_get_clean();
if ($script_file != 'download.php') {
$verb = strtok($script_file, '_.');
if (!in_array($verb, ['download', 'save'], true)) {
$this->assertContains('<td>', $ob_get_clean);
}
}

View File

@@ -188,6 +188,11 @@ class ParseTest extends TestCase {
$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 [
[

View File

@@ -75,6 +75,19 @@ class UnparseTest extends Testcase {
$this->unparseAndCompare($expected, $fields);
}
public function testUnparseDefaultFirstRowMissing(){
unset($this->csv->data[0]);
$expected = "column1,column2\rvalue3,value4\r";
$this->unparseAndCompare($expected);
}
public function testUnparseDefaultWithoutData(){
unset($this->csv->data[0]);
unset($this->csv->data[1]);
$expected = "column1,column2\r";
$this->unparseAndCompare($expected);
}
private function unparseAndCompare($expected, $fields = array()) {
$str = $this->csv->unparse($this->csv->data, $fields);
$this->assertEquals($expected, $str);