Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates, Release 2.0.0 #6

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ indent_size = 4
[*.yml*]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = true
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
vendor/
composer.phar
build/
composer.lock
phpspec.yml
phpunit.xml
.idea
.phpunit.result.cache
16 changes: 11 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@ dist: trusty
language: php

php:
- 5.6
- 7.1
- 7.2
- 7.3
- 7.4
# - hhvm
# - nightly

matrix:
allow_failures:
# - php: hhvm
# - php: nightly
include:
- php: 5.6
- php: 7.3
env:
- COMPOSER_FLAGS="--prefer-lowest --prefer-stable"
- COMPOSER_FLAGS=" --prefer-stable"
- COVERAGE=true
- PHPUNIT_FLAGS="--coverage-clover=coverage.clover"

install:
- travis_retry composer update ${COMPOSER_FLAGS} --prefer-source --no-interaction

before_script:
- export XDEBUG_MODE=coverage

script:
- vendor/bin/phpunit ${PHPUNIT_FLAGS}

Expand Down
20 changes: 14 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@
"docs": "https://portphp.readthedocs.org"
},
"require": {
"portphp/portphp": "^1.2.0"
"php": ">=7.3",
"portphp/portphp": "^2.0.0"
},
"require-dev": {
"phpunit/phpunit": "^9.2.0",
"phpspec/phpspec": "^7.0",
"friends-of-phpspec/phpspec-code-coverage": "^5.0"
},
"autoload": {
"psr-4": {
"Port\\Csv\\": "src/"
}
},
"require-dev": {
"phpunit/phpunit": "^4.0",
"phpspec/phpspec": "^2.1"
},
"autoload-dev": {
"psr-4": {
"Port\\Csv\\Tests\\": "tests/"
Expand All @@ -43,5 +45,11 @@
"branch-alias": {
"dev-master": "2.0.x-dev"
}
}
},
"repositories": [
{
"type": "git",
"url": "https://github.com/klodoma/portphp-portphp"
}
]
}
5 changes: 5 additions & 0 deletions phpspec.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ suites:
library_suite:
namespace: Port\Csv
psr4_prefix: Port\Csv
extensions:
FriendsOfPhpSpec\PhpSpec\CodeCoverage\CodeCoverageExtension: ~
formatter.name: pretty
code_coverage:
format: clover
output: build/phpspec.coverage.xml
15 changes: 13 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,23 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
verbose="true"
>
>
<testsuites>
<testsuite name="portphp/csv">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>

<logging>
<log type="coverage-xml" target="build/phpunit.coverage.xml"/>
<log type="coverage-clover" target="build/coverage.clover.xml"/>
</logging>

</phpunit>
45 changes: 34 additions & 11 deletions src/CsvReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use Port\Exception\DuplicateHeadersException;
use Port\Reader\CountableReader;
use SeekableIterator;
use SplFileObject;

