\League\Csv\PolyfillEmptyEscapeParser

A Polyfill to PHP's SplFileObject to enable parsing the CSV document without taking into account the escape character.

Summary

Methods
Properties
Constants
parse()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
__construct()
filterDocument()
extractRecord()
extractFieldContent()
extractEnclosedFieldContent()
$document
$delimiter
$enclosure
$trim_mask
$line
N/A

Properties

$document

$document : \SplFileObject|\League\Csv\Stream

Type

\SplFileObject|\League\Csv\Stream

$delimiter

$delimiter : string

Type

string

$enclosure

$enclosure : string

Type

string

$trim_mask

$trim_mask : string

Type

string

$line

$line : string|false

Type

string|false

Methods

parse()

parse(\SplFileObject|\League\Csv\Stream  $document) : \Generator|array<mixed,array>

Converts the document into a CSV record iterator.

In PHP7.4+ you'll be able to do

$file = new SplFileObject('/path/to/file.csv', 'r'); $file->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); $file->setCsvControl($delimiter, $enclosure, ''); foreach ($file as $record) { //$record escape mechanism is blocked by the empty string }

In PHP7.3- you can do

$file = new SplFileObject('/path/to/file.csv', 'r'); $it = EmptyEscapeParser::parse($file); //parsing will be done while ignoring the escape character value. foreach ($it as $record) { //fgetcsv is not directly use hence the escape char is not taken into account }

Each record array contains strings elements.

Parameters

\SplFileObject|\League\Csv\Stream $document

Returns

\Generator|array<mixed,array>

__construct()

__construct() 

filterDocument()

filterDocument(\SplFileObject|\League\Csv\Stream  $document) : \SplFileObject|\League\Csv\Stream

Filters the submitted document.

Parameters

\SplFileObject|\League\Csv\Stream $document

Returns

\SplFileObject|\League\Csv\Stream

extractRecord()

extractRecord() 

Extracts a record form the CSV document.

extractFieldContent()

extractFieldContent() : string|null

Extracts the content from a field without enclosure.

  • Field content can not spread on multiple document lines.
  • Content must be preserved.
  • Trailing line-breaks must be removed.

Returns

string|null

extractEnclosedFieldContent()

extractEnclosedFieldContent() : string|null

Extracts the content from a field with enclosure.

  • Field content can spread on multiple document lines.
  • Content between consecutive enclosure characters must be preserved.
  • Double enclosure sequence must be replaced by single enclosure character.
  • Trailing line break must be removed if they are not part of the field content.
  • Invalid field content is treated as per fgetcsv behavior.

Returns

string|null