CsvExport
in package
A CSV Exporter.
Tags
Table of Contents
Constants
- DIRTY_CHARS : mixed = ' "\r\n\t'
- Identify dirty items.
- MAX_MEMORY : mixed = 5 * 1024 * 1024
- Store blob csvs in memory if under 5mb, otherwise use a temp file.
Properties
- $break : string
- $delimiter : string
- $enclosure : string
- $escape : string
- $formatters : array<string|int, mixed>
- $handle : resource
- $headers : array<string|int, mixed>|null
- $null : string
- $_own_handles : bool
- $dirty_re : string
Methods
- __construct() : mixed
- Configure the CSV output format.
- __destruct() : void
- Close any resources (that we own).
- add() : void
- Add a row.
- addAll() : void
- All all models within a traversable value.
- build() : string
- Format and build the CSV payload all at once.
- clean() : string
- format() : void
- Add a formatter for a named attribute.
- _write() : void
- _format() : string
- Internal formatter function.
Constants
DIRTY_CHARS
Identify dirty items.
public
mixed
DIRTY_CHARS
= ' "\r\n\t'
MAX_MEMORY
Store blob csvs in memory if under 5mb, otherwise use a temp file.
public
mixed
MAX_MEMORY
= 5 * 1024 * 1024
Properties
$break
public
string
$break
= "\n"
Only supported in PHP 8.1+
$delimiter
public
string
$delimiter
= ','
$enclosure
public
string
$enclosure
= '"'
$escape
public
string
$escape
= '\\'
$formatters
public
array<string|int, mixed>
$formatters
= []
$handle
public
resource
$handle
$headers
public
array<string|int, mixed>|null
$headers
= null
$null
public
string
$null
= '\N'
$_own_handles
private
bool
$_own_handles
= false
$dirty_re
private
string
$dirty_re
Methods
__construct()
Configure the CSV output format.
public
__construct([resource|array<string|int, mixed>|null $handle = null ][, array<string|int, mixed> $config = [] ]) : mixed
By default:
- break is a LF \n
- delimiter is a comma ,
- null is \N
- enclosure is "
- escape is \
Note, 'break' is only supported in PHP 8.1+.
Headers is a map of keys -> values.
For example: $csv = new CsvExport([ 'headers' => [ 'val1' => 'Value 1', 'val2' => 'Value 2' ], ]); $csv->add([ 'val1' => 'ABC', 'val2' => 'DEF' ]); $csv->build();
Produces: Value 1,Value 2\n ABC,DEF\n
If headers is null it will produce headers from the first row.
Parameters
- $handle : resource|array<string|int, mixed>|null = null
- $config : array<string|int, mixed> = []
-
[break, delimiter, null, enclosure, escape, headers]
Tags
__destruct()
Close any resources (that we own).
public
__destruct() : void
add()
Add a row.
public
add(iterable<string|int, mixed> $model) : void
Parameters
- $model : iterable<string|int, mixed>
addAll()
All all models within a traversable value.
public
addAll(iterable<string|int, mixed> $models) : void
If an item in not an array or iterable, it is skipped.
Parameters
- $models : iterable<string|int, mixed>
build()
Format and build the CSV payload all at once.
public
build() : string
Return values
stringclean()
public
clean(string $dirty) : string
CsvExporter now uses fputcsv() internally.
Parameters
- $dirty : string
Return values
string —cleaned
format()
Add a formatter for a named attribute.
public
format(string|array<string|int, string> $attribute, callable $cb) : void
Use this for things like DateTime or arrays. Number types and classes with a '__toString()' should be fine.
Or even if you want to format a float, something like this should work: $csv->format('blah', fn($item) => sprintf("%.2f", $item));
Parameters
- $attribute : string|array<string|int, string>
- $cb : callable
_write()
protected
_write(array<string|int, mixed> $row) : void
Parameters
- $row : array<string|int, mixed>
_format()
Internal formatter function.
private
_format(string $attribute, mixed $value) : string
Parameters
- $attribute : string
- $value : mixed