| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Services; |
| 6 |
|
| 7 |
/** |
| 8 |
* Tracks old → new primary keys during import so foreign keys can be rewritten. |
| 9 |
*/ |
| 10 |
final class ExportImportIdMapper |
| 11 |
{ |
| 12 |
/** @var array<string, array<int, int>> */ |
| 13 |
private array $maps = []; |
| 14 |
|
| 15 |
public function remember(string $entity, int $oldId, int $newId): void |
| 16 |
{ |
| 17 |
if ($oldId <= 0 || $newId <= 0) { |
| 18 |
return; |
| 19 |
} |
| 20 |
$this->maps[$entity][$oldId] = $newId; |
| 21 |
} |
| 22 |
|
| 23 |
public function map(string $entity, $oldId): ?int |
| 24 |
{ |
| 25 |
if ($oldId === null || $oldId === '') { |
| 26 |
return null; |
| 27 |
} |
| 28 |
$old = (int) $oldId; |
| 29 |
if ($old <= 0) { |
| 30 |
return null; |
| 31 |
} |
| 32 |
|
| 33 |
return $this->maps[$entity][$old] ?? null; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* For nullable FKs: unmapped IDs become null to avoid pointing at wrong rows. |
| 38 |
* |
| 39 |
* @param mixed $oldId |
| 40 |
*/ |
| 41 |
public function mapFkNullable(string $entity, $oldId): ?int |
| 42 |
{ |
| 43 |
if ($oldId === null || $oldId === '') { |
| 44 |
return null; |
| 45 |
} |
| 46 |
if ((int) $oldId === 0) { |
| 47 |
return null; |
| 48 |
} |
| 49 |
|
| 50 |
return $this->map($entity, $oldId); |
| 51 |
} |
| 52 |
} |
| 53 |
|