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; |
||
14 | |||
15 | use Geocoder\Model\Bounds; |
||
16 | use Geocoder\Provider\Provider; |
||
17 | use Geocoder\Query\GeocodeQuery; |
||
18 | use Geocoder\Query\ReverseQuery; |
||
19 | |||
20 | /** |
||
21 | * @author Tobias Nyholm <[email protected]> |
||
22 | */ |
||
23 | final class StatefulGeocoder implements Geocoder |
||
24 | { |
||
25 | /** |
||
26 | * @var string|null |
||
27 | */ |
||
28 | private $locale; |
||
29 | |||
30 | /** |
||
31 | * @var Bounds |
||
32 | */ |
||
33 | private $bounds; |
||
34 | |||
35 | /** |
||
36 | * @var int |
||
37 | */ |
||
38 | private $limit; |
||
39 | |||
40 | /** |
||
41 | * @var Provider |
||
42 | */ |
||
43 | private $provider; |
||
44 | |||
45 | /** |
||
46 | * @param string $locale |
||
47 | */ |
||
48 | public function __construct(Provider $provider, string $locale = null) |
||
49 | { |
||
50 | $this->provider = $provider; |
||
51 | $this->locale = $locale; |
||
52 | $this->limit = Geocoder::DEFAULT_RESULT_LIMIT; |
||
53 | } |
||
54 | |||
55 | public function geocode(string $value): Collection |
||
56 | { |
||
57 | $query = GeocodeQuery::create($value) |
||
58 | ->withLimit($this->limit); |
||
59 | |||
60 | if (null !== $this->locale && '' !== $this->locale) { |
||
61 | $query = $query->withLocale($this->locale); |
||
62 | } |
||
63 | |||
64 | if (null !== $this->bounds) { |
||
65 | $query = $query->withBounds($this->bounds); |
||
66 | } |
||
67 | |||
68 | return $this->provider->geocodeQuery($query); |
||
69 | } |
||
70 | |||
71 | public function reverse(float $latitude, float $longitude): Collection |
||
72 | { |
||
73 | $query = ReverseQuery::fromCoordinates($latitude, $longitude) |
||
74 | ->withLimit($this->limit); |
||
75 | |||
76 | if (null !== $this->locale && '' !== $this->locale) { |
||
77 | $query = $query->withLocale($this->locale); |
||
78 | } |
||
79 | |||
80 | return $this->provider->reverseQuery($query); |
||
81 | } |
||
82 | |||
83 | public function geocodeQuery(GeocodeQuery $query): Collection |
||
84 | { |
||
85 | $locale = $query->getLocale(); |
||
86 | if ((null === $locale || '' === $locale) && null !== $this->locale) { |
||
87 | $query = $query->withLocale($this->locale); |
||
88 | } |
||
89 | |||
90 | $bounds = $query->getBounds(); |
||
91 | if (empty($bounds) && null !== $this->bounds) { |
||
92 | $query = $query->withBounds($this->bounds); |
||
93 | } |
||
94 | |||
95 | return $this->provider->geocodeQuery($query); |
||
96 | } |
||
97 | |||
98 | public function reverseQuery(ReverseQuery $query): Collection |
||
99 | { |
||
100 | $locale = $query->getLocale(); |
||
101 | if ((null === $locale || '' === $locale) && null !== $this->locale) { |
||
102 | $query = $query->withLocale($this->locale); |
||
103 | } |
||
104 | |||
105 | return $this->provider->reverseQuery($query); |
||
106 | } |
||
107 | |||
108 | public function setLocale(string $locale): self |
||
109 | { |
||
110 | $this->locale = $locale; |
||
111 | |||
112 | return $this; |
||
113 | } |
||
114 | |||
115 | public function setBounds(Bounds $bounds): self |
||
116 | { |
||
117 | $this->bounds = $bounds; |
||
118 | |||
119 | return $this; |
||
120 | } |
||
121 | |||
122 | public function setLimit(int $limit): self |
||
123 | { |
||
124 | $this->limit = $limit; |
||
125 | |||
126 | return $this; |
||
127 | } |
||
128 | |||
129 | public function getName(): string |
||
130 | { |
||
131 | return 'stateful_geocoder'; |
||
132 | } |
||
133 | } |
||
134 |