| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- /**
- * Class UserAddress
- *
- * @property int $id
- * @property int $user_id
- * @property string $name
- * @property string $address_1
- * @property string $address_2
- * @property string $city
- * @property string $county
- * @property string $postal_code
- * @property string $state
- * @property string $country
- * @property double $latitude
- * @property double $longitude
- * @property float $max_radius_miles
- * @property bool $invoice
- */
- class UserAddress extends Model {
- protected static $table = "user_address";
- protected static $id_field = "id";
- protected static $fillable_fields = ["user_id", "name", "address_1", "address_2", "city", "county", "postal_code", "state", "country", "latitude", "longitude", "max_radius_miles", "invoice"];
- const RADIUSES = [
- "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",
- ];
- // 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;
- }
- }
- }
|