@@ -30,7 +30,7 @@ class TerritoryReferenceSeeder extends Seeder
|
||||
|
||||
$records = $data['records'];
|
||||
|
||||
$actualCounts = is_array($records) ? array_count_values(array_column($records, 'location_type')) : [];
|
||||
$actualCounts = array_count_values(array_column($records, 'location_type'));
|
||||
ksort($actualCounts);
|
||||
$expectedCounts = self::EXPECTED_COUNTS;
|
||||
ksort($expectedCounts);
|
||||
@@ -41,7 +41,6 @@ class TerritoryReferenceSeeder extends Seeder
|
||||
|| ! is_string($data['records_sha256'] ?? null)
|
||||
|| ! hash_equals(self::RECORDS_CHECKSUM, $data['records_sha256'])
|
||||
|| ! hash_equals(self::RECORDS_CHECKSUM, hash('sha256', json_encode($records, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR)))
|
||||
|| ! is_array($records)
|
||||
|| $actualCounts !== $expectedCounts) {
|
||||
throw new RuntimeException('Territory reference dataset failed its source or completeness checks.');
|
||||
}
|
||||
@@ -113,7 +112,7 @@ class TerritoryReferenceSeeder extends Seeder
|
||||
return $counts;
|
||||
});
|
||||
|
||||
$this->command?->info(sprintf(
|
||||
$this->command->info(sprintf(
|
||||
'Referências territoriais: %d criadas, %d já existentes, %d classificadas como públicas.',
|
||||
$counts['created'],
|
||||
$counts['unchanged'],
|
||||
@@ -124,9 +123,16 @@ class TerritoryReferenceSeeder extends Seeder
|
||||
/** @param array<string, mixed> $record
|
||||
* @param array<string, bool> $seen
|
||||
* @param array<string, string> $types
|
||||
*
|
||||
* @param-out array<string, bool> $seen
|
||||
* @param-out array<string, string> $types
|
||||
*/
|
||||
private function validateRecord(array $record, array &$seen, array &$types): void
|
||||
{
|
||||
$id = $record['id'] ?? null;
|
||||
if (! is_string($id) || $id === '') {
|
||||
throw new RuntimeException('Territory reference is missing a valid id.');
|
||||
}
|
||||
foreach (['id', 'name', 'location_type', 'code', 'source_code'] as $field) {
|
||||
if (! isset($record[$field]) || $record[$field] === '') {
|
||||
throw new RuntimeException('Territory reference is missing '.$field.'.');
|
||||
@@ -142,7 +148,7 @@ class TerritoryReferenceSeeder extends Seeder
|
||||
'locality' => 'admin_post',
|
||||
];
|
||||
|
||||
if (! array_key_exists($type, $parentTypes) || isset($seen[$record['id']])) {
|
||||
if (! is_string($type) || ! array_key_exists($type, $parentTypes) || isset($seen[$id])) {
|
||||
throw new RuntimeException('Territory reference has an invalid type or duplicate ID.');
|
||||
}
|
||||
|
||||
@@ -152,11 +158,11 @@ class TerritoryReferenceSeeder extends Seeder
|
||||
if ($record['parent_id'] !== null) {
|
||||
throw new RuntimeException('Country reference cannot have a parent.');
|
||||
}
|
||||
} elseif (! $record['parent_id'] || ($types[$record['parent_id']] ?? null) !== $expectedParent) {
|
||||
throw new RuntimeException('Territory reference has a missing or invalid parent: '.$record['id']);
|
||||
} elseif (! is_string($record['parent_id'] ?? null) || ($types[$record['parent_id']] ?? null) !== $expectedParent) {
|
||||
throw new RuntimeException('Territory reference has a missing or invalid parent: '.$id);
|
||||
}
|
||||
|
||||
$seen[$record['id']] = true;
|
||||
$types[$record['id']] = $type;
|
||||
$seen[$id] = true;
|
||||
$types[$id] = $type;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user