Completed
Push — master ( 5dfd0e...1b0014 )
by Tobias
02:15
created

StatefulGeocoder::setBounds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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\Query\GeocodeQuery;
17
use Geocoder\Query\ReverseQuery;
18
use Geocoder\Provider\LocaleAwareGeocoder;
19
use Geocoder\Provider\Provider;
20
21
/**
22
 * @author Tobias Nyholm <[email protected]>
23
 */
24
class StatefulGeocoder implements Geocoder, LocaleAwareGeocoder
25
{
26
    /**
27
     * @var string
28
     */
29
    private $locale;
30
31
    /**
32
     * @var Bounds
33
     */
34
    private $bounds;
35
36
    /**
37
     * @var int
38
     */
39
    private $limit;
40
41
    /**
42
     * @var Provider
43
     */
44
    private $provider;
45
46
    /**
47
     * @param Provider $provider
48
     * @param string   $locale
49
     */
50
    public function __construct(Provider $provider, $locale = null)
51
    {
52
        $this->provider = $provider;
53
        $this->locale = $locale;
54
        $this->limit = Geocoder::DEFAULT_RESULT_LIMIT;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function geocode(string $value): Collection
61
    {
62
        $query = GeocodeQuery::create($value)
63
            ->withLimit($this->limit);
64
65
        if (!empty($this->locale)) {
66
            $query->withLocale($this->locale);
67
        }
68
69
        if (!empty($this->bouds)) {
0 ignored issues
show
Bug introduced by
The property bouds does not seem to exist. Did you mean bounds?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
70
            $query->withBounds($this->bounds);
71
        }
72
73
        return $this->provider->geocodeQuery($query);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function reverse($latitude, $longitude): Collection
80
    {
81
        $query = ReverseQuery::fromCoordinates($latitude, $longitude)
82
            ->withLimit($this->limit);
83
84
        if (!empty($this->locale)) {
85
            $query = $query->withLocale($this->locale);
86
        }
87
88
        return $this->provider->reverseQuery($query);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function geocodeQuery(GeocodeQuery $query): Collection
95
    {
96
        $locale = $query->getLocale();
97
        if (empty($locale) && null !== $this->locale) {
98
            $query = $query->withLocale($this->locale);
99
        }
100
101
        $bounds = $query->getBounds();
102
        if (empty($bounds) && null !== $this->bounds) {
103
            $query = $query->withBounds($this->bounds);
104
        }
105
106
        $this->provider->geocodeQuery($query);
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function reverseQuery(ReverseQuery $query): Collection
113
    {
114
        $locale = $query->getLocale();
115
        if (empty($locale) && null !== $this->locale) {
116
            $query->withLocale($this->locale);
117
        }
118
119
        $this->provider->reverseQuery($query);
120
    }
121
122
    /**
123
     * @param string $locale
124
     *
125
     * @return StatefulGeocoder
126
     */
127
    public function setLocale(string $locale): self
128
    {
129
        $this->locale = $locale;
130
131
        return $this;
132
    }
133
134
    /**
135
     * @param Bounds $bounds
136
     *
137
     * @return StatefulGeocoder
138
     */
139
    public function setBounds(Bounds $bounds): self
140
    {
141
        $this->bounds = $bounds;
142
143
        return $this;
144
    }
145
146
    /**
147
     * @param int $limit
148
     *
149
     * @return StatefulGeocoder
150
     */
151
    public function setLimit(int $limit): self
152
    {
153
        $this->limit = $limit;
154
155
        return $this;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function getName(): string
162
    {
163
        return 'stateful_geocoder';
164
    }
165
}
166