CsvImportService implements Iterator, SeekableIterator, Countable
Copyright (C) 2012-2014 David de Boer <[email protected]>
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.
Table of Contents
DUPLICATE_HEADERS_INCREMENT | 1 | |
---|---|---|
DUPLICATE_HEADERS_MERGE | 2 | |
$headerRowNumber | Number of the row that contains the column names | int |
$file | CSV file | SplFileObject |
$columnHeaders | Column headers as read from the CSV file | array |
$headersCount | Number of column headers, stored and re-used for performance | int |
$count | Total number of rows in the CSV file | int |
$errors | Faulty CSV rows | array |
$duplicateHeadersFlag | How to handle duplicate headers | int |
__construct() | mixed | |
current() | Return the current row as an array | array |
getColumnHeaders() | Get column headers | array |
setColumnHeaders() | Set column headers | mixed |
setHeaderRowNumber() | Set header row number | bool |
rewind() | Rewind the file pointer | mixed |
count() | {@inheritdoc} | mixed |
next() | {@inheritdoc} | mixed |
valid() | {@inheritdoc} | mixed |
key() | {@inheritdoc} | mixed |
seek() | {@inheritdoc} | mixed |
getFields() | {@inheritdoc} | mixed |
getRow() | Get a row | array |
getErrors() | Get rows that have an invalid number of columns | array |
hasErrors() | Does the reader contain any invalid rows? | bool |
readHeaderRow() | Read header row from CSV file | array |
incrementHeaders() | Add an increment to duplicate headers | array |
mergeDuplicates() | Merges values for duplicate headers into an array | array |
convertEncodingRows() | 行の文字エンコーディングを変換する. | mixed |
Constants
DUPLICATE_HEADERS_INCREMENT
public
mixed
$DUPLICATE_HEADERS_INCREMENT
= 1
DUPLICATE_HEADERS_MERGE
public
mixed
$DUPLICATE_HEADERS_MERGE
= 2
Properties
$headerRowNumber
Number of the row that contains the column names
protected
int
$headerRowNumber
$file
CSV file
protected
SplFileObject
$file
$columnHeaders
Column headers as read from the CSV file
protected
array
$columnHeaders
= []
$headersCount
Number of column headers, stored and re-used for performance
protected
int
$headersCount
$count
Total number of rows in the CSV file
protected
int
$count
$errors
Faulty CSV rows
protected
array
$errors
= []
$duplicateHeadersFlag
How to handle duplicate headers
protected
int
$duplicateHeadersFlag
Methods
__construct()
public
__construct(
$file :
SplFileObject
[, $delimiter :
string
= ',' ]
[, $enclosure :
string
= '"' ]
[, $escape :
string
= '\\' ]
)
: mixed
Parameters
- $file : SplFileObject
- $delimiter : string = ','
- $enclosure : string = '"'
- $escape : string = '\\'
Return values
mixedcurrent()
Return the current row as an array
public
current(
)
: array
If a header row has been set, an associative array will be returned
Return values
arraygetColumnHeaders()
Get column headers
public
getColumnHeaders(
)
: array
Return values
arraysetColumnHeaders()
Set column headers
public
setColumnHeaders(
$columnHeaders :
array
)
: mixed
Parameters
- $columnHeaders : array
Return values
mixedsetHeaderRowNumber()
Set header row number
public
setHeaderRowNumber(
$rowNumber :
int
[, $duplicates :
int
= null ]
)
: bool
Parameters
- $rowNumber : int
Number of the row that contains column header names
- $duplicates : int = null
How to handle duplicates (optional). One of:
- CsvReader::DUPLICATE_HEADERS_INCREMENT; increments duplicates (dup, dup1, dup2 etc.)
- CsvReader::DUPLICATE_HEADERS_MERGE; merges values for duplicate headers into an array (dup => [value1, value2, value3])
Return values
boolrewind()
Rewind the file pointer
public
rewind(
)
: mixed
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.
Return values
mixedcount()
{@inheritdoc}
public
count(
)
: mixed
Return values
mixednext()
{@inheritdoc}
public
next(
)
: mixed
Return values
mixedvalid()
{@inheritdoc}
public
valid(
)
: mixed
Return values
mixedkey()
{@inheritdoc}
public
key(
)
: mixed
Return values
mixedseek()
{@inheritdoc}
public
seek(
$pointer :
mixed
)
: mixed
Parameters
- $pointer : mixed
Return values
mixedgetFields()
{@inheritdoc}
public
getFields(
)
: mixed
Return values
mixedgetRow()
Get a row
public
getRow(
$number :
int
)
: array
Parameters
- $number : int
Row number
Return values
arraygetErrors()
Get rows that have an invalid number of columns
public
getErrors(
)
: array
Return values
arrayhasErrors()
Does the reader contain any invalid rows?
public
hasErrors(
)
: bool
Return values
boolreadHeaderRow()
Read header row from CSV file
protected
readHeaderRow(
$rowNumber :
int
)
: array
Parameters
- $rowNumber : int
Row number
Return values
arrayincrementHeaders()
Add an increment to duplicate headers
protected
incrementHeaders(
$headers :
array
)
: array
So the following line: |duplicate|duplicate|duplicate| |first |second |third |
Yields value: $duplicate => 'first', $duplicate1 => 'second', $duplicate2 => 'third'
Parameters
- $headers : array
Return values
arraymergeDuplicates()
Merges values for duplicate headers into an array
protected
mergeDuplicates(
$line :
array
)
: array
So the following line: |duplicate|duplicate|duplicate| |first |second |third |
Yields value: $duplicate => ['first', 'second', 'third']
Parameters
- $line : array
Return values
arrayconvertEncodingRows()
行の文字エンコーディングを変換する.
protected
convertEncodingRows(
$row :
mixed
)
: mixed
Windows 版 PHP7 環境では、ファイルエンコーディングが CP932 になるため UTF-8 に変換する. それ以外の環境では何もしない。
Parameters
- $row : mixed