$id, "user_id" => $user->id ]); } // Object methods /** * @return User|null * @throws Exception */ public function getUser(): ?User { return User::get($this->user_id); } /** * @return string */ public function getDisplayAddress(): string { $parts = []; $fields = ["address_1", "address_2", "city", "county", "postal_code", "state", "country"]; foreach($fields as $field) { $parts[] = $this->$field; } return implode(", ", array_filter($parts)); } /** * @return array */ public function getGeolocation(): array { return [$this->latitude, $this->longitude]; } public function geocode(): bool { try { $results = Geolocation::geocode($this->postal_code . ", " . $this->country); $result = $results[0]; foreach($result->address_components as $component) { $types = $component->types; if(in_array("postal_code", $types)) $this->postal_code = $component->long_name; if(in_array("route", $types)) $this->address_1 = $component->long_name; if(in_array("postal_town", $types)) $this->city = $component->long_name; if(in_array("administrative_area_level_1", $types)) $this->state = $component->long_name; if(in_array("country", $types)) $this->country = $component->long_name; } $this->latitude = $result->geometry->location->lat; $this->longitude = $result->geometry->location->lng; return true; } catch(Exception $e) { return false; } } }