|
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; |
|
13
|
|
|
|
|
14
|
|
|
use Ivory\GoogleMap\Service\Place\AbstractPlaceParsableService; |
|
15
|
|
|
use Ivory\GoogleMap\Service\Place\Autocomplete\Request\PlaceAutocompleteRequestInterface; |
|
16
|
|
|
use Ivory\GoogleMap\Service\Place\Autocomplete\Response\PlaceAutocompleteMatch; |
|
17
|
|
|
use Ivory\GoogleMap\Service\Place\Autocomplete\Response\PlaceAutocompletePrediction; |
|
18
|
|
|
use Ivory\GoogleMap\Service\Place\Autocomplete\Response\PlaceAutocompleteResponse; |
|
19
|
|
|
use Ivory\GoogleMap\Service\Place\Autocomplete\Response\PlaceAutocompleteTerm; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author GeLo <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class PlaceAutocompleteService extends AbstractPlaceParsableService |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
public function setHttps($https) |
|
30
|
|
|
{ |
|
31
|
|
|
if (!$https) { |
|
32
|
|
|
throw new \InvalidArgumentException('The http scheme is not supported.'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
parent::setHttps($https); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param PlaceAutocompleteRequestInterface $request |
|
40
|
|
|
* |
|
41
|
|
|
* @return PlaceAutocompleteResponse |
|
42
|
|
|
*/ |
|
43
|
|
View Code Duplication |
public function process(PlaceAutocompleteRequestInterface $request) |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
$httpRequest = $this->createRequest($request); |
|
46
|
|
|
$httpResponse = $this->getClient()->sendRequest($httpRequest); |
|
47
|
|
|
|
|
48
|
|
|
$data = $this->parse((string) $httpResponse->getBody(), [ |
|
49
|
|
|
'pluralization_rules' => [ |
|
50
|
|
|
'matched_substring' => 'matched_substrings', |
|
51
|
|
|
'prediction' => 'predictions', |
|
52
|
|
|
'term' => 'terms', |
|
53
|
|
|
'type' => 'types', |
|
54
|
|
|
], |
|
55
|
|
|
]); |
|
56
|
|
|
|
|
57
|
|
|
$response = $this->buildResponse($data); |
|
58
|
|
|
$response->setRequest($request); |
|
59
|
|
|
|
|
60
|
|
|
return $response; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param mixed[] $data |
|
65
|
|
|
* |
|
66
|
|
|
* @return PlaceAutocompleteResponse |
|
67
|
|
|
*/ |
|
68
|
|
|
private function buildResponse(array $data) |
|
69
|
|
|
{ |
|
70
|
|
|
$response = new PlaceAutocompleteResponse(); |
|
71
|
|
|
$response->setStatus($data['status']); |
|
72
|
|
|
$response->setPredictions($this->buildPredictions($data['predictions'])); |
|
73
|
|
|
|
|
74
|
|
|
return $response; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param mixed[] $data |
|
79
|
|
|
* |
|
80
|
|
|
* @return PlaceAutocompletePrediction[] |
|
81
|
|
|
*/ |
|
82
|
|
|
private function buildPredictions(array $data) |
|
83
|
|
|
{ |
|
84
|
|
|
$predictions = []; |
|
85
|
|
|
|
|
86
|
|
|
foreach ($data as $prediction) { |
|
87
|
|
|
$predictions[] = $this->buildPrediction($prediction); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $predictions; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param mixed[] $data |
|
95
|
|
|
* |
|
96
|
|
|
* @return PlaceAutocompletePrediction |
|
97
|
|
|
*/ |
|
98
|
|
|
private function buildPrediction(array $data) |
|
99
|
|
|
{ |
|
100
|
|
|
$prediction = new PlaceAutocompletePrediction(); |
|
101
|
|
|
$prediction->setPlaceId($data['place_id']); |
|
102
|
|
|
$prediction->setDescription($data['description']); |
|
103
|
|
|
$prediction->setTypes($data['types']); |
|
104
|
|
|
$prediction->setTerms($this->buildTerms($data['terms'])); |
|
105
|
|
|
$prediction->setMatches($this->buildMatches($data['matched_substrings'])); |
|
106
|
|
|
|
|
107
|
|
|
return $prediction; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param mixed[] $data |
|
112
|
|
|
* |
|
113
|
|
|
* @return PlaceAutocompleteTerm[] |
|
114
|
|
|
*/ |
|
115
|
|
|
private function buildTerms(array $data) |
|
116
|
|
|
{ |
|
117
|
|
|
$terms = []; |
|
118
|
|
|
|
|
119
|
|
|
foreach ($data as $term) { |
|
120
|
|
|
$terms[] = $this->buildTerm($term); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $terms; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param mixed[] $data |
|
128
|
|
|
* |
|
129
|
|
|
* @return PlaceAutocompleteTerm |
|
130
|
|
|
*/ |
|
131
|
|
|
private function buildTerm(array $data) |
|
132
|
|
|
{ |
|
133
|
|
|
$term = new PlaceAutocompleteTerm(); |
|
134
|
|
|
$term->setValue($data['value']); |
|
135
|
|
|
$term->setOffset($data['offset']); |
|
136
|
|
|
|
|
137
|
|
|
return $term; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param mixed[] $data |
|
142
|
|
|
* |
|
143
|
|
|
* @return PlaceAutocompleteMatch[] |
|
144
|
|
|
*/ |
|
145
|
|
|
private function buildMatches(array $data) |
|
146
|
|
|
{ |
|
147
|
|
|
$matches = []; |
|
148
|
|
|
|
|
149
|
|
|
foreach ($data as $match) { |
|
150
|
|
|
$matches[] = $this->buildMatch($match); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $matches; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param mixed[] $data |
|
158
|
|
|
* |
|
159
|
|
|
* @return PlaceAutocompleteMatch |
|
160
|
|
|
*/ |
|
161
|
|
|
private function buildMatch(array $data) |
|
162
|
|
|
{ |
|
163
|
|
|
$match = new PlaceAutocompleteMatch(); |
|
164
|
|
|
$match->setLength($data['length']); |
|
165
|
|
|
$match->setOffset($data['offset']); |
|
166
|
|
|
|
|
167
|
|
|
return $match; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
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.