|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Laravelcm\AbstractIpGeolocation\DataObject; |
| 6 | + |
| 7 | +final class GeolocationData |
| 8 | +{ |
| 9 | + public ?string $ipAddress = null; |
| 10 | + |
| 11 | + public ?string $city = null; |
| 12 | + |
| 13 | + public ?int $cityGeonameId = null; |
| 14 | + |
| 15 | + public ?string $region = null; |
| 16 | + |
| 17 | + public ?string $regionIsoCode = null; |
| 18 | + |
| 19 | + public ?int $regionGeonameId = null; |
| 20 | + |
| 21 | + public ?string $postalCode = null; |
| 22 | + |
| 23 | + public ?string $country = null; |
| 24 | + |
| 25 | + public ?string $countryCode = null; |
| 26 | + |
| 27 | + public ?bool $countryIsEU = null; |
| 28 | + |
| 29 | + public ?string $continent = null; |
| 30 | + |
| 31 | + public ?string $continentCode = null; |
| 32 | + |
| 33 | + public ?int $continentGeonameId = null; |
| 34 | + |
| 35 | + public ?float $longitude = null; |
| 36 | + |
| 37 | + public ?float $latitude = null; |
| 38 | + |
| 39 | + public ?Security $security = null; |
| 40 | + |
| 41 | + public ?Timezone $timezone = null; |
| 42 | + |
| 43 | + public ?Flag $flag = null; |
| 44 | + |
| 45 | + public ?Currency $currency = null; |
| 46 | + |
| 47 | + public ?Connection $connection = null; |
| 48 | + |
| 49 | + public function __construct( |
| 50 | + ?string $ipAddress = null, |
| 51 | + ?string $city = null, |
| 52 | + ?int $cityGeonameId = null, |
| 53 | + ?string $region = null, |
| 54 | + ?string $regionIsoCode = null, |
| 55 | + ?int $regionGeonameId = null, |
| 56 | + ?string $postalCode = null, |
| 57 | + ?string $country = null, |
| 58 | + ?string $countryCode = null, |
| 59 | + ?bool $countryIsEU = null, |
| 60 | + ?string $continent = null, |
| 61 | + ?string $continentCode = null, |
| 62 | + ?int $continentGeonameId = null, |
| 63 | + ?float $longitude = null, |
| 64 | + ?float $latitude = null, |
| 65 | + ?Security $security = null, |
| 66 | + ?Timezone $timezone = null, |
| 67 | + ?Flag $flag = null, |
| 68 | + ?Currency $currency = null, |
| 69 | + ?Connection $connection = null, |
| 70 | + ) { |
| 71 | + $this->ipAddress = $ipAddress; |
| 72 | + $this->city = $city; |
| 73 | + $this->cityGeonameId = $cityGeonameId; |
| 74 | + $this->regionIsoCode = $regionIsoCode; |
| 75 | + $this->regionGeonameId = $regionGeonameId; |
| 76 | + $this->postalCode = $postalCode; |
| 77 | + $this->region = $region; |
| 78 | + $this->country = $country; |
| 79 | + $this->countryCode = $countryCode; |
| 80 | + $this->countryIsEU = $countryIsEU; |
| 81 | + $this->continent = $continent; |
| 82 | + $this->continentCode = $continentCode; |
| 83 | + $this->continentGeonameId = $continentGeonameId; |
| 84 | + $this->longitude = $longitude; |
| 85 | + $this->latitude = $latitude; |
| 86 | + $this->security = $security; |
| 87 | + $this->timezone = $timezone; |
| 88 | + $this->flag = $flag; |
| 89 | + $this->currency = $currency; |
| 90 | + $this->connection = $connection; |
| 91 | + } |
| 92 | + |
| 93 | + public static function fromResponse(array $data): GeolocationData |
| 94 | + { |
| 95 | + return new self( |
| 96 | + ipAddress: self::valueIfExist('ip_address', $data), |
| 97 | + city: self::valueIfExist('city', $data), |
| 98 | + cityGeonameId: self::valueIfExist('city_geoname_id', $data), |
| 99 | + region: self::valueIfExist('region', $data), |
| 100 | + regionIsoCode: self::valueIfExist('region_iso_code', $data), |
| 101 | + regionGeonameId: self::valueIfExist('region_geoname_id', $data), |
| 102 | + postalCode: self::valueIfExist('postal_code', $data), |
| 103 | + country: self::valueIfExist('country', $data), |
| 104 | + countryCode: self::valueIfExist('country_code', $data), |
| 105 | + countryIsEU: self::valueIfExist('country_is_eu', $data), |
| 106 | + continent: self::valueIfExist('continent', $data), |
| 107 | + continentCode: self::valueIfExist('continent', $data), |
| 108 | + continentGeonameId: self::valueIfExist('country_geoname_id', $data), |
| 109 | + longitude: self::valueIfExist('longitude', $data), |
| 110 | + latitude: self::valueIfExist('latitude', $data), |
| 111 | + security: array_key_exists('security', $data) |
| 112 | + ? new Security(isVpn: $data['security']['is_vpn']) |
| 113 | + : null, |
| 114 | + timezone: array_key_exists('timezone', $data) |
| 115 | + ? new Timezone( |
| 116 | + name: $data['timezone']['name'], |
| 117 | + abbreviation: $data['timezone']['abbreviation'], |
| 118 | + gmtOffset: $data['timezone']['gmt_offset'], |
| 119 | + currentTime: $data['timezone']['current_time'], |
| 120 | + isDST: $data['timezone']['is_dst'], |
| 121 | + ) |
| 122 | + : null, |
| 123 | + flag: array_key_exists('flag', $data) |
| 124 | + ? new Flag( |
| 125 | + svg: $data['flag']['svg'], |
| 126 | + png: $data['flag']['png'], |
| 127 | + emoji: $data['flag']['emoji'], |
| 128 | + unicode: $data['flag']['unicode'], |
| 129 | + ) |
| 130 | + : null, |
| 131 | + currency: array_key_exists('currency', $data) |
| 132 | + ? new Currency( |
| 133 | + name: $data['currency']['currency_name'], |
| 134 | + code: $data['currency']['currency_code'] |
| 135 | + ) |
| 136 | + : null, |
| 137 | + connection: array_key_exists('connection', $data) |
| 138 | + ? new Connection( |
| 139 | + connectionType: $data['connection']['connection_type'], |
| 140 | + autonomousSystemNumber: $data['connection']['autonomous_system_number'], |
| 141 | + autonomousSystemOrganization: $data['connection']['autonomous_system_organization'], |
| 142 | + ispName: $data['connection']['isp_name'], |
| 143 | + organizationName: $data['connection']['organization_name'], |
| 144 | + ) |
| 145 | + : null, |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + protected static function valueIfExist(string $key, array $data): mixed |
| 150 | + { |
| 151 | + return array_key_exists($key, $data) ? $data[$key] : null; |
| 152 | + } |
| 153 | +} |
0 commit comments