├── .github └── workflows │ └── provider.yml ├── .gitignore ├── CHANGELOG.md ├── GeoPlugin.php ├── LICENSE ├── Readme.md └── composer.json /.github/workflows/provider.yml: -------------------------------------------------------------------------------- 1 | name: Provider 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | test: 11 | name: PHP ${{ matrix.php-version }} 12 | runs-on: ubuntu-latest 13 | strategy: 14 | fail-fast: false 15 | matrix: 16 | php-version: ['8.0', '8.1', '8.2', '8.3', '8.4'] 17 | steps: 18 | - uses: actions/checkout@v4 19 | - name: Use PHP ${{ matrix.php-version }} 20 | uses: shivammathur/setup-php@v2 21 | with: 22 | php-version: ${{ matrix.php-version }} 23 | extensions: curl 24 | - name: Validate composer.json and composer.lock 25 | run: composer validate --strict 26 | - name: Install dependencies 27 | run: composer update --prefer-stable --prefer-dist --no-progress 28 | - name: Run test suite 29 | run: composer run-script test-ci 30 | - name: Upload Coverage report 31 | run: | 32 | wget https://scrutinizer-ci.com/ocular.phar 33 | php ocular.phar code-coverage:upload --format=php-clover build/coverage.xml 34 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | composer.lock 3 | phpunit.xml 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release. 4 | 5 | ## 4.4.0 6 | 7 | ### Added 8 | 9 | - Add support for PHP Geocoder 5 10 | 11 | ## 4.3.0 12 | 13 | ### Added 14 | 15 | - Add support for PHP 8.1 16 | - Add GitHub Actions workflow 17 | 18 | ### Removed 19 | 20 | - Drop support for PHP 7.3 21 | 22 | ### Changed 23 | 24 | - Migrate from PHP-HTTP to PSR-18 client 25 | 26 | ## 4.2.0 27 | 28 | ### Added 29 | 30 | - Add support for PHP 8.0 31 | 32 | ### Removed 33 | 34 | - Drop support for PHP 7.2 35 | 36 | ### Changed 37 | 38 | - Upgrade PHPUnit to version 9 39 | 40 | ## 4.1.0 41 | 42 | ### Removed 43 | 44 | - Drop support for PHP < 7.2 45 | 46 | ## 4.0.1 47 | 48 | ### Fixed 49 | 50 | - Better check if we got a empty result. 51 | 52 | ## 4.0.0 53 | 54 | First release of this library. 55 | -------------------------------------------------------------------------------- /GeoPlugin.php: -------------------------------------------------------------------------------- 1 | 27 | */ 28 | final class GeoPlugin extends AbstractHttpProvider implements Provider 29 | { 30 | /** 31 | * @var string 32 | */ 33 | public const GEOCODE_ENDPOINT_URL = 'http://www.geoplugin.net/json.gp?ip=%s'; 34 | 35 | public function geocodeQuery(GeocodeQuery $query): Collection 36 | { 37 | $address = $query->getText(); 38 | if (!filter_var($address, FILTER_VALIDATE_IP)) { 39 | throw new UnsupportedOperation('The GeoPlugin provider does not support street addresses, only IP addresses.'); 40 | } 41 | 42 | if (in_array($address, ['127.0.0.1', '::1'])) { 43 | return new AddressCollection([$this->getLocationForLocalhost()]); 44 | } 45 | 46 | $url = sprintf(self::GEOCODE_ENDPOINT_URL, $address); 47 | 48 | return $this->executeQuery($url); 49 | } 50 | 51 | public function reverseQuery(ReverseQuery $query): Collection 52 | { 53 | throw new UnsupportedOperation('The GeoPlugin provider is not able to do reverse geocoding.'); 54 | } 55 | 56 | public function getName(): string 57 | { 58 | return 'geo_plugin'; 59 | } 60 | 61 | private function executeQuery(string $url): AddressCollection 62 | { 63 | $content = $this->getUrlContents($url); 64 | $json = json_decode($content, true); 65 | 66 | if (!is_array($json) || !count($json)) { 67 | throw InvalidServerResponse::create($url); 68 | } 69 | 70 | if (!array_key_exists('geoplugin_status', $json) || (200 !== $json['geoplugin_status'] && 206 !== $json['geoplugin_status'])) { 71 | return new AddressCollection([]); 72 | } 73 | 74 | // Return empty collection if address was not found 75 | if ('' === $json['geoplugin_regionName'] 76 | && '' === $json['geoplugin_regionCode'] 77 | && '' === $json['geoplugin_city'] 78 | && '' === $json['geoplugin_countryName'] 79 | && '' === $json['geoplugin_countryCode'] 80 | && '0' === $json['geoplugin_latitude'] 81 | && '0' === $json['geoplugin_longitude']) { 82 | return new AddressCollection([]); 83 | } 84 | 85 | $data = array_filter($json); 86 | 87 | $adminLevels = []; 88 | 89 | $region = $data['geoplugin_regionName'] ?? null; 90 | $regionCode = $data['geoplugin_regionCode'] ?? null; 91 | 92 | if (null !== $region || null !== $regionCode) { 93 | $adminLevels[] = ['name' => $region, 'code' => $regionCode, 'level' => 1]; 94 | } 95 | 96 | $results = []; 97 | $results[] = Address::createFromArray([ 98 | 'providedBy' => $this->getName(), 99 | 'locality' => isset($data['geoplugin_city']) ? $data['geoplugin_city'] : null, 100 | 'country' => isset($data['geoplugin_countryName']) ? $data['geoplugin_countryName'] : null, 101 | 'countryCode' => isset($data['geoplugin_countryCode']) ? $data['geoplugin_countryCode'] : null, 102 | 'adminLevels' => $adminLevels, 103 | 'latitude' => isset($data['geoplugin_latitude']) ? $data['geoplugin_latitude'] : null, 104 | 'longitude' => isset($data['geoplugin_longitude']) ? $data['geoplugin_longitude'] : null, 105 | ]); 106 | 107 | return new AddressCollection($results); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2011 — William Durand 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # GeoPlugin Geocoder provider 2 | [![Build Status](https://travis-ci.org/geocoder-php/geo-plugin-provider.svg?branch=master)](http://travis-ci.org/geocoder-php/geo-plugin-provider) 3 | [![Latest Stable Version](https://poser.pugx.org/geocoder-php/geo-plugin-provider/v/stable)](https://packagist.org/packages/geocoder-php/geo-plugin-provider) 4 | [![Total Downloads](https://poser.pugx.org/geocoder-php/geo-plugin-provider/downloads)](https://packagist.org/packages/geocoder-php/geo-plugin-provider) 5 | [![Monthly Downloads](https://poser.pugx.org/geocoder-php/geo-plugin-provider/d/monthly.png)](https://packagist.org/packages/geocoder-php/geo-plugin-provider) 6 | [![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/geocoder-php/geo-plugin-provider.svg?style=flat-square)](https://scrutinizer-ci.com/g/geocoder-php/geo-plugin-provider) 7 | [![Quality Score](https://img.shields.io/scrutinizer/g/geocoder-php/geo-plugin-provider.svg?style=flat-square)](https://scrutinizer-ci.com/g/geocoder-php/geo-plugin-provider) 8 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) 9 | 10 | This is the GeoPlugin provider from the PHP Geocoder. This is a **READ ONLY** repository. See the 11 | [main repo](https://github.com/geocoder-php/Geocoder) for information and documentation. 12 | 13 | ### Install 14 | 15 | ```bash 16 | composer require geocoder-php/geo-plugin-provider 17 | ``` 18 | 19 | ### Contribute 20 | 21 | Contributions are very welcome! Send a pull request to the [main repository](https://github.com/geocoder-php/Geocoder) or 22 | report any issues you find on the [issue tracker](https://github.com/geocoder-php/Geocoder/issues). 23 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "geocoder-php/geo-plugin-provider", 3 | "type": "library", 4 | "description": "Geocoder GeoPlugin adapter", 5 | "keywords": [], 6 | "homepage": "http://geocoder-php.org/Geocoder/", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "William Durand", 11 | "email": "william.durand1@gmail.com" 12 | } 13 | ], 14 | "require": { 15 | "php": "^8.0", 16 | "geocoder-php/common-http": "^4.0", 17 | "willdurand/geocoder": "^4.0|^5.0" 18 | }, 19 | "provide": { 20 | "geocoder-php/provider-implementation": "1.0" 21 | }, 22 | "require-dev": { 23 | "geocoder-php/provider-integration-tests": "^1.6.3", 24 | "php-http/message": "^1.0", 25 | "phpunit/phpunit": "^9.6.11" 26 | }, 27 | "extra": { 28 | "branch-alias": { 29 | "dev-master": "4.0-dev" 30 | } 31 | }, 32 | "autoload": { 33 | "psr-4": { 34 | "Geocoder\\Provider\\GeoPlugin\\": "" 35 | }, 36 | "exclude-from-classmap": [ 37 | "/Tests/" 38 | ] 39 | }, 40 | "minimum-stability": "dev", 41 | "prefer-stable": true, 42 | "scripts": { 43 | "test": "vendor/bin/phpunit", 44 | "test-ci": "vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml" 45 | } 46 | } 47 | --------------------------------------------------------------------------------