CsvMulty.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Csv parse
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Framework\File;
  12. class CsvMulty extends \Magento\Framework\File\Csv
  13. {
  14. /**
  15. * Retrieve CSV file data as pairs with duplicates
  16. *
  17. * @param string $file
  18. * @param int $keyIndex
  19. * @param int $valueIndex
  20. * @return array
  21. */
  22. public function getDataPairs($file, $keyIndex = 0, $valueIndex = 1)
  23. {
  24. $data = [];
  25. $csvData = $this->getData($file);
  26. $lineNumber = 0;
  27. foreach ($csvData as $rowData) {
  28. $lineNumber++;
  29. if (isset($rowData[$keyIndex])) {
  30. if (isset($data[$rowData[$keyIndex]])) {
  31. if (isset($data[$rowData[$keyIndex]]['duplicate'])) {
  32. $data[$rowData[$keyIndex]]['duplicate']['line'] .= ', ' . $lineNumber;
  33. } else {
  34. $tmpValue = $data[$rowData[$keyIndex]]['value'];
  35. $tmpLine = $data[$rowData[$keyIndex]]['line'];
  36. $data[$rowData[$keyIndex]]['duplicate'] = [];
  37. $data[$rowData[$keyIndex]]['duplicate']['line'] = $tmpLine . ' ,' . $lineNumber;
  38. $data[$rowData[$keyIndex]]['duplicate']['value'] = $tmpValue;
  39. }
  40. } else {
  41. $data[$rowData[$keyIndex]] = [];
  42. $data[$rowData[$keyIndex]]['line'] = $lineNumber;
  43. $data[$rowData[$keyIndex]]['value'] = isset($rowData[$valueIndex]) ? $rowData[$valueIndex] : null;
  44. }
  45. }
  46. }
  47. return $data;
  48. }
  49. }