Documentation

CsvImport
in package
implements IteratorAggregate

A CSV Importer.

Tags
todo

Tests!

Option for no headers.

Table of Contents

Interfaces

IteratorAggregate

Constants

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
$headers  : array<string|int, mixed>|null
$null  : string
$_escape_re  : string
$_own_handles  : bool
$handle  : resource|null

Methods

__construct()  : mixed
Configure the CSV output format.
fromFile()  : null|self
Create a new importer from a file path.
fromString()  : null|self
Create new import from a csv blob string.
getHeaders()  : array<string|int, string>
Get the CSV headers.
getIterator()  : Traversable
getLine()  : null|array<string|int, mixed>
Get the next line.
_getcsv()  : null|array<string|int, mixed>
Internal wrapper to normalise fgetscsv() behaviour.

Constants

MAX_MEMORY

Store blob csvs in memory if under 5mb, otherwise use a temp file.

public mixed MAX_MEMORY = 5 * 1024 * 1024

Properties

$headers

public array<string|int, mixed>|null $headers = null

$_own_handles

private bool $_own_handles = false

This importer manages it's own handles.

$handle

private resource|null $handle = null

Methods

__construct()

Configure the CSV output format.

public __construct(resource $handle[, array<string|int, mixed> $config = [] ]) : mixed

By default:

  • break is a LF \n
  • delimiter is a comma ,
  • null is a \N
  • enclosure is "
  • escape is \

If headers is null it will produce headers from the first row.

Parameters
$handle : resource

a file resource

$config : array<string|int, mixed> = []

[break, delimiter, null, enclosure, escape, headers]

fromFile()

Create a new importer from a file path.

public static fromFile(string $filename[, array<string|int, mixed> $config = [] ]) : null|self
Parameters
$filename : string
$config : array<string|int, mixed> = []
Return values
null|self

fromString()

Create new import from a csv blob string.

public static fromString(string $csv[, array<string|int, mixed> $config = [] ]) : null|self
Parameters
$csv : string
$config : array<string|int, mixed> = []
Return values
null|self

getHeaders()

Get the CSV headers.

public getHeaders() : array<string|int, string>

Beware, this will consume the first row if headers are not explicitly set.

Return values
array<string|int, string>

getIterator()

public getIterator() : Traversable
Tags
inheritdoc
Return values
Traversable

getLine()

Get the next line.

public getLine() : null|array<string|int, mixed>
Return values
null|array<string|int, mixed> —

An associated array or null if EOF.

_getcsv()

Internal wrapper to normalise fgetscsv() behaviour.

private _getcsv() : null|array<string|int, mixed>
Return values
null|array<string|int, mixed> —

row values or null if EOF.

On this page

Search results