class CsvImportService implements Iterator, SeekableIterator, Countable

Copyright (C) 2012-2014 David de Boer david@ddeboer.nl

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.

Constants

DUPLICATE_HEADERS_INCREMENT

DUPLICATE_HEADERS_MERGE

Properties

protected integer $headerRowNumber Number of the row that contains the column names
protected SplFileObject $file CSV file
protected array $columnHeaders Column headers as read from the CSV file
protected integer $headersCount Number of column headers, stored and re-used for performance
protected integer $count Total number of rows in the CSV file
protected array $errors Faulty CSV rows
protected integer $duplicateHeadersFlag How to handle duplicate headers

Methods

__construct(SplFileObject $file, string $delimiter = ',', string $enclosure = '"', string $escape = '\\')

No description

array
current()

Return the current row as an array

array
getColumnHeaders()

Get column headers

setColumnHeaders(array $columnHeaders)

Set column headers

boolean
setHeaderRowNumber(integer $rowNumber, integer $duplicates = null)

Set header row number

rewind()

Rewind the file pointer

count()

{@inheritdoc}

next()

{@inheritdoc}

valid()

{@inheritdoc}

key()

{@inheritdoc}

seek($pointer)

{@inheritdoc}

getFields()

{@inheritdoc}

array
getRow(integer $number)

Get a row

array
getErrors()

Get rows that have an invalid number of columns

boolean
hasErrors()

Does the reader contain any invalid rows?

array
readHeaderRow(integer $rowNumber)

Read header row from CSV file

array
incrementHeaders(array $headers)

Add an increment to duplicate headers

array
mergeDuplicates(array $line)

Merges values for duplicate headers into an array

convertEncodingRows($row)

行の文字エンコーディングを変換する.

Details

at line 114
__construct(SplFileObject $file, string $delimiter = ',', string $enclosure = '"', string $escape = '\\')

Parameters

SplFileObject $file
string $delimiter
string $enclosure
string $escape

at line 139
array current()

Return the current row as an array

If a header row has been set, an associative array will be returned

Return Value

array

at line 175
array getColumnHeaders()

Get column headers

Return Value

array

at line 185
setColumnHeaders(array $columnHeaders)

Set column headers

Parameters

array $columnHeaders

at line 204
boolean setHeaderRowNumber(integer $rowNumber, integer $duplicates = null)

Set header row number

Parameters

integer $rowNumber Number of the row that contains column header names
integer $duplicates How to handle duplicates (optional). One of: - CsvReader::DUPLICATEHEADERSINCREMENT; increments duplicates (dup, dup1, dup2 etc.) - CsvReader::DUPLICATEHEADERSMERGE; merges values for duplicate headers into an array (dup => [value1, value2, value3])

Return Value

boolean

at line 224
rewind()

Rewind the file pointer

If a header row has been set, the pointer is set just below the header row. That way, when you iterate over the rows, that header row is skipped.

at line 235
count()

{@inheritdoc}

at line 251
next()

{@inheritdoc}

at line 259
valid()

{@inheritdoc}

at line 267
key()

{@inheritdoc}

at line 275
seek($pointer)

{@inheritdoc}

Parameters

$pointer

at line 283
getFields()

{@inheritdoc}

at line 295
array getRow(integer $number)

Get a row

Parameters

integer $number Row number

Return Value

array

at line 307
array getErrors()

Get rows that have an invalid number of columns

Return Value

array

at line 323
boolean hasErrors()

Does the reader contain any invalid rows?

Return Value

boolean

at line 336
protected array readHeaderRow(integer $rowNumber)

Read header row from CSV file

Parameters

integer $rowNumber Row number

Return Value

array

at line 358
protected array incrementHeaders(array $headers)

Add an increment to duplicate headers

So the following line: |duplicate|duplicate|duplicate| |first |second |third |

Yields value: $duplicate => 'first', $duplicate1 => 'second', $duplicate2 => 'third'

Parameters

array $headers

Return Value

array

at line 389
protected array mergeDuplicates(array $line)

Merges values for duplicate headers into an array

So the following line: |duplicate|duplicate|duplicate| |first |second |third |

Yields value: $duplicate => ['first', 'second', 'third']

Parameters

array $line

Return Value

array

at line 413
protected convertEncodingRows($row)

行の文字エンコーディングを変換する.

Windows 版 PHP7 環境では、ファイルエンコーディングが CP932 になるため UTF-8 に変換する. それ以外の環境では何もしない。

Parameters

$row