PHP Class Coseva\CSV

Inheritance: implements IteratorAggregat\IteratorAggregate
Show file Open project: johnnyfreeman/coseva Class Usage Examples

Protected Properties

Property Type Description
$_file SplFileObject Holds the CSV file pointer.
$_fileConfig array Holds config options for opening the file.
$_filters array Storage for filter callbacks to be executed during the parsing stage.
$_flushOnAfterFilter boolean Whether or not to flush empty rows after filtering.
$_garbageCollection boolean Whether or not to do garbage collection after parsing.
$_rows array Storage for parsed CSV rows.

Public Methods

Method Description
__construct ( string $filename, string $open_mode = 'r', boolean $use_include_path = false ) : CSV Constructor for CSV.
__toString ( ) : string If you cast a CSV instance as a string it will print the contents on the CSV to an HTML table.
_applyFilters ( array $row ) : array Apply filters to the given row.
collectGarbage ( boolean $collect = true ) : CSV Whether or not to use garbage collection after parsing.
filter ( integer | callable $column, callable $callable = null ) : CSV Allows you to register any number of filters on a particular column or an entire row.
flushEmptyRows ( boolean $onAfterFilter = null ) : CSV Flush rows that have turned out empty, either after applying filters or rows that simply have been empty in the source CSV from the get-go.
flushFilters ( ) : CSV Flushes all active filters.
getInstance ( string $filename, $open_mode = 'r', $use_include_path = false ) : CSV Get an instance of CSV, based on the filename.
getIterator ( ) : ArrayIterator Get an array iterator for the CSV rows.
parse ( integer $rowOffset ) : CSV This method will convert the csv to an array and will run all registered filters against it.
setDelimiter ( string $delimiter ) : object Set a delimiter for the CSV file.
toJSON ( ) : string Use this to get the entire CSV in JSON format.
toTable ( ) : string This is a great way to display the filtered contents of the csv to you during the development process (for debugging purposes).

Private Methods

Method Description
_flushEmptyRow ( mixed $row, mixed $index, boolean $trim = false ) : void Flush a row if it's empty.
_resolveFilename ( &$filename, boolean $use_include_path = false ) : boolean Resolve a given filename, keeping include paths in mind.

Method Details

__construct() public method

To read a csv file, just pass the path to the .csv file.
See also: http://php.net/manual/en/function.fopen.php for a list of modes
public __construct ( string $filename, string $open_mode = 'r', boolean $use_include_path = false ) : CSV
$filename string The file to read. Should be readable.
$open_mode string The mode in which to open the file
$use_include_path boolean Whether to search through include_path
return CSV $this

__toString() public method

If you cast a CSV instance as a string it will print the contents on the CSV to an HTML table.
public __toString ( ) : string
return string $this->toTable() HTML table of CSV contents

_applyFilters() public method

Apply filters to the given row.
public _applyFilters ( array $row ) : array
$row array
return array $row

collectGarbage() public method

Whether or not to use garbage collection after parsing.
public collectGarbage ( boolean $collect = true ) : CSV
$collect boolean
return CSV $this

filter() public method

Allows you to register any number of filters on a particular column or an entire row.
public filter ( integer | callable $column, callable $callable = null ) : CSV
$column integer | callable Specific column number or the callable to be applied. Optional: Zero-based column number. If this parameter is preset the $callable will recieve the contents of the current column (as a string), and will receive the entire (array based) row otherwise.
$callable callable Either the current row (as an array) or the current column (as a string) as the first parameter. The callable must return the new filtered row or column. Note: You can also use any native PHP functions that permit one parameter and return the new value, like trim, htmlspecialchars, urlencode, etc.
return CSV $this

flushEmptyRows() public method

Flush rows that have turned out empty, either after applying filters or rows that simply have been empty in the source CSV from the get-go.
public flushEmptyRows ( boolean $onAfterFilter = null ) : CSV
$onAfterFilter boolean whether or not to trigger while parsing. Leave this blank to trigger a flush right now.
return CSV $this

flushFilters() public method

Flushes all active filters.
public flushFilters ( ) : CSV
return CSV $this

getInstance() public static method

Get an instance of CSV, based on the filename.
public static getInstance ( string $filename, $open_mode = 'r', $use_include_path = false ) : CSV
$filename string the CSV file to read. Should be readable. Filenames will be resolved. Symlinks will be followed.
return CSV self::$_instances[$key]

getIterator() public method

Required for implementing IteratorAggregate

parse() public method

This method will convert the csv to an array and will run all registered filters against it.
public parse ( integer $rowOffset ) : CSV
$rowOffset integer Determines which row the parser will start on. Zero-based index. Note: When using a row offset, skipped rows will never be parsed nor stored. As such, we encourage to use different instances when mixing offsets, to prevent resultsets from interfering.
return CSV $this

setDelimiter() public method

Set a delimiter for the CSV file.
public setDelimiter ( string $delimiter ) : object
$delimiter string
return object $this CSV instance

toJSON() public method

Use this to get the entire CSV in JSON format.
public toJSON ( ) : string
return string JSON encoded string

toTable() public method

This is a great way to display the filtered contents of the csv to you during the development process (for debugging purposes).
public toTable ( ) : string
return string $output HTML table of CSV contents

Property Details

$_file protected property

Holds the CSV file pointer.
protected SplFileObject $_file
return SplFileObject

$_fileConfig protected property

Holds config options for opening the file.
protected array $_fileConfig
return array

$_filters protected property

Storage for filter callbacks to be executed during the parsing stage.
protected array $_filters
return array

$_flushOnAfterFilter protected property

Whether or not to flush empty rows after filtering.
protected bool $_flushOnAfterFilter
return boolean

$_garbageCollection protected property

Whether or not to do garbage collection after parsing.
protected bool $_garbageCollection
return boolean

$_rows protected property

Storage for parsed CSV rows.
protected array $_rows
return array