Compare commits
79 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7fa971aef8 | ||
|
|
edb6940c74 | ||
|
|
57cb77a53c | ||
|
|
8aedaa5f91 | ||
|
|
87f6e2ad6b | ||
|
|
6bcd0ab21e | ||
|
|
e03b9558a9 | ||
|
|
234dcaff7c | ||
|
|
b8188bc558 | ||
|
|
8f16ed6115 | ||
|
|
8f3331002a | ||
|
|
b4b0341307 | ||
|
|
ee00329652 | ||
|
|
e8fa9a3a24 | ||
|
|
167433de0c | ||
|
|
c584dbfd6b | ||
|
|
c642a32b0c | ||
|
|
decfda7e90 | ||
|
|
3a560452af | ||
|
|
8febab1fe4 | ||
|
|
f0430b613d | ||
|
|
e3289ec416 | ||
|
|
1d1dcec74c | ||
|
|
630765f924 | ||
|
|
b55d42b5cd | ||
|
|
044ca4d085 | ||
|
|
3ba84992c8 | ||
|
|
a6c1f9378e | ||
|
|
1c0817d133 | ||
|
|
59a51ef34d | ||
|
|
3e0fc2a837 | ||
|
|
c898f76a50 | ||
|
|
8a265a763e | ||
|
|
3579ce7297 | ||
|
|
aa88c3040f | ||
|
|
5df4bbee47 | ||
|
|
e60181f13a | ||
|
|
1c37dcac03 | ||
|
|
f4898f7f90 | ||
|
|
6f85654820 | ||
|
|
fc74279d11 | ||
|
|
b8aa4bc429 | ||
|
|
d9a87dd5bd | ||
|
|
b45b3c1ed5 | ||
|
|
3431141943 | ||
|
|
c90fe87d3d | ||
|
|
1c256e1e64 | ||
|
|
1d294bd473 | ||
|
|
9a9a1e86b2 | ||
|
|
893c715047 | ||
|
|
f99ac351ab | ||
|
|
4d9957ccf5 | ||
|
|
3a382f0d26 | ||
|
|
c4867ef7c7 | ||
|
|
bfb8de7ffa | ||
|
|
e38c46950f | ||
|
|
7f81643a98 | ||
|
|
750c23fb5c | ||
|
|
e0baddaec0 | ||
|
|
89f1bbdb65 | ||
|
|
559e62a30c | ||
|
|
b179e29ee7 | ||
|
|
73d6f15823 | ||
|
|
2c23be78fe | ||
|
|
5eda52e1db | ||
|
|
b383cf82b3 | ||
|
|
0f76a28527 | ||
|
|
2b1ec4abbe | ||
|
|
ef618d06c1 | ||
|
|
aeba497703 | ||
|
|
57f6af0bfd | ||
|
|
fd1231e5f8 | ||
|
|
b077a3cda7 | ||
|
|
04629e039b | ||
|
|
f44db25129 | ||
|
|
472ec1ae56 | ||
|
|
94d1e3a96c | ||
|
|
50e3adc37f | ||
|
|
d0821bedcc |
5
.gitignore
vendored
5
.gitignore
vendored
@@ -1,7 +1,10 @@
|
|||||||
bin/
|
_site
|
||||||
|
.gh-pages
|
||||||
|
.idea
|
||||||
composer.lock
|
composer.lock
|
||||||
composer.phar
|
composer.phar
|
||||||
phpunit.xml
|
phpunit.xml
|
||||||
|
geoip2-php.sublime-*
|
||||||
vendor/
|
vendor/
|
||||||
*.sw?
|
*.sw?
|
||||||
t.php
|
t.php
|
||||||
|
|||||||
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
[submodule "maxmind-db"]
|
||||||
|
path = maxmind-db
|
||||||
|
url = git://github.com/maxmind/MaxMind-DB.git
|
||||||
@@ -7,13 +7,17 @@ php:
|
|||||||
|
|
||||||
before_install:
|
before_install:
|
||||||
- composer self-update
|
- composer self-update
|
||||||
- composer install
|
- composer install --dev -n --prefer-source
|
||||||
- pyrus install pear/PHP_CodeSniffer
|
- pyrus install pear/PHP_CodeSniffer
|
||||||
- phpenv rehash
|
- phpenv rehash
|
||||||
|
|
||||||
script:
|
script:
|
||||||
|
- mkdir -p build/logs
|
||||||
- phpcs --standard=PSR2 src/
|
- phpcs --standard=PSR2 src/
|
||||||
- phpunit
|
- phpunit -c phpunit.xml.dist
|
||||||
|
|
||||||
|
after_script:
|
||||||
|
- php vendor/bin/coveralls
|
||||||
|
|
||||||
notifications:
|
notifications:
|
||||||
email:
|
email:
|
||||||
|
|||||||
53
CHANGELOG.md
Normal file
53
CHANGELOG.md
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
CHANGELOG
|
||||||
|
=========
|
||||||
|
|
||||||
|
0.5.0 (2013-10-21)
|
||||||
|
------------------
|
||||||
|
|
||||||
|
* Renamed $languages constructor parameters to $locales for both the Client
|
||||||
|
and Reader classes.
|
||||||
|
* Documentation and code clean-up (Ben Morel).
|
||||||
|
* Added the interface `GeoIp2\ProviderInterface`, which is implemented by both
|
||||||
|
`\GeoIp2\Database\Reader` and `\GeoIp2\WebService\Client`.
|
||||||
|
|
||||||
|
0.4.0 (2013-07-16)
|
||||||
|
------------------
|
||||||
|
|
||||||
|
* This is the first release with the GeoIP2 database reader. Please see the
|
||||||
|
`README.md` file and the `\GeoIp2\Database\Reader` class.
|
||||||
|
* The general exception classes were replaced with specific exception classes
|
||||||
|
representing particular types of errors, such as an authentication error.
|
||||||
|
|
||||||
|
0.3.0 (2013-07-12)
|
||||||
|
------------------
|
||||||
|
|
||||||
|
* In namespaces and class names, "GeoIP2" was renamed to "GeoIp2" to improve
|
||||||
|
consistency.
|
||||||
|
|
||||||
|
0.2.1 (2013-06-10)
|
||||||
|
------------------
|
||||||
|
|
||||||
|
* First official beta release.
|
||||||
|
* Documentation updates and corrections.
|
||||||
|
|
||||||
|
0.2.0 (2013-05-29)
|
||||||
|
------------------
|
||||||
|
|
||||||
|
* `GenericException` was renamed to `GeoIP2Exception`.
|
||||||
|
* We now support more languages. The new languages are de, es, fr, and pt-BR.
|
||||||
|
* The REST API now returns a record with data about your account. There is
|
||||||
|
a new `GeoIP\Records\MaxMind` class for this data.
|
||||||
|
* The `continentCode` attribute on `Continent` was renamed to `code`.
|
||||||
|
* Documentation updates.
|
||||||
|
|
||||||
|
0.1.1 (2013-05-14)
|
||||||
|
------------------
|
||||||
|
|
||||||
|
* Updated Guzzle version requirement.
|
||||||
|
* Fixed Composer example in README.md.
|
||||||
|
|
||||||
|
|
||||||
|
0.1.0 (2013-05-13)
|
||||||
|
------------------
|
||||||
|
|
||||||
|
* Initial release.
|
||||||
179
README.md
179
README.md
@@ -1,28 +1,31 @@
|
|||||||
# GeoIP2 PHP API #
|
# GeoIP2 PHP API #
|
||||||
|
|
||||||
|
## Beta Note ##
|
||||||
|
|
||||||
|
This is a beta release. The API may change before the first production
|
||||||
|
release, which will be numbered 2.0.0.
|
||||||
|
|
||||||
|
You may find information on the GeoIP2 beta release process on [our
|
||||||
|
website](http://www.maxmind.com/en/geoip2_beta).
|
||||||
|
|
||||||
## Description ##
|
## Description ##
|
||||||
|
|
||||||
Currently, this distribution provides an API for the GeoIP2 web services
|
This distribution provides an API for the [GeoIP2 web services]
|
||||||
(as documented at http://dev.maxmind.com/geoip/geoip2/web-services).
|
(http://dev.maxmind.com/geoip/geoip2/web-services) and the [GeoLite2
|
||||||
|
databases](http://dev.maxmind.com/geoip/geoip2/geolite2/). The commercial
|
||||||
In the future, this distribution will also provide the same API for the
|
GeoIP2 databases have not yet been released as a downloadable product.
|
||||||
GeoIP2 downloadable databases. These databases have not yet been
|
|
||||||
released as a downloadable product.
|
|
||||||
|
|
||||||
See GeoIP2\WebService\Client for details on the web service client
|
|
||||||
API.
|
|
||||||
|
|
||||||
## Installation ##
|
## Installation ##
|
||||||
|
|
||||||
### Define Your Dependencies ###
|
### Define Your Dependencies ###
|
||||||
|
|
||||||
We recommend installing this package with [Composer](http://getcomposer.org/).
|
We recommend installing this package with [Composer](http://getcomposer.org/).
|
||||||
To do this, add ```geoip2/geoip2``` to your ```composer.json``` file.
|
To do this, add `geoip2/geoip2` to your `composer.json` file.
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"require": {
|
"require": {
|
||||||
"geoip2/geoip2": "1.0.*"
|
"geoip2/geoip2": "0.5.*"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -50,10 +53,71 @@ You can autoload all dependencies by adding this to your code:
|
|||||||
require 'vendor/autoload.php';
|
require 'vendor/autoload.php';
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage ##
|
### Optional C Extension ###
|
||||||
|
|
||||||
To use this API, you must create a new ``\GeoIP2\WebService\Client``
|
The [MaxMind DB API](https://github.com/maxmind/MaxMind-DB-Reader-php)
|
||||||
object with your ``$userId`` and ``$licenseKey``, then you call the method
|
includes an optional C extension that you may install to dramatically increase
|
||||||
|
the performance of lookups in GeoIP2 or GeoLite2 databases. To install, please
|
||||||
|
follow the instructions included with that API.
|
||||||
|
|
||||||
|
The extension has no effect on web-service lookups.
|
||||||
|
|
||||||
|
## Database Reader ##
|
||||||
|
|
||||||
|
### Usage ###
|
||||||
|
|
||||||
|
To use this API, you must create a new `\GeoIp2\Database\Reader` object with
|
||||||
|
the path to the database file as the first argument to the constructor. You
|
||||||
|
may then call the method corresponding to the database you are using.
|
||||||
|
|
||||||
|
If the lookup succeeds, the method call will return a model class for the
|
||||||
|
record in the database. This model in turn contains multiple container
|
||||||
|
classes for the different parts of the data such as the city in which the
|
||||||
|
IP address is located.
|
||||||
|
|
||||||
|
If the record is not found, a `\GeoIp2\Exception\AddressNotFoundException`
|
||||||
|
is returned. If the database is invalid or corrupt, a
|
||||||
|
`\MaxMind\Db\InvalidDatabaseException` will be thrown.
|
||||||
|
|
||||||
|
See the API documentation for more details.
|
||||||
|
|
||||||
|
### Example ###
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
require_once 'vendor/autoload.php';
|
||||||
|
use GeoIp2\Database\Reader;
|
||||||
|
|
||||||
|
// This creates the Reader object, which should be reused across
|
||||||
|
// lookups.
|
||||||
|
$reader = new Reader('/usr/local/share/GeoIP/GeoIP2-City.mmdb');
|
||||||
|
|
||||||
|
// Replace "city" with the appropriate method for your database, e.g.,
|
||||||
|
// "country".
|
||||||
|
$record = $reader->city('128.101.101.101');
|
||||||
|
|
||||||
|
print($record->country->isoCode . "\n"); // 'US'
|
||||||
|
print($record->country->name . "\n"); // 'United States'
|
||||||
|
print($record->country->names['zh-CN'] . "\n"); // '美国'
|
||||||
|
|
||||||
|
print($record->mostSpecificSubdivision->name . "\n"); // 'Minnesota'
|
||||||
|
print($record->mostSpecificSubdivision->isoCode . "\n"); // 'MN'
|
||||||
|
|
||||||
|
print($record->city->name . "\n"); // 'Minneapolis'
|
||||||
|
|
||||||
|
print($record->postal->code . "\n"); // '55455'
|
||||||
|
|
||||||
|
print($record->location->latitude . "\n"); // 44.9733
|
||||||
|
print($record->location->longitude . "\n"); // -93.2323
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
## Web Service Client ##
|
||||||
|
|
||||||
|
### Usage ###
|
||||||
|
|
||||||
|
To use this API, you must create a new `\GeoIp2\WebService\Client`
|
||||||
|
object with your `$userId` and `$licenseKey`, then you call the method
|
||||||
corresponding to a specific end point, passing it the IP address you want to
|
corresponding to a specific end point, passing it the IP address you want to
|
||||||
look up.
|
look up.
|
||||||
|
|
||||||
@@ -61,41 +125,43 @@ If the request succeeds, the method call will return a model class for the end
|
|||||||
point you called. This model in turn contains multiple record classes, each of
|
point you called. This model in turn contains multiple record classes, each of
|
||||||
which represents part of the data returned by the web service.
|
which represents part of the data returned by the web service.
|
||||||
|
|
||||||
|
If there is an error, a structured exception is thrown.
|
||||||
|
|
||||||
See the API documentation for more details.
|
See the API documentation for more details.
|
||||||
|
|
||||||
## Example ##
|
### Example ###
|
||||||
|
|
||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
require_once 'vendor/autoload.php';
|
require_once 'vendor/autoload.php';
|
||||||
use \GeoIP2\WebService\Client;
|
use GeoIp2\WebService\Client;
|
||||||
|
|
||||||
|
// This creates a Client object that can be reused across requests.
|
||||||
|
// Replace "42" with your user ID and "license_key" with your license
|
||||||
|
// key.
|
||||||
$client = new Client(42, 'abcdef123456');
|
$client = new Client(42, 'abcdef123456');
|
||||||
$omni = $client->omni('24.24.24.24');
|
|
||||||
echo $omni->country->isoCode;
|
// Replace "city" with the method corresponding to the web service that
|
||||||
|
// you are using, e.g., "country", "cityIspOrg", "omni".
|
||||||
|
$record = $client->city('128.101.101.101');
|
||||||
|
|
||||||
|
print($record->country->isoCode . "\n"); // 'US'
|
||||||
|
print($record->country->name . "\n"); // 'United States'
|
||||||
|
print($record->country->names['zh-CN'] . "\n"); // '美国'
|
||||||
|
|
||||||
|
print($record->mostSpecificSubdivision->name . "\n"); // 'Minnesota'
|
||||||
|
print($record->mostSpecificSubdivision->isoCode . "\n"); // 'MN'
|
||||||
|
|
||||||
|
print($record->city->name . "\n"); // 'Minneapolis'
|
||||||
|
|
||||||
|
print($record->postal->code . "\n"); // '55455'
|
||||||
|
|
||||||
|
print($record->location->latitude . "\n"); // 44.9733
|
||||||
|
print($record->location->longitude . "\n"); // -93.2323
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Exceptions ##
|
### What data is returned? ###
|
||||||
|
|
||||||
For details on the possible errors returned by the web service itself, see
|
|
||||||
http://dev.maxmind.com/geoip2/geoip/web-services for the GeoIP2 web service
|
|
||||||
docs.
|
|
||||||
|
|
||||||
If the web service returns an explicit error document, this is thrown as a
|
|
||||||
```\GeoIP2\Exception\WebServiceException```. If some other sort of transport
|
|
||||||
error occurs, this is thrown as a ```\GeoIP2\Exception\HttpException```.
|
|
||||||
The difference is that the web service error includes an error message and
|
|
||||||
error code delivered by the web service. The latter is thrown when some sort
|
|
||||||
of unanticipated error occurs, such as the web service returning a 500 or an
|
|
||||||
invalid error document.
|
|
||||||
|
|
||||||
If the web service returns any status code besides 200, 4xx, or 5xx, this also
|
|
||||||
becomes a ```\GeoIP2\Exception\HttpException```.
|
|
||||||
|
|
||||||
Finally, if the web service returns a 200 but the body is invalid, the client
|
|
||||||
throws a ```\GeoIP2\Exception\GenericException```.
|
|
||||||
|
|
||||||
## What data is returned? ##
|
|
||||||
|
|
||||||
While many of the end points return the same basic records, the attributes
|
While many of the end points return the same basic records, the attributes
|
||||||
which can be populated vary between end points. In addition, while an end
|
which can be populated vary between end points. In addition, while an end
|
||||||
@@ -105,11 +171,12 @@ piece of data for any given IP address.
|
|||||||
Because of these factors, it is possible for any end point to return a record
|
Because of these factors, it is possible for any end point to return a record
|
||||||
where some or all of the attributes are unpopulated.
|
where some or all of the attributes are unpopulated.
|
||||||
|
|
||||||
See http://dev.maxmind.com/geoip/geoip2/web-services for details on what data
|
See the
|
||||||
each end point may return.
|
[GeoIP2 web service docs](http://dev.maxmind.com/geoip/geoip2/web-services)
|
||||||
|
for details on what data each end point may return.
|
||||||
|
|
||||||
The only piece of data which is always returned is the ```ipAddress```
|
The only piece of data which is always returned is the `ipAddress`
|
||||||
attribute in the ``GeoIP2\Record\Traits`` record.
|
attribute in the `GeoIp2\Record\Traits` record.
|
||||||
|
|
||||||
Every record class attribute has a corresponding predicate method so you can
|
Every record class attribute has a corresponding predicate method so you can
|
||||||
check to see if the attribute is set.
|
check to see if the attribute is set.
|
||||||
@@ -119,10 +186,10 @@ check to see if the attribute is set.
|
|||||||
[GeoNames](http://www.geonames.org/) offers web services and downloadable
|
[GeoNames](http://www.geonames.org/) offers web services and downloadable
|
||||||
databases with data on geographical features around the world, including
|
databases with data on geographical features around the world, including
|
||||||
populated places. They offer both free and paid premium data. Each
|
populated places. They offer both free and paid premium data. Each
|
||||||
feature is unique identified by a ```geonameId```, which is an integer.
|
feature is unique identified by a `geonameId`, which is an integer.
|
||||||
|
|
||||||
Many of the records returned by the GeoIP2 web services and databases
|
Many of the records returned by the GeoIP2 web services and databases
|
||||||
include a ```geonameId``` property. This is the ID of a geographical feature
|
include a `geonameId` property. This is the ID of a geographical feature
|
||||||
(city, region, country, etc.) in the GeoNames database.
|
(city, region, country, etc.) in the GeoNames database.
|
||||||
|
|
||||||
Some of the data that MaxMind provides is also sourced from GeoNames. We
|
Some of the data that MaxMind provides is also sourced from GeoNames. We
|
||||||
@@ -132,11 +199,11 @@ the GeoNames premium data set.
|
|||||||
## Reporting data problems ##
|
## Reporting data problems ##
|
||||||
|
|
||||||
If the problem you find is that an IP address is incorrectly mapped,
|
If the problem you find is that an IP address is incorrectly mapped,
|
||||||
please submit your correction to MaxMind at
|
please
|
||||||
http://www.maxmind.com/en/correction.
|
[submit your correction to MaxMind](http://www.maxmind.com/en/correction).
|
||||||
|
|
||||||
If you find some other sort of mistake, like an incorrect spelling,
|
If you find some other sort of mistake, like an incorrect spelling,
|
||||||
please check the GeoNames site (http://www.geonames.org/) first. Once
|
please check the [GeoNames site](http://www.geonames.org/) first. Once
|
||||||
you've searched for a place and found it on the GeoNames map view, there
|
you've searched for a place and found it on the GeoNames map view, there
|
||||||
are a number of links you can use to correct data ("move", "edit",
|
are a number of links you can use to correct data ("move", "edit",
|
||||||
"alternate names", etc.). Once the correction is part of the GeoNames
|
"alternate names", etc.). Once the correction is part of the GeoNames
|
||||||
@@ -144,17 +211,17 @@ data set, it will be automatically incorporated into future MaxMind
|
|||||||
releases.
|
releases.
|
||||||
|
|
||||||
If you are a paying MaxMind customer and you're not sure where to submit
|
If you are a paying MaxMind customer and you're not sure where to submit
|
||||||
a correction, please contact MaxMind support at
|
a correction, please
|
||||||
http://www.maxmind.com/en/support for help.
|
[contact MaxMind support](http://www.maxmind.com/en/support) for help.
|
||||||
|
|
||||||
## Other Support ##
|
## Other Support ##
|
||||||
|
|
||||||
Please report all issues with this code using the GitHub issue tracker
|
Please report all issues with this code using the
|
||||||
at https://github.com/maxmind/GeoIP2-php/issues
|
[GitHub issue tracker](https://github.com/maxmind/GeoIP2-php/issues).
|
||||||
|
|
||||||
If you are having an issue with a MaxMind service that is not specific
|
If you are having an issue with a MaxMind service that is not specific
|
||||||
to the client API please see http://www.maxmind.com/en/support for
|
to the client API, please see
|
||||||
details.
|
[our support page](http://www.maxmind.com/en/support).
|
||||||
|
|
||||||
## Requirements ##
|
## Requirements ##
|
||||||
|
|
||||||
@@ -172,10 +239,6 @@ PSR-2 style guidelines. Please include unit tests whenever possible.
|
|||||||
|
|
||||||
The GeoIP2 PHP API uses [Semantic Versioning](http://semver.org/).
|
The GeoIP2 PHP API uses [Semantic Versioning](http://semver.org/).
|
||||||
|
|
||||||
## Author ##
|
|
||||||
|
|
||||||
[Gregory Oschwald](mailto:goschwald@maxmind.com)
|
|
||||||
|
|
||||||
## Copyright and License ##
|
## Copyright and License ##
|
||||||
|
|
||||||
This software is Copyright (c) 2013 by MaxMind, Inc.
|
This software is Copyright (c) 2013 by MaxMind, Inc.
|
||||||
|
|||||||
63
bin/release.sh
Executable file
63
bin/release.sh
Executable file
@@ -0,0 +1,63 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
TAG=$1
|
||||||
|
|
||||||
|
if [ -z $TAG ]; then
|
||||||
|
echo "Please specify a tag"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$(git status --porcelain)" ]; then
|
||||||
|
echo ". is not clean." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -d .gh-pages ]; then
|
||||||
|
echo "Checking out gh-pages in .gh-pages"
|
||||||
|
git clone -b gh-pages git@git.maxmind.com:GeoIP2-php .gh-pages
|
||||||
|
cd .gh-pages
|
||||||
|
else
|
||||||
|
echo "Updating .gh-pages"
|
||||||
|
cd .gh-pages
|
||||||
|
git pull
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$(git status --porcelain)" ]; then
|
||||||
|
echo ".gh-pages is not clean" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
apigen --quiet --download --title "GeoIP2 PHP API $TAG" --source ../src --destination doc/$TAG
|
||||||
|
|
||||||
|
PAGE=index.md
|
||||||
|
cat <<EOF > $PAGE
|
||||||
|
---
|
||||||
|
layout: default
|
||||||
|
title: MaxMind GeoIP2 PHP API
|
||||||
|
language: php
|
||||||
|
version: $TAG
|
||||||
|
---
|
||||||
|
|
||||||
|
EOF
|
||||||
|
|
||||||
|
cat ../README.md >> $PAGE
|
||||||
|
|
||||||
|
git add doc/
|
||||||
|
git commit -m "Updated for $TAG" -a
|
||||||
|
|
||||||
|
read -e -p "Push to origin? " SHOULD_PUSH
|
||||||
|
|
||||||
|
if [ "$SHOULD_PUSH" != "y" ]; then
|
||||||
|
echo "Aborting"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# If we don't push directly to github, the page doesn't get built for some
|
||||||
|
# reason.
|
||||||
|
git push git@github.com:maxmind/GeoIP2-php.git
|
||||||
|
git push
|
||||||
|
|
||||||
|
cd ..
|
||||||
|
git tag -a $TAG
|
||||||
|
git push
|
||||||
|
git push --tags
|
||||||
@@ -13,13 +13,15 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"guzzle/guzzle": "~3.1.1",
|
"guzzle/guzzle": "3.*",
|
||||||
|
"maxmind-db/reader": "0.2.*",
|
||||||
"php": ">=5.3.1"
|
"php": ">=5.3.1"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "3.7.*"
|
"phpunit/phpunit": "3.7.*",
|
||||||
|
"satooshi/php-coveralls": "dev-master"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-0": { "GeoIP2": "src/" }
|
"psr-0": { "GeoIp2": "src/" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
1
maxmind-db
Submodule
1
maxmind-db
Submodule
Submodule maxmind-db added at f7b9342b84
@@ -3,13 +3,18 @@
|
|||||||
<phpunit bootstrap="./tests/bootstrap.php" colors="true">
|
<phpunit bootstrap="./tests/bootstrap.php" colors="true">
|
||||||
<testsuites>
|
<testsuites>
|
||||||
<testsuite name="GeoIP2 Test Suite">
|
<testsuite name="GeoIP2 Test Suite">
|
||||||
<directory suffix="Test.php">./tests/GeoIP2/Test/</directory>
|
<directory suffix="Test.php">./tests/GeoIp2/Test/</directory>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
</testsuites>
|
</testsuites>
|
||||||
|
|
||||||
<filter>
|
<filter>
|
||||||
<whitelist>
|
<whitelist>
|
||||||
<directory suffix=".php">./src/GeoIP2/</directory>
|
<directory suffix=".php">./src/GeoIp2/</directory>
|
||||||
</whitelist>
|
</whitelist>
|
||||||
</filter>
|
</filter>
|
||||||
|
|
||||||
|
<logging>
|
||||||
|
<log type="coverage-clover" target="build/logs/clover.xml"/>
|
||||||
|
</logging>
|
||||||
|
|
||||||
</phpunit>
|
</phpunit>
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace GeoIP2\Exception;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class represents a generic error.
|
|
||||||
*/
|
|
||||||
class GenericException extends \Exception
|
|
||||||
{
|
|
||||||
}
|
|
||||||
149
src/GeoIp2/Database/Reader.php
Normal file
149
src/GeoIp2/Database/Reader.php
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace GeoIp2\Database;
|
||||||
|
|
||||||
|
use GeoIp2\Exception\AddressNotFoundException;
|
||||||
|
use GeoIp2\ProviderInterface;
|
||||||
|
use MaxMind\Db\Reader as DbReader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instances of this class provide a reader for the GeoIP2 database format.
|
||||||
|
* IP addresses can be looked up using the <code>country</code>
|
||||||
|
* and <code>city</code> methods. We also provide <code>cityIspOrg</code>
|
||||||
|
* and <code>omni</code> methods to ease compatibility with the web service
|
||||||
|
* client, although we may offer the ability to specify additional databases
|
||||||
|
* to replicate these web services in the future (e.g., the ISP/Org database).
|
||||||
|
*
|
||||||
|
* **Usage**
|
||||||
|
*
|
||||||
|
* The basic API for this class is the same for every database. First, you
|
||||||
|
* create a reader object, specifying a file name. You then call the method
|
||||||
|
* corresponding to the specific database, passing it the IP address you want
|
||||||
|
* to look up.
|
||||||
|
*
|
||||||
|
* If the request succeeds, the method call will return a model class for
|
||||||
|
* the method you called. This model in turn contains multiple record classes,
|
||||||
|
* each of which represents part of the data returned by the database. If
|
||||||
|
* the database does not contain the requested information, the attributes
|
||||||
|
* on the record class will have a <code>null</code> value.
|
||||||
|
*
|
||||||
|
* If the address is not in the database, an
|
||||||
|
* {@link \GeoIp2\Exception\AddressNotFoundException} exception will be
|
||||||
|
* thrown. If an invalid IP address is passed to one of the methods, a
|
||||||
|
* 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
|
||||||
|
{
|
||||||
|
private $dbReader;
|
||||||
|
private $locales;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
|
||||||
|
* is corrupt or invalid
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
$filename,
|
||||||
|
$locales = array('en')
|
||||||
|
) {
|
||||||
|
$this->dbReader = new DbReader($filename);
|
||||||
|
$this->locales = $locales;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method returns a GeoIP2 City model.
|
||||||
|
*
|
||||||
|
* @param string $ipAddress IPv4 or IPv6 address as a string.
|
||||||
|
*
|
||||||
|
* @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)
|
||||||
|
{
|
||||||
|
return $this->modelFor('City', $ipAddress);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method returns a GeoIP2 Country model.
|
||||||
|
*
|
||||||
|
* @param string $ipAddress IPv4 or IPv6 address as a string.
|
||||||
|
*
|
||||||
|
* @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)
|
||||||
|
{
|
||||||
|
return $this->modelFor('Country', $ipAddress);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method returns a GeoIP2 City/ISP/Org model.
|
||||||
|
*
|
||||||
|
* @param string $ipAddress IPv4 or IPv6 address as a string.
|
||||||
|
*
|
||||||
|
* @return \GeoIp2\Model\CityIspOrg
|
||||||
|
*
|
||||||
|
* @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 cityIspOrg($ipAddress)
|
||||||
|
{
|
||||||
|
return $this->modelFor('CityIspOrg', $ipAddress);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method returns a GeoIP2 Omni model.
|
||||||
|
*
|
||||||
|
* @param string $ipAddress IPv4 or IPv6 address as a string.
|
||||||
|
*
|
||||||
|
* @return \GeoIp2\Model\Omni
|
||||||
|
*
|
||||||
|
* @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 omni($ipAddress)
|
||||||
|
{
|
||||||
|
return $this->modelFor('Omni', $ipAddress);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function modelFor($class, $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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Closes the GeoIP2 database and returns the resources to the system.
|
||||||
|
*/
|
||||||
|
public function close()
|
||||||
|
{
|
||||||
|
$this->dbReader->close();
|
||||||
|
}
|
||||||
|
}
|
||||||
10
src/GeoIp2/Exception/AddressNotFoundException.php
Normal file
10
src/GeoIp2/Exception/AddressNotFoundException.php
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace GeoIp2\Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class represents a generic error.
|
||||||
|
*/
|
||||||
|
class AddressNotFoundException extends GeoIp2Exception
|
||||||
|
{
|
||||||
|
}
|
||||||
10
src/GeoIp2/Exception/AuthenticationException.php
Normal file
10
src/GeoIp2/Exception/AuthenticationException.php
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace GeoIp2\Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class represents a generic error.
|
||||||
|
*/
|
||||||
|
class AuthenticationException extends GeoIp2Exception
|
||||||
|
{
|
||||||
|
}
|
||||||
10
src/GeoIp2/Exception/GeoIp2Exception.php
Normal file
10
src/GeoIp2/Exception/GeoIp2Exception.php
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace GeoIp2\Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class represents a generic error.
|
||||||
|
*/
|
||||||
|
class GeoIp2Exception extends \Exception
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace GeoIP2\Exception;
|
namespace GeoIp2\Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class represents an HTTP transport error.
|
* This class represents an HTTP transport error.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class HttpException extends GenericException
|
class HttpException extends GeoIp2Exception
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* The URI queried
|
* The URI queried
|
||||||
@@ -17,7 +17,7 @@ class HttpException extends GenericException
|
|||||||
$message,
|
$message,
|
||||||
$httpStatus,
|
$httpStatus,
|
||||||
$uri,
|
$uri,
|
||||||
Exception $previous = null
|
\Exception $previous = null
|
||||||
) {
|
) {
|
||||||
$this->uri = $uri;
|
$this->uri = $uri;
|
||||||
parent::__construct($message, $httpStatus, $previous);
|
parent::__construct($message, $httpStatus, $previous);
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace GeoIP2\Exception;
|
namespace GeoIp2\Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class represents an error returned by MaxMind's GeoIP2 Precision
|
* This class represents an error returned by MaxMind's GeoIP2
|
||||||
* web service.
|
* web service.
|
||||||
*/
|
*/
|
||||||
class WebServiceException extends HttpException
|
class InvalidRequestException extends HttpException
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* The code returned by the MaxMind web service
|
* The code returned by the MaxMind web service
|
||||||
@@ -18,7 +18,7 @@ class WebServiceException extends HttpException
|
|||||||
$error,
|
$error,
|
||||||
$httpStatus,
|
$httpStatus,
|
||||||
$uri,
|
$uri,
|
||||||
Exception $previous = null
|
\Exception $previous = null
|
||||||
) {
|
) {
|
||||||
$this->error = $error;
|
$this->error = $error;
|
||||||
parent::__construct($message, $httpStatus, $uri, $previous);
|
parent::__construct($message, $httpStatus, $uri, $previous);
|
||||||
10
src/GeoIp2/Exception/OutOfQueriesException.php
Normal file
10
src/GeoIp2/Exception/OutOfQueriesException.php
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace GeoIp2\Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class represents a generic error.
|
||||||
|
*/
|
||||||
|
class OutOfQueriesException extends GeoIp2Exception
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -1,39 +1,42 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace GeoIP2\Model;
|
namespace GeoIp2\Model;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class provides a model for the data returned by the GeoIP2 Precision
|
* This class provides a model for the data returned by the GeoIP2
|
||||||
* City end point.
|
* City end point.
|
||||||
*
|
*
|
||||||
* The only difference between the City, City/ISP/Org, and Omni model
|
* The only difference between the City, City/ISP/Org, and Omni model
|
||||||
* classes is which fields in each record may be populated. See
|
* classes is which fields in each record may be populated. See
|
||||||
* http://dev.maxmind.com/geoip/geoip2/web-services more details.
|
* http://dev.maxmind.com/geoip/geoip2/web-services more details.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\City $city Country data for the requested IP
|
* @property \GeoIp2\Record\City $city City data for the requested IP
|
||||||
* address.
|
* address.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\Continent $continent Continent data for the
|
* @property \GeoIp2\Record\Continent $continent Continent data for the
|
||||||
* requested IP address.
|
* requested IP address.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\Country $country Country data for the requested
|
* @property \GeoIp2\Record\Country $country Country data for the requested
|
||||||
* IP address. This object represents the country where MaxMind believes the
|
* IP address. This object represents the country where MaxMind believes the
|
||||||
* end user is located.
|
* end user is located.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\Location $location Location data for the
|
* @property \GeoIp2\Record\Location $location Location data for the
|
||||||
* requested IP address.
|
* requested IP address.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\Country $registeredCountry Registered country
|
* @property \GeoIp2\Record\MaxMind $maxmind Data related to your MaxMind
|
||||||
|
* account.
|
||||||
|
*
|
||||||
|
* @property \GeoIp2\Record\Country $registeredCountry Registered country
|
||||||
* data for the requested IP address. This record represents the country
|
* data for the requested IP address. This record represents the country
|
||||||
* where the ISP has registered a given IP block in and may differ from the
|
* where the ISP has registered a given IP block and may differ from the
|
||||||
* user's country.
|
* user's country.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\RepresentedCountry $representedCountry
|
* @property \GeoIp2\Record\RepresentedCountry $representedCountry
|
||||||
* Represented country data for the requested IP address. The represented
|
* Represented country data for the requested IP address. The represented
|
||||||
* country is used for things like military bases or embassies. It is only
|
* country is used for things like military bases or embassies. It is only
|
||||||
* present when the represented country differs from the country.
|
* present when the represented country differs from the country.
|
||||||
*
|
*
|
||||||
* @property array $subdivisions An array of {@link \GeoIP2\Record\Subdivision}
|
* @property array $subdivisions An array of {@link \GeoIp2\Record\Subdivision}
|
||||||
* objects representing the country subdivisions for the requested IP
|
* objects representing the country subdivisions for the requested IP
|
||||||
* address. The number and type of subdivisions varies by country, but a
|
* address. The number and type of subdivisions varies by country, but a
|
||||||
* subdivision is typically a state, province, county, etc. Subdivisions
|
* subdivision is typically a state, province, county, etc. Subdivisions
|
||||||
@@ -41,12 +44,12 @@ namespace GeoIP2\Model;
|
|||||||
* If the response did not contain any subdivisions, this method returns
|
* If the response did not contain any subdivisions, this method returns
|
||||||
* an empty array.
|
* an empty array.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\Subdivision $mostSpecificSubdivision An object
|
* @property \GeoIp2\Record\Subdivision $mostSpecificSubdivision An object
|
||||||
* representing the most specific subdivision returned. If the response
|
* representing the most specific subdivision returned. If the response
|
||||||
* did not contain any subdivisions, this method returns an empty
|
* did not contain any subdivisions, this method returns an empty
|
||||||
* {@link \GeoIP2\Record\Subdivision} object.
|
* {@link \GeoIp2\Record\Subdivision} object.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\Traits $traits Data for the traits of the
|
* @property \GeoIp2\Record\Traits $traits Data for the traits of the
|
||||||
* requested IP address.
|
* requested IP address.
|
||||||
*/
|
*/
|
||||||
class City extends Country
|
class City extends Country
|
||||||
@@ -71,18 +74,18 @@ class City extends Country
|
|||||||
/**
|
/**
|
||||||
* @ignore
|
* @ignore
|
||||||
*/
|
*/
|
||||||
public function __construct($raw, $languages)
|
public function __construct($raw, $locales)
|
||||||
{
|
{
|
||||||
parent::__construct($raw, $languages);
|
parent::__construct($raw, $locales);
|
||||||
|
|
||||||
$this->city = new \GeoIP2\Record\City($this->get('city'), $languages);
|
$this->city = new \GeoIp2\Record\City($this->get('city'), $locales);
|
||||||
$this->location = new \GeoIP2\Record\Location($this->get('location'));
|
$this->location = new \GeoIp2\Record\Location($this->get('location'));
|
||||||
$this->postal = new \GeoIP2\Record\Postal($this->get('postal'));
|
$this->postal = new \GeoIp2\Record\Postal($this->get('postal'));
|
||||||
|
|
||||||
$this->createSubdivisions($raw, $languages);
|
$this->createSubdivisions($raw, $locales);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createSubdivisions($raw, $languages)
|
private function createSubdivisions($raw, $locales)
|
||||||
{
|
{
|
||||||
if (!isset($raw['subdivisions'])) {
|
if (!isset($raw['subdivisions'])) {
|
||||||
return;
|
return;
|
||||||
@@ -91,7 +94,7 @@ class City extends Country
|
|||||||
foreach ($raw['subdivisions'] as $sub) {
|
foreach ($raw['subdivisions'] as $sub) {
|
||||||
array_push(
|
array_push(
|
||||||
$this->subdivisions,
|
$this->subdivisions,
|
||||||
new \GeoIP2\Record\Subdivision($sub, $languages)
|
new \GeoIp2\Record\Subdivision($sub, $locales)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -110,8 +113,8 @@ class City extends Country
|
|||||||
|
|
||||||
private function mostSpecificSubdivision()
|
private function mostSpecificSubdivision()
|
||||||
{
|
{
|
||||||
return empty($this->subdivisions)?
|
return empty($this->subdivisions) ?
|
||||||
new \GeoIP2\Record\Subdivision(array(), $this->languages):
|
new \GeoIp2\Record\Subdivision(array(), $this->locales) :
|
||||||
end($this->subdivisions);
|
end($this->subdivisions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,39 +1,42 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace GeoIP2\Model;
|
namespace GeoIp2\Model;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class provides a model for the data returned by the GeoIP2 Precision
|
* This class provides a model for the data returned by the GeoIP2
|
||||||
* City/ISP/Org end point.
|
* City/ISP/Org end point.
|
||||||
*
|
*
|
||||||
* The only difference between the City, City/ISP/Org, and Omni model
|
* The only difference between the City, City/ISP/Org, and Omni model
|
||||||
* classes is which fields in each record may be populated. See
|
* classes is which fields in each record may be populated. See
|
||||||
* http://dev.maxmind.com/geoip/geoip2/web-services more details.
|
* http://dev.maxmind.com/geoip/geoip2/web-services more details.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\City $city Country data for the requested IP
|
* @property \GeoIp2\Record\City $city City data for the requested IP
|
||||||
* address.
|
* address.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\Continent $continent Continent data for the
|
* @property \GeoIp2\Record\Continent $continent Continent data for the
|
||||||
* requested IP address.
|
* requested IP address.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\Country $country Country data for the requested
|
* @property \GeoIp2\Record\Country $country Country data for the requested
|
||||||
* IP address. This object represents the country where MaxMind believes the
|
* IP address. This object represents the country where MaxMind believes the
|
||||||
* end user is located.
|
* end user is located.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\Location $location Location data for the
|
* @property \GeoIp2\Record\Location $location Location data for the
|
||||||
* requested IP address.
|
* requested IP address.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\Country $registeredCountry Registered country
|
* @property \GeoIp2\Record\MaxMind $maxmind Data related to your MaxMind
|
||||||
|
* account.
|
||||||
|
*
|
||||||
|
* @property \GeoIp2\Record\Country $registeredCountry Registered country
|
||||||
* data for the requested IP address. This record represents the country
|
* data for the requested IP address. This record represents the country
|
||||||
* where the ISP has registered a given IP block in and may differ from the
|
* where the ISP has registered a given IP block and may differ from the
|
||||||
* user's country.
|
* user's country.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\RepresentedCountry $representedCountry
|
* @property \GeoIp2\Record\RepresentedCountry $representedCountry
|
||||||
* Represented country data for the requested IP address. The represented
|
* Represented country data for the requested IP address. The represented
|
||||||
* country is used for things like military bases or embassies. It is only
|
* country is used for things like military bases or embassies. It is only
|
||||||
* present when the represented country differs from the country.
|
* present when the represented country differs from the country.
|
||||||
*
|
*
|
||||||
* @property array $subdivisions An array of {@link \GeoIP2\Record\Subdivision}
|
* @property array $subdivisions An array of {@link \GeoIp2\Record\Subdivision}
|
||||||
* objects representing the country subdivisions for the requested IP
|
* objects representing the country subdivisions for the requested IP
|
||||||
* address. The number and type of subdivisions varies by country, but a
|
* address. The number and type of subdivisions varies by country, but a
|
||||||
* subdivision is typically a state, province, county, etc. Subdivisions
|
* subdivision is typically a state, province, county, etc. Subdivisions
|
||||||
@@ -41,12 +44,12 @@ namespace GeoIP2\Model;
|
|||||||
* If the response did not contain any subdivisions, this method returns
|
* If the response did not contain any subdivisions, this method returns
|
||||||
* an empty array.
|
* an empty array.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\Subdivision $mostSpecificSubdivision An object
|
* @property \GeoIp2\Record\Subdivision $mostSpecificSubdivision An object
|
||||||
* representing the most specific subdivision returned. If the response
|
* representing the most specific subdivision returned. If the response
|
||||||
* did not contain any subdivisions, this method returns an empty
|
* did not contain any subdivisions, this method returns an empty
|
||||||
* {@link \GeoIP2\Record\Subdivision} object.
|
* {@link \GeoIp2\Record\Subdivision} object.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\Traits $traits Data for the traits of the
|
* @property \GeoIp2\Record\Traits $traits Data for the traits of the
|
||||||
* requested IP address.
|
* requested IP address.
|
||||||
*/
|
*/
|
||||||
class CityIspOrg extends City
|
class CityIspOrg extends City
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace GeoIP2\Model;
|
namespace GeoIp2\Model;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class provides a model for the data returned by the GeoIP2 Country
|
* This class provides a model for the data returned by the GeoIP2 Country
|
||||||
@@ -10,31 +10,35 @@ namespace GeoIP2\Model;
|
|||||||
* classes is which fields in each record may be populated. See
|
* classes is which fields in each record may be populated. See
|
||||||
* http://dev.maxmind.com/geoip/geoip2/web-services more details.
|
* http://dev.maxmind.com/geoip/geoip2/web-services more details.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\Continent $continent Continent data for the
|
* @property \GeoIp2\Record\Continent $continent Continent data for the
|
||||||
* requested IP address.
|
* requested IP address.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\Country $country Country data for the requested
|
* @property \GeoIp2\Record\Country $country Country data for the requested
|
||||||
* IP address. This object represents the country where MaxMind believes the
|
* IP address. This object represents the country where MaxMind believes the
|
||||||
* end user is located.
|
* end user is located.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\Country $registeredCountry Registered country
|
* @property \GeoIp2\Record\MaxMind $maxmind Data related to your MaxMind
|
||||||
|
* account.
|
||||||
|
*
|
||||||
|
* @property \GeoIp2\Record\Country $registeredCountry Registered country
|
||||||
* data for the requested IP address. This record represents the country
|
* data for the requested IP address. This record represents the country
|
||||||
* where the ISP has registered a given IP block in and may differ from the
|
* where the ISP has registered a given IP block and may differ from the
|
||||||
* user's country.
|
* user's country.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\RepresentedCountry $representedCountry
|
* @property \GeoIp2\Record\RepresentedCountry $representedCountry
|
||||||
* Represented country data for the requested IP address. The represented
|
* Represented country data for the requested IP address. The represented
|
||||||
* country is used for things like military bases or embassies. It is only
|
* country is used for things like military bases or embassies. It is only
|
||||||
* present when the represented country differs from the country.
|
* present when the represented country differs from the country.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\Traits $traits Data for the traits of the
|
* @property \GeoIp2\Record\Traits $traits Data for the traits of the
|
||||||
* requested IP address.
|
* requested IP address.
|
||||||
*/
|
*/
|
||||||
class Country
|
class Country
|
||||||
{
|
{
|
||||||
private $continent;
|
private $continent;
|
||||||
private $country;
|
private $country;
|
||||||
private $languages;
|
private $locales;
|
||||||
|
private $maxmind;
|
||||||
private $registeredCountry;
|
private $registeredCountry;
|
||||||
private $representedCountry;
|
private $representedCountry;
|
||||||
private $traits;
|
private $traits;
|
||||||
@@ -43,29 +47,30 @@ class Country
|
|||||||
/**
|
/**
|
||||||
* @ignore
|
* @ignore
|
||||||
*/
|
*/
|
||||||
public function __construct($raw, $languages)
|
public function __construct($raw, $locales)
|
||||||
{
|
{
|
||||||
$this->raw = $raw;
|
$this->raw = $raw;
|
||||||
|
|
||||||
$this->continent = new \GeoIP2\Record\Continent(
|
$this->continent = new \GeoIp2\Record\Continent(
|
||||||
$this->get('continent'),
|
$this->get('continent'),
|
||||||
$languages
|
$locales
|
||||||
);
|
);
|
||||||
$this->country = new \GeoIP2\Record\Country(
|
$this->country = new \GeoIp2\Record\Country(
|
||||||
$this->get('country'),
|
$this->get('country'),
|
||||||
$languages
|
$locales
|
||||||
);
|
);
|
||||||
$this->registeredCountry = new \GeoIP2\Record\Country(
|
$this->maxmind = new \GeoIp2\Record\MaxMind($this->get('maxmind'));
|
||||||
|
$this->registeredCountry = new \GeoIp2\Record\Country(
|
||||||
$this->get('registered_country'),
|
$this->get('registered_country'),
|
||||||
$languages
|
$locales
|
||||||
);
|
);
|
||||||
$this->representedCountry = new \GeoIP2\Record\RepresentedCountry(
|
$this->representedCountry = new \GeoIp2\Record\RepresentedCountry(
|
||||||
$this->get('represented_country'),
|
$this->get('represented_country'),
|
||||||
$languages
|
$locales
|
||||||
);
|
);
|
||||||
$this->traits = new \GeoIP2\Record\Traits($this->get('traits'));
|
$this->traits = new \GeoIp2\Record\Traits($this->get('traits'));
|
||||||
|
|
||||||
$this->languages = $languages;
|
$this->locales = $locales;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -79,7 +84,7 @@ class Country
|
|||||||
/**
|
/**
|
||||||
* @ignore
|
* @ignore
|
||||||
*/
|
*/
|
||||||
public function __get ($attr)
|
public function __get($attr)
|
||||||
{
|
{
|
||||||
if ($attr != "instance" && isset($this->$attr)) {
|
if ($attr != "instance" && isset($this->$attr)) {
|
||||||
return $this->$attr;
|
return $this->$attr;
|
||||||
@@ -1,39 +1,42 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace GeoIP2\Model;
|
namespace GeoIp2\Model;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class provides a model for the data returned by the GeoIP2 Precision
|
* This class provides a model for the data returned by the GeoIP2
|
||||||
* Omni end point.
|
* Omni end point.
|
||||||
*
|
*
|
||||||
* The only difference between the City, City/ISP/Org, and Omni model
|
* The only difference between the City, City/ISP/Org, and Omni model
|
||||||
* classes is which fields in each record may be populated. See
|
* classes is which fields in each record may be populated. See
|
||||||
* http://dev.maxmind.com/geoip/geoip2/web-services more details.
|
* http://dev.maxmind.com/geoip/geoip2/web-services more details.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\City $city Country data for the requested IP
|
* @property \GeoIp2\Record\City $city City data for the requested IP
|
||||||
* address.
|
* address.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\Continent $continent Continent data for the
|
* @property \GeoIp2\Record\Continent $continent Continent data for the
|
||||||
* requested IP address.
|
* requested IP address.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\Country $country Country data for the requested
|
* @property \GeoIp2\Record\Country $country Country data for the requested
|
||||||
* IP address. This object represents the country where MaxMind believes the
|
* IP address. This object represents the country where MaxMind believes the
|
||||||
* end user is located.
|
* end user is located.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\Location $location Location data for the
|
* @property \GeoIp2\Record\Location $location Location data for the
|
||||||
* requested IP address.
|
* requested IP address.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\Country $registeredCountry Registered country
|
* @property \GeoIp2\Record\MaxMind $maxmind Data related to your MaxMind
|
||||||
|
* account.
|
||||||
|
*
|
||||||
|
* @property \GeoIp2\Record\Country $registeredCountry Registered country
|
||||||
* data for the requested IP address. This record represents the country
|
* data for the requested IP address. This record represents the country
|
||||||
* where the ISP has registered a given IP block in and may differ from the
|
* where the ISP has registered a given IP block and may differ from the
|
||||||
* user's country.
|
* user's country.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\RepresentedCountry $representedCountry
|
* @property \GeoIp2\Record\RepresentedCountry $representedCountry
|
||||||
* Represented country data for the requested IP address. The represented
|
* Represented country data for the requested IP address. The represented
|
||||||
* country is used for things like military bases or embassies. It is only
|
* country is used for things like military bases or embassies. It is only
|
||||||
* present when the represented country differs from the country.
|
* present when the represented country differs from the country.
|
||||||
*
|
*
|
||||||
* @property array $subdivisions An array of {@link \GeoIP2\Record\Subdivision}
|
* @property array $subdivisions An array of {@link \GeoIp2\Record\Subdivision}
|
||||||
* objects representing the country subdivisions for the requested IP
|
* objects representing the country subdivisions for the requested IP
|
||||||
* address. The number and type of subdivisions varies by country, but a
|
* address. The number and type of subdivisions varies by country, but a
|
||||||
* subdivision is typically a state, province, county, etc. Subdivisions
|
* subdivision is typically a state, province, county, etc. Subdivisions
|
||||||
@@ -41,12 +44,12 @@ namespace GeoIP2\Model;
|
|||||||
* If the response did not contain any subdivisions, this method returns
|
* If the response did not contain any subdivisions, this method returns
|
||||||
* an empty array.
|
* an empty array.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\Subdivision $mostSpecificSubdivision An object
|
* @property \GeoIp2\Record\Subdivision $mostSpecificSubdivision An object
|
||||||
* representing the most specific subdivision returned. If the response
|
* representing the most specific subdivision returned. If the response
|
||||||
* did not contain any subdivisions, this method returns an empty
|
* did not contain any subdivisions, this method returns an empty
|
||||||
* {@link \GeoIP2\Record\Subdivision} object.
|
* {@link \GeoIp2\Record\Subdivision} object.
|
||||||
*
|
*
|
||||||
* @property \GeoIP2\Record\Traits $traits Data for the traits of the
|
* @property \GeoIp2\Record\Traits $traits Data for the traits of the
|
||||||
* requested IP address.
|
* requested IP address.
|
||||||
*/
|
*/
|
||||||
class Omni extends CityIspOrg
|
class Omni extends CityIspOrg
|
||||||
34
src/GeoIp2/ProviderInterface.php
Normal file
34
src/GeoIp2/ProviderInterface.php
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace GeoIp2;
|
||||||
|
|
||||||
|
interface ProviderInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param ipAddress
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
public function city($ipAddress);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ipAddress
|
||||||
|
* IPv4 or IPv6 address to lookup.
|
||||||
|
* @return \GeoIp2\Model\CityIspOrg A CityIspOrg model for the requested IP address.
|
||||||
|
*/
|
||||||
|
public function cityIspOrg($ipAddress);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ipAddress
|
||||||
|
* IPv4 or IPv6 address to lookup.
|
||||||
|
* @return \GeoIp2\Model\Omni An Omni model for the requested IP address.
|
||||||
|
*/
|
||||||
|
public function omni($ipAddress);
|
||||||
|
}
|
||||||
@@ -1,17 +1,17 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace GeoIP2\Record;
|
namespace GeoIp2\Record;
|
||||||
|
|
||||||
abstract class AbstractPlaceRecord extends AbstractRecord
|
abstract class AbstractPlaceRecord extends AbstractRecord
|
||||||
{
|
{
|
||||||
private $languages;
|
private $locales;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ignore
|
* @ignore
|
||||||
*/
|
*/
|
||||||
public function __construct($record, $languages)
|
public function __construct($record, $locales)
|
||||||
{
|
{
|
||||||
$this->languages = $languages;
|
$this->locales = $locales;
|
||||||
parent::__construct($record);
|
parent::__construct($record);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,9 +29,9 @@ abstract class AbstractPlaceRecord extends AbstractRecord
|
|||||||
|
|
||||||
private function name()
|
private function name()
|
||||||
{
|
{
|
||||||
foreach ($this->languages as $language) {
|
foreach ($this->locales as $locale) {
|
||||||
if (isset($this->names[$language])) {
|
if (isset($this->names[$locale])) {
|
||||||
return $this->names[$language];
|
return $this->names[$locale];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace GeoIP2\Record;
|
namespace GeoIp2\Record;
|
||||||
|
|
||||||
abstract class AbstractRecord
|
abstract class AbstractRecord
|
||||||
{
|
{
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace GeoIP2\Record;
|
namespace GeoIp2\Record;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* City-level data associated with an IP address.
|
* City-level data associated with an IP address.
|
||||||
@@ -14,10 +14,10 @@ namespace GeoIP2\Record;
|
|||||||
* @property int $geonameId The GeoName ID for the city. This attribute
|
* @property int $geonameId The GeoName ID for the city. This attribute
|
||||||
* is returned by all end points.
|
* is returned by all end points.
|
||||||
*
|
*
|
||||||
* @property string $name The name of the city based on the languages list
|
* @property string $name The name of the city based on the locales list
|
||||||
* passed to the constructor. This attribute is returned by all end points.
|
* passed to the constructor. This attribute is returned by all end points.
|
||||||
*
|
*
|
||||||
* @property array $names A array map where the keys are language codes
|
* @property array $names A array map where the keys are locale codes
|
||||||
* and the values are names. This attribute is returned by all end points.
|
* and the values are names. This attribute is returned by all end points.
|
||||||
*/
|
*/
|
||||||
class City extends AbstractPlaceRecord
|
class City extends AbstractPlaceRecord
|
||||||
@@ -1,24 +1,23 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace GeoIP2\Record;
|
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 the end points.
|
* This record is returned by all the end points.
|
||||||
*
|
*
|
||||||
* @property string $continentCode A two character continent code
|
* @property string $code A two character continent code like "NA" (North
|
||||||
* like "NA" (North America) or "OC" (Oceania). This attribute is returned
|
* America) or "OC" (Oceania). This attribute is returned by all end points.
|
||||||
* by all end points.
|
|
||||||
*
|
*
|
||||||
* @property int $geonameId The GeoName ID for the continent. This attribute
|
* @property int $geonameId The GeoName ID for the continent. This attribute
|
||||||
* is returned by all end points.
|
* is returned by all end points.
|
||||||
*
|
*
|
||||||
* @property string $name Returns the name of the continent based on the
|
* @property string $name Returns the name of the continent based on the
|
||||||
* languages list passed to the constructor. This attribute is returned by
|
* locales list passed to the constructor. This attribute is returned by
|
||||||
* all end points.
|
* all end points.
|
||||||
*
|
*
|
||||||
* @property array $names An array map where the keys are language codes
|
* @property array $names An array map where the keys are locale codes
|
||||||
* and the values are names. This attribute is returned by all end points.
|
* and the values are names. This attribute is returned by all end points.
|
||||||
*/
|
*/
|
||||||
class Continent extends AbstractPlaceRecord
|
class Continent extends AbstractPlaceRecord
|
||||||
@@ -27,7 +26,7 @@ class Continent extends AbstractPlaceRecord
|
|||||||
* @ignore
|
* @ignore
|
||||||
*/
|
*/
|
||||||
protected $validAttributes = array(
|
protected $validAttributes = array(
|
||||||
'continentCode',
|
'code',
|
||||||
'geonameId',
|
'geonameId',
|
||||||
'names'
|
'names'
|
||||||
);
|
);
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace GeoIP2\Record;
|
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
|
||||||
@@ -18,10 +18,10 @@ namespace GeoIP2\Record;
|
|||||||
* two-character ISO 3166-1 alpha code} for the country. This attribute is
|
* two-character ISO 3166-1 alpha code} for the country. This attribute is
|
||||||
* returned by all end points.
|
* returned by all end points.
|
||||||
*
|
*
|
||||||
* @property string $name The name of the country based on the languages list
|
* @property string $name The name of the country based on the locales list
|
||||||
* passed to the constructor. This attribute is returned by all end points.
|
* passed to the constructor. This attribute is returned by all end points.
|
||||||
*
|
*
|
||||||
* @property array $names An array map where the keys are language codes and
|
* @property array $names An array map where the keys are locale codes and
|
||||||
* the values are names. This attribute is returned by all end points.
|
* the values are names. This attribute is returned by all end points.
|
||||||
*/
|
*/
|
||||||
class Country extends AbstractPlaceRecord
|
class Country extends AbstractPlaceRecord
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace GeoIP2\Record;
|
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
|
||||||
19
src/GeoIp2/Record/MaxMind.php
Normal file
19
src/GeoIp2/Record/MaxMind.php
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace GeoIp2\Record;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains data about your account.
|
||||||
|
*
|
||||||
|
* This record is returned by all the end points.
|
||||||
|
*
|
||||||
|
* @property int $queriesRemaining The number of remaining queries you have
|
||||||
|
* for the end point you are calling.
|
||||||
|
*/
|
||||||
|
class MaxMind extends AbstractRecord
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @ignore
|
||||||
|
*/
|
||||||
|
protected $validAttributes = array('queriesRemaining');
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace GeoIP2\Record;
|
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
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace GeoIP2\Record;
|
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
|
||||||
@@ -22,10 +22,10 @@ namespace GeoIP2\Record;
|
|||||||
* two-character ISO 3166-1 alpha code} for the country. This attribute is
|
* two-character ISO 3166-1 alpha code} for the country. This attribute is
|
||||||
* returned by all end points.
|
* returned by all end points.
|
||||||
*
|
*
|
||||||
* @property string $name The name of the country based on the languages list
|
* @property string $name The name of the country based on the locales list
|
||||||
* passed to the constructor. This attribute is returned by all end points.
|
* passed to the constructor. This attribute is returned by all end points.
|
||||||
*
|
*
|
||||||
* @property array $names An array map where the keys are language codes and
|
* @property array $names An array map where the keys are locale codes and
|
||||||
* the values are names. This attribute is returned by all end points.
|
* the values are names. This attribute is returned by all end points.
|
||||||
*
|
*
|
||||||
* @property string $type A string indicating the type of entity that is
|
* @property string $type A string indicating the type of entity that is
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace GeoIP2\Record;
|
namespace GeoIp2\Record;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -20,14 +20,14 @@ namespace GeoIP2\Record;
|
|||||||
* http://en.wikipedia.org/wiki/ISO_3166-2 ISO 3166-2 code}. This attribute
|
* http://en.wikipedia.org/wiki/ISO_3166-2 ISO 3166-2 code}. This attribute
|
||||||
* is returned by all end points except Country.
|
* is returned by all end points except Country.
|
||||||
*
|
*
|
||||||
* @property string $name The name of the subdivision based on the languages
|
* @property string $name The name of the subdivision based on the locales
|
||||||
* list passed to the constructor. This attribute is returned by all end
|
* list passed to the constructor. This attribute is returned by all end
|
||||||
* points except Country.
|
* points except Country.
|
||||||
*
|
*
|
||||||
* @property array $names An array map where the keys are language codes and
|
* @property array $names An array map where the keys are locale codes and
|
||||||
* the values are names. This attribute is returned by all end points except
|
* the values are names. This attribute is returned by all end points except
|
||||||
* Country.
|
* Country.
|
||||||
*/
|
*/
|
||||||
class Subdivision extends AbstractPlaceRecord
|
class Subdivision extends AbstractPlaceRecord
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace GeoIP2\Record;
|
namespace GeoIp2\Record;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -35,12 +35,16 @@ namespace GeoIP2\Record;
|
|||||||
* anonymous proxy. See {@link http://dev.maxmind.com/faq/geoip#anonproxy}
|
* anonymous proxy. See {@link http://dev.maxmind.com/faq/geoip#anonproxy}
|
||||||
* for further details. This attribute is returned by all end points.
|
* for further details. This attribute is returned by all end points.
|
||||||
*
|
*
|
||||||
* @property string $isp The name of the ISP associated the IP address.
|
* @property boolean $isSatelliteProvider This is true if the IP belongs
|
||||||
|
* to a satellite Internet provider. This attribute is returned by all
|
||||||
|
* end points.
|
||||||
|
*
|
||||||
|
* @property string $isp The name of the ISP associated with the IP address.
|
||||||
* This attribute is only available from the City/ISP/Org and Omni end
|
* This attribute is only available from the City/ISP/Org and Omni end
|
||||||
* points.
|
* points.
|
||||||
*
|
*
|
||||||
* @property string $organization The name of the organization associated
|
* @property string $organization The name of the organization associated
|
||||||
* the IP address. This attribute is only available from the City/ISP/Org
|
* with the IP address. This attribute is only available from the City/ISP/Org
|
||||||
* and Omni end points.
|
* and Omni end points.
|
||||||
*
|
*
|
||||||
* @property string $userType <p>The user type associated with the IP
|
* @property string $userType <p>The user type associated with the IP
|
||||||
@@ -1,21 +1,21 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace GeoIP2\WebService;
|
namespace GeoIp2\WebService;
|
||||||
|
|
||||||
use GeoIP2\Exception\GenericException;
|
use GeoIp2\Exception\AddressNotFoundException;
|
||||||
use GeoIP2\Exception\HttpException;
|
use GeoIp2\Exception\AuthenticationException;
|
||||||
use GeoIP2\Exception\WebServiceException;
|
use GeoIp2\Exception\GeoIp2Exception;
|
||||||
use GeoIP2\Model\City;
|
use GeoIp2\Exception\HttpException;
|
||||||
use GeoIP2\Model\CityIspOrg;
|
use GeoIp2\Exception\InvalidRequestException;
|
||||||
use GeoIP2\Model\Country;
|
use GeoIp2\Exception\OutOfQueriesException;
|
||||||
use GeoIP2\Model\Omni;
|
use GeoIp2\ProviderInterface;
|
||||||
use Guzzle\Http\Client as GuzzleClient;
|
|
||||||
use Guzzle\Common\Exception\RuntimeException;
|
use Guzzle\Common\Exception\RuntimeException;
|
||||||
|
use Guzzle\Http\Client as GuzzleClient;
|
||||||
use Guzzle\Http\Exception\ClientErrorResponseException;
|
use Guzzle\Http\Exception\ClientErrorResponseException;
|
||||||
use Guzzle\Http\Exception\ServerErrorResponseException;
|
use Guzzle\Http\Exception\ServerErrorResponseException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class provides a client API for all the GeoIP Precision web service's
|
* This class provides a client API for all the GeoIP2 web service's
|
||||||
* end points. The end points are Country, City, City/ISP/Org, and Omni. Each
|
* end points. The end points are Country, City, City/ISP/Org, and Omni. Each
|
||||||
* end point returns a different set of data about an IP address, with Country
|
* end point returns a different set of data about an IP address, with Country
|
||||||
* returning the least data and Omni the most.
|
* returning the least data and Omni the most.
|
||||||
@@ -44,41 +44,21 @@ use Guzzle\Http\Exception\ServerErrorResponseException;
|
|||||||
* service.
|
* service.
|
||||||
*
|
*
|
||||||
* If the request fails, the client class throws an exception.
|
* If the request fails, the client class throws an exception.
|
||||||
*
|
|
||||||
* **Exceptions**
|
|
||||||
*
|
|
||||||
* For details on the possible errors returned by the web service itself, see
|
|
||||||
* {@link http://dev.maxmind.com/geoip2/geoip/web-services the GeoIP2 web
|
|
||||||
* service docs}.
|
|
||||||
*
|
|
||||||
* If the web service returns an explicit error document, this is thrown as a
|
|
||||||
* {@link \GeoIP2\Exception\WebServiceException}. If some other sort of
|
|
||||||
* transport error occurs, this is thrown as a {@link
|
|
||||||
* \GeoIP2\Exception\HttpException}. The difference is that the web service
|
|
||||||
* error includes an error message and error code delivered by the web
|
|
||||||
* service. The latter is thrown when some sort of unanticipated error occurs,
|
|
||||||
* such as the web service returning a 500 or an invalid error document.
|
|
||||||
*
|
|
||||||
* If the web service returns any status code besides 200, 4xx, or 5xx, this
|
|
||||||
* also becomes a {@link \GeoIP2\Exception\HttpException}.
|
|
||||||
*
|
|
||||||
* Finally, if the web service returns a 200 but the body is invalid, the
|
|
||||||
* client throws a {@link \GeoIP2\Exception\GenericException}.
|
|
||||||
*/
|
*/
|
||||||
class Client
|
class Client implements ProviderInterface
|
||||||
{
|
{
|
||||||
private $userId;
|
private $userId;
|
||||||
private $licenseKey;
|
private $licenseKey;
|
||||||
private $languages;
|
private $locales;
|
||||||
private $host;
|
private $host;
|
||||||
private $guzzleClient;
|
private $guzzleClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param int $userId Your MaxMind user ID
|
* @param int $userId Your MaxMind user ID
|
||||||
* @param string $licenseKey Your MaxMind license key
|
* @param string $licenseKey Your MaxMind license key
|
||||||
* @param array $languages List of language codes to use in name property
|
* @param array $locales List of locale codes to use in name property
|
||||||
* from most preferred to least preferred.
|
* from most preferred to least preferred.
|
||||||
* @param string $host Optional host parameter
|
* @param string $host Optional host parameter
|
||||||
* @param object $guzzleClient Optional Guzzle client to use (to facilitate
|
* @param object $guzzleClient Optional Guzzle client to use (to facilitate
|
||||||
@@ -87,33 +67,44 @@ class Client
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
$userId,
|
$userId,
|
||||||
$licenseKey,
|
$licenseKey,
|
||||||
$languages = array('en'),
|
$locales = array('en'),
|
||||||
$host = 'geoip.maxmind.com',
|
$host = 'geoip.maxmind.com',
|
||||||
$guzzleClient = null
|
$guzzleClient = null
|
||||||
) {
|
) {
|
||||||
$this->userId = $userId;
|
$this->userId = $userId;
|
||||||
$this->licenseKey = $licenseKey;
|
$this->licenseKey = $licenseKey;
|
||||||
$this->languages = $languages;
|
$this->locales = $locales;
|
||||||
$this->host = $host;
|
$this->host = $host;
|
||||||
// To enable unit testing
|
// To enable unit testing
|
||||||
$this->guzzleClient = $guzzleClient;
|
$this->guzzleClient = $guzzleClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method calls the GeoIP2 Precision City endpoint.
|
* This method calls the GeoIP2 City endpoint.
|
||||||
*
|
*
|
||||||
* @param string $ipAddress IPv4 or IPv6 address as a string. If no
|
* @param string $ipAddress IPv4 or IPv6 address as a string. If no
|
||||||
* address is provided, the address that the web service is called
|
* address is provided, the address that the web service is called
|
||||||
* from will be used.
|
* from will be used.
|
||||||
*
|
*
|
||||||
* @return \GeoIP2\Model\City
|
* @return \GeoIp2\Model\City
|
||||||
*
|
*
|
||||||
* @throws \GeoIP2\Exception\GenericException if there was a generic
|
* @throws \GeoIp2\Exception\AddressNotFoundException if the address you
|
||||||
* error processing your request.
|
* provided is not in our database (e.g., a private address).
|
||||||
* @throws \GeoIP2\Exception\HttpException if there was an HTTP transport
|
* @throws \GeoIp2\Exception\AuthenticationException if there is a problem
|
||||||
* error.
|
* with the user ID or license key that you provided.
|
||||||
* @throws \GeoIP2\Exception\WebServiceException if an error was returned
|
* @throws \GeoIp2\Exception\OutOfQueriesException if your account is out
|
||||||
* by MaxMind's GeoIP2 web service.
|
* 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 city($ipAddress = 'me')
|
public function city($ipAddress = 'me')
|
||||||
{
|
{
|
||||||
@@ -127,14 +118,25 @@ class Client
|
|||||||
* address is provided, the address that the web service is called
|
* address is provided, the address that the web service is called
|
||||||
* from will be used.
|
* from will be used.
|
||||||
*
|
*
|
||||||
* @return \GeoIP2\Model\Country
|
* @return \GeoIp2\Model\Country
|
||||||
*
|
*
|
||||||
* @throws \GeoIP2\Exception\GenericException if there was a generic
|
* @throws \GeoIp2\Exception\AddressNotFoundException if the address you
|
||||||
* error processing your request.
|
* provided is not in our database (e.g., a private address).
|
||||||
* @throws \GeoIP2\Exception\HttpException if there was an HTTP transport
|
* @throws \GeoIp2\Exception\AuthenticationException if there is a problem
|
||||||
* error.
|
* with the user ID or license key that you provided.
|
||||||
* @throws \GeoIP2\Exception\WebServiceException if an error was returned
|
* @throws \GeoIp2\Exception\OutOfQueriesException if your account is out
|
||||||
* by MaxMind's GeoIP2 web service.
|
* 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')
|
public function country($ipAddress = 'me')
|
||||||
{
|
{
|
||||||
@@ -142,20 +144,31 @@ class Client
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method calls the GeoIP2 Precision City/ISP/Org endpoint.
|
* This method calls the GeoIP2 City/ISP/Org endpoint.
|
||||||
*
|
*
|
||||||
* @param string $ipAddress IPv4 or IPv6 address as a string. If no
|
* @param string $ipAddress IPv4 or IPv6 address as a string. If no
|
||||||
* address is provided, the address that the web service is called
|
* address is provided, the address that the web service is called
|
||||||
* from will be used.
|
* from will be used.
|
||||||
*
|
*
|
||||||
* @return \GeoIP2\Model\CityIspOrg
|
* @return \GeoIp2\Model\CityIspOrg
|
||||||
*
|
*
|
||||||
* @throws \GeoIP2\Exception\GenericException if there was a generic
|
* @throws \GeoIp2\Exception\AddressNotFoundException if the address you
|
||||||
* error processing your request.
|
* provided is not in our database (e.g., a private address).
|
||||||
* @throws \GeoIP2\Exception\HttpException if there was an HTTP transport
|
* @throws \GeoIp2\Exception\AuthenticationException if there is a problem
|
||||||
* error.
|
* with the user ID or license key that you provided.
|
||||||
* @throws \GeoIP2\Exception\WebServiceException if an error was returned
|
* @throws \GeoIp2\Exception\OutOfQueriesException if your account is out
|
||||||
* by MaxMind's GeoIP2 web service.
|
* 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 cityIspOrg($ipAddress = 'me')
|
public function cityIspOrg($ipAddress = 'me')
|
||||||
{
|
{
|
||||||
@@ -163,20 +176,31 @@ class Client
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method calls the GeoIP2 Precision Omni endpoint.
|
* This method calls the GeoIP2 Omni endpoint.
|
||||||
*
|
*
|
||||||
* @param string $ipAddress IPv4 or IPv6 address as a string. If no
|
* @param string $ipAddress IPv4 or IPv6 address as a string. If no
|
||||||
* address is provided, the address that the web service is called
|
* address is provided, the address that the web service is called
|
||||||
* from will be used.
|
* from will be used.
|
||||||
*
|
*
|
||||||
* @return \GeoIP2\Model\Omni
|
* @return \GeoIp2\Model\Omni
|
||||||
*
|
*
|
||||||
* @throws \GeoIP2\Exception\GenericException if there was a generic
|
* @throws \GeoIp2\Exception\AddressNotFoundException if the address you
|
||||||
* error processing your request.
|
* provided is not in our database (e.g., a private address).
|
||||||
* @throws \GeoIP2\Exception\HttpException if there was an HTTP transport
|
* @throws \GeoIp2\Exception\AuthenticationException if there is a problem
|
||||||
* error.
|
* with the user ID or license key that you provided.
|
||||||
* @throws \GeoIP2\Exception\WebServiceException if an error was returned
|
* @throws \GeoIp2\Exception\OutOfQueriesException if your account is out
|
||||||
* by MaxMind's GeoIP2 web service.
|
* 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 omni($ipAddress = 'me')
|
public function omni($ipAddress = 'me')
|
||||||
{
|
{
|
||||||
@@ -191,11 +215,8 @@ class Client
|
|||||||
$this->guzzleClient : new GuzzleClient();
|
$this->guzzleClient : new GuzzleClient();
|
||||||
$request = $client->get($uri, array('Accept' => 'application/json'));
|
$request = $client->get($uri, array('Accept' => 'application/json'));
|
||||||
$request->setAuth($this->userId, $this->licenseKey);
|
$request->setAuth($this->userId, $this->licenseKey);
|
||||||
$ua = $request->getHeader('User-Agent');
|
$this->setUserAgent($request);
|
||||||
$ua = "GeoIP2 PHP API ($ua)";
|
|
||||||
$request->setHeader('User-Agent', $ua);
|
|
||||||
|
|
||||||
$response = null;
|
|
||||||
try {
|
try {
|
||||||
$response = $request->send();
|
$response = $request->send();
|
||||||
} catch (ClientErrorResponseException $e) {
|
} catch (ClientErrorResponseException $e) {
|
||||||
@@ -206,8 +227,8 @@ class Client
|
|||||||
|
|
||||||
if ($response && $response->isSuccessful()) {
|
if ($response && $response->isSuccessful()) {
|
||||||
$body = $this->handleSuccess($response, $uri);
|
$body = $this->handleSuccess($response, $uri);
|
||||||
$class = "GeoIP2\\Model\\" . $class;
|
$class = "GeoIp2\\Model\\" . $class;
|
||||||
return new $class($body, $this->languages);
|
return new $class($body, $this->locales);
|
||||||
} else {
|
} else {
|
||||||
$this->handleNon200($response, $uri);
|
$this->handleNon200($response, $uri);
|
||||||
}
|
}
|
||||||
@@ -216,7 +237,7 @@ class Client
|
|||||||
private function handleSuccess($response, $uri)
|
private function handleSuccess($response, $uri)
|
||||||
{
|
{
|
||||||
if ($response->getContentLength() == 0) {
|
if ($response->getContentLength() == 0) {
|
||||||
throw new GenericException(
|
throw new GeoIp2Exception(
|
||||||
"Received a 200 response for $uri but did not " .
|
"Received a 200 response for $uri but did not " .
|
||||||
"receive a HTTP body."
|
"receive a HTTP body."
|
||||||
);
|
);
|
||||||
@@ -225,7 +246,7 @@ class Client
|
|||||||
try {
|
try {
|
||||||
return $response->json();
|
return $response->json();
|
||||||
} catch (RuntimeException $e) {
|
} catch (RuntimeException $e) {
|
||||||
throw new GenericException(
|
throw new GeoIp2Exception(
|
||||||
"Received a 200 response for $uri but could not decode " .
|
"Received a 200 response for $uri but could not decode " .
|
||||||
"the response as JSON: " . $e->getMessage()
|
"the response as JSON: " . $e->getMessage()
|
||||||
);
|
);
|
||||||
@@ -237,14 +258,12 @@ class Client
|
|||||||
{
|
{
|
||||||
$status = $response->getStatusCode();
|
$status = $response->getStatusCode();
|
||||||
|
|
||||||
$body = array();
|
|
||||||
|
|
||||||
if ($response->getContentLength() > 0) {
|
if ($response->getContentLength() > 0) {
|
||||||
if (strstr($response->getContentType(), 'json')) {
|
if (strstr($response->getContentType(), 'json')) {
|
||||||
try {
|
try {
|
||||||
$body = $response->json();
|
$body = $response->json();
|
||||||
if (!isset($body['code']) || !isset($body['error'])) {
|
if (!isset($body['code']) || !isset($body['error'])) {
|
||||||
throw new GenericException(
|
throw new GeoIp2Exception(
|
||||||
'Response contains JSON but it does not specify ' .
|
'Response contains JSON but it does not specify ' .
|
||||||
'code or error keys: ' . $response->getBody()
|
'code or error keys: ' . $response->getBody()
|
||||||
);
|
);
|
||||||
@@ -273,8 +292,7 @@ class Client
|
|||||||
$uri
|
$uri
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
$this->handleWebServiceError(
|
||||||
throw new WebServiceException(
|
|
||||||
$body['error'],
|
$body['error'],
|
||||||
$body['code'],
|
$body['code'],
|
||||||
$status,
|
$status,
|
||||||
@@ -282,6 +300,28 @@ class Client
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function handleWebServiceError($message, $code, $status, $uri)
|
||||||
|
{
|
||||||
|
switch ($code) {
|
||||||
|
case 'IP_ADDRESS_NOT_FOUND':
|
||||||
|
case 'IP_ADDRESS_RESERVED':
|
||||||
|
throw new AddressNotFoundException($message);
|
||||||
|
case 'AUTHORIZATION_INVALID':
|
||||||
|
case 'LICENSE_KEY_REQUIRED':
|
||||||
|
case 'USER_ID_REQUIRED':
|
||||||
|
throw new AuthenticationException($message);
|
||||||
|
case 'OUT_OF_QUERIES':
|
||||||
|
throw new OutOfQueriesException($message);
|
||||||
|
default:
|
||||||
|
throw new InvalidRequestException(
|
||||||
|
$message,
|
||||||
|
$code,
|
||||||
|
$status,
|
||||||
|
$uri
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private function handle5xx($response, $uri)
|
private function handle5xx($response, $uri)
|
||||||
{
|
{
|
||||||
$status = $response->getStatusCode();
|
$status = $response->getStatusCode();
|
||||||
@@ -305,6 +345,13 @@ class Client
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function setUserAgent($request)
|
||||||
|
{
|
||||||
|
$userAgent = $request->getHeader('User-Agent');
|
||||||
|
$userAgent = "GeoIP2 PHP API ($userAgent)";
|
||||||
|
$request->setHeader('User-Agent', $userAgent);
|
||||||
|
}
|
||||||
|
|
||||||
private function baseUri()
|
private function baseUri()
|
||||||
{
|
{
|
||||||
return 'https://' . $this->host . '/geoip/v2.0';
|
return 'https://' . $this->host . '/geoip/v2.0';
|
||||||
80
tests/GeoIp2/Test/Database/ReaderTest.php
Normal file
80
tests/GeoIp2/Test/Database/ReaderTest.php
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace GeoIp2\Test\WebService;
|
||||||
|
|
||||||
|
use GeoIp2\Database\Reader;
|
||||||
|
|
||||||
|
class ReaderTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
public function testDefaultLocale()
|
||||||
|
{
|
||||||
|
$reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
|
||||||
|
// Needed for PHP 5.3
|
||||||
|
$that = $this;
|
||||||
|
$this->checkAllMethods(
|
||||||
|
function ($method) use (&$that, &$reader) {
|
||||||
|
$record = $reader->$method('81.2.69.160');
|
||||||
|
$that->assertEquals('United Kingdom', $record->country->name);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
$reader->close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testLocaleList()
|
||||||
|
{
|
||||||
|
$reader = new Reader(
|
||||||
|
'maxmind-db/test-data/GeoIP2-City-Test.mmdb',
|
||||||
|
array('xx', 'ru', 'pt-BR', 'es', 'en')
|
||||||
|
);
|
||||||
|
$that = $this;
|
||||||
|
$this->checkAllMethods(
|
||||||
|
function ($method) use (&$that, &$reader) {
|
||||||
|
$record = $reader->$method('81.2.69.160');
|
||||||
|
$that->assertEquals('Великобритания', $record->country->name);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
$reader->close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testHasIpAddress()
|
||||||
|
{
|
||||||
|
$reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
|
||||||
|
$that = $this;
|
||||||
|
$this->checkAllMethods(
|
||||||
|
function ($method) use (&$that, &$reader) {
|
||||||
|
$record = $reader->$method('81.2.69.160');
|
||||||
|
$that->assertEquals('81.2.69.160', $record->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-Test.mmdb');
|
||||||
|
$reader->city('10.10.10.10');
|
||||||
|
$reader->close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException InvalidArgumentException
|
||||||
|
* @expectedExceptionMessage is not a valid IP address
|
||||||
|
*/
|
||||||
|
public function testInvalidAddress()
|
||||||
|
{
|
||||||
|
$reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
|
||||||
|
$reader->city('invalid');
|
||||||
|
$reader->close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function checkAllMethods($testCb)
|
||||||
|
{
|
||||||
|
foreach (array('city', 'cityIspOrg', 'country', 'omni') as $method) {
|
||||||
|
$testCb($method);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,27 +1,27 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace GeoIP2\Test\Model;
|
namespace GeoIp2\Test\Model;
|
||||||
|
|
||||||
use GeoIP2\Model\Country;
|
use GeoIp2\Model\Country;
|
||||||
|
|
||||||
class CountryTest extends \PHPUnit_Framework_TestCase
|
class CountryTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
private $raw = array(
|
private $raw = array(
|
||||||
'continent' => array(
|
'continent' => array(
|
||||||
'continent_code' => 'NA',
|
'code' => 'NA',
|
||||||
'geoname_id' => 42,
|
'geoname_id' => 42,
|
||||||
'names' => array( 'en' => 'North America' ),
|
'names' => array('en' => 'North America'),
|
||||||
),
|
),
|
||||||
'country' => array(
|
'country' => array(
|
||||||
'geoname_id' => 1,
|
'geoname_id' => 1,
|
||||||
'iso_code' => 'US',
|
'iso_code' => 'US',
|
||||||
'names' => array( 'en' => 'United States of America' ),
|
'names' => array('en' => 'United States of America'),
|
||||||
),
|
),
|
||||||
'registered_country' => array(
|
'registered_country' => array(
|
||||||
'geoname_id' => 2,
|
'geoname_id' => 2,
|
||||||
'iso_code' => 'CA',
|
'iso_code' => 'CA',
|
||||||
'names' => array( 'en' => 'Canada' ),
|
'names' => array('en' => 'Canada'),
|
||||||
),
|
),
|
||||||
'traits' => array(
|
'traits' => array(
|
||||||
'ip_address' => '1.2.3.4',
|
'ip_address' => '1.2.3.4',
|
||||||
@@ -30,36 +30,36 @@ class CountryTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
private $model;
|
private $model;
|
||||||
|
|
||||||
public function setUp ()
|
public function setUp()
|
||||||
{
|
{
|
||||||
$this->model = new Country($this->raw, array('en'));
|
$this->model = new Country($this->raw, array('en'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testObjects ()
|
public function testObjects()
|
||||||
{
|
{
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Model\Country',
|
'GeoIp2\Model\Country',
|
||||||
$this->model,
|
$this->model,
|
||||||
'minimal GeoIP2::Model::Country object'
|
'minimal GeoIp2::Model::Country object'
|
||||||
);
|
);
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Record\Continent',
|
'GeoIp2\Record\Continent',
|
||||||
$this->model->continent
|
$this->model->continent
|
||||||
);
|
);
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Record\Country',
|
'GeoIp2\Record\Country',
|
||||||
$this->model->country
|
$this->model->country
|
||||||
);
|
);
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Record\Country',
|
'GeoIp2\Record\Country',
|
||||||
$this->model->registeredCountry
|
$this->model->registeredCountry
|
||||||
);
|
);
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Record\RepresentedCountry',
|
'GeoIp2\Record\RepresentedCountry',
|
||||||
$this->model->representedCountry
|
$this->model->representedCountry
|
||||||
);
|
);
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Record\Traits',
|
'GeoIp2\Record\Traits',
|
||||||
$this->model->traits
|
$this->model->traits
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -75,12 +75,12 @@ class CountryTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'NA',
|
'NA',
|
||||||
$this->model->continent->continentCode,
|
$this->model->continent->code,
|
||||||
'continent continent_code is NA'
|
'continent code is NA'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
array( 'en' => 'North America' ),
|
array('en' => 'North America'),
|
||||||
$this->model->continent->names,
|
$this->model->continent->names,
|
||||||
'continent names'
|
'continent names'
|
||||||
);
|
);
|
||||||
@@ -104,7 +104,7 @@ class CountryTest extends \PHPUnit_Framework_TestCase
|
|||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
array( 'en' => 'United States of America' ),
|
array('en' => 'United States of America'),
|
||||||
$this->model->country->names,
|
$this->model->country->names,
|
||||||
'country name'
|
'country name'
|
||||||
);
|
);
|
||||||
@@ -134,7 +134,7 @@ class CountryTest extends \PHPUnit_Framework_TestCase
|
|||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
array( 'en' => 'Canada' ),
|
array('en' => 'Canada'),
|
||||||
$this->model->registeredCountry->names,
|
$this->model->registeredCountry->names,
|
||||||
'registered_country names'
|
'registered_country names'
|
||||||
);
|
);
|
||||||
@@ -145,7 +145,7 @@ class CountryTest extends \PHPUnit_Framework_TestCase
|
|||||||
'registered_country name is Canada'
|
'registered_country name is Canada'
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach (array( 'isAnonymousProxy', 'isSatelliteProvider' ) as $meth) {
|
foreach (array('isAnonymousProxy', 'isSatelliteProvider') as $meth) {
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
0,
|
0,
|
||||||
$this->model->traits->$meth,
|
$this->model->traits->$meth,
|
||||||
@@ -1,37 +1,37 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace GeoIP2\Test\Model;
|
namespace GeoIp2\Test\Model;
|
||||||
|
|
||||||
use GeoIP2\Model\Country;
|
use GeoIp2\Model\Country;
|
||||||
|
|
||||||
class NameTest extends \PHPUnit_Framework_TestCase
|
class NameTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
public $raw = array(
|
public $raw = array(
|
||||||
'continent' => array(
|
'continent' => array(
|
||||||
'continent_code' => 'NA',
|
'code' => 'NA',
|
||||||
'geoname_id' => 42,
|
'geoname_id' => 42,
|
||||||
'names' => array(
|
'names' => array(
|
||||||
'en' => 'North America',
|
'en' => 'North America',
|
||||||
'zh-CN' => '北美洲',
|
'zh-CN' => '北美洲',
|
||||||
),
|
|
||||||
),
|
),
|
||||||
'country' => array(
|
),
|
||||||
'geoname_id' => 1,
|
'country' => array(
|
||||||
'iso_code' => 'US',
|
'geoname_id' => 1,
|
||||||
'names' => array(
|
'iso_code' => 'US',
|
||||||
'en' => 'United States of America',
|
'names' => array(
|
||||||
'ru' => 'объединяет государства',
|
'en' => 'United States of America',
|
||||||
'zh-CN' => '美国',
|
'ru' => 'объединяет государства',
|
||||||
),
|
'zh-CN' => '美国',
|
||||||
),
|
),
|
||||||
'traits' => array(
|
),
|
||||||
'ip_address' => '1.2.3.4',
|
'traits' => array(
|
||||||
),
|
'ip_address' => '1.2.3.4',
|
||||||
);
|
),
|
||||||
|
);
|
||||||
|
|
||||||
public function testFallback()
|
public function testFallback()
|
||||||
{
|
{
|
||||||
$model = new Country($this->raw, array( 'ru', 'zh-CN', 'en' ));
|
$model = new Country($this->raw, array('ru', 'zh-CN', 'en'));
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'北美洲',
|
'北美洲',
|
||||||
@@ -46,7 +46,8 @@ class NameTest extends \PHPUnit_Framework_TestCase
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testTwoFallbacks() {
|
public function testTwoFallbacks()
|
||||||
|
{
|
||||||
$model = new Country($this->raw, array('ru', 'ja'));
|
$model = new Country($this->raw, array('ru', 'ja'));
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace GeoIP2\Test\Model;
|
namespace GeoIp2\Test\Model;
|
||||||
|
|
||||||
use GeoIP2\Model\Omni;
|
use GeoIp2\Model\Omni;
|
||||||
|
|
||||||
class OmniTest extends \PHPUnit_Framework_TestCase
|
class OmniTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
@@ -14,119 +14,128 @@ class OmniTest extends \PHPUnit_Framework_TestCase
|
|||||||
'city' => array(
|
'city' => array(
|
||||||
'confidence' => 76,
|
'confidence' => 76,
|
||||||
'geoname_id' => 9876,
|
'geoname_id' => 9876,
|
||||||
'names' => array( 'en' => 'Minneapolis' ),
|
'names' => array('en' => 'Minneapolis'),
|
||||||
),
|
),
|
||||||
'continent' => array(
|
'continent' => array(
|
||||||
'continent_code' => 'NA',
|
'code' => 'NA',
|
||||||
'geoname_id' => 42,
|
'geoname_id' => 42,
|
||||||
'names' => array( 'en' => 'North America' ),
|
'names' => array('en' => 'North America'),
|
||||||
),
|
),
|
||||||
'country' => array(
|
'country' => array(
|
||||||
'confidence' => 99,
|
'confidence' => 99,
|
||||||
'geoname_id' => 1,
|
'geoname_id' => 1,
|
||||||
'iso_code' => 'US',
|
'iso_code' => 'US',
|
||||||
'names' => array( 'en' => 'United States of America' ),
|
'names' => array('en' => 'United States of America'),
|
||||||
),
|
),
|
||||||
'location' => array(
|
'location' => array(
|
||||||
'accuracy_radius' => 1500,
|
'accuracy_radius' => 1500,
|
||||||
'latitude' => 44.98,
|
'latitude' => 44.98,
|
||||||
'longitude' => 93.2636,
|
'longitude' => 93.2636,
|
||||||
'metro_code' => 765,
|
'metro_code' => 765,
|
||||||
'postal_code' => '55401',
|
'postal_code' => '55401',
|
||||||
'postal_confidence' => 33,
|
'postal_confidence' => 33,
|
||||||
'time_zone' => 'America/Chicago',
|
'time_zone' => 'America/Chicago',
|
||||||
|
),
|
||||||
|
'maxmind' => array(
|
||||||
|
'queries_remaining' => 22,
|
||||||
),
|
),
|
||||||
'registered_country' => array(
|
'registered_country' => array(
|
||||||
'geoname_id' => 2,
|
'geoname_id' => 2,
|
||||||
'iso_code' => 'CA',
|
'iso_code' => 'CA',
|
||||||
'names' => array( 'en' => 'Canada' ),
|
'names' => array('en' => 'Canada'),
|
||||||
),
|
),
|
||||||
'represented_country' => array(
|
'represented_country' => array(
|
||||||
'geoname_id' => 3,
|
'geoname_id' => 3,
|
||||||
'iso_code' => 'GB',
|
'iso_code' => 'GB',
|
||||||
'names' => array( 'en' => 'United Kingdom' ),
|
'names' => array('en' => 'United Kingdom'),
|
||||||
),
|
),
|
||||||
'subdivisions' => array(
|
'subdivisions' => array(
|
||||||
array(
|
array(
|
||||||
'confidence' => 88,
|
'confidence' => 88,
|
||||||
'geoname_id' => 574635,
|
'geoname_id' => 574635,
|
||||||
'iso_code' => 'MN',
|
'iso_code' => 'MN',
|
||||||
'names' => array( 'en' => 'Minnesota' ),
|
'names' => array('en' => 'Minnesota'),
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
'traits' => array(
|
'traits' => array(
|
||||||
'autonomous_system_number' => 1234,
|
'autonomous_system_number' => 1234,
|
||||||
'autonomous_system_organization' => 'AS Organization',
|
'autonomous_system_organization' => 'AS Organization',
|
||||||
'domain' => 'example.com',
|
'domain' => 'example.com',
|
||||||
'ip_address' => '1.2.3.4',
|
'ip_address' => '1.2.3.4',
|
||||||
'is_satellite_provider' => 1,
|
'is_satellite_provider' => 1,
|
||||||
'isp' => 'Comcast',
|
'isp' => 'Comcast',
|
||||||
'organization' => 'Blorg',
|
'organization' => 'Blorg',
|
||||||
'user_type' => 'college',
|
'user_type' => 'college',
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
$model = new Omni($raw, array('en'));
|
$model = new Omni($raw, array('en'));
|
||||||
|
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Model\Omni',
|
'GeoIp2\Model\Omni',
|
||||||
$model,
|
$model,
|
||||||
'GeoIP2\Model\Omni object'
|
'GeoIp2\Model\Omni object'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Record\City',
|
'GeoIp2\Record\City',
|
||||||
$model->city,
|
$model->city,
|
||||||
'$model->city'
|
'$model->city'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Record\Continent',
|
'GeoIp2\Record\Continent',
|
||||||
$model->continent,
|
$model->continent,
|
||||||
'$model->continent'
|
'$model->continent'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Record\Country',
|
'GeoIp2\Record\Country',
|
||||||
$model->country,
|
$model->country,
|
||||||
'$model->country'
|
'$model->country'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Record\Location',
|
'GeoIp2\Record\Location',
|
||||||
$model->location,
|
$model->location,
|
||||||
'$model->location'
|
'$model->location'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Record\Country',
|
'GeoIp2\Record\Country',
|
||||||
$model->registeredCountry,
|
$model->registeredCountry,
|
||||||
'$model->registeredCountry'
|
'$model->registeredCountry'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Record\RepresentedCountry',
|
'GeoIp2\Record\RepresentedCountry',
|
||||||
$model->representedCountry,
|
$model->representedCountry,
|
||||||
'$model->representedCountry'
|
'$model->representedCountry'
|
||||||
);
|
);
|
||||||
|
|
||||||
$subdivisions = $model->subdivisions;
|
$subdivisions = $model->subdivisions;
|
||||||
foreach ($subdivisions as $subdiv) {
|
foreach ($subdivisions as $subdiv) {
|
||||||
$this->assertInstanceOf('GeoIP2\Record\Subdivision', $subdiv);
|
$this->assertInstanceOf('GeoIp2\Record\Subdivision', $subdiv);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Record\Subdivision',
|
'GeoIp2\Record\Subdivision',
|
||||||
$model->mostSpecificSubdivision,
|
$model->mostSpecificSubdivision,
|
||||||
'$model->mostSpecificSubdivision'
|
'$model->mostSpecificSubdivision'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Record\Traits',
|
'GeoIp2\Record\Traits',
|
||||||
$model->traits,
|
$model->traits,
|
||||||
'$model->traits'
|
'$model->traits'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
22,
|
||||||
|
$model->maxmind->queriesRemaining,
|
||||||
|
'queriesRemaining is correct'
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$raw,
|
$raw,
|
||||||
$model->raw,
|
$model->raw,
|
||||||
@@ -136,52 +145,52 @@ class OmniTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testEmptyObjects()
|
public function testEmptyObjects()
|
||||||
{
|
{
|
||||||
$raw = array( 'traits' => array( 'ip_address' => '5.6.7.8' ) );
|
$raw = array('traits' => array('ip_address' => '5.6.7.8'));
|
||||||
|
|
||||||
$model = new Omni($raw, array('en'));
|
$model = new Omni($raw, array('en'));
|
||||||
|
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Model\Omni',
|
'GeoIp2\Model\Omni',
|
||||||
$model,
|
$model,
|
||||||
'GeoIP2\Model\Omni object with no data except traits.ipAddress'
|
'GeoIp2\Model\Omni object with no data except traits.ipAddress'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Record\City',
|
'GeoIp2\Record\City',
|
||||||
$model->city,
|
$model->city,
|
||||||
'$model->city'
|
'$model->city'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Record\Continent',
|
'GeoIp2\Record\Continent',
|
||||||
$model->continent,
|
$model->continent,
|
||||||
'$model->continent'
|
'$model->continent'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Record\Country',
|
'GeoIp2\Record\Country',
|
||||||
$model->country,
|
$model->country,
|
||||||
'$model->country'
|
'$model->country'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Record\Location',
|
'GeoIp2\Record\Location',
|
||||||
$model->location,
|
$model->location,
|
||||||
'$model->location'
|
'$model->location'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Record\Country',
|
'GeoIp2\Record\Country',
|
||||||
$model->registeredCountry,
|
$model->registeredCountry,
|
||||||
'$model->registeredCountry'
|
'$model->registeredCountry'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Record\RepresentedCountry',
|
'GeoIp2\Record\RepresentedCountry',
|
||||||
$model->representedCountry,
|
$model->representedCountry,
|
||||||
'$model->representedCountry'
|
'$model->representedCountry'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertCount(
|
$this->assertCount(
|
||||||
0,
|
0,
|
||||||
$model->subdivisions,
|
$model->subdivisions,
|
||||||
@@ -189,13 +198,13 @@ class OmniTest extends \PHPUnit_Framework_TestCase
|
|||||||
);
|
);
|
||||||
|
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Record\Subdivision',
|
'GeoIp2\Record\Subdivision',
|
||||||
$model->mostSpecificSubdivision,
|
$model->mostSpecificSubdivision,
|
||||||
'$model->mostSpecificSubdivision'
|
'$model->mostSpecificSubdivision'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Record\Traits',
|
'GeoIp2\Record\Traits',
|
||||||
$model->traits,
|
$model->traits,
|
||||||
'$model->traits'
|
'$model->traits'
|
||||||
);
|
);
|
||||||
@@ -211,21 +220,21 @@ class OmniTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testUnknown()
|
public function testUnknown()
|
||||||
{
|
{
|
||||||
$raw = array(
|
$raw = array(
|
||||||
'new_top_level' => array( 'foo' => 42 ),
|
'new_top_level' => array('foo' => 42),
|
||||||
'city' => array(
|
'city' => array(
|
||||||
'confidence' => 76,
|
'confidence' => 76,
|
||||||
'geoname_id_id' => 9876,
|
'geoname_id_id' => 9876,
|
||||||
'names' => array( 'en' => 'Minneapolis' ),
|
'names' => array('en' => 'Minneapolis'),
|
||||||
'population' => 50,
|
'population' => 50,
|
||||||
),
|
),
|
||||||
'traits' => array( 'ip_address' => '5.6.7.8' )
|
'traits' => array('ip_address' => '5.6.7.8')
|
||||||
);
|
);
|
||||||
|
|
||||||
// checking whether there are exceptions with unknown keys
|
// checking whether there are exceptions with unknown keys
|
||||||
$model = new Omni($raw, array('en'));
|
$model = new Omni($raw, array('en'));
|
||||||
|
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Model\Omni',
|
'GeoIp2\Model\Omni',
|
||||||
$model,
|
$model,
|
||||||
'no exception when Omni model gets raw data with unknown keys'
|
'no exception when Omni model gets raw data with unknown keys'
|
||||||
);
|
);
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace GeoIP2\Test\WebService;
|
namespace GeoIp2\Test\WebService;
|
||||||
|
|
||||||
use GeoIP2\WebService\Client;
|
use GeoIp2\WebService\Client;
|
||||||
use Guzzle\Http\Client as GuzzleClient;
|
use Guzzle\Http\Client as GuzzleClient;
|
||||||
use Guzzle\Http\Message\Response;
|
use Guzzle\Http\Message\Response;
|
||||||
use Guzzle\Plugin\Mock\MockPlugin;
|
use Guzzle\Plugin\Mock\MockPlugin;
|
||||||
@@ -13,15 +13,16 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
private $country
|
private $country
|
||||||
= array(
|
= array(
|
||||||
'continent' => array(
|
'continent' => array(
|
||||||
'continent_code' => 'NA',
|
'code' => 'NA',
|
||||||
'geoname_id' => 42,
|
'geoname_id' => 42,
|
||||||
'names' => array( 'en' => 'North America' ),
|
'names' => array('en' => 'North America'),
|
||||||
),
|
),
|
||||||
'country' => array(
|
'country' => array(
|
||||||
'geoname_id' => 1,
|
'geoname_id' => 1,
|
||||||
'iso_code' => 'US',
|
'iso_code' => 'US',
|
||||||
'names' => array( 'en' => 'United States of America' ),
|
'names' => array('en' => 'United States of America'),
|
||||||
),
|
),
|
||||||
|
'maxmind' => array('queries_remaining' => 11),
|
||||||
'traits' => array(
|
'traits' => array(
|
||||||
'ip_address' => '1.2.3.4',
|
'ip_address' => '1.2.3.4',
|
||||||
),
|
),
|
||||||
@@ -43,11 +44,11 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
),
|
),
|
||||||
'1.2.3.5' => $this->response('country', 200),
|
'1.2.3.5' => $this->response('country', 200),
|
||||||
'2.2.3.5' => $this->response('country', 200, 'bad body'),
|
'2.2.3.5' => $this->response('country', 200, 'bad body'),
|
||||||
'1.2.3.6'=> $this->response(
|
'1.2.3.6' => $this->response(
|
||||||
'error',
|
'error',
|
||||||
400,
|
400,
|
||||||
array(
|
array(
|
||||||
'code' => 'IP_ADDRESS_INVALID',
|
'code' => 'IP_ADDRESS_INVALID',
|
||||||
'error' => 'The value "1.2.3" is not a valid ip address'
|
'error' => 'The value "1.2.3" is not a valid ip address'
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
@@ -58,7 +59,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
'1.2.3.8' => $this->response(
|
'1.2.3.8' => $this->response(
|
||||||
'error',
|
'error',
|
||||||
400,
|
400,
|
||||||
array( 'weird' => 42 )
|
array('weird' => 42)
|
||||||
),
|
),
|
||||||
'1.2.3.9' => $this->response(
|
'1.2.3.9' => $this->response(
|
||||||
'error',
|
'error',
|
||||||
@@ -81,6 +82,54 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
null,
|
null,
|
||||||
'text/plain'
|
'text/plain'
|
||||||
),
|
),
|
||||||
|
'1.2.3.13' => $this->response(
|
||||||
|
'error',
|
||||||
|
404,
|
||||||
|
array(
|
||||||
|
'code' => 'IP_ADDRESS_NOT_FOUND',
|
||||||
|
'error' => 'The address "1.2.3.13" is not in our database.'
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'1.2.3.14' => $this->response(
|
||||||
|
'error',
|
||||||
|
400,
|
||||||
|
array(
|
||||||
|
'code' => 'IP_ADDRESS_RESERVED',
|
||||||
|
'error' => 'The address "1.2.3.14" is a private address.'
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'1.2.3.15' => $this->response(
|
||||||
|
'error',
|
||||||
|
401,
|
||||||
|
array(
|
||||||
|
'code' => 'AUTHORIZATION_INVALID',
|
||||||
|
'error' => 'A user ID and license key are required to use this service'
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'1.2.3.16' => $this->response(
|
||||||
|
'error',
|
||||||
|
401,
|
||||||
|
array(
|
||||||
|
'code' => 'LICENSE_KEY_REQUIRED',
|
||||||
|
'error' => 'A license key is required to use this service'
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'1.2.3.17' => $this->response(
|
||||||
|
'error',
|
||||||
|
401,
|
||||||
|
array(
|
||||||
|
'code' => 'USER_ID_REQUIRED',
|
||||||
|
'error' => 'A user ID is required to use this service'
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'1.2.3.18' => $this->response(
|
||||||
|
'error',
|
||||||
|
402,
|
||||||
|
array(
|
||||||
|
'code' => 'OUT_OF_QUERIES',
|
||||||
|
'error' => 'The license key you have provided is out of queries.'
|
||||||
|
)
|
||||||
|
),
|
||||||
);
|
);
|
||||||
return $responses[$ip];
|
return $responses[$ip];
|
||||||
}
|
}
|
||||||
@@ -90,7 +139,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
$country = $this->client($this->getResponse('1.2.3.4'))
|
$country = $this->client($this->getResponse('1.2.3.4'))
|
||||||
->country('1.2.3.4');
|
->country('1.2.3.4');
|
||||||
|
|
||||||
$this->assertInstanceOf('GeoIP2\Model\Country', $country);
|
$this->assertInstanceOf('GeoIp2\Model\Country', $country);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
42,
|
42,
|
||||||
@@ -100,8 +149,8 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'NA',
|
'NA',
|
||||||
$country->continent->continentCode,
|
$country->continent->code,
|
||||||
'continent continent_code is NA'
|
'continent code is NA'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
@@ -129,7 +178,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
array( 'en' => 'United States of America' ),
|
array('en' => 'United States of America'),
|
||||||
$country->country->names,
|
$country->country->names,
|
||||||
'country names'
|
'country names'
|
||||||
);
|
);
|
||||||
@@ -139,6 +188,36 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
$country->country->name,
|
$country->country->name,
|
||||||
'country name is United States of America'
|
'country name is United States of America'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
11,
|
||||||
|
$country->maxmind->queriesRemaining,
|
||||||
|
'queriesRemaining is correct'
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function testOmni()
|
||||||
|
{
|
||||||
|
$omni = $this->client($this->getResponse('1.2.3.4'))
|
||||||
|
->omni('1.2.3.4');
|
||||||
|
|
||||||
|
$this->assertInstanceOf('GeoIp2\Model\Omni', $omni);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
42,
|
||||||
|
$omni->continent->geonameId,
|
||||||
|
'continent geoname_id is 42'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCity()
|
||||||
|
{
|
||||||
|
$city = $this->client($this->getResponse('1.2.3.4'))
|
||||||
|
->city('1.2.3.4');
|
||||||
|
|
||||||
|
$this->assertInstanceOf('GeoIp2\Model\City', $city);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testMe()
|
public function testMe()
|
||||||
@@ -146,14 +225,14 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
$client = $this->client($this->getResponse('me'));
|
$client = $this->client($this->getResponse('me'));
|
||||||
|
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIP2\Model\CityIspOrg',
|
'GeoIp2\Model\CityIspOrg',
|
||||||
$client->cityIspOrg('me'),
|
$client->cityIspOrg('me'),
|
||||||
'can set ip parameter to me'
|
'can set ip parameter to me'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException GeoIP2\Exception\GenericException
|
* @expectedException GeoIp2\Exception\GeoIp2Exception
|
||||||
* @expectedExceptionMessage Received a 200 response for https://geoip.maxmind.com/geoip/v2.0/country/1.2.3.5 but did not receive a HTTP body.
|
* @expectedExceptionMessage Received a 200 response for https://geoip.maxmind.com/geoip/v2.0/country/1.2.3.5 but did not receive a HTTP body.
|
||||||
*/
|
*/
|
||||||
public function testNoBodyException()
|
public function testNoBodyException()
|
||||||
@@ -164,7 +243,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException GeoIP2\Exception\GenericException
|
* @expectedException GeoIp2\Exception\GeoIp2Exception
|
||||||
* @expectedExceptionMessage Received a 200 response for https://geoip.maxmind.com/geoip/v2.0/country/2.2.3.5 but could not decode the response as JSON:
|
* @expectedExceptionMessage Received a 200 response for https://geoip.maxmind.com/geoip/v2.0/country/2.2.3.5 but could not decode the response as JSON:
|
||||||
*/
|
*/
|
||||||
public function testBadBodyException()
|
public function testBadBodyException()
|
||||||
@@ -176,7 +255,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException GeoIP2\Exception\WebServiceException
|
* @expectedException GeoIp2\Exception\InvalidRequestException
|
||||||
* @expectedExceptionCode 400
|
* @expectedExceptionCode 400
|
||||||
* @expectedExceptionMessage The value "1.2.3" is not a valid ip address
|
* @expectedExceptionMessage The value "1.2.3" is not a valid ip address
|
||||||
*/
|
*/
|
||||||
@@ -185,11 +264,10 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
$client = $this->client($this->getResponse('1.2.3.6'));
|
$client = $this->client($this->getResponse('1.2.3.6'));
|
||||||
|
|
||||||
$client->country('1.2.3.6');
|
$client->country('1.2.3.6');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException GeoIP2\Exception\HttpException
|
* @expectedException GeoIp2\Exception\HttpException
|
||||||
* @expectedExceptionCode 400
|
* @expectedExceptionCode 400
|
||||||
* @expectedExceptionMessage with no body
|
* @expectedExceptionMessage with no body
|
||||||
*/
|
*/
|
||||||
@@ -201,7 +279,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException GeoIP2\Exception\GenericException
|
* @expectedException GeoIp2\Exception\GeoIp2Exception
|
||||||
* @expectedExceptionMessage Response contains JSON but it does not specify code or error keys
|
* @expectedExceptionMessage Response contains JSON but it does not specify code or error keys
|
||||||
*/
|
*/
|
||||||
public function testWeirdErrorBodyIPException()
|
public function testWeirdErrorBodyIPException()
|
||||||
@@ -213,7 +291,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException GeoIP2\Exception\HttpException
|
* @expectedException GeoIp2\Exception\HttpException
|
||||||
* @expectedExceptionCode 400
|
* @expectedExceptionCode 400
|
||||||
* @expectedExceptionMessage did not include the expected JSON body
|
* @expectedExceptionMessage did not include the expected JSON body
|
||||||
*/
|
*/
|
||||||
@@ -226,7 +304,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException GeoIP2\Exception\HttpException
|
* @expectedException GeoIp2\Exception\HttpException
|
||||||
* @expectedExceptionCode 500
|
* @expectedExceptionCode 500
|
||||||
* @expectedExceptionMessage Received a server error (500)
|
* @expectedExceptionMessage Received a server error (500)
|
||||||
*/
|
*/
|
||||||
@@ -239,7 +317,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException GeoIP2\Exception\HttpException
|
* @expectedException GeoIp2\Exception\HttpException
|
||||||
* @expectedExceptionCode 300
|
* @expectedExceptionCode 300
|
||||||
* @expectedExceptionMessage Received a very surprising HTTP status (300)
|
* @expectedExceptionMessage Received a very surprising HTTP status (300)
|
||||||
*/
|
*/
|
||||||
@@ -252,7 +330,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException GeoIP2\Exception\HttpException
|
* @expectedException GeoIp2\Exception\HttpException
|
||||||
* @expectedExceptionCode 406
|
* @expectedExceptionCode 406
|
||||||
* @expectedExceptionMessage Received a 406 error for https://geoip.maxmind.com/geoip/v2.0/country/1.2.3.12 with the following body: Cannot satisfy your Accept-Charset requirements
|
* @expectedExceptionMessage Received a 406 error for https://geoip.maxmind.com/geoip/v2.0/country/1.2.3.12 with the following body: Cannot satisfy your Accept-Charset requirements
|
||||||
*/
|
*/
|
||||||
@@ -262,6 +340,72 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
$client->country('1.2.3.12');
|
$client->country('1.2.3.12');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException GeoIp2\Exception\AddressNotFoundException
|
||||||
|
* @expectedExceptionMessage The address "1.2.3.13" is not in our database.
|
||||||
|
*/
|
||||||
|
public function testAddressNotFoundException()
|
||||||
|
{
|
||||||
|
$client = $this->client($this->getResponse('1.2.3.13'));
|
||||||
|
|
||||||
|
$client->country('1.2.3.13');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException GeoIp2\Exception\AddressNotFoundException
|
||||||
|
* @expectedExceptionMessage The address "1.2.3.14" is a private address.
|
||||||
|
*/
|
||||||
|
public function testAddressReservedException()
|
||||||
|
{
|
||||||
|
$client = $this->client($this->getResponse('1.2.3.14'));
|
||||||
|
|
||||||
|
$client->country('1.2.3.14');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException GeoIp2\Exception\AuthenticationException
|
||||||
|
* @expectedExceptionMessage A user ID and license key are required to use this service
|
||||||
|
*/
|
||||||
|
public function testAuthorizationException()
|
||||||
|
{
|
||||||
|
$client = $this->client($this->getResponse('1.2.3.15'));
|
||||||
|
|
||||||
|
$client->country('1.2.3.15');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException GeoIp2\Exception\AuthenticationException
|
||||||
|
* @expectedExceptionMessage A license key is required to use this service
|
||||||
|
*/
|
||||||
|
public function testMissingLicenseKeyException()
|
||||||
|
{
|
||||||
|
$client = $this->client($this->getResponse('1.2.3.16'));
|
||||||
|
|
||||||
|
$client->country('1.2.3.16');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException GeoIp2\Exception\AuthenticationException
|
||||||
|
* @expectedExceptionMessage A user ID is required to use this service
|
||||||
|
*/
|
||||||
|
public function testMissingUserIdException()
|
||||||
|
{
|
||||||
|
$client = $this->client($this->getResponse('1.2.3.17'));
|
||||||
|
|
||||||
|
$client->country('1.2.3.17');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException GeoIp2\Exception\OutOfQueriesException
|
||||||
|
* @expectedExceptionMessage The license key you have provided is out of queries.
|
||||||
|
*/
|
||||||
|
public function testOutOfQueriesException()
|
||||||
|
{
|
||||||
|
$client = $this->client($this->getResponse('1.2.3.18'));
|
||||||
|
|
||||||
|
$client->country('1.2.3.18');
|
||||||
|
}
|
||||||
|
|
||||||
public function testParams()
|
public function testParams()
|
||||||
{
|
{
|
||||||
$plugin = new MockPlugin();
|
$plugin = new MockPlugin();
|
||||||
@@ -280,7 +424,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$all_requests = $plugin->getReceivedRequests();
|
$all_requests = $plugin->getReceivedRequests();
|
||||||
$request = $all_requests[0];
|
$request = $all_requests[0];
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'https://geoip.maxmind.com/geoip/v2.0/country/1.2.3.4',
|
'https://geoip.maxmind.com/geoip/v2.0/country/1.2.3.4',
|
||||||
$request->getUrl(),
|
$request->getUrl(),
|
||||||
@@ -306,7 +450,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function client($response, $languages = array('en'))
|
private function client($response, $locales = array('en'))
|
||||||
{
|
{
|
||||||
$plugin = new MockPlugin();
|
$plugin = new MockPlugin();
|
||||||
$plugin->addResponse($response);
|
$plugin->addResponse($response);
|
||||||
@@ -316,7 +460,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
$client = new Client(
|
$client = new Client(
|
||||||
42,
|
42,
|
||||||
'abcdef123456',
|
'abcdef123456',
|
||||||
$languages,
|
$locales,
|
||||||
'geoip.maxmind.com',
|
'geoip.maxmind.com',
|
||||||
$guzzleClient
|
$guzzleClient
|
||||||
);
|
);
|
||||||
@@ -334,7 +478,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
$headers = array();
|
$headers = array();
|
||||||
if ($contentType) {
|
if ($contentType) {
|
||||||
$headers['Content-Type'] = $contentType;
|
$headers['Content-Type'] = $contentType;
|
||||||
} elseif ($status == 200 || ( $status >= 400 && $status < 500 )) {
|
} elseif ($status == 200 || ($status >= 400 && $status < 500)) {
|
||||||
$headers['Content-Type'] = 'application/vnd.maxmind.com-'
|
$headers['Content-Type'] = 'application/vnd.maxmind.com-'
|
||||||
. $endpoint . '+json; charset=UTF-8; version=1.0;';
|
. $endpoint . '+json; charset=UTF-8; version=1.0;';
|
||||||
}
|
}
|
||||||
@@ -342,7 +486,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
if ($bad) {
|
if ($bad) {
|
||||||
$body = '{ invalid: }';
|
$body = '{ invalid: }';
|
||||||
} elseif (is_array($body)) {
|
} elseif (is_array($body)) {
|
||||||
$body = json_encode($body);
|
$body = json_encode($body);
|
||||||
}
|
}
|
||||||
|
|
||||||
$headers['Content-Length'] = strlen($body);
|
$headers['Content-Length'] = strlen($body);
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
if (!$loader = @include __DIR__.'/../vendor/autoload.php') {
|
if (!$loader = @include __DIR__ . '/../vendor/autoload.php') {
|
||||||
die('Project dependencies missing');
|
die('Project dependencies missing');
|
||||||
}
|
}
|
||||||
|
|
||||||
$loader->add('GeoIP2\Test', __DIR__);
|
$loader->add('GeoIp2\Test', __DIR__);
|
||||||
|
|||||||
Reference in New Issue
Block a user