1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2010 Google Inc. |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
if (!class_exists('Google_Client')) { |
19
|
|
|
require_once dirname(__FILE__) . '/../autoload.php'; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Implements the actual methods/resources of the discovered Google API using magic function |
24
|
|
|
* calling overloading (__call()), which on call will see if the method name (plus.activities.list) |
25
|
|
|
* is available in this service, and if so construct an apiHttpRequest representing it. |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
class Google_Service_Resource |
29
|
|
|
{ |
30
|
|
|
// Valid query parameters that work, but don't appear in discovery. |
31
|
|
|
private $stackParameters = array( |
32
|
|
|
'alt' => array('type' => 'string', 'location' => 'query'), |
33
|
|
|
'fields' => array('type' => 'string', 'location' => 'query'), |
34
|
|
|
'trace' => array('type' => 'string', 'location' => 'query'), |
35
|
|
|
'userIp' => array('type' => 'string', 'location' => 'query'), |
36
|
|
|
'quotaUser' => array('type' => 'string', 'location' => 'query'), |
37
|
|
|
'data' => array('type' => 'string', 'location' => 'body'), |
38
|
|
|
'mimeType' => array('type' => 'string', 'location' => 'header'), |
39
|
|
|
'uploadType' => array('type' => 'string', 'location' => 'query'), |
40
|
|
|
'mediaUpload' => array('type' => 'complex', 'location' => 'query'), |
41
|
|
|
'prettyPrint' => array('type' => 'string', 'location' => 'query'), |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
/** @var string $rootUrl */ |
45
|
|
|
private $rootUrl; |
46
|
|
|
|
47
|
|
|
/** @var Google_Client $client */ |
48
|
|
|
private $client; |
49
|
|
|
|
50
|
|
|
/** @var string $serviceName */ |
51
|
|
|
private $serviceName; |
52
|
|
|
|
53
|
|
|
/** @var string $servicePath */ |
54
|
|
|
private $servicePath; |
55
|
|
|
|
56
|
|
|
/** @var string $resourceName */ |
57
|
|
|
private $resourceName; |
58
|
|
|
|
59
|
|
|
/** @var array $methods */ |
60
|
|
|
private $methods; |
61
|
|
|
|
62
|
|
|
public function __construct($service, $serviceName, $resourceName, $resource) |
63
|
|
|
{ |
64
|
|
|
$this->rootUrl = $service->rootUrl; |
65
|
|
|
$this->client = $service->getClient(); |
66
|
|
|
$this->servicePath = $service->servicePath; |
67
|
|
|
$this->serviceName = $serviceName; |
68
|
|
|
$this->resourceName = $resourceName; |
69
|
|
|
$this->methods = is_array($resource) && isset($resource['methods']) ? |
70
|
|
|
$resource['methods'] : |
71
|
|
|
array($resourceName => $resource); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* TODO: This function needs simplifying. |
76
|
|
|
* @param $name |
77
|
|
|
* @param $arguments |
78
|
|
|
* @param $expected_class - optional, the expected class name |
79
|
|
|
* @return Google_Http_Request|expected_class |
80
|
|
|
* @throws Google_Exception |
81
|
|
|
*/ |
82
|
|
|
public function call($name, $arguments, $expected_class = null) |
83
|
|
|
{ |
84
|
|
|
if (! isset($this->methods[$name])) { |
85
|
|
|
$this->client->getLogger()->error( |
86
|
|
|
'Service method unknown', |
87
|
|
|
array( |
88
|
|
|
'service' => $this->serviceName, |
89
|
|
|
'resource' => $this->resourceName, |
90
|
|
|
'method' => $name |
91
|
|
|
) |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
throw new Google_Exception( |
95
|
|
|
"Unknown function: " . |
96
|
|
|
"{$this->serviceName}->{$this->resourceName}->{$name}()" |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
$method = $this->methods[$name]; |
100
|
|
|
$parameters = $arguments[0]; |
101
|
|
|
|
102
|
|
|
// postBody is a special case since it's not defined in the discovery |
103
|
|
|
// document as parameter, but we abuse the param entry for storing it. |
104
|
|
|
$postBody = null; |
105
|
|
|
if (isset($parameters['postBody'])) { |
106
|
|
|
if ($parameters['postBody'] instanceof Google_Model) { |
107
|
|
|
// In the cases the post body is an existing object, we want |
108
|
|
|
// to use the smart method to create a simple object for |
109
|
|
|
// for JSONification. |
110
|
|
|
$parameters['postBody'] = $parameters['postBody']->toSimpleObject(); |
111
|
|
|
} else if (is_object($parameters['postBody'])) { |
112
|
|
|
// If the post body is another kind of object, we will try and |
113
|
|
|
// wrangle it into a sensible format. |
114
|
|
|
$parameters['postBody'] = |
115
|
|
|
$this->convertToArrayAndStripNulls($parameters['postBody']); |
116
|
|
|
} |
117
|
|
|
$postBody = json_encode($parameters['postBody']); |
118
|
|
|
unset($parameters['postBody']); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// TODO: optParams here probably should have been |
122
|
|
|
// handled already - this may well be redundant code. |
123
|
|
|
if (isset($parameters['optParams'])) { |
124
|
|
|
$optParams = $parameters['optParams']; |
125
|
|
|
unset($parameters['optParams']); |
126
|
|
|
$parameters = array_merge($parameters, $optParams); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (!isset($method['parameters'])) { |
130
|
|
|
$method['parameters'] = array(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$method['parameters'] = array_merge( |
134
|
|
|
$method['parameters'], |
135
|
|
|
$this->stackParameters |
136
|
|
|
); |
137
|
|
|
foreach ($parameters as $key => $val) { |
138
|
|
View Code Duplication |
if ($key != 'postBody' && ! isset($method['parameters'][$key])) { |
139
|
|
|
$this->client->getLogger()->error( |
140
|
|
|
'Service parameter unknown', |
141
|
|
|
array( |
142
|
|
|
'service' => $this->serviceName, |
143
|
|
|
'resource' => $this->resourceName, |
144
|
|
|
'method' => $name, |
145
|
|
|
'parameter' => $key |
146
|
|
|
) |
147
|
|
|
); |
148
|
|
|
throw new Google_Exception("($name) unknown parameter: '$key'"); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
foreach ($method['parameters'] as $paramName => $paramSpec) { |
153
|
|
View Code Duplication |
if (isset($paramSpec['required']) && |
154
|
|
|
$paramSpec['required'] && |
155
|
|
|
! isset($parameters[$paramName]) |
156
|
|
|
) { |
157
|
|
|
$this->client->getLogger()->error( |
158
|
|
|
'Service parameter missing', |
159
|
|
|
array( |
160
|
|
|
'service' => $this->serviceName, |
161
|
|
|
'resource' => $this->resourceName, |
162
|
|
|
'method' => $name, |
163
|
|
|
'parameter' => $paramName |
164
|
|
|
) |
165
|
|
|
); |
166
|
|
|
throw new Google_Exception("($name) missing required param: '$paramName'"); |
167
|
|
|
} |
168
|
|
|
if (isset($parameters[$paramName])) { |
169
|
|
|
$value = $parameters[$paramName]; |
170
|
|
|
$parameters[$paramName] = $paramSpec; |
171
|
|
|
$parameters[$paramName]['value'] = $value; |
172
|
|
|
unset($parameters[$paramName]['required']); |
173
|
|
|
} else { |
174
|
|
|
// Ensure we don't pass nulls. |
175
|
|
|
unset($parameters[$paramName]); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$this->client->getLogger()->info( |
180
|
|
|
'Service Call', |
181
|
|
|
array( |
182
|
|
|
'service' => $this->serviceName, |
183
|
|
|
'resource' => $this->resourceName, |
184
|
|
|
'method' => $name, |
185
|
|
|
'arguments' => $parameters, |
186
|
|
|
) |
187
|
|
|
); |
188
|
|
|
|
189
|
|
|
$url = Google_Http_REST::createRequestUri( |
190
|
|
|
$this->servicePath, |
191
|
|
|
$method['path'], |
192
|
|
|
$parameters |
193
|
|
|
); |
194
|
|
|
$httpRequest = new Google_Http_Request( |
195
|
|
|
$url, |
196
|
|
|
$method['httpMethod'], |
197
|
|
|
null, |
|
|
|
|
198
|
|
|
$postBody |
199
|
|
|
); |
200
|
|
|
|
201
|
|
|
if ($this->rootUrl) { |
202
|
|
|
$httpRequest->setBaseComponent($this->rootUrl); |
203
|
|
|
} else { |
204
|
|
|
$httpRequest->setBaseComponent($this->client->getBasePath()); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
if ($postBody) { |
|
|
|
|
208
|
|
|
$contentTypeHeader = array(); |
209
|
|
|
$contentTypeHeader['content-type'] = 'application/json; charset=UTF-8'; |
210
|
|
|
$httpRequest->setRequestHeaders($contentTypeHeader); |
211
|
|
|
$httpRequest->setPostBody($postBody); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
$httpRequest = $this->client->getAuth()->sign($httpRequest); |
215
|
|
|
$httpRequest->setExpectedClass($expected_class); |
216
|
|
|
|
217
|
|
|
if (isset($parameters['data']) && |
218
|
|
|
($parameters['uploadType']['value'] == 'media' || $parameters['uploadType']['value'] == 'multipart')) { |
219
|
|
|
// If we are doing a simple media upload, trigger that as a convenience. |
220
|
|
|
$mfu = new Google_Http_MediaFileUpload( |
|
|
|
|
221
|
|
|
$this->client, |
222
|
|
|
$httpRequest, |
223
|
|
|
isset($parameters['mimeType']) ? $parameters['mimeType']['value'] : 'application/octet-stream', |
224
|
|
|
$parameters['data']['value'] |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
if (isset($parameters['alt']) && $parameters['alt']['value'] == 'media') { |
229
|
|
|
$httpRequest->enableExpectedRaw(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
if ($this->client->shouldDefer()) { |
233
|
|
|
// If we are in batch or upload mode, return the raw request. |
234
|
|
|
return $httpRequest; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $this->client->execute($httpRequest); |
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
protected function convertToArrayAndStripNulls($o) |
241
|
|
|
{ |
242
|
|
|
$o = (array) $o; |
243
|
|
|
foreach ($o as $k => $v) { |
244
|
|
|
if ($v === null) { |
245
|
|
|
unset($o[$k]); |
246
|
|
|
} elseif (is_object($v) || is_array($v)) { |
247
|
|
|
$o[$k] = $this->convertToArrayAndStripNulls($o[$k]); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
return $o; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: