Issues (3)

src/Actions/Abstracts/PlacesSearchAction.php (2 issues)

1
<?php
2
3
namespace Sfneal\GooglePlaces\Actions\Abstracts;
4
5
use Sfneal\Actions\Action;
6
use Sfneal\GooglePlaces\Actions\CurlRequestAction;
7
8
abstract class PlacesSearchAction extends Action
9
{
10
    /**
11
     * @var array Longitude & Latitude of the location biases (Milford, MA)
12
     */
13
    private $location_bias_coords;
14
15
    /**
16
     * @var int A radius in miles from location bias coordinates
17
     */
18
    private $radius_bias;
19
20
    /**
21
     * @var string Google places country code (defaults to 'us')
22
     */
23
    private $country;
24
25
    /**
26
     * @var string Query parameters
27
     */
28
    public $query;
29
30
    /**
31
     * @var array Places predictions results
32
     */
33
    public $results;
34
35
    /**
36
     * @var null Place ID for retrieving a zip or other information about a place
37
     */
38
    public $place_id;
39
40
    /**
41
     * PlacesSearchAction constructor.
42
     *
43
     * @param string $query
44
     * @param null $place_id
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $place_id is correct as it would always require null to be passed?
Loading history...
45
     */
46
    public function __construct(string $query, $place_id = null)
47
    {
48
        // Set params
49
        $this->query = self::sanitize($query);
50
        $this->place_id = $place_id;
51
52
        // Set optional env values
53
        $this->location_bias_coords = config('google-places.location_bias');
54
        $this->radius_bias = config('google-places.radius');
55
        $this->country = config('google-places.country');
56
    }
57
58
    /**
59
     * Retrieve the API endpoint for a google places AutocompleteCity search.
60
     *
61
     * @return string
62
     */
63
    public function getEndpoint(): string
64
    {
65
        $endpoint = 'https://maps.googleapis.com/maps/api/place/autocomplete/json?input='.$this->query;
66
        $endpoint .= '&types=(regions)';
67
        $endpoint .= '&region=us';
68
69
        // Add location bias if set
70
        if (isset($this->location_bias_coords)) {
71
            $endpoint .= '&location='.str_replace(' ', '', $this->location_bias_coords);
0 ignored issues
show
Are you sure str_replace(' ', '', $this->location_bias_coords) of type string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
            $endpoint .= '&location='./** @scrutinizer ignore-type */ str_replace(' ', '', $this->location_bias_coords);
Loading history...
72
        }
73
74
        // Add radius bias if set
75
        if (isset($this->radius_bias)) {
76
            $endpoint .= '&radius='.$this->radius_bias;
77
        }
78
79
        // Add language
80
        $endpoint .= '&language=en_EN';
81
        $endpoint .= '&components=country:'.$this->country;
82
        $endpoint .= '&key='.config('google-places.api_key');
83
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
    {
103
        return str_replace(' ', '-', $query);
104
    }
105
106
    /**
107
     * Retrieve the API URL endpoint, cURL execute the request and return response.
108
     *
109
     * @return mixed
110
     */
111
    public function execute()
112
    {
113
        return $this->parseResponse((new CurlRequestAction($this->getEndpoint()))->execute());
114
    }
115
}
116