|
6 | 6 | use Illuminate\Console\Command; |
7 | 7 | use Illuminate\Support\Facades\Storage; |
8 | 8 | use InvalidArgumentException; |
| 9 | +use App\Support\StudyRegionResolver; |
9 | 10 |
|
10 | 11 | class SplitWorldHeritageJson extends Command |
11 | 12 | { |
@@ -703,74 +704,54 @@ private function normalizeSiteRowImportReady(array $row, int $siteId): array |
703 | 704 | $category = $this->normalizeCategoryOrFallback($row['category'] ?? null); |
704 | 705 | $criteria = $this->resolveCriteriaList($row); |
705 | 706 |
|
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 | | - |
719 | 707 | $iso2List = $this->extractIsoCodes($row['iso_codes'] ?? null); |
| 708 | + $stateNames = $this->normalizeStatesNames($row['states_names'] ?? null); |
| 709 | + $countryName = $stateNames[0] ?? null; |
720 | 710 |
|
721 | 711 | $stateParty = null; |
722 | 712 | $country = null; |
723 | 713 | if (count($iso2List) === 1) { |
724 | 714 | $iso3 = $this->toIso3OrNull($iso2List[0]); |
725 | 715 | $stateParty = $iso3; |
726 | | - $country = $iso3; |
| 716 | + $country = $countryName ?? $iso3; |
727 | 717 | } |
728 | 718 |
|
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 | | - |
745 | 719 | return [ |
746 | 720 | 'id' => $siteId, |
747 | 721 | 'official_name' => $this->stringOrFallback($row['official_name'] ?? ($row['name_en'] ?? null), (string) $siteId), |
748 | 722 | 'name' => $this->stringOrFallback($row['name_en'] ?? null, (string) $siteId), |
749 | 723 | 'name_jp' => $this->stringOrNull($row['name_jp'] ?? null), |
| 724 | + 'study_region' => $countryName |
| 725 | + ? StudyRegionResolver::resolve($countryName)->value |
| 726 | + : null, |
750 | 727 | 'country' => $country, |
751 | 728 | 'region' => $region, |
752 | 729 | 'state_party' => $stateParty, |
753 | 730 | 'category' => $category, |
754 | 731 | '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, |
761 | 737 | '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, |
765 | 740 | 'unesco_site_url' => $this->stringOrNull($row['unesco_site_url'] ?? ($row['url'] ?? null)), |
766 | | - 'created_at' => null, |
767 | | - 'updated_at' => null, |
768 | | - 'deleted_at' => null, |
769 | 741 | ]; |
770 | 742 | } |
771 | 743 |
|
772 | 744 | private function mergeSiteRowPreferExisting(array $existing, array $incoming): array |
773 | 745 | { |
| 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 | + |
774 | 755 | $fill = function (string $key, mixed $value) use (&$existing): void { |
775 | 756 | if ((!array_key_exists($key, $existing) || $existing[$key] === null || $existing[$key] === '') && ($value !== null && $value !== '')) { |
776 | 757 | $existing[$key] = $value; |
|
0 commit comments