Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LocationDataLoader.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Helpers;
  3. use Illuminate\Support\Collection;
  4. use Illuminate\Support\Facades\Cache;
  5. use Illuminate\Support\Facades\Log;
  6. class LocationDataLoader
  7. {
  8. protected static ?array $countries = null;
  9. protected static ?array $states = null;
  10. protected static ?array $cities = null;
  11. public static function loadData(string $type): void
  12. {
  13. // Try to get data from cache first.
  14. static::${$type} = Cache::remember("location_data_{$type}", now()->addMinutes(30), static function () use ($type) {
  15. $csvPath = resource_path("data/{$type}.csv");
  16. $data = [];
  17. try {
  18. $handle = fopen($csvPath, 'rb');
  19. // Get the header of the CSV file
  20. $header = fgetcsv($handle);
  21. // Read each line of the CSV
  22. while (($row = fgetcsv($handle)) !== false) {
  23. $data[] = array_combine($header, $row);
  24. }
  25. fclose($handle);
  26. return $data;
  27. } catch (\Exception $e) {
  28. Log::error("CSV reading failed for {$type}: {$e->getMessage()}");
  29. return [];
  30. }
  31. });
  32. }
  33. public static function getCountry($countryCode, $hydrate = true)
  34. {
  35. static::loadData('countries');
  36. $countryCode = strtoupper($countryCode);
  37. $country = collect(static::$countries)->firstWhere('iso2', $countryCode);
  38. return $hydrate ? new Country($country) : $country;
  39. }
  40. public static function getAllCountries($hydrate = true): Collection
  41. {
  42. static::loadData('countries');
  43. $countries = collect(static::$countries);
  44. return $hydrate ? $countries->map(static fn ($country) => new Country($country)) : $countries;
  45. }
  46. public static function getState($countryCode, $stateCode, $hydrate = true)
  47. {
  48. static::loadData('states');
  49. $countryCode = strtoupper($countryCode);
  50. $stateCode = strtoupper($stateCode);
  51. $state = collect(static::$states)->first(static function ($item) use ($countryCode, $stateCode) {
  52. return $item['country_code'] === $countryCode && $item['state_code'] === $stateCode;
  53. });
  54. if ($state) {
  55. return $hydrate ? new State($state) : $state;
  56. }
  57. return null;
  58. }
  59. public static function getAllStates($countryCode, $hydrate = true): Collection
  60. {
  61. static::loadData('states');
  62. $countryCode = strtoupper($countryCode);
  63. $states = collect(static::$states)->where('country_code', $countryCode);
  64. if ($states->isEmpty()) {
  65. return collect();
  66. }
  67. if ($hydrate) {
  68. return $states->map(static fn ($state) => new State($state));
  69. }
  70. return $states;
  71. }
  72. public static function getCity($cityId, $hydrate = true)
  73. {
  74. static::loadData('cities');
  75. $city = collect(static::$cities)->firstWhere('id', $cityId);
  76. if ($city) {
  77. return $hydrate ? new City($city) : $city;
  78. }
  79. return null;
  80. }
  81. public static function getAllCities($countryCode, $stateCode, $hydrate = true): Collection
  82. {
  83. static::loadData('cities');
  84. // Filter cities based on country and state codes
  85. $filteredCities = collect(static::$cities)
  86. ->where('country_code', strtoupper($countryCode))
  87. ->where('state_code', strtoupper($stateCode));
  88. if ($filteredCities->isEmpty()) {
  89. return collect();
  90. }
  91. return $hydrate ? $filteredCities->map(static fn ($city) => new City($city)) : $filteredCities;
  92. }
  93. }