| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?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",
- ];
- // Static methods
- /**
- * @param int $id
- * @param User $user
- * @return object|UserAddress|null
- * @throws Exception
- */
- public static function getByUser(int $id, User $user) {
- return self::queryOne([
- "id" => $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;
- }
- }
- }
|