Skip to content

Commit 6c57e67

Browse files
committed
Added option to pass array of config to the package constructor
1 parent 54f17ea commit 6c57e67

File tree

5 files changed

+48
-22
lines changed

5 files changed

+48
-22
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,29 @@ Require the vendor autoload file in your php script.
5050
require_once 'path/to/vendor/autoload.php';
5151
```
5252
## Usage
53-
localhost IP `127.0.0.1` and `::1` will return `169.159.82.111` to assert a valid geo response.
53+
Localhost IP `127.0.0.1`, `192.168.65.0` and `::1` will return `169.159.82.111` to assert a valid geo response.
5454
```php
5555
use Victorybiz\GeoIPLocation\GeoIPLocation;
5656

5757
$geoip = new GeoIPLocation();
58+
59+
// OR
60+
61+
$geoip = new GeoIPLocation([
62+
'ip' = null, // Set IP. Default is NULL, will be auto set by the package
63+
'baseCurrency' = 'USD', // Set base currency for exchange rate. Default is USD
64+
]);
5865
```
5966
Alternatively
6067
```php
6168
$geoip = new \Victorybiz\GeoIPLocation\GeoIPLocation();
69+
70+
// OR
71+
72+
$geoip = new \Victorybiz\GeoIPLocation\GeoIPLocation([
73+
'ip' = null, // Set IP. Default is NULL, will be auto set by the package
74+
'baseCurrency' = 'USD', // Set base currency for exchange rate. Default is USD
75+
]);
6276
```
6377
You're good to go, explore the package
6478
```php

src/GeoIPLocation.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class GeoIPLocation
88
{
9+
protected $config = null;
10+
911
protected $ipLocated = false;
1012

1113
protected $ip = null;
@@ -29,19 +31,27 @@ class GeoIPLocation
2931
];
3032

3133
protected $invalidIps = [
32-
'192.168.65.0', '::1', '127.0.0.1'
34+
'::1', '127.0.0.1', '192.168.65.0'
3335
];
3436

3537
protected $localhostIp = '169.159.82.111';
3638

37-
protected $currency = 'NGN';
39+
protected $baseCurrency = 'USD';
3840

39-
public function __construct($ip = null, $currency = null)
41+
public function __construct($config = [])
4042
{
41-
$this->ip = $ip;
42-
if ($currency) {
43-
$this->defaultCurrency = $currency;
44-
}
43+
44+
if (isset($config['ip'])) {
45+
$this->ip = $config['ip'];
46+
}
47+
48+
if (isset($config['baseCurrency'])) {
49+
$this->baseCurrency = $config['baseCurrency'];
50+
} else {
51+
$config['baseCurrency'] = $this->baseCurrency;
52+
}
53+
54+
$this->config = $config;
4555
}
4656

4757
protected function locate()
@@ -50,15 +60,16 @@ protected function locate()
5060

5161
switch ($default_web_service) {
5262
case 'ipinfo':
53-
$webService = new IpInfoWebService($this->getIP(), $token);
63+
$token = null;
64+
$webService = new IpInfoWebService($this->getIP(), $this->config);
5465
$this->geoData = $webService->locate();
5566
break;
5667
case 'geoplugin':
57-
$webService = new GeoPluginWebService($this->getIP(), $this->currency);
68+
$webService = new GeoPluginWebService($this->getIP(), $this->config);
5869
$this->geoData = $webService->locate();
5970
break;
6071
default:
61-
$webService = new GeoPluginWebService($this->getIP(), $this->currency);
72+
$webService = new GeoPluginWebService($this->getIP(), $this->config);
6273
$this->geoData = $webService->locate();
6374
break;
6475
}

src/WebServices/GeoPluginWebService.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ class GeoPluginWebService extends WebService
1010
*
1111
* @return void
1212
*/
13-
public function __construct($ip, $currency)
13+
public function __construct($ip, $config)
1414
{
15-
parent::__construct($ip, $currency);
15+
parent::__construct($ip, $config);
1616
}
1717

1818
public function locate()
1919
{
2020
$ip_api_url = $this->apiUrl;
2121
$ip_api_url = str_replace( '{IP}', $this->ip, $ip_api_url );
22-
$ip_api_url = str_replace( '{CURRENCY}', $this->currency, $ip_api_url );
22+
$ip_api_url = str_replace( '{CURRENCY}', $this->config['baseCurrency'], $ip_api_url );
2323

2424
try {
2525
$client = $this->guzzleClient; // Guzzle HTTP Client

src/WebServices/IpInfoWebService.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@
55

66
class IpInfoWebService extends WebService
77
{
8-
protected $apiUrl = 'https://ipinfo.io?token={TOKEN}';
8+
protected $apiUrl = 'https://ipinfo.io?token={TOKEN}';
99

10-
protected $token = null
10+
protected $token = null;
1111

1212
/**
1313
* Class Constructor
1414
*
1515
* @return void
1616
*/
17-
public function __construct($ip, $token)
17+
public function __construct($ip, $config)
1818
{
1919
parent::__construct($ip);
20-
$this->token = $token;
20+
$this->config = $config;
2121
}
2222

2323
public function locate()
2424
{
2525
try {
26-
$client = new IPinfo($this->token, [
26+
$client = new IPinfo($this->config['token'], [
2727
'timeout' => $this->connectTimeout,
2828
]);
2929
$details = $client->getDetails($this->ip);

src/WebServices/WebService.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ class WebService
99

1010
protected $connectTimeout = 5;
1111

12-
protected $ip = null;
12+
protected $config = null;
1313

14-
protected $currency = null;
14+
protected $ip = null;
1515

16-
public function __construct($ip, $currency = null)
16+
public function __construct($ip, $config)
1717
{
1818
$this->ip = $ip;
19+
$this->config = $config;
1920
$this->guzzleClient = new Client();
2021
}
2122

0 commit comments

Comments
 (0)