|
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\ProviderInterface; |
|
20
|
|
|
use GeoIp2\WebService\Client; |
|
21
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
22
|
|
|
|
|
23
|
|
|
final class GeoIP2Factory extends AbstractFactory |
|
24
|
|
|
{ |
|
25
|
|
|
protected static $dependencies = [ |
|
26
|
|
|
['requiredClass' => GeoIP2::class, 'packageName' => 'geocoder-php/geoip2-provider'], |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
1 |
|
protected function getProvider(array $config): Provider |
|
30
|
|
|
{ |
|
31
|
1 |
|
$provider = $config['provider']; |
|
32
|
1 |
|
if ($provider === 'webservice') { |
|
33
|
1 |
|
$provider = new Client($config['user_id'], $config['license_key'], $config['locales'], $config['webservice_options']); |
|
34
|
|
|
} elseif ($provider === 'database') { |
|
35
|
|
|
$provider = new Reader($config['database_filename'], $config['locales']); |
|
36
|
|
|
} else { |
|
37
|
|
|
$provider = $config['provider_service']; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
$adapter = new GeoIP2Adapter($provider, $config['model']); |
|
41
|
|
|
|
|
42
|
1 |
|
return new GeoIP2($adapter); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
protected static function configureOptionResolver(OptionsResolver $resolver) |
|
46
|
|
|
{ |
|
47
|
1 |
|
$resolver->setDefaults([ |
|
48
|
1 |
|
'model' => GeoIP2Adapter::GEOIP2_MODEL_CITY, |
|
49
|
|
|
'database_filename' => null, |
|
50
|
|
|
'user_id' => null, |
|
51
|
|
|
'license_key' => null, |
|
52
|
|
|
'webservice_options' => [], |
|
53
|
|
|
'locales' => ['en'], |
|
54
|
|
|
'provider_service' => null, |
|
55
|
|
|
]); |
|
56
|
|
|
|
|
57
|
1 |
|
$resolver->setRequired('provider'); |
|
58
|
1 |
|
$resolver->setAllowedTypes('provider', ['string']); |
|
59
|
1 |
|
$resolver->setAllowedTypes('provider_service', [ProviderInterface::class, 'null']); |
|
60
|
1 |
|
$resolver->setAllowedTypes('model', ['string']); |
|
61
|
1 |
|
$resolver->setAllowedTypes('user_id', ['string', 'null']); |
|
62
|
1 |
|
$resolver->setAllowedTypes('license_key', ['string', 'null']); |
|
63
|
1 |
|
$resolver->setAllowedTypes('locales', ['array']); |
|
64
|
1 |
|
$resolver->setAllowedTypes('webservice_options', ['array']); |
|
65
|
1 |
|
$resolver->setAllowedTypes('database_filename', ['string', 'null']); |
|
66
|
|
|
|
|
67
|
1 |
|
$resolver->setAllowedValues('model', [GeoIP2Adapter::GEOIP2_MODEL_CITY, GeoIP2Adapter::GEOIP2_MODEL_COUNTRY]); |
|
68
|
1 |
|
$resolver->setAllowedValues('provider', ['webservice', 'database', 'service']); |
|
69
|
1 |
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|