Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
5a25d00
refactor: reorganize tests into reader and writer tests
odinuv Apr 16, 2018
fb574c8
refactor: reorganize tests into reader and writer tests
odinuv Apr 16, 2018
9018818
refactor: reorganize tests into reader and writer tests
odinuv Apr 16, 2018
68ab0a6
refactor: split into reader and writer classes
odinuv Apr 17, 2018
ca998d7
refactor: split into CSVReader and CSVWriter
odinuv Apr 23, 2018
19e0c81
refactor: remove lazy initialization
odinuv Apr 23, 2018
1f1589c
tests: fix invalid line break test
odinuv Apr 23, 2018
92f45cb
chore: cs fixes
odinuv Apr 23, 2018
fe2a040
fix: remove + modes
odinuv Apr 26, 2018
3c5325a
fix: use correct defaults
odinuv Apr 26, 2018
2161707
tests: polished
odinuv Apr 26, 2018
4179ac8
tests: polished
odinuv Apr 26, 2018
ca0e242
tests: polished
odinuv Apr 26, 2018
17fc0cc
fix: use correct exception params
odinuv Apr 28, 2018
fda01c0
refactor: remove exception context params, because they are not reall…
odinuv Apr 28, 2018
92e590f
chore: remove unused variable
odinuv Apr 28, 2018
9a107c5
docs: update
odinuv Apr 29, 2018
9d5ca09
refactor: remove dependency on splfileinfo
odinuv May 1, 2018
9387be9
refactor: make fields private
odinuv May 1, 2018
7325572
feat: no hardcoded line break
odinuv May 1, 2018
69c84b8
refactor: remove string error codes and use standard exception
odinuv May 4, 2018
ed78d27
fix: close only owned file pointer
odinuv May 4, 2018
c5b9822
refactor: no need to have adjustable mode since file pointer can now …
odinuv May 4, 2018
d09b013
polished
odinuv May 4, 2018
1661411
rearrange code
odinuv May 4, 2018
e1d5fe7
docs: add example
odinuv May 4, 2018
035e737
refactor: move file handling code to AbstractCsvFile
odinuv May 4, 2018
537dbb0
tests: simplify
odinuv May 4, 2018
7bd505d
chore: cs fixes
odinuv May 4, 2018
f68c799
fix: better error messages when weird characters are sent as delimite…
odinuv May 12, 2018
5b9ccb0
fix: better and more verbose error message on failed write
odinuv May 12, 2018
eb55800
Merge branch 'master' of https://github.com/keboola/php-csv into odin…
odinuv May 13, 2018
572371a
fix: better error message on failed fwrite, also don't spit notice if…
odinuv May 13, 2018
a101d2f
fix: more robust error checking
odinuv May 14, 2018
44a4914
tests: portable error message
odinuv May 14, 2018
bb673a8
tests: more portable error message
odinuv May 14, 2018
0916f78
tests: catch notices
odinuv May 14, 2018
3bc11a7
Merge branch 'master' of https://github.com/keboola/php-csv into odin…
odinuv May 14, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,25 @@

The library provides a simple reader and writer for CSV files according to [RFC4180](https://tools.ietf.org/html/rfc4180).
The library is licensed under the [MIT](https://github.com/keboola/php-csv/blob/master/LICENSE) license. The library provides
a single `CsvFile` class for both reading and writing CSV files. The class is designed to be **immutable** and minimalistic.
classes `CsvReader` and `CsvWriter` for reading and writing CSV files. The classes are designed to be **immutable**
and minimalistic.

## Usage

### Read CSV

```php
$csvFile = new Keboola\Csv\CsvFile(__DIR__ . '/_data/test-input.csv');
$csvFile = new Keboola\Csv\CsvReader(__DIR__ . '/_data/test-input.csv');
foreach($csvFile as $row) {
var_dump($row);
}
```

#### Skip lines
Skip the first two lines:
Skip the first line:

```php
use Keboola\Csv\CsvFile;
$filename = __DIR__ . '/_data/test-input.csv';
$csvFile = new \Keboola\Csv\CsvFile($fileName, CsvFile::DEFAULT_DELIMITER, CsvFile::DEFAULT_ENCLOSURE, CsvFile::DEFAULT_ENCLOSURE, 2)
$csvFile = new \Keboola\Csv\CsvFile($fileName, CsvFile::DEFAULT_DELIMITER, CsvFile::DEFAULT_ENCLOSURE, CsvFile::DEFAULT_ESCAPED_BY, 1)
foreach($csvFile as $row) {
var_dump($row);
}
Expand All @@ -36,15 +35,15 @@ foreach($csvFile as $row) {
### Write CSV

```php
$csvFile = new Keboola\Csv\CsvFile(__DIR__ . '/_data/test-output.csv');
$rows = array(
array(
$csvFile = new Keboola\Csv\CsvWriter(__DIR__ . '/_data/test-output.csv');
$rows = [
[
'col1', 'col2',
),
array(
],
[
'line without enclosure', 'second column',
),
);
],
];

foreach ($rows as $row) {
$csvFile->writeRow($row);
Expand All @@ -69,5 +68,4 @@ composer require keboola/csv
require 'vendor/autoload.php';
```


Read more in [Composer documentation](http://getcomposer.org/doc/01-basic-usage.md)
104 changes: 104 additions & 0 deletions src/AbstractCsvFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Keboola\Csv;

class AbstractCsvFile
{
const DEFAULT_DELIMITER = ',';
const DEFAULT_ENCLOSURE = '"';

/**
* @var string
*/
protected $delimiter;

/**
* @var string
*/
protected $enclosure;

/**
* @var string
*/
protected $lineBreak;


/**
* @param string $delimiter
* @throws InvalidArgumentException
*/
protected function setDelimiter($delimiter)
{
$this->validateDelimiter($delimiter);
$this->delimiter = $delimiter;
}

/**
* @param string $delimiter
* @throws InvalidArgumentException
*/
protected function validateDelimiter($delimiter)
{
if (strlen($delimiter) > 1) {
throw new InvalidArgumentException(
"Delimiter must be a single character. \"$delimiter\" received",
Exception::INVALID_PARAM,
null,
Exception::INVALID_PARAM_STR
);
}

if (strlen($delimiter) == 0) {
throw new InvalidArgumentException(
"Delimiter cannot be empty.",
Exception::INVALID_PARAM,
null,
Exception::INVALID_PARAM_STR
);
}
}

/**
* @param string $enclosure
* @return $this
* @throws InvalidArgumentException
*/
protected function setEnclosure($enclosure)
{
$this->validateEnclosure($enclosure);
$this->enclosure = $enclosure;
return $this;
}

/**
* @param string $enclosure
* @throws InvalidArgumentException
*/
protected function validateEnclosure($enclosure)
{
if (strlen($enclosure) > 1) {
throw new InvalidArgumentException(
"Enclosure must be a single character. \"$enclosure\" received",
Exception::INVALID_PARAM,
null,
Exception::INVALID_PARAM_STR
);
}
}

/**
* @return string
*/
public function getDelimiter()
{
return $this->delimiter;
}

/**
* @return string
*/
public function getEnclosure()
{
return $this->enclosure;
}
}
Loading