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 |
|
sprintf('Provider "%s" could geocode address: "%s".', $provider->getName(), $query->getText()), |
59
|
1 |
|
['exception' => $e] |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return new AddressCollection(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
1 |
|
public function reverseQuery(ReverseQuery $query): Collection |
71
|
|
|
{ |
72
|
1 |
|
foreach ($this->providers as $provider) { |
73
|
|
|
try { |
74
|
1 |
|
$result = $provider->reverseQuery($query); |
75
|
|
|
|
76
|
1 |
|
if (!$result->isEmpty()) { |
77
|
1 |
|
return $result; |
78
|
|
|
} |
79
|
1 |
|
} catch (\Throwable $e) { |
80
|
1 |
|
$coordinates = $query->getCoordinates(); |
81
|
1 |
|
$this->log( |
82
|
1 |
|
'alert', |
83
|
1 |
|
sprintf('Provider "%s" could reverse coordinates: %f, %f.', $provider->getName(), $coordinates->getLatitude(), $coordinates->getLongitude()), |
84
|
1 |
|
['exception' => $e] |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return new AddressCollection(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
1 |
|
public function getName(): string |
96
|
|
|
{ |
97
|
1 |
|
return 'chain'; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Adds a provider. |
102
|
|
|
* |
103
|
|
|
* @param Provider $provider |
104
|
|
|
* |
105
|
|
|
* @return Chain |
106
|
|
|
*/ |
107
|
1 |
|
public function add(Provider $provider): self |
108
|
|
|
{ |
109
|
1 |
|
$this->providers[] = $provider; |
110
|
|
|
|
111
|
1 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param $level |
116
|
|
|
* @param $message |
117
|
|
|
* @param array $context |
118
|
|
|
*/ |
119
|
2 |
|
private function log($level, $message, array $context = []) |
120
|
|
|
{ |
121
|
2 |
|
if ($this->logger) { |
122
|
|
|
$this->logger->log($level, $message, $context); |
123
|
|
|
} |
124
|
2 |
|
} |
125
|
|
|
} |
126
|
|
|
|