1 | <?php |
||
2 | |||
3 | namespace kalanis\google_maps\Services; |
||
4 | |||
5 | |||
6 | use kalanis\google_maps\ServiceException; |
||
7 | use Psr\Http\Message\RequestInterface; |
||
8 | |||
9 | |||
10 | /** |
||
11 | * Find by Place service |
||
12 | * |
||
13 | * @see https://developers.google.com/maps/documentation/places/web-service/search-find-place |
||
14 | */ |
||
15 | class FindPlace extends AbstractService |
||
16 | { |
||
17 | /** |
||
18 | * @var string[] |
||
19 | */ |
||
20 | protected array $allowedFields = [ |
||
21 | // basic |
||
22 | 'address_components', 'adr_address', 'business_status', 'formatted_address', 'geometry', 'icon', |
||
23 | 'icon_mask_base_uri', 'icon_background_color', 'name', 'photo', 'place_id', 'plus_code', 'type', |
||
24 | 'url', 'utc_offset', 'vicinity', 'wheelchair_accessible_entrance', |
||
25 | // contact |
||
26 | 'current_opening_hours', 'formatted_phone_number', 'international_phone_number', 'opening_hours', |
||
27 | 'secondary_opening_hours', 'website', |
||
28 | // atmosphere |
||
29 | 'curbside_pickup', 'delivery', 'dine_in', 'editorial_summary', 'price_level', 'rating', 'reservable', |
||
30 | 'reviews', 'serves_beer', 'serves_breakfast', 'serves_brunch', 'serves_dinner', 'serves_lunch', |
||
31 | 'serves_vegetarian_food', 'serves_wine', 'takeout', 'user_ratings_total', |
||
32 | ]; |
||
33 | |||
34 | /** |
||
35 | * Find Place lookup |
||
36 | * |
||
37 | * @param string $input what will be searched |
||
38 | * @param string $inputType what kind of data will be get |
||
39 | * @param string[] $fields which fields you want to get |
||
40 | * @param array<string|int, float>|null $bias ['lat', 'lng', 'rad'] |
||
41 | * @param array<string, string|int|float> $params Query parameters |
||
42 | * @throws ServiceException |
||
43 | * @return RequestInterface |
||
44 | */ |
||
45 | 9 | public function findPlace( |
|
46 | string $input, |
||
47 | string $inputType, |
||
48 | array $fields = [], |
||
49 | ?array $bias = null, |
||
50 | array $params = [] |
||
51 | ): RequestInterface |
||
52 | { |
||
53 | 9 | if (empty($input) || empty($inputType)) { |
|
54 | 1 | throw new ServiceException('You must set where to look!'); |
|
55 | } |
||
56 | |||
57 | // Main wanted name |
||
58 | 8 | $params['input'] = $input; |
|
59 | 8 | $params['inputtype'] = $inputType; |
|
60 | 8 | if (!empty($fields)) { |
|
61 | 1 | $params['fields'] = implode(',', array_intersect($this->allowedFields, $fields)); |
|
62 | } |
||
63 | |||
64 | // `locationbias` seems to only allow `lat,lng,rad` pattern |
||
65 | 8 | if (!empty($bias)) { |
|
66 | |||
67 | 5 | if (isset($bias['lat']) && isset($bias['lng']) && isset($bias['rad'])) { |
|
68 | 1 | $params['locationbias'] = sprintf('circle:%1.02F@%1.06F,%1.06F', $bias['rad'], $bias['lat'], $bias['lng']); |
|
69 | |||
70 | 4 | } elseif (isset($bias['n']) && isset($bias['s']) && isset($bias['e']) && isset($bias['w'])) { |
|
71 | 1 | $params['locationbias'] = sprintf('rectangle:%1.06F,%1.06F|%1.06F,%1.06F', $bias['s'], $bias['w'], $bias['n'], $bias['e']); |
|
72 | |||
73 | 3 | } elseif (isset($bias[0]) && isset($bias[1]) && isset($bias[2]) && isset($bias[3])) { |
|
74 | 1 | $params['locationbias'] = sprintf('rectangle:%1.06F,%1.06F|%1.06F,%1.06F', $bias[0], $bias[1], $bias[2], $bias[3]); |
|
75 | |||
76 | 2 | } elseif (isset($bias[0]) && isset($bias[1]) && isset($bias[2])) { |
|
77 | 1 | $params['locationbias'] = sprintf('circle:%1.02F@%1.06F,%1.06F', $bias[2], $bias[0], $bias[1]); |
|
78 | |||
79 | } else { |
||
80 | 5 | throw new ServiceException('Passed invalid values into coordinates! You must use either array with lat and lng and rad or 0, 1, 2 and 3 keys.'); |
|
81 | |||
82 | } |
||
83 | 3 | } elseif (!is_null($bias)) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
84 | 1 | $params['locationbias'] = 'ipbias'; |
|
85 | |||
86 | } |
||
87 | |||
88 | 7 | return $this->getWithDefaults( |
|
89 | 7 | static::API_HOST . '/maps/api/place/findplacefromtext/json', |
|
90 | 7 | $this->queryParamsLang($params) |
|
91 | 7 | ); |
|
92 | } |
||
93 | } |
||
94 |