Completed
Push — master ( d33c99...5d6517 )
by Roberto
03:55 queued 10s
created

Places::findPlaceByPhoneNumber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 1
nc 1
nop 3
1
<?php
2
/**
3
 * Copyright (c) 2019 - present
4
 * Google Maps PHP - Places.phpp
5
 * author: Roberto Belotti - [email protected]
6
 * web : robertobelotti.com, github.com/biscolab
7
 * Initial version created on: 28/3/2019
8
 * MIT license: https://github.com/biscolab/google-maps-php/blob/master/LICENSE
9
 */
10
11
namespace Biscolab\GoogleMaps\Api;
12
13
use Biscolab\GoogleMaps\Abstracts\Api;
14
use Biscolab\GoogleMaps\Config\Util;
0 ignored issues
show
Bug introduced by
The type Biscolab\GoogleMaps\Config\Util was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Biscolab\GoogleMaps\Enum\PlaceServicesEndpoints;
16
use Biscolab\GoogleMaps\Exception\InvalidArgumentException;
17
use Biscolab\GoogleMaps\Fields\GoogleMapsRequestFields;
18
use Biscolab\GoogleMaps\Fields\GoogleMapsResultFields;
19
use Biscolab\GoogleMaps\Http\GoogleMapsResultsCollection;
20
use Biscolab\GoogleMaps\Http\Result\PlaceResultsCollection;
21
use Biscolab\GoogleMaps\Object\Location;
22
use Biscolab\GoogleMaps\Values\PlaceInputTypeValues;
23
use Biscolab\GoogleMaps\Values\RankByValues;
24
25
/**
26
 * Class Places
27
 * @package Biscolab\GoogleMaps\Api
28
 *
29
 * @since   0.5.0
30
 * @see     https://developers.google.com/places/web-service/intro
31
 */
