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\Place\Autocomplete\Request; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author GeLo <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class PlaceAutocompleteRequest extends AbstractPlaceAutocompleteRequest |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string[] |
21
|
|
|
*/ |
22
|
|
|
private $types = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string[] |
26
|
|
|
*/ |
27
|
|
|
private $components = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
28 |
|
public function hasTypes() |
33
|
|
|
{ |
34
|
28 |
|
return !empty($this->types); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return string[] |
39
|
|
|
*/ |
40
|
20 |
|
public function getTypes() |
41
|
|
|
{ |
42
|
20 |
|
return $this->types; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string[] $types |
47
|
|
|
*/ |
48
|
12 |
|
public function setTypes(array $types) |
49
|
|
|
{ |
50
|
12 |
|
$this->types = []; |
51
|
12 |
|
$this->addTypes($types); |
52
|
12 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string[] $types |
56
|
|
|
*/ |
57
|
12 |
|
public function addTypes(array $types) |
58
|
|
|
{ |
59
|
12 |
|
foreach ($types as $type) { |
60
|
12 |
|
$this->addType($type); |
61
|
|
|
} |
62
|
12 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $type |
66
|
|
|
* |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
20 |
|
public function hasType($type) |
70
|
|
|
{ |
71
|
20 |
|
return in_array($type, $this->types, true); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $type |
76
|
|
|
*/ |
77
|
20 |
|
public function addType($type) |
78
|
|
|
{ |
79
|
20 |
|
if (!$this->hasType($type)) { |
80
|
20 |
|
$this->types[] = $type; |
81
|
|
|
} |
82
|
20 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $type |
86
|
|
|
*/ |
87
|
4 |
|
public function removeType($type) |
88
|
|
|
{ |
89
|
4 |
|
unset($this->types[array_search($type, $this->types, true)]); |
90
|
4 |
|
$this->types = empty($this->types) ? [] : array_values($this->types); |
91
|
4 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
28 |
|
public function hasComponents() |
97
|
|
|
{ |
98
|
28 |
|
return !empty($this->components); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string[] |
103
|
|
|
*/ |
104
|
20 |
|
public function getComponents() |
105
|
|
|
{ |
106
|
20 |
|
return $this->components; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string[] $components |
111
|
|
|
*/ |
112
|
12 |
|
public function setComponents(array $components) |
113
|
|
|
{ |
114
|
12 |
|
$this->components = []; |
115
|
12 |
|
$this->addComponents($components); |
116
|
12 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string[] $components |
120
|
|
|
*/ |
121
|
12 |
|
public function addComponents(array $components) |
122
|
|
|
{ |
123
|
12 |
|
foreach ($components as $type => $value) { |
124
|
12 |
|
$this->setComponent($type, $value); |
125
|
|
|
} |
126
|
12 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $type |
130
|
|
|
* |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
12 |
|
public function hasComponent($type) |
134
|
|
|
{ |
135
|
12 |
|
return isset($this->components[$type]); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $type |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
12 |
|
public function getComponent($type) |
144
|
|
|
{ |
145
|
12 |
|
return $this->hasComponent($type) ? $this->components[$type] : null; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $type |
150
|
|
|
* @param string $value |
151
|
|
|
*/ |
152
|
20 |
|
public function setComponent($type, $value) |
153
|
|
|
{ |
154
|
20 |
|
$this->components[$type] = $value; |
155
|
20 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $type |
159
|
|
|
*/ |
160
|
4 |
|
public function removeComponent($type) |
161
|
|
|
{ |
162
|
4 |
|
unset($this->components[$type]); |
163
|
4 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* {@inheritdoc} |
167
|
|
|
*/ |
168
|
|
|
public function buildContext() |
169
|
|
|
{ |
170
|
|
|
return 'autocomplete'; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* {@inheritdoc} |
175
|
|
|
*/ |
176
|
8 |
|
public function buildQuery() |
177
|
|
|
{ |
178
|
8 |
|
$query = parent::buildQuery(); |
179
|
|
|
|
180
|
8 |
|
if ($this->hasTypes()) { |
181
|
4 |
|
$query['types'] = implode('|', $this->types); |
182
|
|
|
} |
183
|
|
|
|
184
|
8 |
View Code Duplication |
if ($this->hasComponents()) { |
|
|
|
|
185
|
4 |
|
$query['components'] = implode('|', array_map(function ($key, $value) { |
186
|
4 |
|
return $key.':'.$value; |
187
|
4 |
|
}, array_keys($this->components), array_values($this->components))); |
188
|
|
|
} |
189
|
|
|
|
190
|
8 |
|
return $query; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
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.