Passed
Push — master ( 48a16c...0e515b )
by Roberto
02:38
created

Elevation::getBySampledPath()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 19
rs 9.2222
c 0
b 0
f 0
cc 6
nc 3
nop 2
1
<?php
2
/**
3
 * Copyright (c) 2018 - present
4
 * Google Maps PHP - Elevation.php
5
 * author: Roberto Belotti - [email protected]
6
 * web : robertobelotti.com, github.com/biscolab
7
 * Initial version created on: 28/9/2018
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\Exception\InvalidArgumentException;
15
use Biscolab\GoogleMaps\Fields\GoogleMapsRequestFields;
16
use Biscolab\GoogleMaps\Http\GoogleMapsResultsCollection;
17
use Biscolab\GoogleMaps\Http\Result\ElevationResultsCollection;
18
use Biscolab\GoogleMaps\Object\LatLng;
19
use Biscolab\GoogleMaps\Object\Path;
20
21
/**
22
 * Class Elevation
23
 * @package Biscolab\GoogleMaps\Api
24
 *
25
 * @since   0.3.0
26
 * @see     https://developers.google.com/maps/documentation/elevation/start
27
 */
28
class Elevation extends Api {
29
30
	/**
31
	 * @var string
32
	 */
33
	const SERVICE_ENDPOINT = 'elevation';
34
35
	/**
36
	 * @var string
37
	 */
38
	protected $result_collection = ElevationResultsCollection::class;
39
40
	/**
41
	 * Positional Requests
42
	 *
43
	 * @param LatLng|string|array $locations
44
	 * This parameter takes either a single location or multiple locations passed as an array or as an encoded polyline
45
	 *
46
	 * @return GoogleMapsResultsCollection
47
	 *
48
	 * @since 0.3.0
49
	 */
50
	public function getByLocations($locations): GoogleMapsResultsCollection {
51
52
		$locations = $this->parseLocations($locations);
53
54
		$request = $this->createRequest([
55
			GoogleMapsRequestFields::LOCATIONS => $locations
56
		]);
57
58
		return $this->getResultsCollections($request);
59
	}
60
61
	/**
62
	 * @param array|string $locations
63
	 *
64
	 * @return string
65
	 *
66
	 * @since   0.3.0
67
	 */
68
	public function parseLocations($locations): string {
69
70
		if($locations instanceof Path) {
0 ignored issues
show
introduced by
$locations is never a sub-type of Biscolab\GoogleMaps\Object\Path.
Loading history...
71
			$locations = $locations->toArray();
72
		}
73
74
		if (is_array($locations)) {
75
			$locations = implode('|', array_map(function ($item) {
76
77
				return (string)$item;
78
			}, $locations));
79
		}
80
81
		return (string)$locations;
82
	}
83
84
	/**
85
	 * Sampled Path Requests
86
	 *
87
	 * @param array|string $path
88
	 * This parameter takes either a multiple locations passed as an array or as an encoded polyline
89
	 *
90
	 * @param int          $samples
91
	 * This will be the number of results as well
92
	 *
93
	 * @throws InvalidArgumentException
94
	 * @return GoogleMapsResultsCollection
95
	 *
96
	 * @since 0.4.0
97
	 */
98
	public function getBySampledPath($path, int $samples): GoogleMapsResultsCollection {
99
100
		if ((is_array($path) && count($path) < 2) ||
101
			$path instanceof Path && $path->count() < 2) {
102
			throw new InvalidArgumentException('The number of items provided in the path must be greater than 1 (One)');
103
		}
104
105
		if ($samples <= 0) {
106
			throw new InvalidArgumentException('The number of samples must be greater than 0 (Zero)');
107
		}
108
109
		$path = $this->parseLocations($path);
110
111
		$request = $this->createRequest([
112
			GoogleMapsRequestFields::PATH    => $path,
113
			GoogleMapsRequestFields::SAMPLES => $samples,
114
		]);
115
116
		return $this->getResultsCollections($request);
117
	}
118
119
}