user_address.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Class UserAddress
  4. *
  5. * @property int $id
  6. * @property int $user_id
  7. * @property string $name
  8. * @property string $address_1
  9. * @property string $address_2
  10. * @property string $city
  11. * @property string $county
  12. * @property string $postal_code
  13. * @property string $state
  14. * @property string $country
  15. * @property double $latitude
  16. * @property double $longitude
  17. * @property float $max_radius_miles
  18. * @property bool $invoice
  19. */
  20. class UserAddress extends Model {
  21. protected static $table = "user_address";
  22. protected static $id_field = "id";
  23. protected static $fillable_fields = ["user_id", "name", "address_1", "address_2", "city", "county", "postal_code", "state", "country", "latitude", "longitude", "max_radius_miles", "invoice"];
  24. const RADIUSES = [
  25. "0.25", "0.50", "1.00", "1.50", "2.00", "2.50", "3.00", "4.00", "5.00", "7.50", "10.00", "12.50", "15.00", "20.00", "25.00",
  26. ];
  27. // Static methods
  28. /**
  29. * @param int $id
  30. * @param User $user
  31. * @return object|UserAddress|null
  32. * @throws Exception
  33. */
  34. public static function getByUser(int $id, User $user) {
  35. return self::queryOne([
  36. "id" => $id,
  37. "user_id" => $user->id
  38. ]);
  39. }
  40. // Object methods
  41. /**
  42. * @return User|null
  43. * @throws Exception
  44. */
  45. public function getUser(): ?User {
  46. return User::get($this->user_id);
  47. }
  48. /**
  49. * @return string
  50. */
  51. public function getDisplayAddress(): string {
  52. $parts = [];
  53. $fields = ["address_1", "address_2", "city", "county", "postal_code", "state", "country"];
  54. foreach($fields as $field) {
  55. $parts[] = $this->$field;
  56. }
  57. return implode(", ", array_filter($parts));
  58. }
  59. /**
  60. * @return array
  61. */
  62. public function getGeolocation(): array {
  63. return [$this->latitude, $this->longitude];
  64. }
  65. public function geocode(): bool {
  66. try {
  67. $results = Geolocation::geocode($this->postal_code . ", " . $this->country);
  68. $result = $results[0];
  69. foreach($result->address_components as $component) {
  70. $types = $component->types;
  71. if(in_array("postal_code", $types)) $this->postal_code = $component->long_name;
  72. if(in_array("route", $types)) $this->address_1 = $component->long_name;
  73. if(in_array("postal_town", $types)) $this->city = $component->long_name;
  74. if(in_array("administrative_area_level_1", $types)) $this->state = $component->long_name;
  75. if(in_array("country", $types)) $this->country = $component->long_name;
  76. }
  77. $this->latitude = $result->geometry->location->lat;
  78. $this->longitude = $result->geometry->location->lng;
  79. return true;
  80. }
  81. catch(Exception $e) {
  82. return false;
  83. }
  84. }
  85. }