|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Ivory Google Map package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Ivory\GoogleMap\Service\Geocoder\Request; |
|
13
|
|
|
|
|
14
|
|
|
use Ivory\GoogleMap\Base\Bound; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @see http://code.google.com/apis/maps/documentation/javascript/reference.html#GeocoderRequest |
|
18
|
|
|
* |
|
19
|
|
|
* @author GeLo <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class GeocoderAddressRequest extends AbstractGeocoderRequest |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $address; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var mixed[] |
|
30
|
|
|
*/ |
|
31
|
|
|
private $components = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var Bound|null |
|
35
|
|
|
*/ |
|
36
|
|
|
private $bound; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string|null |
|
40
|
|
|
*/ |
|
41
|
|
|
private $region; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $address |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct($address) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->setAddress($address); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getAddress() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->address; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $address |
|
61
|
|
|
*/ |
|
62
|
|
|
public function setAddress($address) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->address = $address; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return bool |
|
69
|
|
|
*/ |
|
70
|
|
|
public function hasComponents() |
|
71
|
|
|
{ |
|
72
|
|
|
return !empty($this->components); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return mixed[] |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getComponents() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->components; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param mixed[] $components |
|
85
|
|
|
*/ |
|
86
|
|
|
public function setComponents(array $components) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->components = []; |
|
89
|
|
|
$this->addComponents($components); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param mixed[] $components |
|
94
|
|
|
*/ |
|
95
|
|
|
public function addComponents(array $components) |
|
96
|
|
|
{ |
|
97
|
|
|
foreach ($components as $type => $value) { |
|
98
|
|
|
$this->setComponent($type, $value); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $type |
|
104
|
|
|
* |
|
105
|
|
|
* @return bool |
|
106
|
|
|
*/ |
|
107
|
|
|
public function hasComponent($type) |
|
108
|
|
|
{ |
|
109
|
|
|
return isset($this->components[$type]); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param string $type |
|
114
|
|
|
* |
|
115
|
|
|
* @return mixed |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getComponent($type) |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->hasComponent($type) ? $this->components[$type] : null; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param string $type |
|
124
|
|
|
* @param mixed $value |
|
125
|
|
|
*/ |
|
126
|
|
|
public function setComponent($type, $value) |
|
127
|
|
|
{ |
|
128
|
|
|
$this->components[$type] = $value; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param string $type |
|
133
|
|
|
*/ |
|
134
|
|
|
public function removeComponent($type) |
|
135
|
|
|
{ |
|
136
|
|
|
unset($this->components[$type]); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return bool |
|
141
|
|
|
*/ |
|
142
|
|
|
public function hasBound() |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->bound !== null; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return Bound|null |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getBound() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->bound; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param Bound|null $bound |
|
157
|
|
|
*/ |
|
158
|
|
|
public function setBound(Bound $bound = null) |
|
159
|
|
|
{ |
|
160
|
|
|
$this->bound = $bound; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return bool |
|
165
|
|
|
*/ |
|
166
|
|
|
public function hasRegion() |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->region !== null; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @return string|null |
|
173
|
|
|
*/ |
|
174
|
|
|
public function getRegion() |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->region; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param string|null $region |
|
181
|
|
|
*/ |
|
182
|
|
|
public function setRegion($region = null) |
|
183
|
|
|
{ |
|
184
|
|
|
$this->region = $region; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* {@inheritdoc} |
|
189
|
|
|
*/ |
|
190
|
|
|
public function buildQuery() |
|
191
|
|
|
{ |
|
192
|
|
|
$query = ['address' => $this->address]; |
|
193
|
|
|
|
|
194
|
|
View Code Duplication |
if ($this->hasComponents()) { |
|
|
|
|
|
|
195
|
|
|
$query['components'] = implode('|', array_map(function ($type, $value) { |
|
196
|
|
|
return $type.':'.$value; |
|
197
|
|
|
}, array_keys($this->components), $this->components)); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
if ($this->hasBound()) { |
|
201
|
|
|
$query['bound'] = implode('|', [ |
|
202
|
|
|
$this->buildCoordinate($this->bound->getSouthWest()), |
|
203
|
|
|
$this->buildCoordinate($this->bound->getNorthEast()), |
|
204
|
|
|
]); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
if ($this->hasRegion()) { |
|
208
|
|
|
$query['region'] = $this->region; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
return array_merge($query, parent::buildQuery()); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.