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