Пример #1
0
 /**
  * Returns the Location for the provided IPv4 address.
  *
  * @param string $ip4 IPv4 address.
  *
  * @return Location
  * @throws ProviderGeoIpNotFound
  */
 public function getLocationFromIPv4($ip4)
 {
     // get by starting block
     $subNet = $this->str($ip4)->explode('.')->removeLast()->implode('.')->val();
     $ipStart = $subNet . '.0';
     // get by start ip
     $ipLong = ip2long($ip4);
     $cityBlock = CityBlockIp4Entity::findOne(['rangeStart' => ip2long($ipStart)]);
     // verify that end ip is within range
     if (!$cityBlock || !($ipLong <= $cityBlock->rangeEnd)) {
         throw new ProviderGeoIpNotFound('GeoIp entry not found');
     }
     // get location info
     $locationInfo = LocationEntity::findOne(['geoId' => $cityBlock->geoId]);
     // populate location variable
     $location = new Location();
     $location->setContinent($locationInfo->continentCode, $locationInfo->continentName);
     $location->setCountry($locationInfo->countryCode, $locationInfo->countryName);
     $location->setCityName($locationInfo->cityName);
     $location->setSubdivision1($locationInfo->subdivision1Code, $locationInfo->subdivision1Name);
     $location->setSubdivision2($locationInfo->subdivision2Code, $locationInfo->subdivision2Name);
     $location->setTimeZone($locationInfo->timeZone);
     return $location;
 }