Documentation

AbstractCsv
in package
implements JsonSerializable, IteratorAggregate Uses Controls, Output, QueryFilter, StreamFilter

An abstract class to enable basic CSV manipulation

Tags
since
4.0.0

Interfaces, Classes and Traits

JsonSerializable
IteratorAggregate

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  : int
Stream filtering mode to apply on all filters
$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
fetchDelimitersOccurrence()  : array<string|int, mixed>
Detect Delimiters occurences in the CSV
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()  : StreamIterator|SplFileObject
Returns the inner CSV Document Iterator object
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
__construct()  : mixed
Creates a new instance
applyBomStripping()  : Iterator
Remove the BOM sequence from the CSV
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
getQueryIterator()  : Iterator
Returns the CSV Iterator
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
newInstance()  : static
Return a new {@link AbstractCsv} instance from another {@link AbstractCsv} object
sanitizeStreamFilter()  : string
Sanitize the stream filter name
setIterator()  : StreamIterator|SplFileObject
Set the Inner Iterator
validateInteger()  : mixed
validateString()  : string
validate a string

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

Stream filtering mode to apply on all filters

protected int $stream_filter_mode = STREAM_FILTER_ALL

$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

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>

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()

Returns the inner CSV Document Iterator object

public getIterator() : StreamIterator|SplFileObject
Return values
StreamIterator|SplFileObject

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

__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

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.

getQueryIterator()

Returns the CSV Iterator

protected getQueryIterator() : Iterator
Return values
Iterator

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

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

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

validateString()

validate a string

protected static validateString(mixed $str) : string
Parameters
$str : mixed

the value to evaluate as a string

Tags
throws
InvalidArgumentException

if the submitted data can not be converted to string

Return values
string

Search results