CsvReader example

$progress = $this->export($context, ProductDefinition::ENTITY_NAME, null, null, $profile->getId());

        static::assertImportExportSucceeded($progress$this->getInvalidLogContent($progress->getInvalidRecordsLogId()));

        $filesystem = $this->getContainer()->get('shopware.filesystem.private');
        $logfile = $this->getLogEntity($progress->getLogId())->getFile();
        static::assertInstanceOf(ImportExportFileEntity::class$logfile);
        $csv = $filesystem->read($logfile->getPath());
        static::assertIsString($csv);
        $resource = fopen('data://text/plain;base64,' . base64_encode($csv), 'rb');
        static::assertIsResource($resource);
        $reader = new CsvReader();
        $record = null;
        foreach ($reader->read(new Config([][][])$resource, 0) as $row) {
            $record = $row;

            break;
        }

        static::assertNotNull($record);
        static::assertEquals('foo', $record['custom_field_1']);
        static::assertEquals('23', $record['custom_field_2']);
        static::assertEquals('1', $record['custom_field_3']);
        
/** * @return array<array<string, mixed>> */
    protected function getInvalidLogContent(?string $invalidLogId): array
    {
        if (!$invalidLogId) {
            return [];
        }

        $logEntity = $this->getLogEntity($invalidLogId);
        $config = Config::fromLog($logEntity);
        $reader = new CsvReader();
        $filesystem = $this->getContainer()->get('shopware.filesystem.private');

        /** @var ImportExportFileEntity $file */
        $file = $logEntity->getFile();
        $resource = $filesystem->readStream($file->getPath());
        $log = $reader->read($config$resource, 0);

        return $log instanceof \Traversable ? iterator_to_array($log) : [];
    }

    /** * @param array<array<string, string>> $customFields */

    private const BOM_UTF8 = "\xEF\xBB\xBF";

    public function testSimpleCsv(): void
    {
        $content = implode(\PHP_EOL, [
            'foo;bar',
            '1;2',
            '"asdf";"zxcv"',
        ]);

        $reader = new CsvReader();
        $resource = fopen('data://text/plain,' . $content, 'rb');
        $result = $this->getAll($reader->read(new Config([][][])$resource, 0));

        static::assertCount(2, $result);
        static::assertSame(['foo' => '1', 'bar' => '2']$result[0]);
        static::assertSame(['foo' => 'asdf', 'bar' => 'zxcv']$result[1]);
    }

    public function testIncremental(): void
    {
        $content = 'foo;bar' . \PHP_EOL;
        
namespace Shopware\Core\Content\ImportExport\Processing\Reader;

use Shopware\Core\Content\ImportExport\Aggregate\ImportExportLog\ImportExportLogEntity;
use Shopware\Core\Framework\Log\Package;

#[Package('system-settings')] class CsvReaderFactory extends AbstractReaderFactory
{
    public function create(ImportExportLogEntity $logEntity): AbstractReader
    {
        return new CsvReader();
    }

    public function supports(ImportExportLogEntity $logEntity): bool
    {
        return $logEntity->getProfile()->getFileType() === 'text/csv';
    }
}
return $result->first();
    }

    private function printErrors(ImportExport $importExport, ImportExportLogEntity $log, SymfonyStyle $io, bool $deleteLog): void
    {
        if (!$importExport->getLogEntity()->getInvalidRecordsLog() || !$log->getFile()) {
            return;
        }

        $config = Config::fromLog($importExport->getLogEntity()->getInvalidRecordsLog());
        $reader = new CsvReader();
        $invalidLogFilePath = $log->getFile()->getPath() . '_invalid';
        $resource = $this->filesystem->readStream($invalidLogFilePath);

        $invalidRows = $reader->read($config$resource, 0);

        foreach ($invalidRows as $invalidRow) {
            $io->note($invalidRow['_error']);
            $io->newLine();
        }

        if ($deleteLog) {
            
Home | Imprint | This part of the site doesn't use cookies.