Documentation

Reader extends AbstractCsv
in package

A class to manage extracting and filtering a CSV

Tags
since
3.0.0

Table of Contents

BOM_UTF16_BE  = "��"
UTF-16 BE BOM sequence
BOM_UTF16_LE  = "��"
UTF-16 LE BOM sequence
BOM_UTF32_BE  = "��"
UTF-32 BE BOM sequence
BOM_UTF32_LE  = "��"
UTF-32 LE BOM sequence
BOM_UTF8  = ""
UTF-8 BOM sequence
$delimiter  : string
the field delimiter (one character only)
$enclosure  : string
the field enclosure character (one character only)
$escape  : string
the field escape character (one character only)
$input_bom  : string
The Input file BOM character
$input_encoding  : string
Charset Encoding for the CSV
$iterator_filters  : array<string|int, callable>
Callables to filter the iterator
$iterator_limit  : int
iterator maximum length
$iterator_offset  : int
iterator Offset
$iterator_sort_by  : array<string|int, callable>
Callables to sort the iterator
$newline  : string
newline character
$open_mode  : string
The file open mode flag
$output_bom  : string
The Output file BOM character
$path  : StreamIterator|SplFileObject|string
The path
$stream_filter_mode  : mixed
$stream_filters  : array<string|int, mixed>
collection of stream filters
$stream_regex  : string
PHP Stream Filter Regex
$stream_uri  : string
the real path
$strip_bom  : bool
Stripping BOM status
__destruct()  : mixed
The destructor
__toString()  : string
Retrieves the CSV content
addFilter()  : $this
Set the Iterator filter method
addSortBy()  : $this
Set an Iterator sorting callable function
appendStreamFilter()  : $this
append a stream filter
clearStreamFilter()  : $this
Remove all registered stream filter
createFromFileObject()  : static
Return a new {@link AbstractCsv} from a SplFileObject
createFromPath()  : static
Return a new {@link AbstractCsv} from a file path
createFromStream()  : static
Return a new {@link AbstractCsv} from a PHP resource stream or a StreamIterator
createFromString()  : static
Return a new {@link AbstractCsv} from a string
each()  : int
Applies a callback function on the CSV
fetch()  : Iterator
Fetch the next row from a result set
fetchAll()  : array<string|int, mixed>
Returns a sequential array of all CSV lines
fetchAssoc()  : Iterator
Fetch the next row from a result set
fetchColumn()  : Iterator
Returns the next value from a single CSV column
fetchDelimitersOccurrence()  : array<string|int, mixed>
Detect Delimiters occurences in the CSV
fetchOne()  : array<string|int, mixed>
Returns a single row from the CSV
fetchPairs()  : Generator
Fetches the next key-value pairs from a result set (first column is the key, second column is the value).
fetchPairsWithoutDuplicates()  : array<string|int, mixed>
Retrieve CSV data as pairs
getDelimiter()  : string
Returns the current field delimiter
getEnclosure()  : mixed
getEncodingFrom()  : string
Gets the source CSV encoding charset
getEscape()  : string
Returns the current field escape character
getInputBOM()  : mixed
getInputEncoding()  : string
Gets the source CSV encoding charset
getIterator()  : mixed
getNewline()  : string
Returns the current newline sequence characters
getOutputBOM()  : string
Returns the BOM sequence in use on Output methods
getStreamFilterMode()  : int
stream filter mode getter
hasStreamFilter()  : bool
Detect if the stream filter is already present
isActiveStreamFilter()  : bool
Tells whether the stream filter capabilities can be used
jsonSerialize()  : mixed
newReader()  : Reader
Return a new {@link Reader} instance from a {@link AbstractCsv} object
newWriter()  : Writer
Return a new {@link Writer} instance from a {@link AbstractCsv} object
output()  : int
Outputs all data on the CSV file
prependStreamFilter()  : $this
prepend a stream filter
removeStreamFilter()  : $this
Remove a filter from the collection
setDelimiter()  : $this
Sets the field delimiter
setEnclosure()  : $this
Sets the field enclosure
setEncodingFrom()  : static
Sets the CSV encoding charset
setEscape()  : $this
Sets the field escape character
setInputEncoding()  : static
Sets the CSV encoding charset
setLimit()  : $this
Set LimitIterator Count
setNewline()  : static
Sets the newline sequence characters
setOffset()  : $this
Set LimitIterator Offset
setOutputBOM()  : static
Sets the BOM sequence to prepend the CSV on output
setStreamFilterMode()  : $this
stream filter mode Setter
stripBom()  : $this
Stripping BOM setter
toHTML()  : string
Returns a HTML table representation of the CSV Table
toXML()  : DomDocument
Transforms a CSV into a XML
validateString()  : mixed
__construct()  : mixed
Creates a new instance
applyBomStripping()  : Iterator
Remove the BOM sequence from the CSV
applyCallable()  : Iterator
Apply The callable function
applyIteratorFilter()  : Iterator
Filter the Iterator
applyIteratorInterval()  : Iterator
Sort the Iterator
applyIteratorSortBy()  : Iterator
Sort the Iterator
assertStreamable()  : mixed
Check if the trait methods can be used
convertToUtf8()  : Iterator
Convert Csv file into UTF-8
fetchStreamModeAsInt()  : int
Get the stream mode
fpassthru()  : int
Outputs all data from the CSV
getAssocKeys()  : array<string|int, mixed>
Selects the array to be used as key for the fetchAssoc method
getQueryIterator()  : Iterator
Returns the CSV Iterator
getRow()  : array<string|int, mixed>
Returns a single row from the CSV without filtering
getStreamFilterPath()  : string
Return the filter path
getStreamFilterPrefix()  : string
Return PHP stream filter prefix
getStripBomIterator()  : Iterator
Return the Iterator without the BOM sequence
initStreamFilter()  : mixed
Internal path setter
isBomStrippable()  : bool
Tell whether we can strip or not the leading BOM sequence
isValidCsvControls()  : bool
Tell whether the submitted string is a valid CSV Control character
isValidKey()  : bool
Returns whether the submitted value can be used as string
newInstance()  : static
Return a new {@link AbstractCsv} instance from another {@link AbstractCsv} object
sanitizeStreamFilter()  : string
Sanitize the stream filter name
seekRow()  : mixed
Returns the row at a given offset
setIterator()  : StreamIterator|SplFileObject
Set the Inner Iterator
validateInteger()  : mixed
validateKeys()  : array<string|int, mixed>
Validates the array to be used by the fetchAssoc method

