Csv.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\File;
  7. use Magento\Framework\Filesystem\Driver\File;
  8. /**
  9. * Csv parse
  10. *
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. */
  13. class Csv
  14. {
  15. /**
  16. * @var int
  17. */
  18. protected $_lineLength = 0;
  19. /**
  20. * @var string
  21. */
  22. protected $_delimiter = ',';
  23. /**
  24. * @var string
  25. */
  26. protected $_enclosure = '"';
  27. /**
  28. * @var File
  29. */
  30. protected $file;
  31. /**
  32. * Constructor
  33. *
  34. * @param File $file File Driver used for writing CSV
  35. */
  36. public function __construct(File $file)
  37. {
  38. $this->file = $file;
  39. }
  40. /**
  41. * Set max file line length
  42. *
  43. * @param int $length
  44. * @return \Magento\Framework\File\Csv
  45. */
  46. public function setLineLength($length)
  47. {
  48. $this->_lineLength = $length;
  49. return $this;
  50. }
  51. /**
  52. * Set CSV column delimiter
  53. *
  54. * @param string $delimiter
  55. * @return \Magento\Framework\File\Csv
  56. */
  57. public function setDelimiter($delimiter)
  58. {
  59. $this->_delimiter = $delimiter;
  60. return $this;
  61. }
  62. /**
  63. * Set CSV column value enclosure
  64. *
  65. * @param string $enclosure
  66. * @return \Magento\Framework\File\Csv
  67. */
  68. public function setEnclosure($enclosure)
  69. {
  70. $this->_enclosure = $enclosure;
  71. return $this;
  72. }
  73. /**
  74. * Retrieve CSV file data as array
  75. *
  76. * @param string $file
  77. * @return array
  78. * @throws \Exception
  79. */
  80. public function getData($file)
  81. {
  82. $data = [];
  83. if (!file_exists($file)) {
  84. throw new \Exception('File "' . $file . '" does not exist');
  85. }
  86. $fh = fopen($file, 'r');
  87. while ($rowData = fgetcsv($fh, $this->_lineLength, $this->_delimiter, $this->_enclosure)) {
  88. $data[] = $rowData;
  89. }
  90. fclose($fh);
  91. return $data;
  92. }
  93. /**
  94. * Retrieve CSV file data as pairs
  95. *
  96. * @param string $file
  97. * @param int $keyIndex
  98. * @param int $valueIndex
  99. * @return array
  100. */
  101. public function getDataPairs($file, $keyIndex = 0, $valueIndex = 1)
  102. {
  103. $data = [];
  104. $csvData = $this->getData($file);
  105. foreach ($csvData as $rowData) {
  106. if (isset($rowData[$keyIndex])) {
  107. $data[$rowData[$keyIndex]] = isset($rowData[$valueIndex]) ? $rowData[$valueIndex] : null;
  108. }
  109. }
  110. return $data;
  111. }
  112. /**
  113. * Saving data row array into file
  114. *
  115. * @param string $file
  116. * @param array $data
  117. * @return $this
  118. * @throws \Magento\Framework\Exception\FileSystemException
  119. * @deprecated 102.0.0
  120. * @see appendData
  121. */
  122. public function saveData($file, $data)
  123. {
  124. return $this->appendData($file, $data, 'w');
  125. }
  126. /**
  127. * Replace the saveData method by allowing to select the input mode
  128. *
  129. * @param string $file
  130. * @param array $data
  131. * @param string $mode
  132. *
  133. * @return $this
  134. *
  135. * @throws \Magento\Framework\Exception\FileSystemException
  136. */
  137. public function appendData($file, $data, $mode = 'w')
  138. {
  139. $fileHandler = fopen($file, $mode);
  140. foreach ($data as $dataRow) {
  141. $this->file->filePutCsv($fileHandler, $dataRow, $this->_delimiter, $this->_enclosure);
  142. }
  143. fclose($fileHandler);
  144. return $this;
  145. }
  146. }