summaryrefslogtreecommitdiffstats
path: root/vendor/geoip2/geoip2/src/Database/Reader.php
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--vendor/geoip2/geoip2/src/Database/Reader.php104
1 files changed, 58 insertions, 46 deletions
diff --git a/vendor/geoip2/geoip2/src/Database/Reader.php b/vendor/geoip2/geoip2/src/Database/Reader.php
index 9c33e3d..40d32bc 100644
--- a/vendor/geoip2/geoip2/src/Database/Reader.php
+++ b/vendor/geoip2/geoip2/src/Database/Reader.php
@@ -1,8 +1,19 @@
<?php
+declare(strict_types=1);
+
namespace GeoIp2\Database;
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;
@@ -33,8 +44,19 @@ use MaxMind\Db\Reader\InvalidDatabaseException;
*/
class Reader implements ProviderInterface
{
+ /**
+ * @var DbReader
+ */
private $dbReader;
+
+ /**
+ * @var string
+ */
private $dbType;
+
+ /**
+ * @var array<string>
+ */
private $locales;
/**
@@ -48,8 +70,8 @@ class Reader implements ProviderInterface
* is corrupt or invalid
*/
public function __construct(
- $filename,
- $locales = ['en']
+ string $filename,
+ array $locales = ['en']
) {
$this->dbReader = new DbReader($filename);
$this->dbType = $this->dbReader->metadata()->databaseType;
@@ -65,12 +87,11 @@ class Reader implements ProviderInterface
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
- *
- * @return \GeoIp2\Model\City
*/
- public function city($ipAddress)
+ public function city(string $ipAddress): City
{
- return $this->modelFor('City', 'City', $ipAddress);
+ // @phpstan-ignore-next-line
+ return $this->modelFor(City::class, 'City', $ipAddress);
}
/**
@@ -82,12 +103,11 @@ class Reader implements ProviderInterface
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
- *
- * @return \GeoIp2\Model\Country
*/
- public function country($ipAddress)
+ public function country(string $ipAddress): Country
{
- return $this->modelFor('Country', 'Country', $ipAddress);
+ // @phpstan-ignore-next-line
+ return $this->modelFor(Country::class, 'Country', $ipAddress);
}
/**
@@ -99,13 +119,12 @@ class Reader implements ProviderInterface
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
- *
- * @return \GeoIp2\Model\AnonymousIp
*/
- public function anonymousIp($ipAddress)
+ public function anonymousIp(string $ipAddress): AnonymousIp
{
+ // @phpstan-ignore-next-line
return $this->flatModelFor(
- 'AnonymousIp',
+ AnonymousIp::class,
'GeoIP2-Anonymous-IP',
$ipAddress
);
@@ -120,13 +139,12 @@ class Reader implements ProviderInterface
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
- *
- * @return \GeoIp2\Model\Asn
*/
- public function asn($ipAddress)
+ public function asn(string $ipAddress): Asn
{
+ // @phpstan-ignore-next-line
return $this->flatModelFor(
- 'Asn',
+ Asn::class,
'GeoLite2-ASN',
$ipAddress
);
@@ -141,13 +159,12 @@ class Reader implements ProviderInterface
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
- *
- * @return \GeoIp2\Model\ConnectionType
*/
- public function connectionType($ipAddress)
+ public function connectionType(string $ipAddress): ConnectionType
{
+ // @phpstan-ignore-next-line
return $this->flatModelFor(
- 'ConnectionType',
+ ConnectionType::class,
'GeoIP2-Connection-Type',
$ipAddress
);
@@ -162,13 +179,12 @@ class Reader implements ProviderInterface
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
- *
- * @return \GeoIp2\Model\Domain
*/
- public function domain($ipAddress)
+ public function domain(string $ipAddress): Domain
{
+ // @phpstan-ignore-next-line
return $this->flatModelFor(
- 'Domain',
+ Domain::class,
'GeoIP2-Domain',
$ipAddress
);
@@ -183,12 +199,11 @@ class Reader implements ProviderInterface
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
- *
- * @return \GeoIp2\Model\Enterprise
*/
- public function enterprise($ipAddress)
+ public function enterprise(string $ipAddress): Enterprise
{
- return $this->modelFor('Enterprise', 'Enterprise', $ipAddress);
+ // @phpstan-ignore-next-line
+ return $this->modelFor(Enterprise::class, 'Enterprise', $ipAddress);
}
/**
@@ -200,50 +215,47 @@ class Reader implements ProviderInterface
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
- *
- * @return \GeoIp2\Model\Isp
*/
- public function isp($ipAddress)
+ public function isp(string $ipAddress): Isp
{
+ // @phpstan-ignore-next-line
return $this->flatModelFor(
- 'Isp',
+ Isp::class,
'GeoIP2-ISP',
$ipAddress
);
}
- private function modelFor($class, $type, $ipAddress)
+ private function modelFor(string $class, string $type, string $ipAddress): AbstractModel
{
- list($record, $prefixLen) = $this->getRecord($class, $type, $ipAddress);
+ [$record, $prefixLen] = $this->getRecord($class, $type, $ipAddress);
$record['traits']['ip_address'] = $ipAddress;
$record['traits']['prefix_len'] = $prefixLen;
- $class = 'GeoIp2\\Model\\' . $class;
-
return new $class($record, $this->locales);
}
- private function flatModelFor($class, $type, $ipAddress)
+ private function flatModelFor(string $class, string $type, string $ipAddress): AbstractModel
{
- list($record, $prefixLen) = $this->getRecord($class, $type, $ipAddress);
+ [$record, $prefixLen] = $this->getRecord($class, $type, $ipAddress);
$record['ip_address'] = $ipAddress;
$record['prefix_len'] = $prefixLen;
- $class = 'GeoIp2\\Model\\' . $class;
return new $class($record);
}
- private function getRecord($class, $type, $ipAddress)
+ 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"
);
}
- list($record, $prefixLen) = $this->dbReader->getWithPrefixLen($ipAddress);
+ [$record, $prefixLen] = $this->dbReader->getWithPrefixLen($ipAddress);
if ($record === null) {
throw new AddressNotFoundException(
"The address $ipAddress is not in the database."
@@ -272,7 +284,7 @@ class Reader implements ProviderInterface
*
* @return \MaxMind\Db\Reader\Metadata object for the database
*/
- public function metadata()
+ public function metadata(): DbReader\Metadata
{
return $this->dbReader->metadata();
}
@@ -280,7 +292,7 @@ class Reader implements ProviderInterface
/**
* Closes the GeoIP2 database and returns the resources to the system.
*/
- public function close()
+ public function close(): void
{
$this->dbReader->close();
}