Constants

BOM_UTF16_BE

UTF-16 BE BOM sequence

public mixed BOM_UTF16_BE = "��"

BOM_UTF16_LE

UTF-16 LE BOM sequence

public mixed BOM_UTF16_LE = "��"

BOM_UTF32_BE

UTF-32 BE BOM sequence

public mixed BOM_UTF32_BE = "��"

BOM_UTF32_LE

UTF-32 LE BOM sequence

public mixed BOM_UTF32_LE = "��"

BOM_UTF8

UTF-8 BOM sequence

public mixed BOM_UTF8 = ""

Properties

$delimiter

the field delimiter (one character only)

protected string $delimiter = ','

$enclosure

the field enclosure character (one character only)

protected string $enclosure = '"'

$escape

the field escape character (one character only)

protected string $escape = '\'

$input_bom

The Input file BOM character

protected string $input_bom

$input_encoding

Charset Encoding for the CSV

protected string $input_encoding = 'UTF-8'

$iterator_filters

Callables to filter the iterator

protected array<string|int, callable> $iterator_filters = []

$iterator_limit

iterator maximum length

protected int $iterator_limit = -1

$iterator_offset

iterator Offset

protected int $iterator_offset = 0

$iterator_sort_by

Callables to sort the iterator

protected array<string|int, callable> $iterator_sort_by = []

$newline

newline character

protected string $newline = " "

$open_mode

The file open mode flag

protected string $open_mode

$output_bom

The Output file BOM character

protected string $output_bom = ''

$path

The path

protected StreamIterator|SplFileObject|string $path

can be a StreamIterator object, a SplFileObject object or the string path to a file

$stream_filter_mode

