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

Public Member Functions

 __construct ( $file, \Magento\Framework\Filesystem\Directory\Read $directory, $delimiter=',', $enclosure='"' )
 
 destruct ()
 
 rewind ()
 
- Public Member Functions inherited from AbstractSource
 __construct (array $colNames)
 
 getColNames ()
 
 current ()
 
 next ()
 
 key ()
 
 valid ()
 
 rewind ()
 
 seek ($position)
 

Protected Member Functions

 _getNextRow ()
 
- Protected Member Functions inherited from AbstractSource
 _getNextRow ()
 

Protected Attributes

 $_file
 
 $_delimiter = ','
 
 $_enclosure = ''
 
- Protected Attributes inherited from AbstractSource
 $_colNames = []
 
 $_colQty
 
 $_row = []
 
 $_key = -1
 
 $_foundWrongQuoteFlag = false
 

Detailed Description

CSV import adapter

Definition at line 11 of file Csv.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $file,
\Magento\Framework\Filesystem\Directory\Read  $directory,
  $delimiter = ',',
  $enclosure = '"' 
)

Open file and detect column names

There must be column names in the first line

Parameters
string$file
\Magento\Framework\Filesystem\Directory\Read$directory
string$delimiter
string$enclosure
Exceptions

Definition at line 41 of file Csv.php.

46  {
47  register_shutdown_function([$this, 'destruct']);
48  try {
49  $this->_file = $directory->openFile($directory->getRelativePath($file), 'r');
50  } catch (\Magento\Framework\Exception\FileSystemException $e) {
51  throw new \LogicException("Unable to open file: '{$file}'");
52  }
53  if ($delimiter) {
54  $this->_delimiter = $delimiter;
55  }
56  $this->_enclosure = $enclosure;
57  parent::__construct($this->_getNextRow());
58  }

Member Function Documentation

◆ _getNextRow()

_getNextRow ( )
protected

Read next line from CSV-file

Returns
array|bool

Definition at line 77 of file Csv.php.

78  {
79  $parsed = $this->_file->readCsv(0, $this->_delimiter, $this->_enclosure);
80  if (is_array($parsed) && count($parsed) != $this->_colQty) {
81  foreach ($parsed as $element) {
82  if (strpos($element, "'") !== false) {
83  $this->_foundWrongQuoteFlag = true;
84  break;
85  }
86  }
87  } else {
88  $this->_foundWrongQuoteFlag = false;
89  }
90  return is_array($parsed) ? $parsed : [];
91  }
$element
Definition: element.phtml:12

◆ destruct()

destruct ( )

Close file handle

Returns
void

Definition at line 65 of file Csv.php.

66  {
67  if (is_object($this->_file)) {
68  $this->_file->close();
69  }
70  }

◆ rewind()

rewind ( )

Rewind the \Iterator to the first element (\Iterator interface)

Returns
void

Definition at line 98 of file Csv.php.

99  {
100  $this->_file->seek(0);
101  $this->_getNextRow();
102  // skip first line with the header
103  parent::rewind();
104  }

Field Documentation

◆ $_delimiter

$_delimiter = ','
protected

Definition at line 23 of file Csv.php.

◆ $_enclosure

$_enclosure = ''
protected

Definition at line 28 of file Csv.php.

◆ $_file

$_file
protected

Definition at line 16 of file Csv.php.


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