Skip to content

Commit

Permalink
Added Connection-Type, Domain, and ISP databases
Browse files Browse the repository at this point in the history
  • Loading branch information
oschwald committed Jun 17, 2014
1 parent 9f0c743 commit 22babda
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 51 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"php": ">=5.3.1"
},
"require-dev": {
"phpunit/phpunit": "3.7.*",
"phpunit/phpunit": "4.1.*",
"satooshi/php-coveralls": "dev-master"
},
"autoload": {
Expand Down
40 changes: 36 additions & 4 deletions src/GeoIp2/Database/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,50 @@ public function omni($ipAddress)
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) {
throw new AddressNotFoundException(
"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;
}

/**
Expand Down
54 changes: 54 additions & 0 deletions src/GeoIp2/Model/AbstractModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace GeoIp2\Model;


/**
* @ignore
*/
abstract class AbstractModel implements \JsonSerializable
{
private $raw;


/**
* @ignore
*/
public function __construct($raw)
{
$this->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;
}
}
31 changes: 31 additions & 0 deletions src/GeoIp2/Model/ConnectionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace GeoIp2\Model;

/**
* This class provides the GeoIP2 Connection-Type model.
*
* @property string $connectionType The connection type may take the following
* values: "Dialup", "Cable/DSL", "Corporate", "Cellular". Additional
* values may be added in the future.
*
* @property string $ipAddress The IP address that the data in the model is
* for.
*
*/
class ConnectionType extends AbstractModel
{
protected $connectionType;
protected $ipAddress;

/**
* @ignore
*/
public function __construct($raw)
{
parent::__construct($raw);

$this->connectionType = $this->get('connection_type');
$this->ipAddress = $this->get('ip_address');
}
}
55 changes: 10 additions & 45 deletions src/GeoIp2/Model/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -33,23 +32,22 @@
* @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'),
Expand All @@ -72,37 +70,4 @@ public function __construct($raw, $locales = array('en'))

$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;
}
}
31 changes: 31 additions & 0 deletions src/GeoIp2/Model/Domain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace GeoIp2\Model;

/**
* This class provides the GeoIP2 Domain model.
*
* @property string $domain The second level domain associated with the IP
* address. This will be something like "example.com" or "example.co.uk",
* not "foo.example.com".
*
* @property string $ipAddress The IP address that the data in the model is
* for.
*
*/
class Domain extends AbstractModel
{
protected $domain;
protected $ipAddress;

/**
* @ignore
*/
public function __construct($raw)
{
parent::__construct($raw);

$this->domain = $this->get('domain');
$this->ipAddress = $this->get('ip_address');
}
}
45 changes: 45 additions & 0 deletions src/GeoIp2/Model/Isp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace GeoIp2\Model;

/**
* This class provides the GeoIP2 Connection-Type model.
*
* @property integer $autonomousSystemNumber The autonomous system number
* associated with the IP address.
*
* @property string $autonomousSystemOrganization The organization associated
* with the registered autonomous system number for the IP address.
*
* @property string $isp The name of the ISP associated with the IP address.
*
* @property string $organization The name of the organization associated with
* the IP address.
*
* @property string $ipAddress The IP address that the data in the model is
* for.
*
*/
class Isp extends AbstractModel
{
protected $autonomousSystemNumber;
protected $autonomousSystemOrganization;
protected $isp;
protected $organization;
protected $ipAddress;

/**
* @ignore
*/
public function __construct($raw)
{
parent::__construct($raw);
$this->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');
}
}
40 changes: 40 additions & 0 deletions tests/GeoIp2/Test/Database/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,46 @@ public function testInvalidAddress()
$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) {
Expand Down

0 comments on commit 22babda

Please sign in to comment.