1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DH\NavigationBundle\Provider\GoogleMaps\DistanceMatrix; |
4
|
|
|
|
5
|
|
|
use DH\NavigationBundle\Contract\DistanceMatrix\DistanceMatrixResponseInterface; |
6
|
|
|
use DH\NavigationBundle\Model\Address; |
7
|
|
|
use DH\NavigationBundle\Model\Distance; |
8
|
|
|
use DH\NavigationBundle\Model\DistanceMatrix\Element; |
9
|
|
|
use DH\NavigationBundle\Model\DistanceMatrix\Row; |
10
|
|
|
use DH\NavigationBundle\Model\Duration; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
|
13
|
|
|
class DistanceMatrixResponse implements DistanceMatrixResponseInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $status; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \stdClass |
22
|
|
|
*/ |
23
|
|
|
private $responseObject; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Address[]|array |
27
|
|
|
*/ |
28
|
|
|
private $originAddresses; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Address[]|array |
32
|
|
|
*/ |
33
|
|
|
private $destinationAddresses; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array|\DH\NavigationBundle\Model\DistanceMatrix\Row[] |
37
|
|
|
*/ |
38
|
|
|
private $rows; |
39
|
|
|
|
40
|
|
|
public function __construct(ResponseInterface $response) |
41
|
|
|
{ |
42
|
|
|
$responseObject = json_decode($response->getBody()->getContents()); |
43
|
|
|
$this->responseObject = $responseObject; |
44
|
|
|
$this->originAddresses = []; |
45
|
|
|
$this->destinationAddresses = []; |
46
|
|
|
$this->rows = []; |
47
|
|
|
$this->initialize(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function addOriginAddress(Address $originAddress): void |
51
|
|
|
{ |
52
|
|
|
$this->originAddresses[] = $originAddress; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function addDestinationAddress(Address $destinationAddress): void |
56
|
|
|
{ |
57
|
|
|
$this->destinationAddresses[] = $destinationAddress; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function addRow(Row $row): void |
61
|
|
|
{ |
62
|
|
|
$this->rows[] = $row; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
|
|
public function getStatus(): string |
69
|
|
|
{ |
70
|
|
|
return $this->status; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return \stdClass |
75
|
|
|
*/ |
76
|
|
|
public function getResponseObject(): \stdClass |
77
|
|
|
{ |
78
|
|
|
return $this->responseObject; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function getOriginAddresses(): array |
85
|
|
|
{ |
86
|
|
|
return $this->originAddresses; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public function getDestinationAddresses(): array |
93
|
|
|
{ |
94
|
|
|
return $this->destinationAddresses; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
public function getRows(): array |
101
|
|
|
{ |
102
|
|
|
return $this->rows; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function initialize(): void |
106
|
|
|
{ |
107
|
|
|
$this->status = $this->responseObject->status; |
108
|
|
|
|
109
|
|
|
foreach ($this->responseObject->origin_addresses as $originAddress) { |
110
|
|
|
$this->addOriginAddress(new Address($originAddress)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
foreach ($this->responseObject->destination_addresses as $destinationAddress) { |
114
|
|
|
$this->addDestinationAddress(new Address($destinationAddress)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
foreach ($this->responseObject->rows as $row) { |
118
|
|
|
$elements = []; |
119
|
|
|
foreach ($row->elements as $element) { |
120
|
|
|
if (property_exists($element, 'status') && Element::STATUS_OK !== $element->status) { |
121
|
|
|
$status = $element->status; |
122
|
|
|
$distance = null; |
123
|
|
|
$duration = null; |
124
|
|
|
} else { |
125
|
|
|
$status = 'OK'; |
126
|
|
|
$distance = new Distance((int) $element->distance->value); |
127
|
|
|
$duration = new Duration((int) $element->duration->value); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$elements[] = new Element($status, $duration, $distance); |
131
|
|
|
} |
132
|
|
|
$this->addRow(new Row($elements)); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|