32
class Places extends Api
33
{
34
35
	/**
36
	 * @var string
37
	 */
38
	const SERVICE_ENDPOINT = 'place';
39
40
	/**
41
	 * @var string
42
	 */
43
	protected $result_collection = PlaceResultsCollection::class;
44
45
	/**
46
	 * @param string     $query
47
	 * @param array|null $params
48
	 * @param array|null $fields
49
	 *
50
	 * @return GoogleMapsResultsCollection
51
	 * @see https://developers.google.com/places/web-service/search#FindPlaceRequests
52
	 */
53
	public function findPlaceByText(
54
		string $query,
55
		?array $params = [],
56
		?array $fields = []
57
	): GoogleMapsResultsCollection {
58
59
		$params = array_merge($params, [
60
			GoogleMapsRequestFields::INPUT  => $query,
61
			GoogleMapsRequestFields::FIELDS => implode(',', $fields)
62
		]);
63
64
		return $this->findPlace($params);
65
66
	}
67
68
	/**
69
	 * Find Places requests
70
	 *
71
	 * @param array $params
72
	 *        GoogleMapsRequestFields::INPUT required
73
	 *
74
	 * @see     https://developers.google.com/places/web-service/search#FindPlaceRequests
75
	 * @return GoogleMapsResultsCollection
76
	 * @throws InvalidArgumentException
77
	 * @since   0.5.0
78
	 */
79
	public function findPlace(array $params): GoogleMapsResultsCollection
80
	{
81
82
		// see \Biscolab\GoogleMaps\Values\PlaceInputTypeValues
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
83
		if (empty($params[GoogleMapsRequestFields::INPUTTYPE])) {
84
			$params[GoogleMapsRequestFields::INPUTTYPE] = PlaceInputTypeValues::TEXTQUERY;
85
		}
86
87
		if (empty($params[GoogleMapsRequestFields::INPUT])) {
88
			throw new InvalidArgumentException(GoogleMapsRequestFields::INPUT . " field is required");
89
		}
90
91
		return $this->makeApiCall($params, PlaceServicesEndpoints::FINDPLACEFROMTEXT);
92
	}
93
94
	/**
95
	 * @param array  $params
96
	 * @param string $endpoint
97
	 *
98
	 * @return GoogleMapsResultsCollection
99
	 * @since   0.5.0
100
	 */
101
	public function makeApiCall(array $params, string $endpoint): GoogleMapsResultsCollection
102
	{
103
104
		return $this->callApi($params, $endpoint);
105
	}
106
107
	/**
108
	 * @param string     $number
109
	 * @param array|null $params
110
	 * @param array|null $fields
111
	 *
112
	 * @return GoogleMapsResultsCollection
113
	 */
114
	public function findPlaceByPhoneNumber(
115
		string $number,
116
		?array $params = [],
117
		?array $fields = []
118
	): GoogleMapsResultsCollection {
119
120
		$params = array_merge($params, [
121
			GoogleMapsRequestFields::INPUT     => $number,
122
			GoogleMapsRequestFields::INPUTTYPE => PlaceInputTypeValues::PHONENUMBER,
123
			GoogleMapsRequestFields::FIELDS    => implode(',', $fields)
124
		]);
125
126
		return $this->findPlace($params);
127
128
	}
129
130
	/**
131
	 * @param Location   $location
132
	 * @param int        $radius
133
	 * @param array|null $params
134
	 *
135
	 * @return GoogleMapsResultsCollection
136
	 */
137
	public function findNearbyPlaceByRadius(Location $location, int $radius, ?array $params = []): GoogleMapsResultsCollection
138
	{
139
140
		$params = array_merge($params, [
141
			GoogleMapsRequestFields::LOCATION => $location,
142
			GoogleMapsRequestFields::RADIUS => $radius
143
		]);
144
145
		return $this->findNearbyPlace($params);
146
	}
147
148
	/**
149
	 * @param Location $location
150
	 * @param array    $params
151
	 *
152
	 * @return GoogleMapsResultsCollection
153
	 */
154
	public function findNearbyPlaceByDistance(Location $location, array $params): GoogleMapsResultsCollection
155
	{
156
157
		$params = array_merge($params, [
158
			GoogleMapsRequestFields::LOCATION => $location,
159
			GoogleMapsRequestFields::RANKBY => RankByValues::DISTANCE
160
		]);
161
162
		return $this->findNearbyPlace($params);
163
	}
164
165
	/**
166
	 * Nearby Search requests
167
	 *
168
	 * @param array $params
169
	 *
170
	 * @return GoogleMapsResultsCollection
171
	 *
172
	 * @throws InvalidArgumentException
173
	 * @see     https://developers.google.com/places/web-service/search#PlaceSearchRequests
174
	 * @since   0.5.0
175
	 */
176
	public function findNearbyPlace(array $params): GoogleMapsResultsCollection
177
	{
178
179
		if (!empty($params[GoogleMapsRequestFields::LOCATION])) {//-33.8670522,151.1957362
180
			$location = $params[GoogleMapsRequestFields::LOCATION];
181
			if (!$location instanceof Location) {
182
				throw new InvalidArgumentException(GoogleMapsRequestFields::LOCATION . ' field must be instance of ' . Location::class . ' class');
183
			}
184
			$params[GoogleMapsRequestFields::LOCATION] = (string)$params[GoogleMapsRequestFields::LOCATION];
185
		} else {
186
			throw new InvalidArgumentException(GoogleMapsResultFields::LOCATION . ' field is required');
187
		}
188
189
		if (!empty($params[GoogleMapsRequestFields::RANKBY]) &&
190
			$params[GoogleMapsRequestFields::RANKBY] === RankByValues::DISTANCE
191
		) {
192
			if (empty($params[GoogleMapsRequestFields::KEYWORD]) &&
193
				empty($params[GoogleMapsRequestFields::NAME]) &&
194
				empty($params[GoogleMapsRequestFields::TYPE])) {
195
//				If rankby=distance (described under Optional parameters below) is specified,
196
// 				then one or more of keyword, name, or type is required.
197
				throw new InvalidArgumentException('If ' . GoogleMapsRequestFields::RANKBY . ' is set as "' . RankByValues::DISTANCE . '" one or more of ' . GoogleMapsRequestFields::KEYWORD . ', ' . GoogleMapsRequestFields::NAME . ', ' . GoogleMapsRequestFields::TYPE . ' fields are required');
198
			}
199
			if (!empty($params[GoogleMapsRequestFields::RADIUS])) {
200
// 				Note that radius must not be included if rankby=distance (described under Optional parameters below) is specified.
201
				throw new InvalidArgumentException(GoogleMapsRequestFields::RADIUS . ' must not be included if ' . GoogleMapsRequestFields::RANKBY . ' = ' . RankByValues::DISTANCE);
202
			}
203
		} elseif (empty($params[GoogleMapsRequestFields::RADIUS])) {
204
//			radius — Defines the distance (in meters) within which to return place results.
205
			throw new InvalidArgumentException(GoogleMapsRequestFields::RADIUS . ' field is required');
206
		}
207
208
		if (!empty($params[GoogleMapsRequestFields::RADIUS]) && floatval($params[GoogleMapsRequestFields::RADIUS]) > Util::MAX_PLACE_RADIUS_VALUE) {
209
// 			The maximum allowed radius is 50 000 meters.
210
			throw new InvalidArgumentException(GoogleMapsRequestFields::RADIUS . ' must be lower than ' . Util::MAX_PLACE_RADIUS_VALUE);
211
		}
212
213
		return $this->makeApiCall($params, PlaceServicesEndpoints::NEARBYSEARCH);
214
	}
215
216
	/**
217
	 * Nearby Search requests
218
	 *
219
	 * @param string     $query
220
	 * @param array|null $params
221
	 *
222
	 * @see     https://developers.google.com/places/web-service/search#TextSearchRequests
223
	 * @return GoogleMapsResultsCollection
224
	 * @throws InvalidArgumentException
225
	 * @since   0.5.0
226
	 */
227
	public function textSearch(string $query, ?array $params = []): GoogleMapsResultsCollection
228
	{
229
230
		$params = array_merge($params, [
231
			GoogleMapsRequestFields::QUERY => $query
232
		]);
233
234
		return $this->makeApiCall($params, PlaceServicesEndpoints::TEXTSEARCH);
235
	}
236
237
}