| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace App;
- use Exception;
- /**
- * Class Address
- * @property int $id
- * @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 float $latitude
- * @property float $longitude
- */
- class Address extends Model {
- protected static $table = "address";
- protected static $idField = "id";
- protected static $fillableFields = ["address_1", "address_2", "city", "county", "postal_code", "state", "country", "latitude", "longitude"];
- // Static methods
- public static function getCountryOptions(): array {
- return [
- "GB" => __("United Kingdom"),
- "AU" => __("Australia"),
- "BG" => __("Bulgaria"),
- "CA" => __("Canada"),
- "CN" => __("China"),
- "CZ" => __("Czech Republic"),
- "DK" => __("Denmark"),
- "FR" => __("France"),
- "DE" => __("Germany"),
- "HK" => __("Hong Kong"),
- "IE" => __("Ireland"),
- "IT" => __("Italy"),
- "JP" => __("Japan"),
- "NZ" => __("New Zealand"),
- "NO" => __("Norway"),
- "PL" => __("Poland"),
- "ES" => __("Spain"),
- "SE" => __("Sweden"),
- "CH" => __("Switzerland"),
- "US" => __("United States"),
- ];
- }
- // Magic methods
- public function __toString(): string {
- return implode("\n", $this->getLines());
- }
- // Object methods
- public function hasGeolocation(): bool {
- return isset($this->latitude, $this->longitude);
- }
- public function geocode(): ?array {
- try {
- $results = Geolocation::geocode($this->postal_code);
- $result = $results[0];
- $this->latitude = $result->geometry->location->lat;
- $this->longitude = $result->geometry->location->lng;
- return [$this->latitude, $this->longitude];
- }
- catch(Exception $e) {
- return null;
- }
- }
- private function getDisplayCountry(): string {
- if($this->country) {
- return self::getCountryOptions()[$this->country] ?? "";
- }
- return "";
- }
- public function getLines(): array {
- $county_parts = array_filter([$this->county, $this->state, $this->postal_code]);
- return array_values(array_filter([
- $this->address_1,
- $this->address_2,
- $this->city,
- implode(", ", $county_parts),
- $this->getDisplayCountry()
- ]));
- }
- public function getLinesHtml(): string {
- return nl2br(htmlspecialchars(implode("\n", $this->getLines())));
- }
- }
|