1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Sfneal\GooglePlaces\Actions\Abstracts; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Sfneal\Actions\AbstractAction; |
8
|
|
|
use Sfneal\GooglePlaces\Actions\CurlRequestAction; |
9
|
|
|
|
10
|
|
|
abstract class PlacesSearchAction extends AbstractAction |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array Longitude & Latitude of the location biases (Milford, MA) |
14
|
|
|
*/ |
15
|
|
|
private $location_bias_coords; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var int A radius in miles from location bias coordinates |
19
|
|
|
*/ |
20
|
|
|
private $radius_bias; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string Google places country code (defaults to 'us') |
24
|
|
|
*/ |
25
|
|
|
private $country; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string Query parameters |
29
|
|
|
*/ |
30
|
|
|
public $query; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array Places predictions results |
34
|
|
|
*/ |
35
|
|
|
public $results; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var null Place ID for retrieving a zip or other information about a place |
39
|
|
|
*/ |
40
|
|
|
public $place_id; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* PlacesSearchAction constructor. |
44
|
|
|
* |
45
|
|
|
* @param string $query |
46
|
|
|
* @param null $place_id |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
public function __construct(string $query, $place_id = null) |
49
|
|
|
{ |
50
|
|
|
// Set params |
51
|
|
|
$this->query = self::sanitize($query); |
52
|
|
|
$this->place_id = $place_id; |
53
|
|
|
|
54
|
|
|
// Set optional env values |
55
|
|
|
$this->location_bias_coords = env('GOOGLE_PLACES_LOCATION_BIAS_COORD'); |
|
|
|
|
56
|
|
|
$this->radius_bias = env('GOOGLE_PLACES_RADIUS'); |
|
|
|
|
57
|
|
|
$this->country = env('GOOGLE_PLACES_COUNTRY', 'us'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Retrieve the API endpoint for a google places AutocompleteCity search |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function getEndpoint(): string { |
66
|
|
|
$endpoint = 'https://maps.googleapis.com/maps/api/place/autocomplete/json?input=' . $this->query; |
67
|
|
|
$endpoint .= '&types=(regions)'; |
68
|
|
|
$endpoint .= '®ion=us'; |
69
|
|
|
|
70
|
|
|
// Add location bias if set |
71
|
|
|
if (isset($this->location_bias_coords)) { |
72
|
|
|
$endpoint .= '&location=' . str_replace(' ', '', $this->location_bias_coords); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Add radius bias if set |
76
|
|
|
if (isset($this->radius_bias)) { |
77
|
|
|
$endpoint .= '&radius=' . $this->radius_bias; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Add language |
81
|
|
|
$endpoint .= '&language=en_EN'; |
82
|
|
|
$endpoint .= '&components=country:' . $this->country; |
83
|
|
|
$endpoint .= '&key=' . env('GOOGLE_API_KEY'); |
84
|
|
|
return $endpoint; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Parse the Google Places API response to retrieve results set |
89
|
|
|
* |
90
|
|
|
* @param $response |
91
|
|
|
* @return array|string |
92
|
|
|
*/ |
93
|
|
|
abstract public function parseResponse($response); |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Remove spaces and forbidden characters from query string |
97
|
|
|
* |
98
|
|
|
* @param $query |
99
|
|
|
* @return string|string[] |
100
|
|
|
*/ |
101
|
|
|
private static function sanitize($query) { |
102
|
|
|
return str_replace(' ', '-', $query); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Retrieve the API URL endpoint, cURL execute the request and return response |
107
|
|
|
* |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
|
|
public function execute() { |
111
|
|
|
return $this->parseResponse(CurlRequestAction::execute($this->getEndpoint())); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|