Writer
extends AbstractCsv
in package
implements
TabularDataWriter
A class to insert records into a CSV Document.
Table of Contents
Interfaces
- TabularDataWriter
- A class to insert records into a CSV Document.
Constants
- ENCLOSE_ALL = 1
- ENCLOSE_NECESSARY = 0
- ENCLOSE_NONE = -1
- STREAM_FILTER_MODE = \STREAM_FILTER_WRITE
Properties
- $delimiter : string
- $document : SplFileObject|Stream
- $enclose_all : int
- $enclosure : string
- $enclosure_replace : array{0: string[], 1: string[]}
- $escape : string
- $flush_counter : int
- $flush_threshold : int|null
- $formatters : array<string|int, callable(array<string|int, mixed>): array<string|int, mixed>>
- $input_bom : Bom|null
- $is_input_bom_included : bool
- $newline : string
- $output_bom : Bom|null
- $stream_filters : array<string, bool>
- $validators : array<string|int, callable(array<string|int, mixed>): bool>
Methods
- __clone() : mixed
- __toString() : string
- DEPRECATION WARNING! This method will be removed in the next major point release.
- addFormatter() : static
- Adds a record formatter.
- addStreamFilter() : static
- DEPRECATION WARNING! This method will be removed in the next major point release.
- addValidator() : self
- Adds a record validator.
- appendStreamFilterOnRead() : static
- Append a stream filter.
- appendStreamFilterOnWrite() : static
- Append a stream filter.
- chunk() : Generator
- Returns the CSV document as a Generator of string chunk.
- createFromFileObject() : static
- DEPRECATION WARNING! This method will be removed in the next major point release.
- createFromPath() : static
- DEPRECATION WARNING! This method will be removed in the next major point release.
- createFromStream() : static
- DEPRECATION WARNING! This method will be removed in the next major point release.
- createFromString() : static
- DEPRECATION WARNING! This method will be removed in the next major point release.
- download() : int
- Outputs all data on the CSV file.
- encloseAll() : bool
- Tells whether new entries will all be enclosed on writing.
- encloseNecessary() : bool
- Tells whether new entries will be selectively enclosed on writing if the field content requires encoding.
- encloseNone() : bool
- Tells whether new entries will never be enclosed on writing.
- forceEnclosure() : self
- from() : static
- Returns a new instance from a file path.
- fromString() : static
- Returns a new instance from a string.
- getContent() : string
- Retrieves the CSV content.
- getDelimiter() : string
- Returns the current field delimiter.
- getEnclosure() : string
- Returns the current field enclosure.
- getEndOfLine() : string
- Returns the current end of line sequence characters.
- getEscape() : string
- Returns the current field escape character.
- getFlushThreshold() : int|null
- Returns the flush threshold.
- getInputBOM() : string
- Returns the BOM sequence of the given CSV.
- getNewline() : string
- DEPRECATION WARNING! This method will be removed in the next major point release.
- getOutputBOM() : string
- Returns the BOM sequence in use on Output methods.
- getPathname() : string
- Returns the pathname of the underlying document.
- getStreamFilterMode() : int
- DEPRECATION WARNING! This method will be removed in the next major point release.
- hasStreamFilter() : bool
- Tells whether the specified stream filter is attached to the current stream.
- includeInputBOM() : static
- Disables skipping Input BOM.
- insertAll() : int
- Adds multiple records to the CSV document.
- insertOne() : int
- Adds a single record to a CSV document.
- isInputBOMIncluded() : bool
- Tells whether the BOM can be stripped if presents.
- necessaryEnclosure() : self
- noEnclosure() : self
- output() : int
- DEPRECATION WARNING! This method will be removed in the next major point release.
- prependStreamFilterOnRead() : static
- Prepend a stream filter.
- prependStreamFilterOnWrite() : static
- Prepend a stream filter.
- relaxEnclosure() : self
- DEPRECATION WARNING! This method will be removed in the next major point release.
- setDelimiter() : static
- Sets the field delimiter.
- setEnclosure() : static
- Sets the field enclosure.
- setEndOfLine() : self
- Sets the end of line sequence.
- setEscape() : static
- Sets the field escape character.
- setFlushThreshold() : self
- Sets the flush threshold.
- setNewline() : self
- DEPRECATION WARNING! This method will be removed in the next major point release.
- setOutputBOM() : static
- Sets the BOM sequence to prepend the CSV on output.
- skipInputBOM() : static
- Enables BOM Stripping.
- supportsStreamFilter() : bool
- DEPRECATION WARNING! This method will be removed in the next major point release.
- supportsStreamFilterOnRead() : bool
- Tells whether the stream filter read capabilities can be used.
- supportsStreamFilterOnWrite() : bool
- Tells whether the stream filter write capabilities can be used.
- toString() : string
- Retrieves the CSV content.
- __construct() : mixed
- addRecord() : int|false
- DEPRECATION WARNING! This method will be removed in the next major point release.
- consolidate() : int
- DEPRECATION WARNING! This method will be removed in the next major point release.
- formatRecord() : array<string|int, mixed>
- DEPRECATION WARNING! This method will be removed in the next major point release.
- insertRecord() : int|false
- resetProperties() : void
- Reset dynamic object properties to improve performance.
- sendHeaders() : void
- DEPRECATION WARNING! This method will be removed in the next major point release.
- validateRecord() : void
- Validates a record.
Constants
ENCLOSE_ALL
protected
mixed
ENCLOSE_ALL
= 1
ENCLOSE_NECESSARY
protected
mixed
ENCLOSE_NECESSARY
= 0
ENCLOSE_NONE
protected
mixed
ENCLOSE_NONE
= -1
STREAM_FILTER_MODE
protected
mixed
STREAM_FILTER_MODE
= \STREAM_FILTER_WRITE
Properties
$delimiter
protected
string
$delimiter
= ','
$document read-only
protected
SplFileObject|Stream
$document
$enclose_all
protected
int
$enclose_all
= self::ENCLOSE_NECESSARY
$enclosure
protected
string
$enclosure
= '"'
$enclosure_replace
protected
array{0: string[], 1: string[]}
$enclosure_replace
= [[], []]
$escape
protected
string
$escape
= '\\'
$flush_counter
protected
int
$flush_counter
= 0
$flush_threshold
protected
int|null
$flush_threshold
= null
$formatters
protected
array<string|int, callable(array<string|int, mixed>): array<string|int, mixed>>
$formatters
= []
collection of Closure to format the record before reading.
$input_bom
protected
Bom|null
$input_bom
= null
$is_input_bom_included
protected
bool
$is_input_bom_included
= false
$newline
protected
string
$newline
= "\n"
$output_bom
protected
Bom|null
$output_bom
= null
$stream_filters
protected
array<string, bool>
$stream_filters
= []
collection of stream filters.
$validators
protected
array<string|int, callable(array<string|int, mixed>): bool>
$validators
= []
callable collection to validate the record before insertion.
Methods
__clone()
public
__clone() : mixed
Tags
__toString()
DEPRECATION WARNING! This method will be removed in the next major point release.
public
__toString() : string
since version 9.1.0
use League\Csv\AbstractCsv::toString() instead
Tags
Return values
stringaddFormatter()
Adds a record formatter.
public
addFormatter(callable(array<string|int, mixed>): array<string|int, mixed> $formatter) : static
Parameters
- $formatter : callable(array<string|int, mixed>): array<string|int, mixed>
Return values
staticaddStreamFilter()
DEPRECATION WARNING! This method will be removed in the next major point release.
public
addStreamFilter(string $filtername[, array<string|int, mixed>|null $params = null ]) : static
since version 9.22.0
use League\Csv\AbstractCsv::appendStreamFilterOnRead() or League\Csv\AbstractCsv::prependStreamFilterOnRead() instead
Parameters
- $filtername : string
- $params : array<string|int, mixed>|null = null
Tags
Return values
staticaddValidator()
Adds a record validator.
public
addValidator(callable(array<string|int, mixed>): bool $validator, string $validator_name) : self
Parameters
- $validator : callable(array<string|int, mixed>): bool
- $validator_name : string
Return values
selfappendStreamFilterOnRead()
Append a stream filter.
public
appendStreamFilterOnRead(string $filtername[, mixed $params = null ]) : static
Parameters
- $filtername : string
- $params : mixed = null
Tags
Return values
staticappendStreamFilterOnWrite()
Append a stream filter.
public
appendStreamFilterOnWrite(string $filtername[, mixed $params = null ]) : static
Parameters
- $filtername : string
- $params : mixed = null
Tags
Return values
staticchunk()
Returns the CSV document as a Generator of string chunk.
public
chunk(int $length) : Generator
Parameters
- $length : int
Tags
Return values
GeneratorcreateFromFileObject()
DEPRECATION WARNING! This method will be removed in the next major point release.
public
static createFromFileObject(SplFileObject $file) : static
since version 9.27.0
Returns a new instance from a SplFileObject.
use League\Csv\AbstractCsv::from() instead
Parameters
- $file : SplFileObject
Tags
Return values
staticcreateFromPath()
DEPRECATION WARNING! This method will be removed in the next major point release.
public
static createFromPath(string $path[, non-empty-string $open_mode = 'r+' ][, resource|null $context = null ]) : static
since version 9.27.0
Returns a new instance from a file path.
use League\Csv\AbstractCsv::from() instead
Parameters
- $path : string
- $open_mode : non-empty-string = 'r+'
- $context : resource|null = null
-
the resource context
Tags
Return values
staticcreateFromStream()
DEPRECATION WARNING! This method will be removed in the next major point release.
public
static createFromStream(resource $stream) : static
since version 9.27.0
Returns a new instance from a PHP resource stream.
use League\Csv\AbstractCsv::from() instead
Parameters
- $stream : resource
Tags
Return values
staticcreateFromString()
DEPRECATION WARNING! This method will be removed in the next major point release.
public
static createFromString([Stringable|string $content = '' ]) : static
since version 9.27.0
Returns a new instance from a string.
use League\Csv\AbstractCsv::fromString() instead
Parameters
- $content : Stringable|string = ''
Tags
Return values
staticdownload()
Outputs all data on the CSV file.
public
download([string|null $filename = null ]) : int
Returns the number of characters read from the handle and passed through to the output.
Parameters
- $filename : string|null = null
Tags
Return values
intencloseAll()
Tells whether new entries will all be enclosed on writing.
public
encloseAll() : bool
Return values
boolencloseNecessary()
Tells whether new entries will be selectively enclosed on writing if the field content requires encoding.
public
encloseNecessary() : bool
Return values
boolencloseNone()
Tells whether new entries will never be enclosed on writing.
public
encloseNone() : bool
Return values
boolforceEnclosure()
public
forceEnclosure() : self
Return values
selffrom()
Returns a new instance from a file path.
public
static from(SplFileInfo|SplFileObject|resource|string $filename[, non-empty-string $mode = 'r+' ][, resource|null $context = null ]) : static
Parameters
- $filename : SplFileInfo|SplFileObject|resource|string
-
an SPL file object, a resource stream or a file path
- $mode : non-empty-string = 'r+'
-
the file path open mode used with a file path or a SplFileInfo object
- $context : resource|null = null
-
the resource context used with a file pathor a SplFileInfo object
Tags
Return values
staticfromString()
Returns a new instance from a string.
public
static fromString([Stringable|string $content = '' ]) : static
Parameters
- $content : Stringable|string = ''
Return values
staticgetContent()
Retrieves the CSV content.
public
getContent() : string
since version 9.7.0
use League\Csv\AbstractCsv::toString() instead
DEPRECATION WARNING! This method will be removed in the next major point release
Tags
Return values
stringgetDelimiter()
Returns the current field delimiter.
public
getDelimiter() : string
Return values
stringgetEnclosure()
Returns the current field enclosure.
public
getEnclosure() : string
Return values
stringgetEndOfLine()
Returns the current end of line sequence characters.
public
getEndOfLine() : string
Return values
stringgetEscape()
Returns the current field escape character.
public
getEscape() : string
Return values
stringgetFlushThreshold()
Returns the flush threshold.
public
getFlushThreshold() : int|null
Return values
int|nullgetInputBOM()
Returns the BOM sequence of the given CSV.
public
getInputBOM() : string
Return values
stringgetNewline()
DEPRECATION WARNING! This method will be removed in the next major point release.
public
getNewline() : string
Since version 9.10.0
use League\Csv\Writer::getEndOfLine()
Tags
Return values
stringgetOutputBOM()
Returns the BOM sequence in use on Output methods.
public
getOutputBOM() : string
Return values
stringgetPathname()
Returns the pathname of the underlying document.
public
getPathname() : string
Return values
stringgetStreamFilterMode()
DEPRECATION WARNING! This method will be removed in the next major point release.
public
getStreamFilterMode() : int
since version 9.7.0
use League\Csv\AbstractCsv::supportsStreamFilterOnRead() or League\Csv\AbstractCsv::supportsStreamFilterOnWrite() instead
Tags
Return values
inthasStreamFilter()
Tells whether the specified stream filter is attached to the current stream.
public
hasStreamFilter(string $filtername) : bool
Parameters
- $filtername : string
Return values
boolincludeInputBOM()
Disables skipping Input BOM.
public
includeInputBOM() : static
Return values
staticinsertAll()
Adds multiple records to the CSV document.
public
insertAll(TabularDataProvider|TabularData|iterable<string|int, mixed> $records) : int
Parameters
- $records : TabularDataProvider|TabularData|iterable<string|int, mixed>
Tags
Return values
intinsertOne()
Adds a single record to a CSV document.
public
insertOne(array<string|int, mixed> $record) : int
A record is an array that can contain scalar type values, NULL values or objects implementing the __toString method.
Parameters
- $record : array<string|int, mixed>
Tags
Return values
intisInputBOMIncluded()
Tells whether the BOM can be stripped if presents.
public
isInputBOMIncluded() : bool
Return values
boolnecessaryEnclosure()
public
necessaryEnclosure() : self
Return values
selfnoEnclosure()
public
noEnclosure() : self
Return values
selfoutput()
DEPRECATION WARNING! This method will be removed in the next major point release.
public
output([string|null $filename = null ]) : int
since version 9.18.0
use League\Csv\AbstractCsv::download() instead
Parameters
- $filename : string|null = null
Tags
Return values
intprependStreamFilterOnRead()
Prepend a stream filter.
public
prependStreamFilterOnRead(string $filtername[, mixed $params = null ]) : static
Parameters
- $filtername : string
- $params : mixed = null
Tags
Return values
staticprependStreamFilterOnWrite()
Prepend a stream filter.
public
prependStreamFilterOnWrite(string $filtername[, mixed $params = null ]) : static
Parameters
- $filtername : string
- $params : mixed = null
Tags
Return values
staticrelaxEnclosure()
DEPRECATION WARNING! This method will be removed in the next major point release.
public
relaxEnclosure() : self
Since version 9.22.0
use League\Csv\Writer::necessaryEnclosure()
Tags
Return values
selfsetDelimiter()
Sets the field delimiter.
public
setDelimiter(string $delimiter) : static
Parameters
- $delimiter : string
Tags
Return values
staticsetEnclosure()
Sets the field enclosure.
public
setEnclosure(string $enclosure) : static
Parameters
- $enclosure : string
Tags
Return values
staticsetEndOfLine()
Sets the end of line sequence.
public
setEndOfLine(string $endOfLine) : self
Parameters
- $endOfLine : string
Return values
selfsetEscape()
Sets the field escape character.
public
setEscape(string $escape) : static
Parameters
- $escape : string
Tags
Return values
staticsetFlushThreshold()
Sets the flush threshold.
public
setFlushThreshold(int|null $threshold) : self
Parameters
- $threshold : int|null
Tags
Return values
selfsetNewline()
DEPRECATION WARNING! This method will be removed in the next major point release.
public
setNewline(string $newline) : self
Since version 9.10.0
use League\Csv\Writer::setEndOfLine()
Parameters
- $newline : string
Tags
Return values
selfsetOutputBOM()
Sets the BOM sequence to prepend the CSV on output.
public
setOutputBOM(Bom|string|null $str) : static
Parameters
- $str : Bom|string|null
Tags
Return values
staticskipInputBOM()
Enables BOM Stripping.
public
skipInputBOM() : static
Return values
staticsupportsStreamFilter()
DEPRECATION WARNING! This method will be removed in the next major point release.
public
supportsStreamFilter() : bool
since version 9.7.0
use League\Csv\AbstractCsv::supportsStreamFilterOnRead() or League\Csv\AbstractCsv::supportsStreamFilterOnWrite() instead
Tags
Return values
boolsupportsStreamFilterOnRead()
Tells whether the stream filter read capabilities can be used.
public
supportsStreamFilterOnRead() : bool
Return values
boolsupportsStreamFilterOnWrite()
Tells whether the stream filter write capabilities can be used.
public
supportsStreamFilterOnWrite() : bool
Return values
booltoString()
Retrieves the CSV content.
public
toString() : string
Tags
Return values
string__construct()
protected
__construct(SplFileObject|Stream $document) : mixed
Parameters
- $document : SplFileObject|Stream
Tags
addRecord()
DEPRECATION WARNING! This method will be removed in the next major point release.
protected
addRecord(array<string|int, mixed> $record) : int|false
Since version 9.9.0
no longer affecting the class behaviour
Parameters
- $record : array<string|int, mixed>
Tags
Return values
int|falseconsolidate()
DEPRECATION WARNING! This method will be removed in the next major point release.
protected
consolidate() : int
Since version 9.9.0
no longer affecting the class behaviour
Tags
Return values
intformatRecord()
DEPRECATION WARNING! This method will be removed in the next major point release.
protected
formatRecord(array<string|int, mixed> $record, callable $formatter) : array<string|int, mixed>
since version 9.8.0
no longer affecting the class behaviour
Parameters
- $record : array<string|int, mixed>
- $formatter : callable
Tags
Return values
array<string|int, mixed>insertRecord()
protected
insertRecord(array<string|int, mixed> $record) : int|false
Parameters
- $record : array<string|int, mixed>
Return values
int|falseresetProperties()
Reset dynamic object properties to improve performance.
protected
resetProperties() : void
sendHeaders()
DEPRECATION WARNING! This method will be removed in the next major point release.
protected
sendHeaders(string $filename) : void
since version 9.17.0
the method no longer affect the outcome of the class, use League\Csv\HttpHeaders::forFileDownload instead
Parameters
- $filename : string
Tags
validateRecord()
Validates a record.
protected
validateRecord(array<string|int, mixed> $record) : void
Parameters
- $record : array<string|int, mixed>