From 22babda1c9db4f66ea0078b7824f3397babae2ba Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Tue, 17 Jun 2014 09:29:14 -0700 Subject: [PATCH 1/4] Added Connection-Type, Domain, and ISP databases --- composer.json | 2 +- maxmind-db | 2 +- src/GeoIp2/Database/Reader.php | 40 +++++++++++++++-- src/GeoIp2/Model/AbstractModel.php | 54 ++++++++++++++++++++++ src/GeoIp2/Model/ConnectionType.php | 31 +++++++++++++ src/GeoIp2/Model/Country.php | 55 +++++------------------ src/GeoIp2/Model/Domain.php | 31 +++++++++++++ src/GeoIp2/Model/Isp.php | 45 +++++++++++++++++++ tests/GeoIp2/Test/Database/ReaderTest.php | 40 +++++++++++++++++ 9 files changed, 249 insertions(+), 51 deletions(-) create mode 100644 src/GeoIp2/Model/AbstractModel.php create mode 100644 src/GeoIp2/Model/ConnectionType.php create mode 100644 src/GeoIp2/Model/Domain.php create mode 100644 src/GeoIp2/Model/Isp.php diff --git a/composer.json b/composer.json index a414985..381d883 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "php": ">=5.3.1" }, "require-dev": { - "phpunit/phpunit": "3.7.*", + "phpunit/phpunit": "4.1.*", "satooshi/php-coveralls": "dev-master" }, "autoload": { diff --git a/maxmind-db b/maxmind-db index f887fec..f86528a 160000 --- a/maxmind-db +++ b/maxmind-db @@ -1 +1 @@ -Subproject commit f887fec99eabf6d68746e257b894bae854fefc27 +Subproject commit f86528a05b883955a1c696ffe3bfeaa1224703d0 diff --git a/src/GeoIp2/Database/Reader.php b/src/GeoIp2/Database/Reader.php index a08b66a..edcca79 100644 --- a/src/GeoIp2/Database/Reader.php +++ b/src/GeoIp2/Database/Reader.php @@ -125,7 +125,42 @@ class Reader implements ProviderInterface return $this->modelFor('Omni', $ipAddress); } + public function connectionType($ipAddress) + { + return $this->flatModelFor('ConnectionType', $ipAddress); + } + + public function domain($ipAddress) + { + return $this->flatModelFor('Domain', $ipAddress); + } + + public function isp($ipAddress) + { + return $this->flatModelFor('Isp', $ipAddress); + } + private function modelFor($class, $ipAddress) + { + $record = $this->getRecord($ipAddress); + + $record['traits']['ip_address'] = $ipAddress; + $class = "GeoIp2\\Model\\" . $class; + + return new $class($record, $this->locales); + } + + private function flatModelFor($class, $ipAddress) + { + $record = $this->getRecord($ipAddress); + + $record['ip_address'] = $ipAddress; + $class = "GeoIp2\\Model\\" . $class; + + return new $class($record); + } + + private function getRecord($ipAddress) { $record = $this->dbReader->get($ipAddress); if ($record === null) { @@ -133,10 +168,7 @@ class Reader implements ProviderInterface "The address $ipAddress is not in the database." ); } - $record['traits']['ip_address'] = $ipAddress; - $class = "GeoIp2\\Model\\" . $class; - - return new $class($record, $this->locales); + return $record; } /** diff --git a/src/GeoIp2/Model/AbstractModel.php b/src/GeoIp2/Model/AbstractModel.php new file mode 100644 index 0000000..fddf625 --- /dev/null +++ b/src/GeoIp2/Model/AbstractModel.php @@ -0,0 +1,54 @@ +raw = $raw; + } + + /** + * @ignore + */ + protected function get($field) + { + return isset($this->raw[$field]) ? $this->raw[$field] : array(); + } + + /** + * @ignore + */ + public function __get($attr) + { + if ($attr != "instance" && isset($this->$attr)) { + return $this->$attr; + } + + throw new \RuntimeException("Unknown attribute: $attr"); + } + + /** + * @ignore + */ + public function __isset($attr) + { + return $attr != "instance" && isset($this->$attr); + } + + public function jsonSerialize() + { + return $this->raw; + } +} diff --git a/src/GeoIp2/Model/ConnectionType.php b/src/GeoIp2/Model/ConnectionType.php new file mode 100644 index 0000000..ffc6178 --- /dev/null +++ b/src/GeoIp2/Model/ConnectionType.php @@ -0,0 +1,31 @@ +connectionType = $this->get('connection_type'); + $this->ipAddress = $this->get('ip_address'); + } +} diff --git a/src/GeoIp2/Model/Country.php b/src/GeoIp2/Model/Country.php index c3ba143..2fb75a5 100644 --- a/src/GeoIp2/Model/Country.php +++ b/src/GeoIp2/Model/Country.php @@ -3,8 +3,7 @@ namespace GeoIp2\Model; /** - * This class provides a model for the data returned by the GeoIP2 Country - * end point. + * This class provides a model for the data returned by the GeoIP2 Country. * * The only difference between the City, City/ISP/Org, and Omni model * classes is which fields in each record may be populated. See @@ -33,23 +32,22 @@ namespace GeoIp2\Model; * @property \GeoIp2\Record\Traits $traits Data for the traits of the * requested IP address. */ -class Country implements \JsonSerializable +class Country extends AbstractModel { - private $continent; - private $country; - private $locales; - private $maxmind; - private $registeredCountry; - private $representedCountry; - private $traits; - private $raw; + protected $continent; + protected $country; + protected $locales; + protected $maxmind; + protected $registeredCountry; + protected $representedCountry; + protected $traits; /** * @ignore */ public function __construct($raw, $locales = array('en')) { - $this->raw = $raw; + parent::__construct($raw); $this->continent = new \GeoIp2\Record\Continent( $this->get('continent'), @@ -72,37 +70,4 @@ class Country implements \JsonSerializable $this->locales = $locales; } - - /** - * @ignore - */ - protected function get($field) - { - return isset($this->raw[$field]) ? $this->raw[$field] : array(); - } - - /** - * @ignore - */ - public function __get($attr) - { - if ($attr != "instance" && isset($this->$attr)) { - return $this->$attr; - } - - throw new \RuntimeException("Unknown attribute: $attr"); - } - - /** - * @ignore - */ - public function __isset($attr) - { - return $attr != "instance" && isset($this->$attr); - } - - public function jsonSerialize() - { - return $this->raw; - } } diff --git a/src/GeoIp2/Model/Domain.php b/src/GeoIp2/Model/Domain.php new file mode 100644 index 0000000..cd55b04 --- /dev/null +++ b/src/GeoIp2/Model/Domain.php @@ -0,0 +1,31 @@ +domain = $this->get('domain'); + $this->ipAddress = $this->get('ip_address'); + } +} diff --git a/src/GeoIp2/Model/Isp.php b/src/GeoIp2/Model/Isp.php new file mode 100644 index 0000000..30015db --- /dev/null +++ b/src/GeoIp2/Model/Isp.php @@ -0,0 +1,45 @@ +autonomousSystemNumber = $this->get('autonomous_system_number'); + $this->autonomousSystemOrganization = + $this->get('autonomous_system_organization'); + $this->isp = $this->get('isp'); + $this->organization = $this->get('organization'); + + $this->ipAddress = $this->get('ip_address'); + } +} diff --git a/tests/GeoIp2/Test/Database/ReaderTest.php b/tests/GeoIp2/Test/Database/ReaderTest.php index 1b72092..6940e8c 100644 --- a/tests/GeoIp2/Test/Database/ReaderTest.php +++ b/tests/GeoIp2/Test/Database/ReaderTest.php @@ -71,6 +71,46 @@ class ReaderTest extends \PHPUnit_Framework_TestCase $reader->close(); } + public function testConnectionType() + { + $reader = new Reader('maxmind-db/test-data/GeoIP2-Connection-Type-Test.mmdb'); + $ipAddress = '1.0.1.0'; + + $record = $reader->connectionType($ipAddress); + $this->assertEquals('Cable/DSL', $record->connectionType); + $this->assertEquals($ipAddress, $record->ipAddress); + $reader->close(); + } + + public function testDomain() + { + $reader = new Reader('maxmind-db/test-data/GeoIP2-Domain-Test.mmdb'); + + $ipAddress = '1.2.0.0'; + $record = $reader->domain($ipAddress); + $this->assertEquals('maxmind.com', $record->domain); + $this->assertEquals($ipAddress, $record->ipAddress); + $reader->close(); + } + + public function testIsp() + { + $reader = new Reader('maxmind-db/test-data/GeoIP2-ISP-Org-Test.mmdb'); + + $ipAddress = '2001:1700::'; + $record = $reader->isp($ipAddress); + $this->assertEquals(6730, $record->autonomousSystemNumber); + $this->assertEquals( + 'Sunrise Communications AG', + $record->autonomousSystemOrganization + ); + + // XXX - Add org/isp when available + + $this->assertEquals($ipAddress, $record->ipAddress); + $reader->close(); + } + public function checkAllMethods($testCb) { foreach (array('city', 'cityIspOrg', 'country', 'omni') as $method) { From cdf75ec5056ab1745ea7f9f4934f1d505621628e Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Tue, 17 Jun 2014 10:16:14 -0700 Subject: [PATCH 2/4] Expanded examples plus small bug fix --- README.md | 60 +++++++++++++++++++++++++++- src/GeoIp2/Model/AbstractModel.php | 4 +- src/GeoIp2/Record/AbstractRecord.php | 2 +- 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6c753f1..c558801 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ is thrown. If the database is invalid or corrupt, a See the API documentation for more details. -### Example ### +### City Example ### ```php location->longitude . "\n"); // -93.2323 ``` +### Connection-Type Example ### + +```php +connectionType('128.101.101.101'); + +print($record->connectionType . "\n"); // 'Corporate' +print($record->ipAddress . "\n"); // '128.101.101.101' + +``` + +### Domain Example ### + +```php +domain('128.101.101.101'); + +print($record->domain . "\n"); // 'umn.edu' +print($record->ipAddress . "\n"); // '128.101.101.101' + +``` + +### ISP Example ### + +```php +isp('128.101.101.101'); + +print($record->autonomousSystemNumber . "\n"); // 217 +print($record->autonomousSystemOrganization . "\n"); // 'University of Minnesota' +print($record->isp . "\n"); // 'University of Minnesota' +print($record->organization . "\n"); // 'University of Minnesota' + +print($record->ipAddress . "\n"); // '128.101.101.101' + +``` + ## Web Service Client ## ### Usage ### diff --git a/src/GeoIp2/Model/AbstractModel.php b/src/GeoIp2/Model/AbstractModel.php index fddf625..69a86ee 100644 --- a/src/GeoIp2/Model/AbstractModel.php +++ b/src/GeoIp2/Model/AbstractModel.php @@ -24,7 +24,7 @@ abstract class AbstractModel implements \JsonSerializable */ protected function get($field) { - return isset($this->raw[$field]) ? $this->raw[$field] : array(); + return isset($this->raw[$field]) ? $this->raw[$field] : null; } /** @@ -32,7 +32,7 @@ abstract class AbstractModel implements \JsonSerializable */ public function __get($attr) { - if ($attr != "instance" && isset($this->$attr)) { + if ($attr != "instance" && property_exists($this, $attr)) { return $this->$attr; } diff --git a/src/GeoIp2/Record/AbstractRecord.php b/src/GeoIp2/Record/AbstractRecord.php index 7210aff..b81bfea 100644 --- a/src/GeoIp2/Record/AbstractRecord.php +++ b/src/GeoIp2/Record/AbstractRecord.php @@ -11,7 +11,7 @@ abstract class AbstractRecord implements \JsonSerializable */ public function __construct($record) { - $this->record = $record; + $this->record = isset($record) ? $record : array(); } /** From 84299eba92b1acf2c8cc1768a756a4d3d2752786 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Wed, 18 Jun 2014 10:29:54 -0700 Subject: [PATCH 3/4] Updated for new ISP test DB --- maxmind-db | 2 +- tests/GeoIp2/Test/Database/ReaderTest.php | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/maxmind-db b/maxmind-db index f86528a..f538654 160000 --- a/maxmind-db +++ b/maxmind-db @@ -1 +1 @@ -Subproject commit f86528a05b883955a1c696ffe3bfeaa1224703d0 +Subproject commit f53865477d361101969853f8d418458fd58cdc74 diff --git a/tests/GeoIp2/Test/Database/ReaderTest.php b/tests/GeoIp2/Test/Database/ReaderTest.php index 6940e8c..476abb3 100644 --- a/tests/GeoIp2/Test/Database/ReaderTest.php +++ b/tests/GeoIp2/Test/Database/ReaderTest.php @@ -95,17 +95,18 @@ class ReaderTest extends \PHPUnit_Framework_TestCase public function testIsp() { - $reader = new Reader('maxmind-db/test-data/GeoIP2-ISP-Org-Test.mmdb'); + $reader = new Reader('maxmind-db/test-data/GeoIP2-ISP-Test.mmdb'); - $ipAddress = '2001:1700::'; + $ipAddress = '1.128.0.0'; $record = $reader->isp($ipAddress); - $this->assertEquals(6730, $record->autonomousSystemNumber); + $this->assertEquals(1221, $record->autonomousSystemNumber); $this->assertEquals( - 'Sunrise Communications AG', + 'Telstra Pty Ltd', $record->autonomousSystemOrganization ); - // XXX - Add org/isp when available + $this->assertEquals('Telstra Internet', $record->isp); + $this->assertEquals('Telstra Internet', $record->organization); $this->assertEquals($ipAddress, $record->ipAddress); $reader->close(); From 04ccb67cea8680d0935d17f0c3444894cf828811 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Wed, 18 Jun 2014 10:37:21 -0700 Subject: [PATCH 4/4] Fix for hhvm difference --- src/GeoIp2/Model/AbstractModel.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/GeoIp2/Model/AbstractModel.php b/src/GeoIp2/Model/AbstractModel.php index 69a86ee..cd96666 100644 --- a/src/GeoIp2/Model/AbstractModel.php +++ b/src/GeoIp2/Model/AbstractModel.php @@ -8,8 +8,7 @@ namespace GeoIp2\Model; */ abstract class AbstractModel implements \JsonSerializable { - private $raw; - + protected $raw; /** * @ignore