Address.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App;
  3. use Exception;
  4. /**
  5. * Class Address
  6. * @property int $id
  7. * @property string $address_1
  8. * @property string $address_2
  9. * @property string $city
  10. * @property string $county
  11. * @property string $postal_code
  12. * @property string $state
  13. * @property string $country
  14. * @property float $latitude
  15. * @property float $longitude
  16. */
  17. class Address extends Model {
  18. protected static $table = "address";
  19. protected static $idField = "id";
  20. protected static $fillableFields = ["address_1", "address_2", "city", "county", "postal_code", "state", "country", "latitude", "longitude"];
  21. // Static methods
  22. public static function getCountryOptions(): array {
  23. return [
  24. "FR" => __("France"),
  25. "DE" => __("Germany"),
  26. "JP" => __("Japan"),
  27. "ES" => __("Spain"),
  28. "GB" => __("United Kingdom"),
  29. ];
  30. }
  31. // Object methods
  32. public function hasGeolocation(): bool {
  33. return isset($this->latitude, $this->longitude);
  34. }
  35. public function geocode(): ?array {
  36. try {
  37. $results = Geolocation::geocode($this->postal_code);
  38. $result = $results[0];
  39. $this->latitude = $result->geometry->location->lat;
  40. $this->longitude = $result->geometry->location->lng;
  41. return [$this->latitude, $this->longitude];
  42. }
  43. catch(Exception $e) {
  44. return null;
  45. }
  46. }
  47. }