1
|
|
|
<?php namespace Arcanedev\GeoLocation\Google\DistanceMatrix; |
2
|
|
|
|
3
|
|
|
use Arcanedev\GeoLocation\Entities\Measures\Distance; |
4
|
|
|
use Arcanedev\GeoLocation\Entities\Measures\Duration; |
5
|
|
|
use Arcanedev\GeoLocation\Google\AbstractResponse; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class DistanceMatrixResponse |
10
|
|
|
* |
11
|
|
|
* @package Arcanedev\GeoLocation\Google\DistanceMatrix |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class DistanceMatrixResponse extends AbstractResponse |
15
|
|
|
{ |
16
|
|
|
/* ----------------------------------------------------------------- |
17
|
|
|
| Getters & Setters |
18
|
|
|
| ----------------------------------------------------------------- |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Get the first origin address. |
23
|
|
|
* |
24
|
|
|
* @return string|null |
25
|
|
|
*/ |
26
|
15 |
|
public function getOriginAddress() |
27
|
|
|
{ |
28
|
15 |
|
return Arr::first($this->getOriginAddresses(), null, null); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get the original addresses. |
33
|
|
|
* |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
15 |
|
public function getOriginAddresses() |
37
|
|
|
{ |
38
|
15 |
|
return $this->get('origin_addresses', []); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get the first destination address. |
43
|
|
|
* |
44
|
|
|
* @return string|null |
45
|
|
|
*/ |
46
|
15 |
|
public function getDestinationAddress() |
47
|
|
|
{ |
48
|
15 |
|
return Arr::first($this->getDestinationAddresses(), null, null); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the destination addresses. |
53
|
|
|
* |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
15 |
|
public function getDestinationAddresses() |
57
|
|
|
{ |
58
|
15 |
|
return $this->get('destination_addresses', []); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the distance (text or value). |
63
|
|
|
* |
64
|
|
|
* @param bool $text |
65
|
|
|
* |
66
|
|
|
* @return string|int |
67
|
|
|
*/ |
68
|
12 |
|
public function distance($text = true) |
69
|
|
|
{ |
70
|
12 |
|
return $text |
71
|
9 |
|
? $this->getDistance()->text() |
72
|
12 |
|
: $this->getDistance()->value(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the distance. |
77
|
|
|
* |
78
|
|
|
* @return \Arcanedev\GeoLocation\Entities\Measures\Distance |
79
|
|
|
*/ |
80
|
18 |
|
public function getDistance() |
81
|
|
|
{ |
82
|
18 |
|
return Distance::makeFromArray( |
83
|
18 |
|
$this->get('rows.0.elements.0.distance') |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the duration (text or value). |
89
|
|
|
* |
90
|
|
|
* @param bool $text |
91
|
|
|
* |
92
|
|
|
* @return string|int |
93
|
|
|
*/ |
94
|
12 |
|
public function duration($text = true) |
95
|
|
|
{ |
96
|
12 |
|
return $text |
97
|
9 |
|
? $this->getDuration()->text() |
98
|
12 |
|
: $this->getDuration()->value(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the duration (text or value). |
103
|
|
|
* |
104
|
|
|
* @return \Arcanedev\GeoLocation\Entities\Measures\Duration |
105
|
|
|
*/ |
106
|
18 |
|
public function getDuration() |
107
|
|
|
{ |
108
|
18 |
|
return Duration::makeFromArray( |
109
|
18 |
|
$this->get('rows.0.elements.0.duration') |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/* ----------------------------------------------------------------- |
114
|
|
|
| Main Methods |
115
|
|
|
| ----------------------------------------------------------------- |
116
|
|
|
*/ |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Convert the object to array. |
120
|
|
|
* |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
6 |
|
public function toArray() |
124
|
|
|
{ |
125
|
|
|
return [ |
126
|
6 |
|
'origin' => $this->getOriginAddress(), |
127
|
6 |
|
'destination' => $this->getDestinationAddress(), |
128
|
6 |
|
'distance' => $this->getDistance()->toArray(), |
129
|
6 |
|
'duration' => $this->getDuration()->toArray(), |
130
|
|
|
]; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|