Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Csv.php
Go to the documentation of this file.
1 <?php
7 
11 class Csv extends \Magento\ImportExport\Model\Import\AbstractSource
12 {
16  protected $_file;
17 
23  protected $_delimiter = ',';
24 
28  protected $_enclosure = '';
29 
41  public function __construct(
42  $file,
43  \Magento\Framework\Filesystem\Directory\Read $directory,
44  $delimiter = ',',
45  $enclosure = '"'
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  }
59 
65  public function destruct()
66  {
67  if (is_object($this->_file)) {
68  $this->_file->close();
69  }
70  }
71 
77  protected function _getNextRow()
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  }
92 
98  public function rewind()
99  {
100  $this->_file->seek(0);
101  $this->_getNextRow();
102  // skip first line with the header
103  parent::rewind();
104  }
105 }
__construct( $file, \Magento\Framework\Filesystem\Directory\Read $directory, $delimiter=',', $enclosure='"' )
Definition: Csv.php:41
$element
Definition: element.phtml:12