protected mixed $stream_filter_mode = STREAM_FILTER_READ
Tags
inheritdoc

$stream_filters

collection of stream filters

protected array<string|int, mixed> $stream_filters = []

$stream_regex

PHP Stream Filter Regex

protected string $stream_regex = ',^ php://filter/ (?P<mode>:?read=|write=)? # The resource open mode (?P<filters>.*?) # The resource registered filters /resource=(?P<resource>.*) # The resource path $,ix'

$stream_uri

the real path

protected string $stream_uri

the real path to the file

$strip_bom

Stripping BOM status

protected bool $strip_bom = false

Methods

__destruct()

The destructor

public __destruct() : mixed
Return values
mixed

__toString()

Retrieves the CSV content

public __toString() : string
Return values
string

addFilter()

Set the Iterator filter method

public addFilter(callable $callable) : $this
Parameters
$callable : callable
Return values
$this

addSortBy()

Set an Iterator sorting callable function

public addSortBy(callable $callable) : $this
Parameters
$callable : callable
Return values
$this

appendStreamFilter()

append a stream filter

public appendStreamFilter(string $filter_name) : $this
Parameters
$filter_name : string

a string or an object that implements the '__toString' method

Return values
$this

clearStreamFilter()

Remove all registered stream filter

public clearStreamFilter() : $this
Return values
$this

createFromFileObject()

Return a new {@link AbstractCsv} from a SplFileObject

public static createFromFileObject(SplFileObject $file) : static
Parameters
$file : SplFileObject
Return values
static

createFromPath()

Return a new {@link AbstractCsv} from a file path

public static createFromPath(mixed $path[, string $open_mode = 'r+' ]) : static
Parameters
$path : mixed

file path

$open_mode : string = 'r+'

the file open mode flag

Tags
throws
InvalidArgumentException

If $path is a SplTempFileObject object

Return values
static

createFromStream()

Return a new {@link AbstractCsv} from a PHP resource stream or a StreamIterator

public static createFromStream(resource $stream) : static
Parameters
$stream : resource
Return values
static

createFromString()

Return a new {@link AbstractCsv} from a string

public static createFromString(string $str) : static

The string must be an object that implements the __toString method, or a string

Parameters
$str : string

the string

Return values
static

each()

Applies a callback function on the CSV

public each(callable $callable) : int

The callback function must return TRUE in order to continue iterating over the iterator.

Parameters
$callable : callable

a callable function to apply to each selected CSV rows

Return values
int

the iteration count

fetch()

Fetch the next row from a result set

public fetch([callable|null $callable = null ]) : Iterator
Parameters
$callable : callable|null = null

a callable function to be applied to each Iterator item

Return values
Iterator

fetchAll()

Returns a sequential array of all CSV lines

public fetchAll([callable|null $callable = null ]) : array<string|int, mixed>

The callable function will be applied to each Iterator item

Parameters
$callable : callable|null = null

a callable function

Return values
array<string|int, mixed>

fetchAssoc()

Fetch the next row from a result set

public fetchAssoc(int|array<string|int, mixed> $offset_or_keys[, callable $callable = null ]) : Iterator

The rows are presented as associated arrays The callable function will be applied to each row

Parameters
$offset_or_keys : int|array<string|int, mixed>

the name for each key member OR the row Index to be used as the associated named keys

$callable : callable = null

A callable to be applied to each of the rows to be returned.

Tags
throws
InvalidArgumentException

If the submitted keys are invalid

Return values
Iterator

fetchColumn()

Returns the next value from a single CSV column

public fetchColumn(int $column_index[, callable|null $callable = null ]) : Iterator

The callable function will be applied to each value to be return

By default if no column index is provided the first column of the CSV is selected

Parameters
$column_index : int

CSV column index

$callable : callable|null = null

A callable to be applied to each of the value to be returned.

Return values
Iterator

fetchDelimitersOccurrence()

Detect Delimiters occurences in the CSV

public fetchDelimitersOccurrence(array<string|int, string> $delimiters[, int $nb_rows = 1 ]) : array<string|int, mixed>

