\Horde_Data_Csv

Horde_Data implementation for comma-separated data (CSV).

Summary

Methods
Properties
Constants
__construct()
importData()
exportData()
importFile()
importUrl()
exportFile()
getNewline()
getFilename()
getContentType()
warnings()
nextStep()
cleanup()
getCsv()
$storage
No constants found
_mapDate()
$_browser
$_http
$_cleanupCallback
$_contentType
$_extension
$_vars
$_warnings
N/A
No private methods found
No private properties found
N/A

Properties

$_browser

$_browser : \Horde_Browser

Browser object.

Type

\Horde_Browser

$_http

$_http : \Horde_Http_Client

HTTP client object.

Type

\Horde_Http_Client

$_cleanupCallback

$_cleanupCallback : callback

Cleanup callback function.

Type

callback

$_contentType

$_contentType : string

MIME content type.

Type

string

$_extension

$_extension : string

File extension.

Type

string

$_vars

$_vars : \Horde_Variables

Variables object.

Type

\Horde_Variables

$_warnings

$_warnings : array

A list of warnings raised during the last operation.

Type

array

Methods

__construct()

__construct(\Horde_Data_Storage  $storage, array  $params = array()) 

Constructor.

Parameters

\Horde_Data_Storage $storage
array $params

Optional parameters:

  • browser: (Horde_Browser) A browser object.
  • http: (Horde_Http_Client) A HTTP client object.
  • cleanup: (callback) A callback to call at cleanup time.
  • vars: (Horde_Variables) Form data.

importData()

importData(  $text) 

Stub to import passed data.

Parameters

$text

exportData()

exportData(array  $data, boolean  $header = false,   $export_mapping = array()) : string

Builds a CSV file from a given data structure and returns it as a string.

Parameters

array $data

A two-dimensional array containing the data set.

boolean $header

If true, the rows of $data are associative arrays with field names as their keys.

$export_mapping

Returns

string —

The CSV data.

importFile()

importFile(string  $filename, boolean  $header = false, string  $sep = ',', string  $quote = '', integer  $fields = null,   $import_mapping = array(), string  $charset = null, string  $crlf = null) : array

Imports and parses a CSV file.

Parameters

string $filename

The name of the file to parse.

boolean $header

Does the first line contain the field/column names?

string $sep

The field/column separator.

string $quote

The quoting character.

integer $fields

The number or fields/columns.

$import_mapping
string $charset

The file's charset.

string $crlf

The file's linefeed characters.

Throws

\Horde_Data_Exception

Returns

array —

A two-dimensional array of all imported data rows. If $header was true the rows are associative arrays with the field/column names as the keys.

importUrl()

importUrl(  $url,   $header = false) 

Imports a URL.

Parameters

$url
$header

exportFile()

exportFile(string  $filename, array  $data, boolean  $header = false,   $export_mapping = array()) 

Builds a CSV file from a given data structure and triggers its download. It DOES NOT exit the current script but only outputs the correct headers and data.

Parameters

string $filename

The name of the file to be downloaded.

array $data

A two-dimensional array containing the data set.

boolean $header

If true, the rows of $data are associative arrays with field names as their keys.

$export_mapping

getNewline()

getNewline() : string

Tries to determine the expected newline character based on the platform information passed by the browser's agent header.

Returns

string —

The guessed expected newline characters, either \n, \r or \r\n.

getFilename()

getFilename(string  $basename) : string

Returns the full filename including the basename and extension.

Parameters

string $basename

Basename for the file.

Returns

string —

The file name.

getContentType()

getContentType() : string

Returns the content type.

Returns

string —

The content type.

warnings()

warnings() : array

Returns a list of warnings that have been raised during the last operation.

Returns

array —

A (possibly empty) list of warnings.

nextStep()

nextStep(integer  $action, array  $param = array()) : mixed

Takes all necessary actions for the given import step, parameters and form values and returns the next necessary step.

Parameters

integer $action

The current step. One of the IMPORT_* constants.

array $param

An associative array containing needed parameters for the current step. Keys for this driver:

  • check_charset: (boolean) Do some checks to see if the correct charset has been provided. Throws charset exception on error.
  • import_mapping: TODO

Throws

\Horde_Data_Exception
\Horde_Data_Exception_Charset

Returns

mixed —

Either the next step as an integer constant or imported data set after the final step.

cleanup()

cleanup() : mixed

Removes any uploaded and moved files.

Returns

mixed —

If callback called, the return value of this call. This should be the value of the first import step.

getCsv()

getCsv(resource  $file, array  $params = array()) : array|boolean

Wrapper around fgetcsv().

Empty lines will be skipped. If the 'length' parameter is provided, all rows are filled up with empty strings up to this length, or stripped down to this length.

Parameters

resource $file

A file pointer.

array $params

Optional parameters. Possible values:

  • escape: The escape character.
  • length: The expected number of fields.
  • quote: The quote character.
  • separator: The field delimiter.

Returns

array|boolean —

A row from the CSV file or false on error or end of file.

_mapDate()

_mapDate(string  $date, string  $type, array  $params, integer  $key) : string

Maps a date/time string to an associative array.

Parameters

string $date

The date.

string $type

One of 'date', 'time' or 'datetime'.

array $params

Two-dimensional array with additional information about the formatting. Possible keys are:

  • delimiter: The character that seperates the different date/time parts.
  • format: If 'ampm' and $date contains a time we assume that it is in AM/PM format.
  • order: If $type is 'datetime' the order of the day and time parts: -1 (timestamp), 0 (day/time), 1 (time/day).
integer $key

The key to use for $params.

Returns

string —

The date or time in ISO format.