Skip to content

Commit 5f4dde4

Browse files
committed
feat: populate study_region during world heritage import pipeline
1 parent 5f93216 commit 5f4dde4

4 files changed

Lines changed: 86 additions & 82 deletions

File tree

src/app/Console/Commands/ImportCountriesFromSplitFile.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Console\Commands;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Config;
67
use Illuminate\Support\Facades\DB;
78
use Illuminate\Support\Facades\Storage;
89

@@ -58,13 +59,20 @@ public function handle(): int
5859
if ($max > 0 && $imported >= $max) {
5960
break;
6061
}
61-
if (!is_array($row)) { $skipped++; continue; }
62+
63+
if (!is_array($row)) {
64+
$skipped++;
65+
continue;
66+
}
6267

6368
$code = strtoupper(trim((string) ($row['state_party_code'] ?? '')));
6469
if ($code === '' || strlen($code) !== 3) {
6570
$skipped++;
6671
if ($strict) {
67-
$this->error("Strict: invalid state_party_code: " . json_encode($row, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
72+
$this->error(
73+
'Strict: invalid state_party_code: ' .
74+
json_encode($row, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
75+
);
6876
return self::FAILURE;
6977
}
7078
continue;
@@ -78,6 +86,15 @@ public function handle(): int
7886
$nameEn = $code;
7987
}
8088

89+
if ($nameJp === null) {
90+
$nameJp = $this->resolveCountryNameJapanese($code);
91+
}
92+
93+
if ($strict && $nameJp === null) {
94+
$this->error("Strict: name_jp could not be resolved for state_party_code [{$code}]");
95+
return self::FAILURE;
96+
}
97+
8198
$batch[] = [
8299
'state_party_code' => $code,
83100
'name_en' => $nameEn,
@@ -164,6 +181,16 @@ private function toNullableString(mixed $v): ?string
164181
}
165182

166183
$s = trim($v);
184+
167185
return $s === '' ? null : $s;
168186
}
187+
188+
private function resolveCountryNameJapanese(string $iso3): ?string
189+
{
190+
$countryNameJa = Config::get('country_ja.alpha3_to_country.' . strtoupper(trim($iso3)));
191+
192+
return is_string($countryNameJa) && $countryNameJa !== ''
193+
? $countryNameJa
194+
: null;
195+
}
169196
}

src/app/Console/Commands/ImportWorldHeritageFromJson.php

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Models\WorldHeritage;
66
use Carbon\Carbon;
77
use Illuminate\Console\Command;
8+
use App\Support\StudyRegionResolver;
89

910
class ImportWorldHeritageFromJson extends Command
1011
{
@@ -133,26 +134,17 @@ private function mapFromUnescoApiRow(array $row): array
133134
$id = $row['id_no'] ?? null;
134135
$lat = $row['coordinates']['lat'] ?? null;
135136
$lon = $row['coordinates']['lon'] ?? null;
136-
$criteriaRaw = $row['criteria_txt'] ?? $row['criteria'] ?? null;
137-
$stateParty = $row['states'] ?? $row['state_party'] ?? null;
138-
if (is_array($stateParty)) {
139-
$stateParty = $stateParty[0] ?? null;
140-
}
141137

142-
$stateParty = is_string($stateParty) ? strtoupper(trim($stateParty)) : null;
143-
if ($stateParty === '') {
144-
$stateParty = null;
145-
}
146-
if ($stateParty !== null && !preg_match('/^[A-Z]{3}$/', $stateParty)) {
147-
$stateParty = null;
148-
}
138+
$countryName = $this->extractCountryName($row);
139+
$statePartyIso3 = $this->extractIso3StateParty($row);
149140

150141
return [
151142
'id' => $this->toNullableInt($id),
152143
'official_name' => $row['official_name'] ?? null,
153144
'name' => $row['name_en'] ?? $row['name'] ?? null,
154145
'region' => $row['region_en'] ?? $row['region'] ?? null,
155-
'state_party' => $stateParty,
146+
'state_party' => $statePartyIso3,
147+
'study_region' => StudyRegionResolver::resolve($countryName)->value,
156148
'category' => $row['category'] ?? $row['type'] ?? null,
157149
'criteria' => $row['criteria'] ?? null,
158150
'year_inscribed' => $this->toNullableInt($row['date_inscribed'] ?? $row['year_inscribed'] ?? null),
@@ -168,29 +160,6 @@ private function mapFromUnescoApiRow(array $row): array
168160
];
169161
}
170162

171-
private function criteriaFromTxt(mixed $raw): array
172-
{
173-
if ($raw === null) {
174-
return [];
175-
}
176-
177-
$s = trim((string) $raw);
178-
if ($s === '') {
179-
return [];
180-
}
181-
182-
preg_match_all('/\(([^)]+)\)/', $s, $m);
183-
if (isset($m[1]) && $m[1] !== []) {
184-
return array_values(array_filter(array_map(fn($v) => trim((string) $v), $m[1])));
185-
}
186-
187-
$s = trim($s, " \t\n\r\0\x0B()");
188-
if ($s === '') {
189-
return [];
190-
}
191-
return [$s];
192-
}
193-
194163
private function flushBatch(array $batch): int
195164
{
196165
$updateColumns = array_values(array_diff(array_keys($batch[0]), ['id']));
@@ -241,6 +210,26 @@ private function extractIso3StateParty(array $row): ?string
241210
return null;
242211
}
243212

213+
private function extractCountryName(array $row): ?string
214+
{
215+
$states = $row['states'] ?? $row['state_party'] ?? null;
216+
217+
if (is_string($states)) {
218+
$normalized = trim($states);
219+
return $normalized !== '' ? $normalized : null;
220+
}
221+
222+
if (is_array($states)) {
223+
$first = $states[0] ?? null;
224+
if (is_string($first)) {
225+
$normalized = trim($first);
226+
return $normalized !== '' ? $normalized : null;
227+
}
228+
}
229+
230+
return null;
231+
}
232+
244233
private function toNullableInt(mixed $v): ?int
245234
{
246235
if ($v === null || $v === '') {

src/app/Console/Commands/ImportWorldHeritageSiteFromSplitFile.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public function handle(): int
7878
'official_name' => $this->toNullableString($row['official_name'] ?? null),
7979
'name' => $this->toNullableString($row['name'] ?? null),
8080
'name_jp' => $this->toNullableString($row['name_jp'] ?? null),
81+
'study_region' => $this->toNullableString($row['study_region'] ?? null),
8182
'country' => $this->toNullableString($row['country'] ?? null),
8283
'region' => $this->toNullableString($row['region'] ?? null),
8384
'state_party' => $this->toNullableString($row['state_party'] ?? null),
@@ -200,23 +201,29 @@ private function toNullableFloat(mixed $v): ?float
200201
private function toNullableBoolInt(mixed $v): ?int
201202
{
202203
if ($v === null || $v === '') {
203-
return null;
204+
return 0;
204205
}
206+
205207
if (is_bool($v)) {
206208
return $v ? 1 : 0;
207209
}
210+
208211
if (is_int($v) || is_float($v)) {
209212
return ((int) $v) === 1 ? 1 : 0;
210213
}
214+
211215
if (is_string($v)) {
212216
$s = strtolower(trim($v));
217+
213218
if (in_array($s, ['1', 'true', 't', 'yes', 'y', 'on'], true)) {
214219
return 1;
215220
}
221+
216222
if (in_array($s, ['0', 'false', 'f', 'no', 'n', 'off'], true)) {
217223
return 0;
218224
}
219225
}
220-
return null;
226+
227+
return 0;
221228
}
222229
}

src/app/Console/Commands/SplitWorldHeritageJson.php

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Console\Command;
77
use Illuminate\Support\Facades\Storage;
88
use InvalidArgumentException;
9+
use App\Support\StudyRegionResolver;
910

1011
class SplitWorldHeritageJson extends Command
1112
{
@@ -703,74 +704,54 @@ private function normalizeSiteRowImportReady(array $row, int $siteId): array
703704
$category = $this->normalizeCategoryOrFallback($row['category'] ?? null);
704705
$criteria = $this->resolveCriteriaList($row);
705706

706-
$toBool = function ($v): bool {
707-
if (is_bool($v)) {
708-
return $v;
709-
}
710-
if (is_int($v) || is_float($v)) {
711-
return ((int) $v) === 1;
712-
}
713-
714-
$s = strtolower(trim((string) $v));
715-
return in_array($s, ['1', 'true', 't', 'yes', 'y', 'on'], true);
716-
};
717-
$toTinyInt = fn($v) => $toBool($v) ? 1 : 0;
718-
719707
$iso2List = $this->extractIsoCodes($row['iso_codes'] ?? null);
708+
$stateNames = $this->normalizeStatesNames($row['states_names'] ?? null);
709+
$countryName = $stateNames[0] ?? null;
720710

721711
$stateParty = null;
722712
$country = null;
723713
if (count($iso2List) === 1) {
724714
$iso3 = $this->toIso3OrNull($iso2List[0]);
725715
$stateParty = $iso3;
726-
$country = $iso3;
716+
$country = $countryName ?? $iso3;
727717
}
728718

729-
$main = $row['main_image_url']['url'] ?? null;
730-
$imageUrl = null;
731-
732-
if (is_string($main)) {
733-
$main = trim($main);
734-
if ($main !== '') {
735-
$imageUrl = mb_substr($main, 0, 255);
736-
}
737-
}
738-
739-
$primaryImageUrl = null;
740-
741-
$year = isset($row['date_inscribed']) && is_numeric($row['date_inscribed'])
742-
? (int) $row['date_inscribed']
743-
: 0;
744-
745719
return [
746720
'id' => $siteId,
747721
'official_name' => $this->stringOrFallback($row['official_name'] ?? ($row['name_en'] ?? null), (string) $siteId),
748722
'name' => $this->stringOrFallback($row['name_en'] ?? null, (string) $siteId),
749723
'name_jp' => $this->stringOrNull($row['name_jp'] ?? null),
724+
'study_region' => $countryName
725+
? StudyRegionResolver::resolve($countryName)->value
726+
: null,
750727
'country' => $country,
751728
'region' => $region,
752729
'state_party' => $stateParty,
753730
'category' => $category,
754731
'criteria' => $criteria,
755-
'year_inscribed' => $year,
756-
'area_hectares' => isset($row['area_hectares']) && is_numeric($row['area_hectares']) ? (float) $row['area_hectares'] : null,
757-
'buffer_zone_hectares' => isset($row['buffer_zone_hectares']) && is_numeric($row['buffer_zone_hectares']) ? (float) $row['buffer_zone_hectares'] : null,
758-
'is_endangered' => $toTinyInt($row['danger'] ?? $row['is_endangered'] ?? false),
759-
'latitude' => is_numeric($lat) ? (float) $lat : null,
760-
'longitude' => is_numeric($lon) ? (float) $lon : null,
732+
'year_inscribed' => (isset($row['date_inscribed']) && is_numeric($row['date_inscribed'])) ? (int) $row['date_inscribed'] : null,
733+
'area_hectares' => isset($row['area_hectares']) ? (is_numeric($row['area_hectares']) ? (float) $row['area_hectares'] : null) : null,
734+
'buffer_zone_hectares' => isset($row['buffer_zone_hectares']) ? (is_numeric($row['buffer_zone_hectares']) ? (float) $row['buffer_zone_hectares'] : null) : null,
735+
'latitude' => isset($lat) ? (is_numeric($lat) ? (float) $lat : null) : null,
736+
'longitude' => isset($lon) ? (is_numeric($lon) ? (float) $lon : null) : null,
761737
'short_description' => $this->stringOrNull($row['short_description_en'] ?? null),
762-
'image_url' => $imageUrl,
763-
'primary_image_url' => $primaryImageUrl,
764-
'thumbnail_image_id' => null,
738+
'image_url' => null,
739+
'primary_image_url' => null,
765740
'unesco_site_url' => $this->stringOrNull($row['unesco_site_url'] ?? ($row['url'] ?? null)),
766-
'created_at' => null,
767-
'updated_at' => null,
768-
'deleted_at' => null,
769741
];
770742
}
771743

772744
private function mergeSiteRowPreferExisting(array $existing, array $incoming): array
773745
{
746+
if (($existing['study_region'] ?? null) === null) {
747+
$stateNames = $this->normalizeStatesNames($incoming['states_names'] ?? null);
748+
$countryName = $stateNames[0] ?? null;
749+
750+
if ($countryName) {
751+
$existing['study_region'] = StudyRegionResolver::resolve($countryName)->value;
752+
}
753+
}
754+
774755
$fill = function (string $key, mixed $value) use (&$existing): void {
775756
if ((!array_key_exists($key, $existing) || $existing[$key] === null || $existing[$key] === '') && ($value !== null && $value !== '')) {
776757
$existing[$key] = $value;

0 commit comments

Comments
 (0)