Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
CHANGELOG
=========

2.13.0
-------------------

* The model class names are no longer constructed by concatenating strings.
This change was made to improve support for tools like PHP-Scoper.
Reported by Andrew Mead. GitHub #194.

2.12.2 (2021-11-30)
-------------------

Expand Down
45 changes: 25 additions & 20 deletions src/Database/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

use GeoIp2\Exception\AddressNotFoundException;
use GeoIp2\Model\AbstractModel;
use GeoIp2\Model\AnonymousIp;
use GeoIp2\Model\Asn;
use GeoIp2\Model\City;
use GeoIp2\Model\ConnectionType;
use GeoIp2\Model\Country;
use GeoIp2\Model\Domain;
use GeoIp2\Model\Enterprise;
use GeoIp2\Model\Isp;
use GeoIp2\ProviderInterface;
use MaxMind\Db\Reader as DbReader;
use MaxMind\Db\Reader\InvalidDatabaseException;
Expand Down Expand Up @@ -80,10 +88,10 @@ public function __construct(
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function city(string $ipAddress): \GeoIp2\Model\City
public function city(string $ipAddress): City
{
// @phpstan-ignore-next-line
return $this->modelFor('City', 'City', $ipAddress);
return $this->modelFor(City::class, 'City', $ipAddress);
}

/**
Expand All @@ -96,10 +104,10 @@ public function city(string $ipAddress): \GeoIp2\Model\City
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function country(string $ipAddress): \GeoIp2\Model\Country
public function country(string $ipAddress): Country
{
// @phpstan-ignore-next-line
return $this->modelFor('Country', 'Country', $ipAddress);
return $this->modelFor(Country::class, 'Country', $ipAddress);
}

/**
Expand All @@ -112,11 +120,11 @@ public function country(string $ipAddress): \GeoIp2\Model\Country
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function anonymousIp(string $ipAddress): \GeoIp2\Model\AnonymousIp
public function anonymousIp(string $ipAddress): AnonymousIp
{
// @phpstan-ignore-next-line
return $this->flatModelFor(
'AnonymousIp',
AnonymousIp::class,
'GeoIP2-Anonymous-IP',
$ipAddress
);
Expand All @@ -132,11 +140,11 @@ public function anonymousIp(string $ipAddress): \GeoIp2\Model\AnonymousIp
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function asn(string $ipAddress): \GeoIp2\Model\Asn
public function asn(string $ipAddress): Asn
{
// @phpstan-ignore-next-line
return $this->flatModelFor(
'Asn',
Asn::class,
'GeoLite2-ASN',
$ipAddress
);
Expand All @@ -152,11 +160,11 @@ public function asn(string $ipAddress): \GeoIp2\Model\Asn
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function connectionType(string $ipAddress): \GeoIp2\Model\ConnectionType
public function connectionType(string $ipAddress): ConnectionType
{
// @phpstan-ignore-next-line
return $this->flatModelFor(
'ConnectionType',
ConnectionType::class,
'GeoIP2-Connection-Type',
$ipAddress
);
Expand All @@ -172,11 +180,11 @@ public function connectionType(string $ipAddress): \GeoIp2\Model\ConnectionType
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function domain(string $ipAddress): \GeoIp2\Model\Domain
public function domain(string $ipAddress): Domain
{
// @phpstan-ignore-next-line
return $this->flatModelFor(
'Domain',
Domain::class,
'GeoIP2-Domain',
$ipAddress
);
Expand All @@ -192,10 +200,10 @@ public function domain(string $ipAddress): \GeoIp2\Model\Domain
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function enterprise(string $ipAddress): \GeoIp2\Model\Enterprise
public function enterprise(string $ipAddress): Enterprise
{
// @phpstan-ignore-next-line
return $this->modelFor('Enterprise', 'Enterprise', $ipAddress);
return $this->modelFor(Enterprise::class, 'Enterprise', $ipAddress);
}

/**
Expand All @@ -208,11 +216,11 @@ public function enterprise(string $ipAddress): \GeoIp2\Model\Enterprise
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function isp(string $ipAddress): \GeoIp2\Model\Isp
public function isp(string $ipAddress): Isp
{
// @phpstan-ignore-next-line
return $this->flatModelFor(
'Isp',
Isp::class,
'GeoIP2-ISP',
$ipAddress
);
Expand All @@ -225,8 +233,6 @@ private function modelFor(string $class, string $type, string $ipAddress): Abstr
$record['traits']['ip_address'] = $ipAddress;
$record['traits']['prefix_len'] = $prefixLen;

$class = 'GeoIp2\\Model\\' . $class;

return new $class($record, $this->locales);
}

Expand All @@ -236,15 +242,14 @@ private function flatModelFor(string $class, string $type, string $ipAddress): A

$record['ip_address'] = $ipAddress;
$record['prefix_len'] = $prefixLen;
$class = 'GeoIp2\\Model\\' . $class;

return new $class($record);
}

private function getRecord(string $class, string $type, string $ipAddress): array
{
if (strpos($this->dbType, $type) === false) {
$method = lcfirst($class);
$method = lcfirst((new \ReflectionClass($class))->getShortName());

throw new \BadMethodCallException(
"The $method method cannot be used to open a {$this->dbType} database"
Expand Down
11 changes: 5 additions & 6 deletions src/WebService/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private function userAgent(): string
public function city(string $ipAddress = 'me'): City
{
// @phpstan-ignore-next-line
return $this->responseFor('city', 'City', $ipAddress);
return $this->responseFor('city', City::class, $ipAddress);
}

/**
Expand Down Expand Up @@ -168,7 +168,7 @@ public function city(string $ipAddress = 'me'): City
*/
public function country(string $ipAddress = 'me'): Country
{
return $this->responseFor('country', 'Country', $ipAddress);
return $this->responseFor('country', Country::class, $ipAddress);
}

/**
Expand Down Expand Up @@ -199,15 +199,16 @@ public function country(string $ipAddress = 'me'): Country
public function insights(string $ipAddress = 'me'): Insights
{
// @phpstan-ignore-next-line
return $this->responseFor('insights', 'Insights', $ipAddress);
return $this->responseFor('insights', Insights::class, $ipAddress);
}

private function responseFor(string $endpoint, string $class, string $ipAddress): Country
{
$path = implode('/', [self::$basePath, $endpoint, $ipAddress]);

try {
$body = $this->client->get('GeoIP2 ' . $class, $path);
$service = (new \ReflectionClass($class))->getShortName();
$body = $this->client->get('GeoIP2 ' . $service, $path);
} catch (\MaxMind\Exception\IpAddressNotFoundException $ex) {
throw new AddressNotFoundException(
$ex->getMessage(),
Expand Down Expand Up @@ -249,8 +250,6 @@ private function responseFor(string $endpoint, string $class, string $ipAddress)
);
}

$class = 'GeoIp2\\Model\\' . $class;

return new $class($body, $this->locales);
}
}