Skip to content

Commit 404df3e

Browse files
mweghorstpulkitjalan
authored andcommitted
Laravel 6.0 support (#30)
* Update composer.json * Update GeoIPManager.php * Update GeoIPManager.php * Update GeoIPManager.php * Update GeoIP.php * Update GeoIPUpdater.php * Update MaxmindDriver.php * Update IPApiDriver.php * Update TelizeDriver.php * Update IpStackDriver.php * Update composer.json * Update GeoIPUpdater.php * Update composer.json * Update composer.json
1 parent 8d92396 commit 404df3e

File tree

8 files changed

+62
-52
lines changed

8 files changed

+62
-52
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
],
1212
"require": {
1313
"php": ">=7.0",
14-
"illuminate/support": "~5",
15-
"illuminate/console": "~5",
14+
"illuminate/support": "~5|~6",
15+
"illuminate/console": "~5|~6",
1616
"guzzlehttp/guzzle": "~6",
1717
"geoip2/geoip2": "^2.0"
1818
},

src/Drivers/IPApiDriver.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PulkitJalan\GeoIP\Drivers;
44

5+
use Illuminate\Support\Arr;
6+
57
class IPApiDriver extends AbstractGeoIPDriver
68
{
79
/**
@@ -15,20 +17,20 @@ public function get($ip)
1517
{
1618
$data = $this->getRaw($ip);
1719

18-
if (empty($data) || (array_get($data, 'status') === 'fail')) {
20+
if (empty($data) || (Arr::get($data, 'status') === 'fail')) {
1921
return $this->getDefault();
2022
}
2123

2224
return [
23-
'city' => array_get($data, 'city'),
24-
'country' => array_get($data, 'country'),
25-
'countryCode' => array_get($data, 'countryCode'),
26-
'latitude' => (float) number_format(array_get($data, 'lat'), 5),
27-
'longitude' => (float) number_format(array_get($data, 'lon'), 5),
28-
'region' => array_get($data, 'regionName'),
29-
'regionCode' => array_get($data, 'region'),
30-
'timezone' => array_get($data, 'timezone'),
31-
'postalCode' => array_get($data, 'zip'),
25+
'city' => Arr::get($data, 'city'),
26+
'country' => Arr::get($data, 'country'),
27+
'countryCode' => Arr::get($data, 'countryCode'),
28+
'latitude' => (float) number_format(Arr::get($data, 'lat'), 5),
29+
'longitude' => (float) number_format(Arr::get($data, 'lon'), 5),
30+
'region' => Arr::get($data, 'regionName'),
31+
'regionCode' => Arr::get($data, 'region'),
32+
'timezone' => Arr::get($data, 'timezone'),
33+
'postalCode' => Arr::get($data, 'zip'),
3234
];
3335
}
3436

@@ -60,9 +62,9 @@ protected function getUrl($ip)
6062
$key = '';
6163

6264
// if key is set change to pro service
63-
if (array_get($this->config, 'key', false)) {
65+
if (Arr::get($this->config, 'key', false)) {
6466
$baseUrl = 'https://pro.ip-api.com/json/';
65-
$key = array_get($this->config, 'key');
67+
$key = Arr::get($this->config, 'key');
6668
}
6769

6870
return $baseUrl.$ip.(($key) ? '?key='.$key : '');

src/Drivers/IpStackDriver.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PulkitJalan\GeoIP\Drivers;
44

55
use GuzzleHttp\Exception\RequestException;
6+
use Illuminate\Support\Arr;
67
use PulkitJalan\GeoIP\Exceptions\InvalidCredentialsException;
78

89
class IpStackDriver extends AbstractGeoIPDriver
@@ -30,20 +31,20 @@ public function get($ip)
3031
{
3132
$data = $this->getRaw($ip);
3233

33-
if (empty($data) || (array_get($data, 'latitude') === 0 && array_get($data, 'longitude') === 0)) {
34+
if (empty($data) || (Arr::get($data, 'latitude') === 0 && Arr::get($data, 'longitude') === 0)) {
3435
return $this->getDefault();
3536
}
3637

3738
return [
38-
'city' => array_get($data, 'city'),
39-
'country' => array_get($data, 'country_name'),
40-
'countryCode' => array_get($data, 'country_code'),
41-
'latitude' => (float) number_format(array_get($data, 'latitude'), 5),
42-
'longitude' => (float) number_format(array_get($data, 'longitude'), 5),
43-
'region' => array_get($data, 'region_name'),
44-
'regionCode' => array_get($data, 'region_code'),
45-
'timezone' => array_get($data, 'time_zone.id'),
46-
'postalCode' => array_get($data, 'zip'),
39+
'city' => Arr::get($data, 'city'),
40+
'country' => Arr::get($data, 'country_name'),
41+
'countryCode' => Arr::get($data, 'country_code'),
42+
'latitude' => (float) number_format(Arr::get($data, 'latitude'), 5),
43+
'longitude' => (float) number_format(Arr::get($data, 'longitude'), 5),
44+
'region' => Arr::get($data, 'region_name'),
45+
'regionCode' => Arr::get($data, 'region_code'),
46+
'timezone' => Arr::get($data, 'time_zone.id'),
47+
'postalCode' => Arr::get($data, 'zip'),
4748
];
4849
}
4950

@@ -74,6 +75,6 @@ public function getRaw($ip)
7475
*/
7576
protected function getUrl($ip)
7677
{
77-
return 'https://api.ipstack.com/'.$ip.'?access_key='.array_get($this->config, 'key');
78+
return 'https://api.ipstack.com/'.$ip.'?access_key='.Arr::get($this->config, 'key');
7879
}
7980
}

