Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Country.php 3.9KB

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