You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Country.php 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Models\Locale;
  3. use Illuminate\Database\Eloquent\Relations\{BelongsTo, HasMany};
  4. use Illuminate\Support\Collection;
  5. use Squire\Model;
  6. /**
  7. * @property int $id
  8. * @property string $name
  9. * @property string $iso_code_3
  10. * @property string $iso_code_2
  11. * @property int $numeric_code
  12. * @property string $phone_code
  13. * @property string $capital
  14. * @property string $currency_code
  15. * @property string $native_name
  16. * @property string $nationality
  17. * @property float $latitude
  18. * @property float $longitude
  19. * @property string $flag
  20. */
  21. class Country extends Model
  22. {
  23. public static array $schema = [
  24. 'id' => 'integer',
  25. 'name' => 'string',
  26. 'iso_code_3' => 'string',
  27. 'iso_code_2' => 'string',
  28. 'numeric_code' => 'integer',
  29. 'phone_code' => 'string',
  30. 'capital' => 'string',
  31. 'currency_code' => 'string',
  32. 'native_name' => 'string',
  33. 'nationality' => 'string',
  34. 'latitude' => 'float',
  35. 'longitude' => 'float',
  36. 'flag' => 'string',
  37. ];
  38. public function currency(): BelongsTo
  39. {
  40. return $this->belongsTo(Currency::class, 'currency_code', 'code');
  41. }
  42. public function states(): HasMany
  43. {
  44. return $this->hasMany(State::class, 'country_id', 'id');
  45. }
  46. public function cities(): HasMany
  47. {
  48. return $this->hasMany(City::class, 'country_id', 'id');
  49. }
  50. public function timezones(): HasMany
  51. {
  52. return $this->hasMany(Timezone::class, 'country_id', 'id');
  53. }
  54. public static function findByIsoCode2(string $code): ?self
  55. {
  56. return self::where('iso_code_2', $code)->first();
  57. }
  58. public static function getAllCountryCodes(): Collection
  59. {
  60. return self::all()->pluck('iso_code_2');
  61. }
  62. public static function getAvailableCountryOptions(): array
  63. {
  64. return self::all()->mapWithKeys(static function ($country): array {
  65. return [$country->iso_code_2 => $country->name . ' ' . $country->flag];
  66. })->toArray();
  67. }
  68. }