src/Drivers/MaxmindDriver.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use GeoIp2\Database\Reader;
66
use GeoIp2\Exception\AddressNotFoundException;
77
use GeoIp2\WebService\Client;
8+
use Illuminate\Support\Arr;
89
use PulkitJalan\GeoIP\Exceptions\InvalidCredentialsException;
910
use PulkitJalan\GeoIP\Exceptions\InvalidDatabaseException;
1011

@@ -83,12 +84,12 @@ public function getRaw($ip)
8384
protected function create()
8485
{
8586
// if user_id and license_key are set then use the web service
86-
if (array_get($this->config, 'user_id', false)) {
87+
if (Arr::get($this->config, 'user_id', false)) {
8788
return $this->createWebClient();
8889
}
8990

9091
// if database file is set then use database service
91-
if (array_get($this->config, 'database', false)) {
92+
if (Arr::get($this->config, 'database', false)) {
9293
return $this->createDatabase();
9394
}
9495

@@ -104,8 +105,8 @@ protected function create()
104105
*/
105106
protected function createWebClient()
106107
{
107-
$userId = array_get($this->config, 'user_id', false);
108-
$licenseKey = array_get($this->config, 'license_key', false);
108+
$userId = Arr::get($this->config, 'user_id', false);
109+
$licenseKey = Arr::get($this->config, 'license_key', false);
109110

110111
// check and make sure they are set
111112
if (! $userId || ! $licenseKey) {
@@ -124,7 +125,7 @@ protected function createWebClient()
124125
*/
125126
protected function createDatabase()
126127
{
127-
$database = array_get($this->config, 'database', false);
128+
$database = Arr::get($this->config, 'database', false);
128129

129130
// check if file exists first
130131
if (! $database || ! file_exists($database)) {

src/Drivers/TelizeDriver.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PulkitJalan\GeoIP\Drivers;
44

55
use GuzzleHttp\Exception\RequestException;
6+
use Illuminate\Support\Arr;
67
use PulkitJalan\GeoIP\Exceptions\InvalidCredentialsException;
78

89
class TelizeDriver extends AbstractGeoIPDriver
@@ -35,15 +36,15 @@ public function get($ip)
3536
}
3637

3738
return [
38-
'city' => array_get($data, 'city'),
39-
'country' => array_get($data, 'country'),
40-
'countryCode' => array_get($data, 'country_code'),
41-
'latitude' => (float) number_format(array_get($data, 'latitude'), 5),
42-
'longitude' => (float) number_format(array_get($data, 'longitude'), 5),
43-
'region' => array_get($data, 'region'),
44-
'regionCode' => array_get($data, 'region_code'),
45-
'timezone' => array_get($data, 'timezone'),
46-
'postalCode' => array_get($data, 'postal_code'),
39+
'city' => Arr::get($data, 'city'),
40+
'country' => Arr::get($data, 'country'),
41+
'countryCode' => Arr::get($data, 'country_code'),
42+
'latitude' => (float) number_format(Arr::get($data, 'latitude'), 5),
43+
'longitude' => (float) number_format(Arr::get($data, 'longitude'), 5),
44+
'region' => Arr::get($data, 'region'),
45+
'regionCode' => Arr::get($data, 'region_code'),
46+
'timezone' => Arr::get($data, 'timezone'),
47+
'postalCode' => Arr::get($data, 'postal_code'),
4748
];
4849
}
4950

@@ -59,7 +60,7 @@ public function getRaw($ip)
5960
try {
6061
return json_decode($this->guzzle->get($this->getUrl($ip), [
6162
'headers' => [
62-
'X-Mashape-Key' => array_get($this->config, 'key'),
63+
'X-Mashape-Key' => Arr::get($this->config, 'key'),
6364
'Accept' => 'application/json',
6465
],
6566
])->getBody(), true);

src/GeoIP.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PulkitJalan\GeoIP;
44

5+
use Illuminate\Support\Arr;
6+
use Illuminate\Support\Str;
57
use PulkitJalan\GeoIP\Exceptions\GeoIPException;
68

79
class GeoIP
@@ -37,7 +39,7 @@ class GeoIP
3739
public function __construct(array $config = ['driver' => 'ip-api'])
3840
{
3941
$this->driver = with(new GeoIPManager($config))->getDriver();
40-
$this->random = array_get($config, 'random', false);
42+
$this->random = Arr::get($config, 'random', false);
4143
}
4244

4345
/**
@@ -77,7 +79,7 @@ public function getIp()
7779
if ($this->random) {
7880
$this->ip = long2ip(mt_rand());
7981
} else {
80-
$this->ip = array_get($_SERVER, 'HTTP_CLIENT_IP', array_get($_SERVER, 'HTTP_X_FORWARDED_FOR', array_get($_SERVER, 'HTTP_X_FORWARDED', array_get($_SERVER, 'HTTP_FORWARDED_FOR', array_get($_SERVER, 'HTTP_FORWARDED', array_get($_SERVER, 'REMOTE_ADDR', '127.0.0.1'))))));
82+
$this->ip = Arr::get($_SERVER, 'HTTP_CLIENT_IP', Arr::get($_SERVER, 'HTTP_X_FORWARDED_FOR', Arr::get($_SERVER, 'HTTP_X_FORWARDED', Arr::get($_SERVER, 'HTTP_FORWARDED_FOR', Arr::get($_SERVER, 'HTTP_FORWARDED', Arr::get($_SERVER, 'REMOTE_ADDR', '127.0.0.1'))))));
8183
}
8284
}
8385

@@ -105,7 +107,7 @@ public function get($property = '')
105107
return $data;
106108
}
107109

108-
return array_get($data, $property, '');
110+
return Arr::get($data, $property, '');
109111
}
110112

111113
/**
@@ -121,7 +123,7 @@ public function getRaw()
121123
$this->setIp($ip);
122124

123125
// check ip in memory
124-
$data = array_get($this->storeRaw, $ip);
126+
$data = Arr::get($this->storeRaw, $ip);
125127

126128
if (! $data) {
127129
try {
@@ -150,7 +152,7 @@ protected function getData()
150152
$this->setIp($ip);
151153

152154
// check ip in memory
153-
$data = array_get($this->store, $ip);
155+
$data = Arr::get($this->store, $ip);
154156

155157
if (! $data) {
156158
try {
@@ -178,7 +180,7 @@ protected function getData()
178180
*/
179181
public function __call($method, $parameters)
180182
{
181-
if (starts_with($method, 'get')) {
183+
if (Str::startsWith($method, 'get')) {
182184
$param = lcfirst(ltrim($method, 'get'));
183185

184186
return $this->get($param);

src/GeoIPManager.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PulkitJalan\GeoIP;
44

5+
use Illuminate\Support\Arr;
6+
use Illuminate\Support\Str;
57
use PulkitJalan\GeoIP\Drivers\IpStackDriver;
68
use PulkitJalan\GeoIP\Drivers\IPApiDriver;
79
use PulkitJalan\GeoIP\Drivers\MaxmindDriver;
@@ -30,15 +32,15 @@ public function __construct(array $config)
3032
*/
3133
public function getDriver($driver = null)
3234
{
33-
$driver = ($driver) ?: array_get($this->config, 'driver', '');
35+
$driver = ($driver) ?: Arr::get($this->config, 'driver', '');
3436

35-
$method = 'create'.ucfirst(camel_case($driver)).'Driver';
37+
$method = 'create'.ucfirst(Str::camel($driver)).'Driver';
3638

3739
if (! method_exists($this, $method)) {
3840
throw new InvalidDriverException(sprintf('Driver [%s] not supported.', $driver));
3941
}
4042

41-
return $this->{$method}(array_get($this->config, $driver, []));
43+
return $this->{$method}(Arr::get($this->config, $driver, []));
4244
}
4345

4446
/**

src/GeoIPUpdater.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use GuzzleHttp\Client as GuzzleClient;
7+
use Illuminate\Support\Arr;
78

89
class GeoIPUpdater
910
{
@@ -34,7 +35,7 @@ public function __construct(array $config, GuzzleClient $guzzle = null)
3435
*/
3536
public function update()
3637
{
37-
if (array_get($this->config, 'maxmind.database', false)) {
38+
if (Arr::get($this->config, 'maxmind.database', false)) {
3839
return $this->updateMaxmindDatabase();
3940
}
4041

@@ -48,9 +49,9 @@ public function update()
4849
*/
4950
protected function updateMaxmindDatabase()
5051
{
51-
$maxmindDatabaseUrl = array_get($this->config, 'maxmind.download', 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz');
52+
$maxmindDatabaseUrl = Arr::get($this->config, 'maxmind.download', 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz');
5253

53-
$database = array_get($this->config, 'maxmind.database', false);
54+
$database = Arr::get($this->config, 'maxmind.database', false);
5455

5556
if (! file_exists($dir = pathinfo($database, PATHINFO_DIRNAME))) {
5657
mkdir($dir, 0777, true);

0 commit comments

Comments
 (0)