Returns a associative array where each key represents a valid delimiter and each value the number of occurences

Parameters
$delimiters : array<string|int, string>

the delimiters to consider

$nb_rows : int = 1

Detection is made using $nb_rows of the CSV

Return values
array<string|int, mixed>

fetchOne()

Returns a single row from the CSV

public fetchOne(int $offset) : array<string|int, mixed>

By default if no offset is provided the first row of the CSV is selected

Parameters
$offset : int

the CSV row offset

Return values
array<string|int, mixed>

fetchPairs()

Fetches the next key-value pairs from a result set (first column is the key, second column is the value).

public fetchPairs(int $offset_index[, int $value_index = 1 ][, callable|null $callable = null ]) : Generator

By default if no column index is provided:

  • the first CSV column is used to provide the keys
  • the second CSV column is used to provide the value
Parameters
$offset_index : int

The column index to serve as offset

$value_index : int = 1

The column index to serve as value

$callable : callable|null = null

A callable to be applied to each of the rows to be returned.

Return values
Generator

fetchPairsWithoutDuplicates()

Retrieve CSV data as pairs

public fetchPairsWithoutDuplicates(int $offset_index[, int $value_index = 1 ][, callable|null $callable = null ]) : array<string|int, mixed>

DEPRECATION WARNING! This method will be removed in the next major point release

Parameters
$offset_index : int

The column index to serve as offset

$value_index : int = 1

The column index to serve as value

$callable : callable|null = null

A callable to be applied to each of the rows to be returned.

Tags
deprecated

deprecated since version 8.2

see
Reader::fetchPairs

Fetches an associative array of all rows as key-value pairs (first column is the key, second column is the value).

By default if no column index is provided:

  • the first CSV column is used to provide the keys
  • the second CSV column is used to provide the value

If the value from the column key index is duplicated its corresponding value will be overwritten

Return values
array<string|int, mixed>

getDelimiter()

Returns the current field delimiter

public getDelimiter() : string
Return values
string

getEnclosure()

public abstract getEnclosure() : mixed
Tags
inheritdoc
Return values
mixed

getEncodingFrom()

Gets the source CSV encoding charset

public getEncodingFrom() : string

DEPRECATION WARNING! This method will be removed in the next major point release

Tags
deprecated

deprecated since version 8.1

Return values
string

getEscape()

Returns the current field escape character

public getEscape() : string
Return values
string

getInputBOM()

public abstract getInputBOM() : mixed
Tags
inheritdoc
Return values
mixed

getInputEncoding()

Gets the source CSV encoding charset

public getInputEncoding() : string
Return values
string

getIterator()

public abstract getIterator() : mixed
Tags
inheritdoc
Return values
mixed

getNewline()

Returns the current newline sequence characters

public getNewline() : string
Return values
string

getOutputBOM()

Returns the BOM sequence in use on Output methods

public getOutputBOM() : string
Return values
string

getStreamFilterMode()

stream filter mode getter

public getStreamFilterMode() : int
Return values
int

hasStreamFilter()

Detect if the stream filter is already present

public hasStreamFilter(string $filter_name) : bool
Parameters
$filter_name : string
Return values
bool

isActiveStreamFilter()

Tells whether the stream filter capabilities can be used

public isActiveStreamFilter() : bool
Return values
bool

jsonSerialize()

public jsonSerialize() : mixed
Tags
inheritdoc
Return values
mixed

newReader()

Return a new {@link Reader} instance from a {@link AbstractCsv} object

public newReader([string $open_mode = 'r+' ]) : Reader
Parameters
$open_mode : string = 'r+'

the file open mode flag

Return values
Reader

newWriter()

Return a new {@link Writer} instance from a {@link AbstractCsv} object

public newWriter([string $open_mode = 'r+' ]) : Writer
Parameters
$open_mode : string = 'r+'

the file open mode flag

Return values
Writer

output()

Outputs all data on the CSV file

public output([string $filename = null ]) : int
Parameters
$filename : string = null

CSV downloaded name if present adds extra headers

Return values
int

Returns the number of characters read from the handle and passed through to the output.

prependStreamFilter()

prepend a stream filter

