Documentation

Writer extends AbstractCsv
in package
Uses RowFilter

A class to manage data insertion into a CSV

Tags
since
4.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
$csv  : SplFileObject|StreamIterator
The CSV object holder
$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)
$formatters  : array<string|int, callable>
Callables to format the row before insertion
$fputcsv  : ReflectionMethod
fputcsv method from SplFileObject or StreamIterator
$fputcsv_param_count  : int
Nb parameters for SplFileObject::fputcsv method
$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
$validators  : array<string|int, callable>
Callables to validate the row before insertion
__destruct()  : mixed
The destructor
__toString()  : string
Retrieves the CSV content
addFilter()  : $this
Set the Iterator filter method
addFormatter()  : $this
add a formatter to the collection
addSortBy()  : $this
Set an Iterator sorting callable function
addValidator()  : $this
add a Validator to the collection
appendStreamFilter()  : $this
append a stream filter
clearFormatters()  : $this
Remove all registered formatter
clearStreamFilter()  : $this
Remove all registered stream filter
clearValidators()  : $this
Remove all registered validators
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()  : 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
hasFormatter()  : bool
Detect if the formatter is already registered
hasStreamFilter()  : bool
Detect if the stream filter is already present
hasValidator()  : bool
Detect if a validator is already registered
insertAll()  : static
Adds multiple lines to the CSV document
insertOne()  : static
Adds a single line to a CSV document
isActiveStreamFilter()  : mixed
{@inheritdoc}
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
removeFormatter()  : $this
Remove a formatter from the collection
removeStreamFilter()  : $this
Remove a filter from the collection
removeValidator()  : $this
Remove a validator 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
addRow()  : mixed
Add new record to the CSV document
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
formatRow()  : array<string|int, mixed>
Format the given row
fpassthru()  : int
Outputs all data from the CSV
getFputcsvParameters()  : array<string|int, mixed>
returns the parameters for SplFileObject::fputcsv
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
initCsv()  : mixed
Initialize the CSV object and settings
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
validateRow()  : mixed
Validate a row

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

$csv

The CSV object holder

protected SplFileObject|StreamIterator $csv

$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 = '\'

$formatters

Callables to format the row before insertion

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

$fputcsv

fputcsv method from SplFileObject or StreamIterator

protected ReflectionMethod $fputcsv

$fputcsv_param_count

Nb parameters for SplFileObject::fputcsv method

protected int $fputcsv_param_count

$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_WRITE
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

$validators

Callables to validate the row before insertion

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

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

addFormatter()

add a formatter to the collection

public addFormatter(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

addValidator()

add a Validator to the collection

public addValidator(callable $callable, string $name) : $this
Parameters
$callable : callable
$name : string

the rule name

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

clearFormatters()

Remove all registered formatter

public clearFormatters() : $this
Return values
$this

clearStreamFilter()

Remove all registered stream filter

public clearStreamFilter() : $this
Return values
$this

clearValidators()

Remove all registered validators

public clearValidators() : $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()

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

hasFormatter()

Detect if the formatter is already registered

public hasFormatter(callable $callable) : bool
Parameters
$callable : callable
Return values
bool

hasStreamFilter()

Detect if the stream filter is already present

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

hasValidator()

Detect if a validator is already registered

public hasValidator(string $name) : bool
Parameters
$name : string

the validator name

Return values
bool

insertAll()

Adds multiple lines to the CSV document

public insertAll(Traversable|array<string|int, mixed> $rows) : static

a simple wrapper method around insertOne

Parameters
$rows : Traversable|array<string|int, mixed>

a multidimensional array or a Traversable object

Tags
throws
InvalidArgumentException

If the given rows format is invalid

Return values
static

insertOne()

Adds a single line to a CSV document

public insertOne(array<string|int, string>|string $row) : static
Parameters
$row : array<string|int, string>|string

a string, an array or an object implementing to '__toString' method

Return values
static

isActiveStreamFilter()

{@inheritdoc}

public isActiveStreamFilter() : mixed
Return values
mixed

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

removeFormatter()

Remove a formatter from the collection

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

removeStreamFilter()

Remove a filter from the collection

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

removeValidator()

Remove a validator from the collection

public removeValidator(string $name) : $this
Parameters
$name : string

the validator name

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

addRow()

Add new record to the CSV document

protected addRow(array<string|int, mixed> $row) : mixed
Parameters
$row : array<string|int, mixed>

record to add

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

formatRow()

Format the given row

protected formatRow(array<string|int, mixed> $row) : array<string|int, mixed>
Parameters
$row : array<string|int, mixed>
Return values
array<string|int, mixed>

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.

getFputcsvParameters()

returns the parameters for SplFileObject::fputcsv

protected getFputcsvParameters(array<string|int, mixed> $fields) : array<string|int, mixed>
Parameters
$fields : array<string|int, mixed>

The fields to be add

Return values
array<string|int, mixed>

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

initCsv()

Initialize the CSV object and settings

protected initCsv() : mixed
Return values
mixed

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

validateRow()

Validate a row

protected validateRow(array<string|int, mixed> $row) : mixed
Parameters
$row : array<string|int, mixed>
Tags
throws
InvalidRowException

If the validation failed

Return values
mixed

Search results