123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\File;
- use Magento\Framework\Filesystem\Driver\File;
- /**
- * Csv parse
- *
- * @author Magento Core Team <core@magentocommerce.com>
- */
- class Csv
- {
- /**
- * @var int
- */
- protected $_lineLength = 0;
- /**
- * @var string
- */
- protected $_delimiter = ',';
- /**
- * @var string
- */
- protected $_enclosure = '"';
- /**
- * @var File
- */
- protected $file;
- /**
- * Constructor
- *
- * @param File $file File Driver used for writing CSV
- */
- public function __construct(File $file)
- {
- $this->file = $file;
- }
- /**
- * Set max file line length
- *
- * @param int $length
- * @return \Magento\Framework\File\Csv
- */
- public function setLineLength($length)
- {
- $this->_lineLength = $length;
- return $this;
- }
- /**
- * Set CSV column delimiter
- *
- * @param string $delimiter
- * @return \Magento\Framework\File\Csv
- */
- public function setDelimiter($delimiter)
- {
- $this->_delimiter = $delimiter;
- return $this;
- }
- /**
- * Set CSV column value enclosure
- *
- * @param string $enclosure
- * @return \Magento\Framework\File\Csv
- */
- public function setEnclosure($enclosure)
- {
- $this->_enclosure = $enclosure;
- return $this;
- }
- /**
- * Retrieve CSV file data as array
- *
- * @param string $file
- * @return array
- * @throws \Exception
- */
- public function getData($file)
- {
- $data = [];
- if (!file_exists($file)) {
- throw new \Exception('File "' . $file . '" does not exist');
- }
- $fh = fopen($file, 'r');
- while ($rowData = fgetcsv($fh, $this->_lineLength, $this->_delimiter, $this->_enclosure)) {
- $data[] = $rowData;
- }
- fclose($fh);
- return $data;
- }
- /**
- * Retrieve CSV file data as pairs
- *
- * @param string $file
- * @param int $keyIndex
- * @param int $valueIndex
- * @return array
- */
- public function getDataPairs($file, $keyIndex = 0, $valueIndex = 1)
- {
- $data = [];
- $csvData = $this->getData($file);
- foreach ($csvData as $rowData) {
- if (isset($rowData[$keyIndex])) {
- $data[$rowData[$keyIndex]] = isset($rowData[$valueIndex]) ? $rowData[$valueIndex] : null;
- }
- }
- return $data;
- }
- /**
- * Saving data row array into file
- *
- * @param string $file
- * @param array $data
- * @return $this
- * @throws \Magento\Framework\Exception\FileSystemException
- * @deprecated 102.0.0
- * @see appendData
- */
- public function saveData($file, $data)
- {
- return $this->appendData($file, $data, 'w');
- }
- /**
- * Replace the saveData method by allowing to select the input mode
- *
- * @param string $file
- * @param array $data
- * @param string $mode
- *
- * @return $this
- *
- * @throws \Magento\Framework\Exception\FileSystemException
- */
- public function appendData($file, $data, $mode = 'w')
- {
- $fileHandler = fopen($file, $mode);
- foreach ($data as $dataRow) {
- $this->file->filePutCsv($fileHandler, $dataRow, $this->_delimiter, $this->_enclosure);
- }
- fclose($fileHandler);
- return $this;
- }
- }
|