1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace exussum12\TripAdvisor; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use DateTimeInterface; |
7
|
|
|
use DateTimeZone; |
8
|
|
|
use exussum12\TripAdvisor\Clients\Curl; |
9
|
|
|
|
10
|
|
|
class Reviews |
11
|
|
|
{ |
12
|
|
|
const URL = "https://rentals.tripadvisor.com/api/reviews/v1"; |
13
|
|
|
|
14
|
|
|
private $clientCode; |
15
|
|
|
private $secret; |
16
|
|
|
private $client; |
17
|
|
|
private $urlArgs = [ |
18
|
|
|
'offset' => 0, |
19
|
|
|
'limit' => 1000, |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
public function __construct($clientCode, $secret, Client $client = null) |
23
|
|
|
{ |
24
|
|
|
$this->clientCode = $clientCode; |
25
|
|
|
$this->secret = $secret; |
26
|
|
|
|
27
|
|
|
if (is_null($client)) { |
28
|
|
|
$client = new Curl(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->client = $client; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function since(DateTimeInterface $date) |
35
|
|
|
{ |
36
|
|
|
$this->urlArgs['startDate'] = $date->format("Y-m-d"); |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function forAccount($account) |
42
|
|
|
{ |
43
|
|
|
unset($this->urlArgs['externalAccountReference']); |
44
|
|
|
$this->urlArgs['accountReference'] = $account; |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function forExternalAccount($account) |
50
|
|
|
{ |
51
|
|
|
unset($this->urlArgs['accountReference']); |
52
|
|
|
$this->urlArgs['externalAccountReference'] = $account; |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function forListingReference($account) |
58
|
|
|
{ |
59
|
|
|
unset($this->urlArgs['externalListingReference']); |
60
|
|
|
$this->urlArgs['listingReference'] = $account; |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function forExternalReference($reference) |
66
|
|
|
{ |
67
|
|
|
unset($this->urlArgs['listingReference']); |
68
|
|
|
$this->urlArgs['externalListingReference'] = $reference; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function offset($offset) |
74
|
|
|
{ |
75
|
|
|
$offset = max($offset, 0); |
76
|
|
|
$this->urlArgs['offset'] = $offset; |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function limit($limit) |
82
|
|
|
{ |
83
|
|
|
$limit = min($limit, 1000); |
84
|
|
|
$limit = max($limit, 1); |
85
|
|
|
|
86
|
|
|
$this->urlArgs['limit'] = $limit; |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function get($time = '') |
92
|
|
|
{ |
93
|
|
|
$url = parse_url(static::URL); |
94
|
|
|
$args = http_build_query($this->urlArgs); |
95
|
|
|
|
96
|
|
|
$time = new DateTime($time); |
97
|
|
|
$time->setTimezone(new DateTimeZone('UTC')); |
98
|
|
|
$time = $time->format('Y-m-d\TH:i:s\Z'); |
99
|
|
|
|
100
|
|
|
$request = [ |
101
|
|
|
'GET', |
102
|
|
|
$url['path'], |
103
|
|
|
$args, |
104
|
|
|
$time, |
105
|
|
|
hash('sha512', ''), |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
$hashedRequest = hash('sha512', implode("\n", $request)); |
109
|
|
|
|
110
|
|
|
$signature = hash_hmac('sha512', $hashedRequest, $this->secret); |
111
|
|
|
|
112
|
|
|
$authHeader = |
113
|
|
|
"VRS-HMAC-SHA512 " . |
114
|
|
|
"timestamp=$time, " . |
115
|
|
|
"client={$this->clientCode}, " . |
116
|
|
|
"signature=$signature"; |
117
|
|
|
|
118
|
|
|
$url = static::URL; |
119
|
|
|
if (strlen($args) > 0) { |
120
|
|
|
$url .= '?' . $args; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this->makeReviews( |
124
|
|
|
$this->client->getWithSignature($url, $authHeader) |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getSettings() |
129
|
|
|
{ |
130
|
|
|
return $this->urlArgs; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
private function makeReviews(Response $response) |
134
|
|
|
{ |
135
|
|
|
$reviews = $response->getBody(); |
136
|
|
|
$out = []; |
137
|
|
|
if (isset($reviews->reviews)) { |
138
|
|
|
foreach ($reviews->reviews as $review) { |
139
|
|
|
$out[] = new Review($review); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return new ResultSet($this, $out); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|