Passed
Push — master ( 25ef95...88bd2b )
by Stephen
02:06
created

ZipFromPlaceIdAction::getEndpoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 11
rs 10
c 1
b 0
f 0
1
<?php
2
3
4
namespace Sfneal\GooglePlaces\Actions;
5
6
7
use Sfneal\GooglePlaces\Actions\Abstracts\PlacesSearchAction;
8
9
class ZipFromPlaceIdAction extends PlacesSearchAction
10
{
11
    /**
12
     * Google Places details endpoint
13
     *
14
     * @return string
15
     */
16
    public function getEndpoint(): string
17
    {
18
        // Retrieve a city's zip code by inputting it's place ID
19
        $query = $this->place_id ?? $this->query;
20
21
        $endpoint = 'https://maps.googleapis.com/maps/api/place/details/json?placeid=' . $query;
22
        $endpoint .= '&language=en_EN';
23
        $endpoint .= '&region=us';
24
        $endpoint .= '&fields=address_component';
25
        $endpoint .= '&key=' . env('GOOGLE_API_KEY');
26
        return $endpoint;
27
    }
28
29
    /**
30
     * Parse the Google Places API response and return an array
31
     *
32
     * @param $response
33
     * @return string
34
     */
35
    public function parseResponse($response)
36
    {
37
        // Parse predictions
38
        foreach ($response['result']['address_components'] as $res) {
39
            if(in_array('postal_code', $res['types'])) {
40
                return $res['short_name'];
41
            }
42
        }
43
    }
44
}
45