Merge branch 'MDL-60291-master' of git://github.com/ankitagarwal/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2017-10-12 00:42:57 +02:00
commit 5fab66aad8
29 changed files with 361 additions and 312 deletions

View File

@ -5,6 +5,7 @@ namespace GeoIp2\Database;
use GeoIp2\Exception\AddressNotFoundException;
use GeoIp2\ProviderInterface;
use MaxMind\Db\Reader as DbReader;
use MaxMind\Db\Reader\InvalidDatabaseException;
/**
* Instances of this class provide a reader for the GeoIP2 database format.
@ -29,7 +30,6 @@ use MaxMind\Db\Reader as DbReader;
* SPL {@link \InvalidArgumentException} will be thrown. If the database is
* corrupt or invalid, a {@link \MaxMind\Db\Reader\InvalidDatabaseException}
* will be thrown.
*
*/
class Reader implements ProviderInterface
{
@ -39,15 +39,16 @@ class Reader implements ProviderInterface
/**
* Constructor.
*
* @param string $filename The path to the GeoIP2 database file.
* @param array $locales List of locale codes to use in name property
* from most preferred to least preferred.
* @param string $filename the path to the GeoIP2 database file
* @param array $locales list of locale codes to use in name property
* from most preferred to least preferred
*
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
* is corrupt or invalid
*/
public function __construct(
$filename,
$locales = array('en')
$locales = ['en']
) {
$this->dbReader = new DbReader($filename);
$this->locales = $locales;
@ -56,14 +57,14 @@ class Reader implements ProviderInterface
/**
* This method returns a GeoIP2 City model.
*
* @param string $ipAddress IPv4 or IPv6 address as a string.
* @param string $ipAddress an IPv4 or IPv6 address as a string
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*
* @return \GeoIp2\Model\City
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database.
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function city($ipAddress)
{
@ -73,14 +74,14 @@ class Reader implements ProviderInterface
/**
* This method returns a GeoIP2 Country model.
*
* @param string $ipAddress IPv4 or IPv6 address as a string.
* @param string $ipAddress an IPv4 or IPv6 address as a string
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*
* @return \GeoIp2\Model\Country
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database.
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function country($ipAddress)
{
@ -90,14 +91,14 @@ class Reader implements ProviderInterface
/**
* This method returns a GeoIP2 Anonymous IP model.
*
* @param string $ipAddress IPv4 or IPv6 address as a string.
* @param string $ipAddress an IPv4 or IPv6 address as a string
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*
* @return \GeoIp2\Model\AnonymousIp
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database.
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function anonymousIp($ipAddress)
{
@ -108,17 +109,38 @@ class Reader implements ProviderInterface
);
}
/**
* This method returns a GeoLite2 ASN model.
*
* @param string $ipAddress an IPv4 or IPv6 address as a string
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*
* @return \GeoIp2\Model\Asn
*/
public function asn($ipAddress)
{
return $this->flatModelFor(
'Asn',
'GeoLite2-ASN',
$ipAddress
);
}
/**
* This method returns a GeoIP2 Connection Type model.
*
* @param string $ipAddress IPv4 or IPv6 address as a string.
* @param string $ipAddress an IPv4 or IPv6 address as a string
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*
* @return \GeoIp2\Model\ConnectionType
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database.
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function connectionType($ipAddress)
{
@ -132,14 +154,14 @@ class Reader implements ProviderInterface
/**
* This method returns a GeoIP2 Domain model.
*
* @param string $ipAddress IPv4 or IPv6 address as a string.
* @param string $ipAddress an IPv4 or IPv6 address as a string
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*
* @return \GeoIp2\Model\Domain
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database.
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function domain($ipAddress)
{
@ -153,14 +175,14 @@ class Reader implements ProviderInterface
/**
* This method returns a GeoIP2 Enterprise model.
*
* @param string $ipAddress IPv4 or IPv6 address as a string.
* @param string $ipAddress an IPv4 or IPv6 address as a string
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*
* @return \GeoIp2\Model\Enterprise
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database.
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function enterprise($ipAddress)
{
@ -170,14 +192,14 @@ class Reader implements ProviderInterface
/**
* This method returns a GeoIP2 ISP model.
*
* @param string $ipAddress IPv4 or IPv6 address as a string.
* @param string $ipAddress an IPv4 or IPv6 address as a string
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*
* @return \GeoIp2\Model\Isp
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database.
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function isp($ipAddress)
{
@ -193,7 +215,7 @@ class Reader implements ProviderInterface
$record = $this->getRecord($class, $type, $ipAddress);
$record['traits']['ip_address'] = $ipAddress;
$class = "GeoIp2\\Model\\" . $class;
$class = 'GeoIp2\\Model\\' . $class;
return new $class($record, $this->locales);
}
@ -203,7 +225,7 @@ class Reader implements ProviderInterface
$record = $this->getRecord($class, $type, $ipAddress);
$record['ip_address'] = $ipAddress;
$class = "GeoIp2\\Model\\" . $class;
$class = 'GeoIp2\\Model\\' . $class;
return new $class($record);
}
@ -214,7 +236,7 @@ class Reader implements ProviderInterface
$method = lcfirst($class);
throw new \BadMethodCallException(
"The $method method cannot be used to open a "
. $this->metadata()->databaseType . " database"
. $this->metadata()->databaseType . ' database'
);
}
$record = $this->dbReader->get($ipAddress);
@ -223,13 +245,28 @@ class Reader implements ProviderInterface
"The address $ipAddress is not in the database."
);
}
if (!is_array($record)) {
// This can happen on corrupt databases. Generally,
// MaxMind\Db\Reader will throw a
// MaxMind\Db\Reader\InvalidDatabaseException, but occasionally
// the lookup may result in a record that looks valid but is not
// an array. This mostly happens when the user is ignoring all
// exceptions and the more frequent InvalidDatabaseException
// exceptions go unnoticed.
throw new InvalidDatabaseException(
"Expected an array when looking up $ipAddress but received: "
. gettype($record)
);
}
return $record;
}
/**
* @throws \InvalidArgumentException if arguments are passed to the method.
* @throws \BadMethodCallException if the database has been closed.
* @return \MaxMind\Db\Reader\Metadata object for the database.
* @throws \InvalidArgumentException if arguments are passed to the method
* @throws \BadMethodCallException if the database has been closed
*
* @return \MaxMind\Db\Reader\Metadata object for the database
*/
public function metadata()
{

View File

@ -5,11 +5,10 @@ namespace GeoIp2\Exception;
/**
* This class represents an HTTP transport error.
*/
class HttpException extends GeoIp2Exception
{
/**
* The URI queried
* The URI queried.
*/
public $uri;

View File

@ -9,7 +9,7 @@ namespace GeoIp2\Exception;
class InvalidRequestException extends HttpException
{
/**
* The code returned by the MaxMind web service
* The code returned by the MaxMind web service.
*/
public $error;

View File

@ -2,17 +2,17 @@
namespace GeoIp2\Model;
use GeoIp2\Compat\JsonSerializable;
/**
* @ignore
*/
abstract class AbstractModel implements JsonSerializable
abstract class AbstractModel implements \JsonSerializable
{
protected $raw;
/**
* @ignore
*
* @param mixed $raw
*/
public function __construct($raw)
{
@ -21,26 +21,29 @@ abstract class AbstractModel implements JsonSerializable
/**
* @ignore
*
* @param mixed $field
*/
protected function get($field)
{
if (isset($this->raw[$field])) {
return $this->raw[$field];
} else {
if (preg_match('/^is_/', $field)) {
return false;
} else {
return null;
}
}
if (preg_match('/^is_/', $field)) {
return false;
}
return null;
}
/**
* @ignore
*
* @param mixed $attr
*/
public function __get($attr)
{
if ($attr != "instance" && property_exists($this, $attr)) {
if ($attr !== 'instance' && property_exists($this, $attr)) {
return $this->$attr;
}
@ -49,10 +52,12 @@ abstract class AbstractModel implements JsonSerializable
/**
* @ignore
*
* @param mixed $attr
*/
public function __isset($attr)
{
return $attr != "instance" && isset($this->$attr);
return $attr !== 'instance' && isset($this->$attr);
}
public function jsonSerialize()

View File

@ -5,24 +5,18 @@ namespace GeoIp2\Model;
/**
* This class provides the GeoIP2 Anonymous IP model.
*
* @property-read boolean $isAnonymous This is true if the IP address belongs to
* @property-read bool $isAnonymous This is true if the IP address belongs to
* any sort of anonymous network.
*
* @property-read boolean $isAnonymousVpn This is true if the IP address belongs to
* @property-read bool $isAnonymousVpn This is true if the IP address belongs to
* an anonymous VPN system.
*
* @property-read boolean $isHostingProvider This is true if the IP address belongs
* @property-read bool $isHostingProvider This is true if the IP address belongs
* to a hosting provider.
*
* @property-read boolean $isPublicProxy This is true if the IP address belongs to
* @property-read bool $isPublicProxy This is true if the IP address belongs to
* a public proxy.
*
* @property-read boolean $isTorExitNode This is true if the IP address is a Tor
* @property-read bool $isTorExitNode This is true if the IP address is a Tor
* exit node.
*
* @property-read string $ipAddress The IP address that the data in the model is
* for.
*
*/
class AnonymousIp extends AbstractModel
{
@ -35,6 +29,8 @@ class AnonymousIp extends AbstractModel
/**
* @ignore
*
* @param mixed $raw
*/
public function __construct($raw)
{

View File

@ -0,0 +1,35 @@
<?php
namespace GeoIp2\Model;
/**
* This class provides the GeoLite2 ASN model.
*
* @property-read int|null $autonomousSystemNumber The autonomous system number
* associated with the IP address.
* @property-read string|null $autonomousSystemOrganization The organization
* associated with the registered autonomous system number for the IP
* address.
* @property-read string $ipAddress The IP address that the data in the model is
* for.
*/
class Asn extends AbstractModel
{
protected $autonomousSystemNumber;
protected $autonomousSystemOrganization;
protected $ipAddress;
/**
* @ignore
*
* @param mixed $raw
*/
public function __construct($raw)
{
parent::__construct($raw);
$this->autonomousSystemNumber = $this->get('autonomous_system_number');
$this->autonomousSystemOrganization =
$this->get('autonomous_system_organization');
$this->ipAddress = $this->get('ip_address');
}
}

View File

@ -11,33 +11,25 @@ namespace GeoIp2\Model;
*
* @property-read \GeoIp2\Record\City $city City data for the requested IP
* address.
*
* @property-read \GeoIp2\Record\Continent $continent Continent data for the
* requested IP address.
*
* @property-read \GeoIp2\Record\Country $country Country data for the requested
* IP address. This object represents the country where MaxMind believes the
* end user is located.
*
* @property-read \GeoIp2\Record\Location $location Location data for the
* requested IP address.
*
* @property-read \GeoIp2\Record\Postal $postal Postal data for the
* requested IP address.
*
* @property-read \GeoIp2\Record\MaxMind $maxmind Data related to your MaxMind
* account.
*
* @property-read \GeoIp2\Record\Country $registeredCountry Registered country
* data for the requested IP address. This record represents the country
* where the ISP has registered a given IP block and may differ from the
* user's country.
*
* @property-read \GeoIp2\Record\RepresentedCountry $representedCountry
* Represented country data for the requested IP address. The represented
* country is used for things like military bases. It is only present when
* the represented country differs from the country.
*
* @property-read array $subdivisions An array of {@link \GeoIp2\Record\Subdivision}
* objects representing the country subdivisions for the requested IP
* address. The number and type of subdivisions varies by country, but a
@ -45,12 +37,10 @@ namespace GeoIp2\Model;
* are ordered from most general (largest) to most specific (smallest).
* If the response did not contain any subdivisions, this method returns
* an empty array.
*
* @property-read \GeoIp2\Record\Subdivision $mostSpecificSubdivision An object
* representing the most specific subdivision returned. If the response
* did not contain any subdivisions, this method returns an empty
* {@link \GeoIp2\Record\Subdivision} object.
*
* @property-read \GeoIp2\Record\Traits $traits Data for the traits of the
* requested IP address.
*/
@ -71,12 +61,15 @@ class City extends Country
/**
* @ignore
*/
protected $subdivisions = array();
protected $subdivisions = [];
/**
* @ignore
*
* @param mixed $raw
* @param mixed $locales
*/
public function __construct($raw, $locales = array('en'))
public function __construct($raw, $locales = ['en'])
{
parent::__construct($raw, $locales);
@ -103,20 +96,38 @@ class City extends Country
/**
* @ignore
*
* @param mixed $attr
*/
public function __get($attr)
{
if ($attr == 'mostSpecificSubdivision') {
if ($attr === 'mostSpecificSubdivision') {
return $this->$attr();
} else {
return parent::__get($attr);
}
return parent::__get($attr);
}
/**
* @ignore
*
* @param mixed $attr
*/
public function __isset($attr)
{
if ($attr === 'mostSpecificSubdivision') {
// We always return a mostSpecificSubdivision, even if it is the
// empty subdivision
return true;
}
return parent::__isset($attr);
}
private function mostSpecificSubdivision()
{
return empty($this->subdivisions) ?
new \GeoIp2\Record\Subdivision(array(), $this->locales) :
new \GeoIp2\Record\Subdivision([], $this->locales) :
end($this->subdivisions);
}
}

View File

@ -8,10 +8,8 @@ namespace GeoIp2\Model;
* @property-read string|null $connectionType The connection type may take the
* following values: "Dialup", "Cable/DSL", "Corporate", "Cellular".
* Additional values may be added in the future.
*
* @property-read string $ipAddress The IP address that the data in the model is
* for.
*
*/
class ConnectionType extends AbstractModel
{
@ -20,6 +18,8 @@ class ConnectionType extends AbstractModel
/**
* @ignore
*
* @param mixed $raw
*/
public function __construct($raw)
{

View File

@ -11,24 +11,19 @@ namespace GeoIp2\Model;
*
* @property-read \GeoIp2\Record\Continent $continent Continent data for the
* requested IP address.
*
* @property-read \GeoIp2\Record\Country $country Country data for the requested
* IP address. This object represents the country where MaxMind believes the
* end user is located.
*
* @property-read \GeoIp2\Record\MaxMind $maxmind Data related to your MaxMind
* account.
*
* @property-read \GeoIp2\Record\Country $registeredCountry Registered country
* data for the requested IP address. This record represents the country
* where the ISP has registered a given IP block and may differ from the
* user's country.
*
* @property-read \GeoIp2\Record\RepresentedCountry $representedCountry
* Represented country data for the requested IP address. The represented
* country is used for things like military bases. It is only present when
* the represented country differs from the country.
*
* @property-read \GeoIp2\Record\Traits $traits Data for the traits of the
* requested IP address.
*/
@ -44,8 +39,11 @@ class Country extends AbstractModel
/**
* @ignore
*
* @param mixed $raw
* @param mixed $locales
*/
public function __construct($raw, $locales = array('en'))
public function __construct($raw, $locales = ['en'])
{
parent::__construct($raw);

View File

@ -8,10 +8,8 @@ namespace GeoIp2\Model;
* @property-read string|null $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-read string $ipAddress The IP address that the data in the model is
* for.
*
*/
class Domain extends AbstractModel
{
@ -20,6 +18,8 @@ class Domain extends AbstractModel
/**
* @ignore
*
* @param mixed $raw
*/
public function __construct($raw)
{

View File

@ -11,30 +11,23 @@ namespace GeoIp2\Model;
*
* @property-read \GeoIp2\Record\City $city City data for the requested IP
* address.
*
* @property-read \GeoIp2\Record\Continent $continent Continent data for the
* requested IP address.
*
* @property-read \GeoIp2\Record\Country $country Country data for the requested
* IP address. This object represents the country where MaxMind believes the
* end user is located.
*
* @property-read \GeoIp2\Record\Location $location Location data for the
* requested IP address.
*
* @property-read \GeoIp2\Record\MaxMind $maxmind Data related to your MaxMind
* account.
*
* @property-read \GeoIp2\Record\Country $registeredCountry Registered country
* data for the requested IP address. This record represents the country
* where the ISP has registered a given IP block and may differ from the
* user's country.
*
* @property-read \GeoIp2\Record\RepresentedCountry $representedCountry
* Represented country data for the requested IP address. The represented
* country is used for things like military bases. It is only present when
* the represented country differs from the country.
*
* @property-read array $subdivisions An array of {@link \GeoIp2\Record\Subdivision}
* objects representing the country subdivisions for the requested IP
* address. The number and type of subdivisions varies by country, but a
@ -42,12 +35,10 @@ namespace GeoIp2\Model;
* are ordered from most general (largest) to most specific (smallest).
* If the response did not contain any subdivisions, this method returns
* an empty array.
*
* @property-read \GeoIp2\Record\Subdivision $mostSpecificSubdivision An object
* representing the most specific subdivision returned. If the response
* did not contain any subdivisions, this method returns an empty
* {@link \GeoIp2\Record\Subdivision} object.
*
* @property-read \GeoIp2\Record\Traits $traits Data for the traits of the
* requested IP address.
*/

View File

@ -11,30 +11,23 @@ namespace GeoIp2\Model;
*
* @property-read \GeoIp2\Record\City $city City data for the requested IP
* address.
*
* @property-read \GeoIp2\Record\Continent $continent Continent data for the
* requested IP address.
*
* @property-read \GeoIp2\Record\Country $country Country data for the requested
* IP address. This object represents the country where MaxMind believes the
* end user is located.
*
* @property-read \GeoIp2\Record\Location $location Location data for the
* requested IP address.
*
* @property-read \GeoIp2\Record\MaxMind $maxmind Data related to your MaxMind
* account.
*
* @property-read \GeoIp2\Record\Country $registeredCountry Registered country
* data for the requested IP address. This record represents the country
* where the ISP has registered a given IP block and may differ from the
* user's country.
*
* @property-read \GeoIp2\Record\RepresentedCountry $representedCountry
* Represented country data for the requested IP address. The represented
* country is used for things like military bases. It is only present when
* the represented country differs from the country.
*
* @property-read array $subdivisions An array of {@link \GeoIp2\Record\Subdivision}
* objects representing the country subdivisions for the requested IP
* address. The number and type of subdivisions varies by country, but a
@ -42,12 +35,10 @@ namespace GeoIp2\Model;
* are ordered from most general (largest) to most specific (smallest).
* If the response did not contain any subdivisions, this method returns
* an empty array.
*
* @property-read \GeoIp2\Record\Subdivision $mostSpecificSubdivision An object
* representing the most specific subdivision returned. If the response
* did not contain any subdivisions, this method returns an empty
* {@link \GeoIp2\Record\Subdivision} object.
*
* @property-read \GeoIp2\Record\Traits $traits Data for the traits of the
* requested IP address.
*/

View File

@ -3,24 +3,19 @@
namespace GeoIp2\Model;
/**
* This class provides the GeoIP2 Connection-Type model.
* This class provides the GeoIP2 ISP model.
*
* @property-read integer|null $autonomousSystemNumber The autonomous system number
* @property-read int|null $autonomousSystemNumber The autonomous system number
* associated with the IP address.
*
* @property-read string|null $autonomousSystemOrganization The organization
* associated with the registered autonomous system number for the IP
* address.
*
* @property-read string|null $isp The name of the ISP associated with the IP
* address.
*
* @property-read string|null $organization The name of the organization associated
* with the IP address.
*
* @property-read string $ipAddress The IP address that the data in the model is
* for.
*
*/
class Isp extends AbstractModel
{
@ -32,6 +27,8 @@ class Isp extends AbstractModel
/**
* @ignore
*
* @param mixed $raw
*/
public function __construct($raw)
{

View File

@ -5,16 +5,16 @@ namespace GeoIp2;
interface ProviderInterface
{
/**
* @param ipAddress
* IPv4 or IPv6 address to lookup.
* @return \GeoIp2\Model\Country A Country model for the requested IP address.
* @param string $ipAddress an IPv4 or IPv6 address to lookup
*
* @return \GeoIp2\Model\Country a Country model for the requested IP address
*/
public function country($ipAddress);
/**
* @param ipAddress
* IPv4 or IPv6 address to lookup.
* @return \GeoIp2\Model\City A City model for the requested IP address.
* @param string $ipAddress an IPv4 or IPv6 address to lookup
*
* @return \GeoIp2\Model\City a City model for the requested IP address
*/
public function city($ipAddress);
}

View File

@ -8,8 +8,11 @@ abstract class AbstractPlaceRecord extends AbstractRecord
/**
* @ignore
*
* @param mixed $record
* @param mixed $locales
*/
public function __construct($record, $locales = array('en'))
public function __construct($record, $locales = ['en'])
{
$this->locales = $locales;
parent::__construct($record);
@ -17,22 +20,47 @@ abstract class AbstractPlaceRecord extends AbstractRecord
/**
* @ignore
*
* @param mixed $attr
*/
public function __get($attr)
{
if ($attr == 'name') {
if ($attr === 'name') {
return $this->name();
} else {
return parent::__get($attr);
}
return parent::__get($attr);
}
/**
* @ignore
*
* @param mixed $attr
*/
public function __isset($attr)
{
if ($attr === 'name') {
return $this->firstSetNameLocale() === null ? false : true;
}
return parent::__isset($attr);
}
private function name()
{
$locale = $this->firstSetNameLocale();
return $locale === null ? null : $this->names[$locale];
}
private function firstSetNameLocale()
{
foreach ($this->locales as $locale) {
if (isset($this->names[$locale])) {
return $this->names[$locale];
return $locale;
}
}
return null;
}
}

View File

@ -2,22 +2,24 @@
namespace GeoIp2\Record;
use GeoIp2\Compat\JsonSerializable;
abstract class AbstractRecord implements JsonSerializable
abstract class AbstractRecord implements \JsonSerializable
{
private $record;
/**
* @ignore
*
* @param mixed $record
*/
public function __construct($record)
{
$this->record = isset($record) ? $record : array();
$this->record = isset($record) ? $record : [];
}
/**
* @ignore
*
* @param mixed $attr
*/
public function __get($attr)
{
@ -29,12 +31,11 @@ abstract class AbstractRecord implements JsonSerializable
} elseif ($this->validAttribute($attr)) {
if (preg_match('/^is_/', $key)) {
return false;
} else {
return null;
}
} else {
throw new \RuntimeException("Unknown attribute: $attr");
return null;
}
throw new \RuntimeException("Unknown attribute: $attr");
}
public function __isset($attr)
@ -50,7 +51,7 @@ abstract class AbstractRecord implements JsonSerializable
private function validAttribute($attr)
{
return in_array($attr, $this->validAttributes);
return in_array($attr, $this->validAttributes, true);
}
public function jsonSerialize()

View File

@ -11,14 +11,11 @@ namespace GeoIp2\Record;
* @property-read int|null $confidence A value from 0-100 indicating MaxMind's
* confidence that the city is correct. This attribute is only available
* from the Insights service and the GeoIP2 Enterprise database.
*
* @property-read int|null $geonameId The GeoName ID for the city. This attribute
* is returned by all location services and databases.
*
* @property-read string|null $name The name of the city based on the locales list
* passed to the constructor. This attribute is returned by all location
* services and databases.
*
* @property-read array|null $names A array map where the keys are locale codes
* and the values are names. This attribute is returned by all location
* services and databases.
@ -28,5 +25,5 @@ class City extends AbstractPlaceRecord
/**
* @ignore
*/
protected $validAttributes = array('confidence', 'geonameId', 'names');
protected $validAttributes = ['confidence', 'geonameId', 'names'];
}

View File

@ -3,21 +3,18 @@
namespace GeoIp2\Record;
/**
* Contains data for the continent record associated with an IP address
* Contains data for the continent record associated with an IP address.
*
* This record is returned by all location services and databases.
*
* @property-read string|null $code A two character continent code like "NA" (North
* America) or "OC" (Oceania). This attribute is returned by all location
* services and databases.
*
* @property-read int|null $geonameId The GeoName ID for the continent. This
* attribute is returned by all location services and databases.
*
* @property-read string|null $name Returns the name of the continent based on the
* locales list passed to the constructor. This attribute is returned by all location
* services and databases.
*
* @property-read array|null $names An array map where the keys are locale codes
* and the values are names. This attribute is returned by all location
* services and databases.
@ -27,9 +24,9 @@ class Continent extends AbstractPlaceRecord
/**
* @ignore
*/
protected $validAttributes = array(
protected $validAttributes = [
'code',
'geonameId',
'names'
);
'names',
];
}

View File

@ -3,26 +3,22 @@
namespace GeoIp2\Record;
/**
* Contains data for the country record associated with an IP address
* Contains data for the country record associated with an IP address.
*
* This record is returned by all location services and databases.
*
* @property-read int|null $confidence A value from 0-100 indicating MaxMind's
* confidence that the country is correct. This attribute is only available
* from the Insights service and the GeoIP2 Enterprise database.
*
* @property-read int|null $geonameId The GeoName ID for the country. This
* attribute is returned by location services and databases.
*
* @property-read string|null $isoCode The {@link
* http://en.wikipedia.org/wiki/ISO_3166-1 two-character ISO 3166-1 alpha
* @property-read string|null $isoCode The
* {@link * http://en.wikipedia.org/wiki/ISO_3166-1 two-character ISO 3166-1 alpha
* code} for the country. This attribute is returned by all location services
* and databases.
*
* @property-read string|null $name The name of the country based on the locales
* list passed to the constructor. This attribute is returned by all location
* services and databases.
*
* @property-read array|null $names An array map where the keys are locale codes
* and the values are names. This attribute is returned by all location
* services and databases.
@ -32,10 +28,10 @@ class Country extends AbstractPlaceRecord
/**
* @ignore
*/
protected $validAttributes = array(
protected $validAttributes = [
'confidence',
'geonameId',
'isoCode',
'names'
);
'names',
];
}

View File

@ -3,7 +3,7 @@
namespace GeoIp2\Record;
/**
* Contains data for the location record associated with an IP address
* Contains data for the location record associated with an IP address.
*
* This record is returned by all location services and databases besides
* Country.
@ -11,31 +11,24 @@ namespace GeoIp2\Record;
* @property-read int|null $averageIncome The average income in US dollars
* associated with the requested IP address. This attribute is only available
* from the Insights service.
*
* @property-read int|null $accuracyRadius The approximate accuracy radius in
* kilometers around the latitude and longitude for the IP address. This is
* the radius where we have a 67% confidence that the device using the IP
* address resides within the circle centered at the latitude and longitude
* with the provided radius.
*
* @property-read float|null $latitude The approximate latitude of the location
* associated with the IP address. This value is not precise and should not be
* used to identify a particular address or household.
*
* @property-read float|null $longitude The approximate longitude of the location
* associated with the IP address. This value is not precise and should not be
* used to identify a particular address or household.
*
* @property-read int|null $populationDensity The estimated population per square
* kilometer associated with the IP address. This attribute is only available
* from the Insights service.
*
* @property-read int|null $metroCode The metro code of the location if the location
* is in the US. MaxMind returns the same metro codes as the
* {@link
* https://developers.google.com/adwords/api/docs/appendix/cities-DMAregions
* {@link * https://developers.google.com/adwords/api/docs/appendix/cities-DMAregions
* Google AdWords API}.
*
* @property-read string|null $timeZone The time zone associated with location, as
* specified by the {@link http://www.iana.org/time-zones IANA Time Zone
* Database}, e.g., "America/New_York".
@ -45,7 +38,7 @@ class Location extends AbstractRecord
/**
* @ignore
*/
protected $validAttributes = array(
protected $validAttributes = [
'averageIncome',
'accuracyRadius',
'latitude',
@ -54,6 +47,6 @@ class Location extends AbstractRecord
'populationDensity',
'postalCode',
'postalConfidence',
'timeZone'
);
'timeZone',
];
}

View File

@ -15,5 +15,5 @@ class MaxMind extends AbstractRecord
/**
* @ignore
*/
protected $validAttributes = array('queriesRemaining');
protected $validAttributes = ['queriesRemaining'];
}

View File

@ -3,7 +3,7 @@
namespace GeoIp2\Record;
/**
* Contains data for the postal record associated with an IP address
* Contains data for the postal record associated with an IP address.
*
* This record is returned by all location databases and services besides
* Country.
@ -12,7 +12,6 @@ namespace GeoIp2\Record;
* are not available for all countries. In some countries, this will only
* contain part of the postal code. This attribute is returned by all location
* databases and services besides Country.
*
* @property-read int|null $confidence A value from 0-100 indicating MaxMind's
* confidence that the postal code is correct. This attribute is only
* available from the Insights service and the GeoIP2 Enterprise
@ -23,5 +22,5 @@ class Postal extends AbstractRecord
/**
* @ignore
*/
protected $validAttributes = array('code', 'confidence');
protected $validAttributes = ['code', 'confidence'];
}

View File

@ -3,7 +3,7 @@
namespace GeoIp2\Record;
/**
* Contains data for the represented country associated with an IP address
* Contains data for the represented country associated with an IP address.
*
* This class contains the country-level data associated with an IP address
* for the IP's represented country. The represented country is the country
@ -12,29 +12,24 @@ namespace GeoIp2\Record;
* @property-read int|null $confidence A value from 0-100 indicating MaxMind's
* confidence that the country is correct. This attribute is only available
* from the Insights service and the GeoIP2 Enterprise database.
*
* @property-read int|null $geonameId The GeoName ID for the country.
*
* @property-read string|null $isoCode The {@link http://en.wikipedia.org/wiki/ISO_3166-1
* two-character ISO 3166-1 alpha code} for the country.
*
* @property-read string|null $name The name of the country based on the locales list
* passed to the constructor.
*
* @property-read array|null $names An array map where the keys are locale codes and
* the values are names.
*
* @property-read string|null $type A string indicating the type of entity that is
* representing the country. Currently we only return <code>military</code>
* but this could expand to include other types in the future.
*/
class RepresentedCountry extends Country
{
protected $validAttributes = array(
protected $validAttributes = [
'confidence',
'geonameId',
'isoCode',
'names',
'type'
);
'type',
];
}

View File

@ -3,8 +3,7 @@
namespace GeoIp2\Record;
/**
*
* Contains data for the subdivisions associated with an IP address
* Contains data for the subdivisions associated with an IP address.
*
* This record is returned by all location databases and services besides
* Country.
@ -13,20 +12,16 @@ namespace GeoIp2\Record;
* MaxMind's confidence that the subdivision is correct. This attribute is
* only available from the Insights service and the GeoIP2 Enterprise
* database.
*
* @property-read int|null $geonameId This is a GeoName ID for the subdivision.
* This attribute is returned by all location databases and services besides
* Country.
*
* @property-read string|null $isoCode This is a string up to three characters long
* contain the subdivision portion of the {@link
* http://en.wikipedia.org/wiki/ISO_3166-2 ISO 3166-2 code}. This attribute
* contain the subdivision portion of the
* {@link * http://en.wikipedia.org/wiki/ISO_3166-2 ISO 3166-2 code}. This attribute
* is returned by all location databases and services except Country.
*
* @property-read string|null $name The name of the subdivision based on the
* locales list passed to the constructor. This attribute is returned by all
* location databases and services besides Country.
*
* @property-read array|null $names An array map where the keys are locale codes
* and the values are names. This attribute is returned by all location
* databases and services besides Country.
@ -36,10 +31,10 @@ class Subdivision extends AbstractPlaceRecord
/**
* @ignore
*/
protected $validAttributes = array(
protected $validAttributes = [
'confidence',
'geonameId',
'isoCode',
'names'
);
'names',
];
}

View File

@ -3,65 +3,53 @@
namespace GeoIp2\Record;
/**
*
* Contains data for the traits record associated with an IP address
* Contains data for the traits record associated with an IP address.
*
* This record is returned by all location services and databases.
*
* @property-read int|null $autonomousSystemNumber The {@link
* http://en.wikipedia.org/wiki/Autonomous_system_(Internet) autonomous
* @property-read int|null $autonomousSystemNumber The
* {@link * http://en.wikipedia.org/wiki/Autonomous_system_(Internet) autonomous
* system number} associated with the IP address. This attribute is only
* available from the City and Insights web service and the GeoIP2
* Enterprise database.
*
* @property-read string|null $autonomousSystemOrganization The organization
* associated with the registered {@link
* http://en.wikipedia.org/wiki/Autonomous_system_(Internet) autonomous
* associated with the registered {@link * http://en.wikipedia.org/wiki/Autonomous_system_(Internet) autonomous
* system number} for the IP address. This attribute is only available from
* the City and Insights web service and the GeoIP2 Enterprise
* database.
*
* @property-read string|null $connectionType The connection type may take the
* following values: "Dialup", "Cable/DSL", "Corporate", "Cellular".
* Additional values may be added in the future. This attribute is only
* available in the GeoIP2 Enterprise database.
*
* @property-read string|null $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". This attribute is only available from the
* City and Insights web service and the GeoIP2 Enterprise
* database.
*
* @property-read string $ipAddress The IP address that the data in the model
* is for. If you performed a "me" lookup against the web service, this
* will be the externally routable IP address for the system the code is
* running on. If the system is behind a NAT, this may differ from the IP
* address locally assigned to it. This attribute is returned by all end
* points.
*
* @property-read boolean $isAnonymousProxy *Deprecated.* Please see our {@link
* https://www.maxmind.com/en/geoip2-anonymous-ip-database GeoIP2
* @property-read bool $isAnonymousProxy *Deprecated.* Please see our
* {@link * https://www.maxmind.com/en/geoip2-anonymous-ip-database GeoIP2
* Anonymous IP database} to determine whether the IP address is used by an
* anonymizing service.
*
* @property-read boolean $isLegitimateProxy This attribute is true if MaxMind
* @property-read bool $isLegitimateProxy This attribute is true if MaxMind
* believes this IP address to be a legitimate proxy, such as an internal
* VPN used by a corporation. This attribute is only available in the GeoIP2
* Enterprise database.
*
* @property-read boolean $isSatelliteProvider *Deprecated.* Due to the
* @property-read bool $isSatelliteProvider *Deprecated.* Due to the
* increased coverage by mobile carriers, very few satellite providers now
* serve multiple countries. As a result, the output does not provide
* sufficiently relevant data for us to maintain it.
*
* @property-read string|null $isp The name of the ISP associated with the IP
* address. This attribute is only available from the City and Insights web
* services and the GeoIP2 Enterprise database.
*
* @property-read string|null $organization The name of the organization associated
* with the IP address. This attribute is only available from the City and
* Insights web services and the GeoIP2 Enterprise database.
*
* @property-read string|null $userType <p>The user type associated with the IP
* address. This can be one of the following values:</p>
* <ul>
@ -91,7 +79,7 @@ class Traits extends AbstractRecord
/**
* @ignore
*/
protected $validAttributes = array(
protected $validAttributes = [
'autonomousSystemNumber',
'autonomousSystemOrganization',
'connectionType',
@ -102,6 +90,6 @@ class Traits extends AbstractRecord
'isp',
'ipAddress',
'organization',
'userType'
);
'userType',
];
}

View File

@ -9,7 +9,6 @@ use GeoIp2\Exception\HttpException;
use GeoIp2\Exception\InvalidRequestException;
use GeoIp2\Exception\OutOfQueriesException;
use GeoIp2\ProviderInterface;
use MaxMind\Exception\InvalidInputException;
use MaxMind\WebService\Client as WsClient;
/**
@ -48,35 +47,35 @@ class Client implements ProviderInterface
private $client;
private static $basePath = '/geoip/v2.1';
const VERSION = 'v2.4.2';
const VERSION = 'v2.6.0';
/**
* Constructor.
*
* @param int $userId Your MaxMind user ID
* @param string $licenseKey Your MaxMind license key
* @param array $locales List of locale codes to use in name property
* from most preferred to least preferred.
* @param array $options Array of options. Valid options include:
* * `host` - The host to use when querying the web service.
* * `timeout` - Timeout in seconds.
* * `connectTimeout` - Initial connection timeout in seconds.
* * `proxy` - The HTTP proxy to use. May include a schema, port,
* username, and password, e.g.,
* `http://username:password@127.0.0.1:10`.
* @param int $userId your MaxMind user ID
* @param string $licenseKey your MaxMind license key
* @param array $locales list of locale codes to use in name property
* from most preferred to least preferred
* @param array $options array of options. Valid options include:
* * `host` - The host to use when querying the web service.
* * `timeout` - Timeout in seconds.
* * `connectTimeout` - Initial connection timeout in seconds.
* * `proxy` - The HTTP proxy to use. May include a schema, port,
* username, and password, e.g.,
* `http://username:password@127.0.0.1:10`.
*/
public function __construct(
$userId,
$licenseKey,
$locales = array('en'),
$options = array()
$locales = ['en'],
$options = []
) {
$this->locales = $locales;
// This is for backwards compatibility. Do not remove except for a
// major version bump.
if (is_string($options)) {
$options = array( 'host' => $options );
$options = ['host' => $options];
}
if (!isset($options['host'])) {
@ -90,35 +89,34 @@ class Client implements ProviderInterface
private function userAgent()
{
return 'GeoIP2-API/' . Client::VERSION;
return 'GeoIP2-API/' . self::VERSION;
}
/**
* This method calls the GeoIP2 Precision: City service.
*
* @param string $ipAddress IPv4 or IPv6 address as a string. If no
* address is provided, the address that the web service is called
* from will be used.
*
* @return \GeoIp2\Model\City
* address is provided, the address that the web service is called
* from will be used.
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address you
* provided is not in our database (e.g., a private address).
* @throws \GeoIp2\Exception\AuthenticationException if there is a problem
* with the user ID or license key that you provided.
* @throws \GeoIp2\Exception\OutOfQueriesException if your account is out
* of queries.
* @throws \GeoIp2\Exception\InvalidRequestException} if your request was
* received by the web service but is invalid for some other reason.
* This may indicate an issue with this API. Please report the error to
* MaxMind.
* @throws \GeoIp2\Exception\HttpException if an unexpected HTTP error
* code or message was returned. This could indicate a problem with the
* connection between your server and the web service or that the web
* service returned an invalid document or 500 error code.
* @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent
* class to the above exceptions. It will be thrown directly if a 200
* status code is returned but the body is invalid.
* provided is not in our database (e.g., a private address).
* @throws \GeoIp2\Exception\AuthenticationException if there is a problem
* with the user ID or license key that you provided
* @throws \GeoIp2\Exception\OutOfQueriesException if your account is out
* of queries
* @throws \GeoIp2\Exception\InvalidRequestException} if your request was received by the web service but is
* invalid for some other reason. This may indicate an issue
* with this API. Please report the error to MaxMind.
* @throws \GeoIp2\Exception\HttpException if an unexpected HTTP error code or message was returned.
* This could indicate a problem with the connection between
* your server and the web service or that the web service
* returned an invalid document or 500 error code.
* @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent
* class to the above exceptions. It will be thrown directly
* if a 200 status code is returned but the body is invalid.
*
* @return \GeoIp2\Model\City
*/
public function city($ipAddress = 'me')
{
@ -129,28 +127,27 @@ class Client implements ProviderInterface
* This method calls the GeoIP2 Precision: Country service.
*
* @param string $ipAddress IPv4 or IPv6 address as a string. If no
* address is provided, the address that the web service is called
* from will be used.
* address is provided, the address that the web service is called
* from will be used.
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address you provided is not in our database (e.g.,
* a private address).
* @throws \GeoIp2\Exception\AuthenticationException if there is a problem
* with the user ID or license key that you provided
* @throws \GeoIp2\Exception\OutOfQueriesException if your account is out of queries
* @throws \GeoIp2\Exception\InvalidRequestException} if your request was received by the web service but is
* invalid for some other reason. This may indicate an
* issue with this API. Please report the error to MaxMind.
* @throws \GeoIp2\Exception\HttpException if an unexpected HTTP error
* code or message was returned. This could indicate a problem
* with the connection between your server and the web service
* or that the web service returned an invalid document or 500
* error code.
* @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent class to the above exceptions. It
* will be thrown directly if a 200 status code is returned but
* the body is invalid.
*
* @return \GeoIp2\Model\Country
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address you
* provided is not in our database (e.g., a private address).
* @throws \GeoIp2\Exception\AuthenticationException if there is a problem
* with the user ID or license key that you provided.
* @throws \GeoIp2\Exception\OutOfQueriesException if your account is out
* of queries.
* @throws \GeoIp2\Exception\InvalidRequestException} if your request was
* received by the web service but is invalid for some other reason.
* This may indicate an issue with this API. Please report the error to
* MaxMind.
* @throws \GeoIp2\Exception\HttpException if an unexpected HTTP error
* code or message was returned. This could indicate a problem with the
* connection between your server and the web service or that the web
* service returned an invalid document or 500 error code.
* @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent
* class to the above exceptions. It will be thrown directly if a 200
* status code is returned but the body is invalid.
*/
public function country($ipAddress = 'me')
{
@ -161,28 +158,27 @@ class Client implements ProviderInterface
* This method calls the GeoIP2 Precision: Insights service.
*
* @param string $ipAddress IPv4 or IPv6 address as a string. If no
* address is provided, the address that the web service is called
* from will be used.
*
* @return \GeoIp2\Model\Insights
* address is provided, the address that the web service is called
* from will be used.
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address you
* provided is not in our database (e.g., a private address).
* @throws \GeoIp2\Exception\AuthenticationException if there is a problem
* with the user ID or license key that you provided.
* @throws \GeoIp2\Exception\OutOfQueriesException if your account is out
* of queries.
* @throws \GeoIp2\Exception\InvalidRequestException} if your request was
* received by the web service but is invalid for some other reason.
* This may indicate an issue with this API. Please report the error to
* MaxMind.
* @throws \GeoIp2\Exception\HttpException if an unexpected HTTP error
* code or message was returned. This could indicate a problem with the
* connection between your server and the web service or that the web
* service returned an invalid document or 500 error code.
* @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent
* class to the above exceptions. It will be thrown directly if a 200
* status code is returned but the body is invalid.
* provided is not in our database (e.g., a private address).
* @throws \GeoIp2\Exception\AuthenticationException if there is a problem
* with the user ID or license key that you provided
* @throws \GeoIp2\Exception\OutOfQueriesException if your account is out
* of queries
* @throws \GeoIp2\Exception\InvalidRequestException} if your request was received by the web service but is
* invalid for some other reason. This may indicate an
* issue with this API. Please report the error to MaxMind.
* @throws \GeoIp2\Exception\HttpException if an unexpected HTTP error code or message was returned.
* This could indicate a problem with the connection between
* your server and the web service or that the web service
* returned an invalid document or 500 error code.
* @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent
* class to the above exceptions. It will be thrown directly
* if a 200 status code is returned but the body is invalid.
*
* @return \GeoIp2\Model\Insights
*/
public function insights($ipAddress = 'me')
{
@ -191,7 +187,7 @@ class Client implements ProviderInterface
private function responseFor($endpoint, $class, $ipAddress)
{
$path = implode('/', array(self::$basePath, $endpoint, $ipAddress));
$path = implode('/', [self::$basePath, $endpoint, $ipAddress]);
try {
$body = $this->client->get('GeoIP2 ' . $class, $path);
@ -236,7 +232,8 @@ class Client implements ProviderInterface
);
}
$class = "GeoIp2\\Model\\" . $class;
$class = 'GeoIp2\\Model\\' . $class;
return new $class($body, $this->locales);
}
}

View File

@ -16,6 +16,7 @@ class Reader
private static $DATA_SECTION_SEPARATOR_SIZE = 16;
private static $METADATA_START_MARKER = "\xAB\xCD\xEFMaxMind.com";
private static $METADATA_START_MARKER_LENGTH = 14;
private static $METADATA_MAX_SIZE = 131072; // 128 * 1024 = 128KB
private $decoder;
private $fileHandle;
@ -237,8 +238,10 @@ class Reader
$fileSize = $fstat['size'];
$marker = self::$METADATA_START_MARKER;
$markerLength = self::$METADATA_START_MARKER_LENGTH;
$metadataMaxLengthExcludingMarker
= min(self::$METADATA_MAX_SIZE, $fileSize) - $markerLength;
for ($i = 0; $i < $fileSize - $markerLength + 1; $i++) {
for ($i = 0; $i <= $metadataMaxLengthExcludingMarker; $i++) {
for ($j = 0; $j < $markerLength; $j++) {
fseek($handle, $fileSize - $i - $j - 1);
$matchBit = fgetc($handle);

View File

@ -276,13 +276,13 @@
<location>maxmind/GeoIp2</location>
<name>GeoIP2 PHP API</name>
<license>Apache 2.0</license>
<version>2.4.2</version>
<version>2.6.0</version>
</library>
<library>
<location>maxmind/MaxMind</location>
<name>MaxMind DB Reader API</name>
<license>Apache 2.0</license>
<version>1.1.0</version>
<version>1.1.3</version>
</library>
<library>
<location>ltiprovider</location>