/**
* Reads a CSV file, using as little memory as possible
*
* @author David de Boer <[email protected]>
*/
class CsvReader implements CountableReader, \SeekableIterator
class CsvReader implements CountableReader, SeekableIterator
{
const DUPLICATE_HEADERS_INCREMENT = 1;
const DUPLICATE_HEADERS_MERGE = 2;
Expand All @@ -25,7 +27,7 @@ class CsvReader implements CountableReader, \SeekableIterator
/**
* CSV file
*
* @var \SplFileObject
* @var SplFileObject
*/
protected $file;

Expand Down Expand Up @@ -74,21 +76,21 @@ class CsvReader implements CountableReader, \SeekableIterator
protected $duplicateHeadersFlag;

/**
* @param \SplFileObject $file
* @param SplFileObject $file
* @param string $delimiter
* @param string $enclosure
* @param string $escape
*/
public function __construct(\SplFileObject $file, $delimiter = ',', $enclosure = '"', $escape = '\\')
public function __construct(SplFileObject $file, $delimiter = ',', $enclosure = '"', $escape = '\\')
{
ini_set('auto_detect_line_endings', true);

$this->file = $file;
$this->file->setFlags(
\SplFileObject::READ_CSV |
\SplFileObject::SKIP_EMPTY |
\SplFileObject::READ_AHEAD |
\SplFileObject::DROP_NEW_LINE
SplFileObject::READ_CSV |
SplFileObject::SKIP_EMPTY |
SplFileObject::READ_AHEAD |
SplFileObject::DROP_NEW_LINE
);
$this->file->setCsvControl(
$delimiter,
Expand All @@ -108,12 +110,12 @@ public function current()
{
// If the CSV has no column headers just return the line
if (empty($this->columnHeaders)) {
return $this->file->current();
return $this->getCurrentLine();
}

// Since the CSV has column headers use them to construct an associative array for the columns in this line
do {
$line = $this->file->current();
$line = $this->getCurrentLine();

// In non-strict mode pad/slice the line to match the column headers
if (!$this->isStrict()) {
Expand Down Expand Up @@ -323,12 +325,13 @@ public function setStrict($strict)
protected function readHeaderRow($rowNumber)
{
$this->file->seek($rowNumber);
$headers = $this->file->current();
$headers = $this->getCurrentLine();

// Test for duplicate column headers
$diff = array_diff_assoc($headers, array_unique($headers));
if (count($diff) > 0) {
switch ($this->duplicateHeadersFlag) {
/** @noinspection PhpMissingBreakStatementInspection */
case self::DUPLICATE_HEADERS_INCREMENT:
$headers = $this->incrementHeaders($headers);
// Fall through
Expand Down Expand Up @@ -404,4 +407,24 @@ protected function mergeDuplicates(array $line)

return $values;
}

/**
* Returns the current line from the file pointer.
* If found the BOM is removed
*
* @return array|false|string
*/
protected function getCurrentLine()
{
$key = $this->file->key();
$line = $this->file->current();

//remove the BOM from the first line
if ($key === 0 && array_key_exists(0, $line)) {
$bom = pack('H*', 'EFBBBF');
$line[0] = preg_replace("/^$bom/", '', $line[0]);
}

return $line;
}
}
5 changes: 3 additions & 2 deletions src/CsvReaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Port\Csv;

use Port\Reader\ReaderFactory;
use SplFileObject;

/**
* Factory that creates CsvReaders
Expand Down Expand Up @@ -58,11 +59,11 @@ public function __construct(
}

/**
* @param \SplFileObject $file
* @param SplFileObject $file
*
* @return CsvReader
*/
public function getReader(\SplFileObject $file)
public function getReader(SplFileObject $file)
{
$reader = new CsvReader($file, $this->delimiter, $this->enclosure, $this->escape);

Expand Down
10 changes: 6 additions & 4 deletions tests/CsvReaderFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
<?php

namespace Port\Tests\Csv\Factory;
namespace Port\Csv\Tests;

use PHPUnit\Framework\TestCase;
use Port\Csv\CsvReaderFactory;
use SplFileObject;

class CsvReaderFactoryTest extends \PHPUnit_Framework_TestCase
class CsvReaderFactoryTest extends TestCase
{
public function testGetReader()
{
$factory = new CsvReaderFactory();
$reader = $factory->getReader(new \SplFileObject(__DIR__.'/fixtures/data_column_headers.csv'));
$reader = $factory->getReader(new SplFileObject(__DIR__.'/fixtures/data_column_headers.csv'));

$this->assertInstanceOf('Port\Csv\CsvReader', $reader);
$this->assertCount(4, $reader);

$factory = new CsvReaderFactory(0);
$reader = $factory->getReader(new \SplFileObject(__DIR__.'/fixtures/data_column_headers.csv'));
$reader = $factory->getReader(new SplFileObject(__DIR__.'/fixtures/data_column_headers.csv'));

$this->assertCount(3, $reader);
}
Expand Down
Loading