Varien_File_Csv Class Reference

Inheritance diagram for Varien_File_Csv:

Varien_File_Csv_Multy

List of all members.

Public Member Functions

 __construct ()
 setLineLength ($length)
 setDelimiter ($delimiter)
 setEnclosure ($enclosure)
 getData ($file)
 getDataPairs ($file, $keyIndex=0, $valueIndex=1)
 saveData ($file, $data)
 fputcsv (&$handle, $fields=array(), $delimiter= ',', $enclosure= '"')

Protected Attributes

 $_lineLength = 0
 $_delimiter = ','
 $_enclosure = '"'


Detailed Description

Csv parse

Author:
Magento Core Team <core@magentocommerce.com>

Definition at line 32 of file Csv.php.


Constructor & Destructor Documentation

__construct (  ) 

Definition at line 38 of file Csv.php.

00039     {
00040         
00041     }


Member Function Documentation

fputcsv ( &$  handle,
fields = array(),
delimiter = ',',
enclosure = '"' 
)

Definition at line 137 of file Csv.php.

00137                                                                                              {
00138         $str = '';
00139         $escape_char = '\\';
00140         foreach ($fields as $value) {
00141             if (strpos($value, $delimiter) !== false ||
00142                 strpos($value, $enclosure) !== false ||
00143                 strpos($value, "\n") !== false ||
00144                 strpos($value, "\r") !== false ||
00145                 strpos($value, "\t") !== false ||
00146                 strpos($value, ' ') !== false) {
00147                 $str2 = $enclosure;
00148                 $escaped = 0;
00149                 $len = strlen($value);
00150                 for ($i=0;$i<$len;$i++) {
00151                     if ($value[$i] == $escape_char) {
00152                         $escaped = 1;
00153                     } else if (!$escaped && $value[$i] == $enclosure) {
00154                         $str2 .= $enclosure;
00155                     } else {
00156                         $escaped = 0;
00157                     }
00158                         $str2 .= $value[$i];
00159                 }
00160                 $str2 .= $enclosure;
00161                 $str .= $str2.$delimiter;
00162             } else {
00163                 $str .= $enclosure.$value.$enclosure.$delimiter;
00164             }
00165         }
00166         $str = substr($str,0,-1);
00167         $str .= "\n";
00168         return fwrite($handle, $str);
00169     }

getData ( file  ) 

Retrieve CSV file data as array

Parameters:
string $file
Returns:
array

Definition at line 85 of file Csv.php.

00086     {
00087         $data = array();
00088         if (!file_exists($file)) {
00089             throw new Exception('File "'.$file.'" do not exists');
00090         }
00091         
00092         $fh = fopen($file, 'r');
00093         while ($rowData = fgetcsv($fh, $this->_lineLength, $this->_delimiter, $this->_enclosure)) {
00094             $data[] = $rowData;
00095         }
00096         fclose($fh);
00097         return $data;
00098     }

getDataPairs ( file,
keyIndex = 0,
valueIndex = 1 
)

Retrieve CSV file data as pairs

Parameters:
string $file
int $keyIndex
int $valueIndex
Returns:
array

Reimplemented in Varien_File_Csv_Multy.

Definition at line 108 of file Csv.php.

00109     {
00110         $data = array();
00111         $csvData = $this->getData($file);
00112         foreach ($csvData as $rowData) {
00113             if (isset($rowData[$keyIndex])) {
00114                 $data[$rowData[$keyIndex]] = isset($rowData[$valueIndex]) ? $rowData[$valueIndex] : null;
00115             }
00116         }
00117         return $data;
00118     }

saveData ( file,
data 
)

Saving data row array into file

Parameters:
string $file
array $data
Returns:
Varien_File_Csv

Definition at line 127 of file Csv.php.

00128     {
00129         $fh = fopen($file, 'w');
00130         foreach ($data as $dataRow) {
00131             $this->fputcsv($fh, $dataRow, $this->_delimiter, $this->_enclosure);
00132         }
00133         fclose($fh);
00134         return $this;
00135     }

setDelimiter ( delimiter  ) 

Set CSV column delimiter

Parameters:
string $delimiter
Returns:
Varien_File_Csv

Definition at line 61 of file Csv.php.

00062     {
00063         $this->_delimiter = $delimiter;
00064         return $this;
00065     }

setEnclosure ( enclosure  ) 

Set CSV column value enclosure

Parameters:
string $enclosure
Returns:
Varien_File_Csv

Definition at line 73 of file Csv.php.

00074     {
00075         $this->_enclosure = $enclosure;
00076         return $this;
00077     }

setLineLength ( length  ) 

Set max file line length

Parameters:
int $length
Returns:
Varien_File_Csv

Definition at line 49 of file Csv.php.

00050     {
00051         $this->_lineLength = $length;
00052         return $this;
00053     }


Member Data Documentation

$_delimiter = ',' [protected]

Definition at line 35 of file Csv.php.

$_enclosure = '"' [protected]

Definition at line 36 of file Csv.php.

$_lineLength = 0 [protected]

Definition at line 34 of file Csv.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:25:02 2009 for Magento by  doxygen 1.5.8