選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Country.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace App\Models\Locale;
  3. use App\Models\Common\Address;
  4. use Illuminate\Database\Eloquent\Casts\Attribute;
  5. use Illuminate\Database\Eloquent\Relations\HasMany;
  6. use Illuminate\Support\Collection;
  7. use Squire\Model;
  8. use Symfony\Component\Intl\Countries;
  9. use Symfony\Component\Intl\Locales;
  10. /**
  11. * @property string $id
  12. * @property string $name
  13. * @property string $iso_code_3
  14. * @property string $iso_code_2
  15. * @property int $numeric_code
  16. * @property string $phone_code
  17. * @property string $capital
  18. * @property string $currency_code
  19. * @property string $native_name
  20. * @property string $nationality
  21. * @property float $latitude
  22. * @property float $longitude
  23. * @property string $flag
  24. */
  25. class Country extends Model
  26. {
  27. public static array $schema = [
  28. 'id' => 'string',
  29. 'name' => 'string',
  30. 'iso_code_3' => 'string',
  31. 'iso_code_2' => 'string',
  32. 'numeric_code' => 'integer',
  33. 'phone_code' => 'string',
  34. 'capital' => 'string',
  35. 'currency_code' => 'string',
  36. 'native_name' => 'string',
  37. 'nationality' => 'string',
  38. 'latitude' => 'float',
  39. 'longitude' => 'float',
  40. 'flag' => 'string',
  41. ];
  42. public function addresses(): HasMany
  43. {
  44. return $this->hasMany(Address::class, 'country_code', 'id');
  45. }
  46. public function states(): HasMany
  47. {
  48. return $this->hasMany(State::class, 'country_id', 'id');
  49. }
  50. protected function name(): Attribute
  51. {
  52. return Attribute::get(static function (mixed $value, array $attributes): string {
  53. $exists = Countries::exists($attributes['id']);
  54. return $exists ? Countries::getName($attributes['id']) : $value;
  55. });
  56. }
  57. public static function findByIsoCode2(string $code): ?self
  58. {
  59. return self::where('id', $code)->first();
  60. }
  61. public static function getAllCountryCodes(): Collection
  62. {
  63. return self::query()
  64. ->select('id')
  65. ->pluck('id');
  66. }
  67. public static function getAvailableCountryOptions(): array
  68. {
  69. return self::query()
  70. ->select(['id', 'name', 'flag'])
  71. ->orderBy('name')
  72. ->get()
  73. ->mapWithKeys(static fn ($country) => [
  74. $country->id => $country->name . ' ' . $country->flag,
  75. ])
  76. ->toArray();
  77. }
  78. public static function getSearchResultsUsing(string $search): array
  79. {
  80. return self::query()
  81. ->select(['id', 'name', 'flag'])
  82. ->where(static function ($query) use ($search) {
  83. $query->whereLike('name', "%{$search}%")
  84. ->orWhereLike('id', "%{$search}%");
  85. })
  86. ->orderByRaw('
  87. CASE
  88. WHEN id = ? THEN 1
  89. WHEN id LIKE ? THEN 2
  90. WHEN name LIKE ? THEN 3
  91. ELSE 4
  92. END
  93. ', [$search, $search . '%', $search . '%'])
  94. ->limit(50)
  95. ->get()
  96. ->mapWithKeys(static fn (self $country) => [
  97. $country->id => $country->name . ' ' . $country->flag,
  98. ])
  99. ->toArray();
  100. }
  101. public static function getLanguagesByCountryCode(?string $code = null): array
  102. {
  103. if (! $code) {
  104. return Locales::getNames();
  105. }
  106. $locales = Locales::getNames();
  107. $languages = [];
  108. foreach (array_keys($locales) as $locale) {
  109. $localeRegion = locale_get_region($locale);
  110. $localeLanguage = locale_get_primary_language($locale);
  111. if ($localeRegion === $code) {
  112. $languages[$localeLanguage] = Locales::getName($localeLanguage);
  113. }
  114. }
  115. return $languages;
  116. }
  117. }