public prependStreamFilter(string $filter_name) : $this
Parameters
$filter_name : string

a string or an object that implements the '__toString' method

Return values
$this

removeStreamFilter()

Remove a filter from the collection

public removeStreamFilter(string $filter_name) : $this
Parameters
$filter_name : string
Return values
$this

setDelimiter()

Sets the field delimiter

public setDelimiter(string $delimiter) : $this
Parameters
$delimiter : string
Tags
throws
InvalidArgumentException

If $delimiter is not a single character

Return values
$this

setEnclosure()

Sets the field enclosure

public setEnclosure(string $enclosure) : $this
Parameters
$enclosure : string
Tags
throws
InvalidArgumentException

If $enclosure is not a single character

Return values
$this

setEncodingFrom()

Sets the CSV encoding charset

public setEncodingFrom(string $str) : static

DEPRECATION WARNING! This method will be removed in the next major point release

Parameters
$str : string
Tags
deprecated

deprecated since version 8.1

Return values
static

setEscape()

Sets the field escape character

public setEscape(string $escape) : $this
Parameters
$escape : string
Tags
throws
InvalidArgumentException

If $escape is not a single character

Return values
$this

setInputEncoding()

Sets the CSV encoding charset

public setInputEncoding(string $str) : static
Parameters
$str : string
Return values
static

setLimit()

Set LimitIterator Count

public setLimit([int $limit = -1 ]) : $this
Parameters
$limit : int = -1
Return values
$this

setNewline()

Sets the newline sequence characters

public setNewline(string $newline) : static
Parameters
$newline : string
Return values
static

setOffset()

Set LimitIterator Offset

public setOffset( $offset) : $this
Parameters
$offset :
Return values
$this

setOutputBOM()

Sets the BOM sequence to prepend the CSV on output

public setOutputBOM(string $str) : static
Parameters
$str : string

The BOM sequence

Return values
static

setStreamFilterMode()

stream filter mode Setter

public setStreamFilterMode(int $mode) : $this

Set the new Stream Filter mode and remove all previously attached stream filters

Parameters
$mode : int
Tags
throws
OutOfBoundsException

If the mode is invalid

Return values
$this

stripBom()

Stripping BOM setter

public stripBom(bool $status) : $this
Parameters
$status : bool
Return values
$this

toHTML()

Returns a HTML table representation of the CSV Table

public toHTML([string $class_attr = 'table-csv-data' ]) : string
Parameters
$class_attr : string = 'table-csv-data'

optional classname

Return values
string

toXML()

Transforms a CSV into a XML

public toXML([string $root_name = 'csv' ][, string $row_name = 'row' ][, string $cell_name = 'cell' ]) : DomDocument
Parameters
$root_name : string = 'csv'

XML root node name

$row_name : string = 'row'

XML row node name

$cell_name : string = 'cell'

XML cell node name

Return values
DomDocument

validateString()

public abstract validateString(mixed $str) : mixed
Parameters
$str : mixed
Tags
inheritdoc
Return values
mixed

__construct()

Creates a new instance

protected __construct(StreamIterator|SplFileObject|string $path[, string $open_mode = 'r+' ]) : mixed

The path must be an SplFileInfo object an object that implements the __toString method a path to a file

Parameters
$path : StreamIterator|SplFileObject|string

The file path

$open_mode : string = 'r+'

The file open mode flag

Return values
mixed

applyBomStripping()

Remove the BOM sequence from the CSV

protected applyBomStripping(Iterator $iterator) : Iterator
Parameters
$iterator : Iterator
Return values
Iterator

applyCallable()

Apply The callable function

protected applyCallable(Iterator $iterator[, callable|null $callable = null ]) : Iterator
Parameters
$iterator : Iterator
$callable : callable|null = null
Return values
Iterator

applyIteratorFilter()

Filter the Iterator

protected applyIteratorFilter(Iterator $iterator) : Iterator
Parameters
$iterator : Iterator
Return values
Iterator

applyIteratorInterval()

Sort the Iterator

protected applyIteratorInterval(Iterator $iterator) : Iterator
Parameters
$iterator : Iterator
Return values
Iterator

