diff --git a/composer.json b/composer.json index 2964e1e..dc9fe33 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ ], "require": { "guzzle/guzzle": "3.*", - "maxmind-db/reader": "0.*", + "maxmind-db/reader": "dev-master", "php": ">=5.3.1" }, "require-dev": { diff --git a/src/GeoIp2/Database/Reader.php b/src/GeoIp2/Database/Reader.php index 8165060..799d9a0 100644 --- a/src/GeoIp2/Database/Reader.php +++ b/src/GeoIp2/Database/Reader.php @@ -7,6 +7,7 @@ use GeoIp2\Model\City; use GeoIp2\Model\CityIspOrg; use GeoIp2\Model\Country; use GeoIp2\Model\Omni; +use MaxMind\Db\Reader as DbReader; /** * Instances of this class provide a reader for the GeoIP2 database format. @@ -53,7 +54,7 @@ class Reader $filename, $languages = array('en') ) { - $this->dbReader = new MaxMind\Db\Reader($filename); + $this->dbReader = new DbReader($filename); $this->languages = $languages; } @@ -71,7 +72,7 @@ class Reader */ public function city($ipAddress) { - return $this->responseFor('City', $ipAddress); + return $this->modelFor('City', $ipAddress); } /** @@ -88,7 +89,7 @@ class Reader */ public function country($ipAddress) { - return $this->responseFor('Country', $ipAddress); + return $this->modelFor('Country', $ipAddress); } /** @@ -105,7 +106,7 @@ class Reader */ public function cityIspOrg($ipAddress) { - return $this->responseFor('CityIspOrg', $ipAddress); + return $this->modelFor('CityIspOrg', $ipAddress); } /** @@ -122,19 +123,25 @@ class Reader */ public function omni($ipAddress) { - return $this->responseFor('Omni', $ipAddress); + return $this->modelFor('Omni', $ipAddress); } - private function responseFor($class, $ipAddress) + private function modelFor($class, $ipAddress) { $record = $this->dbReader->get($ipAddress); if ($record === null) { throw new AddressNotFoundException( - "$ipAddress was not found in the database." + "The address $ipAddress is not in the database." ); } + $record['traits']['ip_address'] = $ipAddress; $class = "GeoIp2\\Model\\" . $class; return new $class($record, $this->languages); } + + public function close() + { + $this->dbReader->close(); + } } diff --git a/src/GeoIp2/Exception/AddressNotFoundException.php b/src/GeoIp2/Exception/AddressNotFoundException.php index bbdc559..d548338 100644 --- a/src/GeoIp2/Exception/AddressNotFoundException.php +++ b/src/GeoIp2/Exception/AddressNotFoundException.php @@ -5,6 +5,6 @@ namespace GeoIp2\Exception; /** * This class represents a generic error. */ -class AddressNotFoundException extends \GeoIp2Exception +class AddressNotFoundException extends GeoIp2Exception { } diff --git a/tests/GeoIp2/Test/Database/ReaderTest.php b/tests/GeoIp2/Test/Database/ReaderTest.php new file mode 100644 index 0000000..369e8cc --- /dev/null +++ b/tests/GeoIp2/Test/Database/ReaderTest.php @@ -0,0 +1,46 @@ +city('81.2.69.160'); + $this->assertEquals('London', $city->city->name); + $reader->close(); + } + + public function testLanguageList() + { + $reader = new Reader( + 'maxmind-db/test-data/GeoIP2-City.mmdb', + array('xx', 'ru', 'pt-BR', 'es', 'en') + ); + $omni = $reader->omni('81.2.69.160'); + $this->assertEquals('Лондон', $omni->city->name); + $reader->close(); + } + + public function testHasIpAddress() + { + $reader = new Reader('maxmind-db/test-data/GeoIP2-City.mmdb'); + $cio = $reader->cityIspOrg('81.2.69.160'); + $this->assertEquals('81.2.69.160', $cio->traits->ipAddress); + $reader->close(); + } + + /** + * @expectedException GeoIp2\Exception\AddressNotFoundException + * @expectedExceptionMessage The address 10.10.10.10 is not in the database. + */ + public function testUnknownAddress() + { + $reader = new Reader('maxmind-db/test-data/GeoIP2-City.mmdb'); + $reader->city('10.10.10.10'); + $reader->close(); + } +}