Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
Csv Class Reference
Inheritance diagram for Csv:
CsvMulty

Public Member Functions

 __construct (File $file)
 
 setLineLength ($length)
 
 setDelimiter ($delimiter)
 
 setEnclosure ($enclosure)
 
 getData ($file)
 
 getDataPairs ($file, $keyIndex=0, $valueIndex=1)
 
 saveData ($file, $data)
 
 appendData ($file, $data, $mode='w')
 

Protected Attributes

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

Detailed Description

Csv parse

Author
Magento Core Team core@.nosp@m.mage.nosp@m.ntoco.nosp@m.mmer.nosp@m.ce.co.nosp@m.m

Definition at line 16 of file Csv.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( File  $file)

Constructor

Parameters
File$fileFile Driver used for writing CSV

Definition at line 43 of file Csv.php.

44  {
45  $this->file = $file;
46  }

Member Function Documentation

◆ appendData()

appendData (   $file,
  $data,
  $mode = 'w' 
)

Replace the saveData method by allowing to select the input mode

Parameters
string$file
array$data
string$mode
Returns
$this
Exceptions

Definition at line 152 of file Csv.php.

153  {
154  $fileHandler = fopen($file, $mode);
155  foreach ($data as $dataRow) {
156  $this->file->filePutCsv($fileHandler, $dataRow, $this->_delimiter, $this->_enclosure);
157  }
158  fclose($fileHandler);
159 
160  return $this;
161  }
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15

◆ getData()

getData (   $file)

Retrieve CSV file data as array

Parameters
string$file
Returns
array
Exceptions

Definition at line 91 of file Csv.php.

92  {
93  $data = [];
94  if (!file_exists($file)) {
95  throw new \Exception('File "' . $file . '" does not exist');
96  }
97 
98  $fh = fopen($file, 'r');
99  while ($rowData = fgetcsv($fh, $this->_lineLength, $this->_delimiter, $this->_enclosure)) {
100  $data[] = $rowData;
101  }
102  fclose($fh);
103  return $data;
104  }

◆ getDataPairs()

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

Retrieve CSV file data as pairs

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

Definition at line 114 of file Csv.php.

115  {
116  $data = [];
117  $csvData = $this->getData($file);
118  foreach ($csvData as $rowData) {
119  if (isset($rowData[$keyIndex])) {
120  $data[$rowData[$keyIndex]] = isset($rowData[$valueIndex]) ? $rowData[$valueIndex] : null;
121  }
122  }
123  return $data;
124  }

◆ saveData()

saveData (   $file,
  $data 
)

Saving data row array into file

Parameters
string$file
array$data
Returns
$this
Exceptions

Definition at line 136 of file Csv.php.

137  {
138  return $this->appendData($file, $data, 'w');
139  }
appendData($file, $data, $mode='w')
Definition: Csv.php:152

◆ setDelimiter()

setDelimiter (   $delimiter)

Set CSV column delimiter

Parameters
string$delimiter
Returns
\Magento\Framework\File\Csv

Definition at line 66 of file Csv.php.

67  {
68  $this->_delimiter = $delimiter;
69  return $this;
70  }

◆ setEnclosure()

setEnclosure (   $enclosure)

Set CSV column value enclosure

Parameters
string$enclosure
Returns
\Magento\Framework\File\Csv

Definition at line 78 of file Csv.php.

79  {
80  $this->_enclosure = $enclosure;
81  return $this;
82  }

◆ setLineLength()

setLineLength (   $length)

Set max file line length

Parameters
int$length
Returns
\Magento\Framework\File\Csv

Definition at line 54 of file Csv.php.

55  {
56  $this->_lineLength = $length;
57  return $this;
58  }

Field Documentation

◆ $_delimiter

$_delimiter = ','
protected

Definition at line 26 of file Csv.php.

◆ $_enclosure

$_enclosure = '"'
protected

Definition at line 31 of file Csv.php.

◆ $_lineLength

$_lineLength = 0
protected

Definition at line 21 of file Csv.php.

◆ $file

$file
protected

Definition at line 36 of file Csv.php.


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