applyIteratorSortBy()

Sort the Iterator

protected applyIteratorSortBy(Iterator $iterator) : Iterator
Parameters
$iterator : Iterator
Return values
Iterator

assertStreamable()

Check if the trait methods can be used

protected assertStreamable() : mixed
Tags
throws
LogicException

If the API can not be use

Return values
mixed

convertToUtf8()

Convert Csv file into UTF-8

protected convertToUtf8(Iterator $iterator) : Iterator
Parameters
$iterator : Iterator
Return values
Iterator

fetchStreamModeAsInt()

Get the stream mode

protected fetchStreamModeAsInt(string $mode) : int
Parameters
$mode : string
Return values
int

fpassthru()

Outputs all data from the CSV

protected fpassthru() : int
Return values
int

Returns the number of characters read from the handle and passed through to the output.

getAssocKeys()

Selects the array to be used as key for the fetchAssoc method

protected getAssocKeys(int|array<string|int, mixed> $offset_or_keys) : array<string|int, mixed>
Parameters
$offset_or_keys : int|array<string|int, mixed>

the assoc key OR the row Index to be used as the key index

Tags
throws
InvalidArgumentException

If the row index and/or the resulting array is invalid

Return values
array<string|int, mixed>

getQueryIterator()

Returns the CSV Iterator

protected getQueryIterator() : Iterator
Return values
Iterator

getRow()

Returns a single row from the CSV without filtering

protected getRow(int $offset) : array<string|int, mixed>
Parameters
$offset : int
Tags
throws
InvalidArgumentException

If the $offset is not valid or the row does not exist

Return values
array<string|int, mixed>

getStreamFilterPath()

Return the filter path

protected getStreamFilterPath() : string
Return values
string

getStreamFilterPrefix()

Return PHP stream filter prefix

protected getStreamFilterPrefix() : string
Return values
string

getStripBomIterator()

Return the Iterator without the BOM sequence

protected getStripBomIterator(Iterator $iterator) : Iterator
Parameters
$iterator : Iterator
Return values
Iterator

initStreamFilter()

Internal path setter

protected initStreamFilter(StreamIterator|SplFileObject|string $path) : mixed

The path must be an SplFileInfo object an object that implements the __toString method a path to a file

Parameters
$path : StreamIterator|SplFileObject|string

The file path

Return values
mixed

isBomStrippable()

Tell whether we can strip or not the leading BOM sequence

protected isBomStrippable() : bool
Return values
bool

isValidCsvControls()

Tell whether the submitted string is a valid CSV Control character

protected isValidCsvControls(string $str) : bool
Parameters
$str : string

The submitted string

Return values
bool

isValidKey()

Returns whether the submitted value can be used as string

protected isValidKey(mixed $value) : bool
Parameters
$value : mixed
Return values
bool

newInstance()

Return a new {@link AbstractCsv} instance from another {@link AbstractCsv} object

protected newInstance(string $class, string $open_mode) : static
Parameters
$class : string

the class to be instantiated

$open_mode : string

the file open mode flag

Return values
static

sanitizeStreamFilter()

Sanitize the stream filter name

protected sanitizeStreamFilter(string $filter_name) : string
Parameters
$filter_name : string

the stream filter name

Return values
string

seekRow()

Returns the row at a given offset

protected seekRow(int $offset) : mixed
Parameters
$offset : int
Return values
mixed

setIterator()

Set the Inner Iterator

protected setIterator() : StreamIterator|SplFileObject
Return values
StreamIterator|SplFileObject

validateInteger()

protected abstract validateInteger(mixed $int, mixed $minValue, mixed $errorMessage) : mixed
Parameters
$int : mixed
$minValue : mixed
$errorMessage : mixed
Tags
inheritdoc
Return values
mixed

validateKeys()

Validates the array to be used by the fetchAssoc method

protected validateKeys(array<string|int, mixed> $keys) : array<string|int, mixed>
Parameters
$keys : array<string|int, mixed>
Tags
throws
InvalidArgumentException

If the submitted array fails the assertion

Return values
array<string|int, mixed>

Search results