|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\ImportExport\Test\Fixture; |
| 9 | + |
| 10 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 11 | +use Magento\Framework\DataObject; |
| 12 | +use Magento\Framework\DataObjectFactory; |
| 13 | +use Magento\Framework\Filesystem; |
| 14 | +use Magento\TestFramework\Fixture\Data\ProcessorInterface; |
| 15 | +use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface; |
| 16 | + |
| 17 | +class CsvFile implements RevertibleDataFixtureInterface |
| 18 | +{ |
| 19 | + private const DEFAULT_DATA = [ |
| 20 | + 'directory' => DirectoryList::TMP, |
| 21 | + 'path' => 'import/%uniqid%.csv', |
| 22 | + 'rows' => [], |
| 23 | + ]; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var Filesystem |
| 27 | + */ |
| 28 | + private Filesystem $filesystem; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var ProcessorInterface |
| 32 | + */ |
| 33 | + private ProcessorInterface $dataProcessor; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var DataObjectFactory |
| 37 | + */ |
| 38 | + private DataObjectFactory $dataObjectFactory; |
| 39 | + |
| 40 | + /** |
| 41 | + * @param Filesystem $filesystem |
| 42 | + * @param ProcessorInterface $dataProcessor |
| 43 | + * @param DataObjectFactory $dataObjectFactory |
| 44 | + */ |
| 45 | + public function __construct( |
| 46 | + Filesystem $filesystem, |
| 47 | + ProcessorInterface $dataProcessor, |
| 48 | + DataObjectFactory $dataObjectFactory |
| 49 | + ) { |
| 50 | + $this->filesystem = $filesystem; |
| 51 | + $this->dataProcessor = $dataProcessor; |
| 52 | + $this->dataObjectFactory = $dataObjectFactory; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * {@inheritdoc} |
| 57 | + * @param array $data Parameters. Same format as CsvFile::DEFAULT_DATA. |
| 58 | + * Additional fields: |
| 59 | + * - $data['rows']: CSV data to be written into the file in the following format: |
| 60 | + * - headers are listed in the first array and the following array |
| 61 | + * [ |
| 62 | + * ['col1', 'col2'], |
| 63 | + * ['row1col1', 'row1col2'], |
| 64 | + * ] |
| 65 | + * - headers are listed as array keys |
| 66 | + * [ |
| 67 | + * ['col1' => 'row1col1', 'col2' => 'row1col2'], |
| 68 | + * ['col1' => 'row2col1', 'col2' => 'row2col2'], |
| 69 | + * [ |
| 70 | + * |
| 71 | + * @see CsvFile::DEFAULT_DATA |
| 72 | + */ |
| 73 | + public function apply(array $data = []): ?DataObject |
| 74 | + { |
| 75 | + $data = $this->dataProcessor->process($this, array_merge(self::DEFAULT_DATA, $data)); |
| 76 | + $rows = $data['rows']; |
| 77 | + $row = reset($rows); |
| 78 | + |
| 79 | + if (array_is_list($row)) { |
| 80 | + $cols = $row; |
| 81 | + $colsCount = count($cols); |
| 82 | + foreach ($rows as $row) { |
| 83 | + if ($colsCount !== count($row)) { |
| 84 | + throw new \InvalidArgumentException('Arrays in "rows" must be the same size'); |
| 85 | + } |
| 86 | + } |
| 87 | + } else { |
| 88 | + $cols = array_keys($row); |
| 89 | + $lines[] = $cols; |
| 90 | + foreach ($rows as $row) { |
| 91 | + $line = []; |
| 92 | + if (array_diff($cols, array_keys($row))) { |
| 93 | + throw new \InvalidArgumentException('Arrays in "rows" must have same keys'); |
| 94 | + } |
| 95 | + foreach ($cols as $field) { |
| 96 | + $line[] = $row[$field]; |
| 97 | + } |
| 98 | + $lines[] = $line; |
| 99 | + } |
| 100 | + $rows = $lines; |
| 101 | + } |
| 102 | + $directory = $this->filesystem->getDirectoryWrite($data['directory']); |
| 103 | + $file = $directory->openFile($data['path'], 'w+'); |
| 104 | + foreach ($rows as $row) { |
| 105 | + $file->writeCsv($row); |
| 106 | + } |
| 107 | + $file->close(); |
| 108 | + $data['absolute_path'] = $directory->getAbsolutePath($data['path']); |
| 109 | + |
| 110 | + return $this->dataObjectFactory->create(['data' => $data]); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @inheritDoc |
| 115 | + */ |
| 116 | + public function revert(DataObject $data): void |
| 117 | + { |
| 118 | + $directory = $this->filesystem->getDirectoryWrite($data['directory']); |
| 119 | + $directory->delete($data['path']); |
| 120 | + } |
| 121 | +} |
0 commit comments