1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Geocoder 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 Geocoder\Provider\Chain; |
14
|
|
|
|
15
|
|
|
use Geocoder\Collection; |
16
|
|
|
use Geocoder\Model\AddressCollection; |
17
|
|
|
use Geocoder\Query\GeocodeQuery; |
18
|
|
|
use Geocoder\Query\ReverseQuery; |
19
|
|
|
use Geocoder\Provider\Provider; |
20
|
|
|
use Psr\Log\LoggerAwareInterface; |
21
|
|
|
use Psr\Log\LoggerAwareTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Markus Bachmann <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class Chain implements Provider, LoggerAwareInterface |
27
|
|
|
{ |
28
|
|
|
use LoggerAwareTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Provider[] |
32
|
|
|
*/ |
33
|
|
|
private $providers = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Provider[] $providers |
37
|
|
|
*/ |
38
|
4 |
|
public function __construct(array $providers = []) |
39
|
|
|
{ |
40
|
4 |
|
$this->providers = $providers; |
41
|
4 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
1 |
|
public function geocodeQuery(GeocodeQuery $query): Collection |
47
|
|
|
{ |
48
|
1 |
|
foreach ($this->providers as $provider) { |
49
|
|
|
try { |
50
|
1 |
|
$result = $provider->geocodeQuery($query); |
51
|
|
|
|
52
|
1 |
|
if (!$result->isEmpty()) { |
53
|
1 |
|
return $result; |
54
|
|
|
} |
55
|
1 |
|
} catch (\Throwable $e) { |
56
|
1 |
|
$this->log( |
57
|
1 |
|
'alert', |
58
|
1 |
|
'Provider "{providerName}" could not geocode address: "{address}".', |
59
|
|
|
[ |
60
|
1 |
|
'exception' => $e, |
61
|
1 |
|
'providerName' => $provider->getName(), |
62
|
1 |
|
'address' => $query->getText(), |
63
|
|
|
] |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return new AddressCollection(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
1 |
|
public function reverseQuery(ReverseQuery $query): Collection |
75
|
|
|
{ |
76
|
1 |
|
foreach ($this->providers as $provider) { |
77
|
|
|
try { |
78
|
1 |
|
$result = $provider->reverseQuery($query); |
79
|
|
|
|
80
|
1 |
|
if (!$result->isEmpty()) { |
81
|
1 |
|
return $result; |
82
|
|
|
} |
83
|
1 |
|
} catch (\Throwable $e) { |
84
|
1 |
|
$coordinates = $query->getCoordinates(); |
85
|
1 |
|
$this->log( |
86
|
1 |
|
'alert', |
87
|
1 |
|
sprintf('Provider "%s" could reverse coordinates: %f, %f.', $provider->getName(), $coordinates->getLatitude(), $coordinates->getLongitude()), |
88
|
1 |
|
['exception' => $e] |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return new AddressCollection(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
1 |
|
public function getName(): string |
100
|
|
|
{ |
101
|
1 |
|
return 'chain'; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Adds a provider. |
106
|
|
|
* |
107
|
|
|
* @param Provider $provider |
108
|
|
|
* |
109
|
|
|
* @return Chain |
110
|
|
|
*/ |
111
|
1 |
|
public function add(Provider $provider): self |
112
|
|
|
{ |
113
|
1 |
|
$this->providers[] = $provider; |
114
|
|
|
|
115
|
1 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $level |
120
|
|
|
* @param $message |
121
|
|
|
* @param array $context |
122
|
|
|
*/ |
123
|
2 |
|
private function log($level, $message, array $context = []) |
124
|
|
|
{ |
125
|
2 |
|
if ($this->logger) { |
126
|
|
|
$this->logger->log($level, $message, $context); |
127
|
|
|
} |
128
|
2 |
|
} |
129
|
|
|
} |
130
|
|
|
|