1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the BazingaGeocoderBundle package. |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license MIT License |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Bazinga\GeocoderBundle\ProviderFactory; |
14
|
|
|
|
15
|
|
|
use Geocoder\Provider\GeoIP2\GeoIP2; |
16
|
|
|
use Geocoder\Provider\GeoIP2\GeoIP2Adapter; |
17
|
|
|
use Geocoder\Provider\Provider; |
18
|
|
|
use GeoIp2\Database\Reader; |
19
|
|
|
use GeoIp2\WebService\Client; |
20
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
21
|
|
|
|
22
|
|
|
final class GeoIP2Factory extends AbstractFactory |
23
|
|
|
{ |
24
|
|
|
protected static $dependencies = [ |
25
|
|
|
['requiredClass' => GeoIP2::class, 'packageName' => 'geocoder-php/geoip2-provider'], |
26
|
|
|
]; |
27
|
|
|
|
28
|
1 |
|
protected function getProvider(array $config): Provider |
29
|
|
|
{ |
30
|
1 |
|
$provider = $config['provider']; |
31
|
1 |
|
if ('webservice' === $provider) { |
32
|
1 |
|
$userId = isset($config['user_id']) ? (int) $config['user_id'] : null; |
33
|
|
|
|
34
|
1 |
|
$provider = new Client($userId, $config['license_key'], $config['locales'], $config['webservice_options']); |
35
|
|
|
} elseif ('database' === $provider) { |
36
|
|
|
$provider = new Reader($config['database_filename'], $config['locales']); |
37
|
|
|
} else { |
38
|
|
|
$provider = $config['provider_service']; |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
$adapter = new GeoIP2Adapter($provider, $config['model']); |
42
|
|
|
|
43
|
1 |
|
return new GeoIP2($adapter); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
protected static function configureOptionResolver(OptionsResolver $resolver) |
47
|
|
|
{ |
48
|
1 |
|
$resolver->setDefaults([ |
49
|
1 |
|
'model' => GeoIP2Adapter::GEOIP2_MODEL_CITY, |
50
|
|
|
'database_filename' => null, |
51
|
|
|
'user_id' => null, |
52
|
|
|
'license_key' => null, |
53
|
|
|
'webservice_options' => [], |
54
|
|
|
'locales' => ['en'], |
55
|
|
|
'provider_service' => null, |
56
|
|
|
]); |
57
|
|
|
|
58
|
1 |
|
$resolver->setRequired('provider'); |
59
|
1 |
|
$resolver->setAllowedTypes('provider', ['string']); |
60
|
1 |
|
$resolver->setAllowedTypes('provider_service', ['object', 'null']); |
61
|
1 |
|
$resolver->setAllowedTypes('model', ['string']); |
62
|
1 |
|
$resolver->setAllowedTypes('user_id', ['string', 'int', 'null']); |
63
|
1 |
|
$resolver->setAllowedTypes('license_key', ['string', 'null']); |
64
|
1 |
|
$resolver->setAllowedTypes('locales', ['array']); |
65
|
1 |
|
$resolver->setAllowedTypes('webservice_options', ['array']); |
66
|
1 |
|
$resolver->setAllowedTypes('database_filename', ['string', 'null']); |
67
|
|
|
|
68
|
1 |
|
$resolver->setAllowedValues('model', [GeoIP2Adapter::GEOIP2_MODEL_CITY, GeoIP2Adapter::GEOIP2_MODEL_COUNTRY]); |
69
|
1 |
|
$resolver->setAllowedValues('provider', ['webservice', 'database', 'service']); |
70
|
1 |
|
} |
71
|
|
|
} |
72
|
|
|
|