| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
if (!defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* Import/export functionality delegated to focused services: |
| 10 |
* - ABJ_404_Solution_ExportService for export-side calls |
| 11 |
* - ABJ_404_Solution_ImportService for import-side calls |
| 12 |
* |
| 13 |
* Standalone class composed from PluginLogic. Holds factories so the |
| 14 |
* underlying services are constructed lazily on first use. |
| 15 |
*/ |
| 16 |
class ABJ_404_Solution_PluginLogicImportExport { |
| 17 |
|
| 18 |
/** @var callable */ |
| 19 |
private $exportServiceFactory; |
| 20 |
|
| 21 |
/** @var callable */ |
| 22 |
private $importServiceFactory; |
| 23 |
|
| 24 |
/** @var ABJ_404_Solution_ExportService|null */ |
| 25 |
private $exportService = null; |
| 26 |
|
| 27 |
/** @var ABJ_404_Solution_ImportService|null */ |
| 28 |
private $importService = null; |
| 29 |
|
| 30 |
/** |
| 31 |
* @param callable $exportServiceFactory Returns an ABJ_404_Solution_ExportService instance. |
| 32 |
* @param callable $importServiceFactory Returns an ABJ_404_Solution_ImportService instance. |
| 33 |
*/ |
| 34 |
function __construct(callable $exportServiceFactory, callable $importServiceFactory) { |
| 35 |
$this->exportServiceFactory = $exportServiceFactory; |
| 36 |
$this->importServiceFactory = $importServiceFactory; |
| 37 |
} |
| 38 |
|
| 39 |
/** @return ABJ_404_Solution_ExportService */ |
| 40 |
private function getExportService() { |
| 41 |
if ($this->exportService === null) { |
| 42 |
$this->exportService = ($this->exportServiceFactory)(); |
| 43 |
} |
| 44 |
return $this->exportService; |
| 45 |
} |
| 46 |
|
| 47 |
/** @return ABJ_404_Solution_ImportService */ |
| 48 |
private function getImportService() { |
| 49 |
if ($this->importService === null) { |
| 50 |
$this->importService = ($this->importServiceFactory)(); |
| 51 |
} |
| 52 |
return $this->importService; |
| 53 |
} |
| 54 |
|
| 55 |
/** @return string */ |
| 56 |
function getExportFilename(string $format = 'native'): string { |
| 57 |
return $this->getExportService()->getExportFilename($format); |
| 58 |
} |
| 59 |
|
| 60 |
/** @return void */ |
| 61 |
function doExport(): void { |
| 62 |
$this->getExportService()->doExport(); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* @param string $sourceFile Native export file path. |
| 67 |
* @param string $destinationFile Output file path. |
| 68 |
* @return string Empty string on success, error message otherwise. |
| 69 |
*/ |
| 70 |
function convertExportCsvToRedirectionFormat($sourceFile, $destinationFile) { |
| 71 |
return $this->getExportService()->convertExportCsvToRedirectionFormat($sourceFile, $destinationFile); |
| 72 |
} |
| 73 |
|
| 74 |
/** @return string */ |
| 75 |
function doImportFile(): string { |
| 76 |
return $this->getImportService()->doImportFile(); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* @param array<string, mixed> $dataArray |
| 81 |
* @param bool $dryRun |
| 82 |
* @return array<int, string> |
| 83 |
*/ |
| 84 |
function loadDataArrayFromFile(array $dataArray, bool $dryRun = false): array { |
| 85 |
return $this->getImportService()->loadDataArrayFromFile($dataArray, $dryRun); |
| 86 |
} |
| 87 |
|
| 88 |
/** @return array<string, string> */ |
| 89 |
function splitCsvLine(string $line): array { |
| 90 |
return $this->getImportService()->splitCsvLine($line); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* @param array<int, string> $columns |
| 95 |
* @return bool |
| 96 |
*/ |
| 97 |
function isCompatibleImportHeaderRow(array $columns): bool { |
| 98 |
return $this->getImportService()->isCompatibleImportHeaderRow($columns); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* @param array<int, string> $columns |
| 103 |
* @return array<int, string> |
| 104 |
*/ |
| 105 |
function normalizeImportHeaders(array $columns): array { |
| 106 |
$result = $this->getImportService()->normalizeImportHeaders($columns); |
| 107 |
return array_map(function ($v) { |
| 108 |
return is_string($v) ? $v : ''; |
| 109 |
}, $result); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* @param array<int, string> $row |
| 114 |
* @param array<int, string> $normalizedHeaders |
| 115 |
* @return array<string, string> |
| 116 |
*/ |
| 117 |
function mapImportRowByHeaders(array $row, array $normalizedHeaders): array { |
| 118 |
return $this->getImportService()->mapImportRowByHeaders($row, $normalizedHeaders); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* @param array<int, string> $columns |
| 123 |
* @return string |
| 124 |
*/ |
| 125 |
function detectImportFormatFromHeaders(array $columns): string { |
| 126 |
return $this->getImportService()->detectImportFormatFromHeaders($columns); |
| 127 |
} |
| 128 |
|
| 129 |
} |
| 130 |
|