| 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 |
/** |
| 19 |
* Implements the actual methods/resources of the discovered Google API using magic function |
| 20 |
* calling overloading (__call()), which on call will see if the method name (plus.activities.list) |
| 21 |
* is available in this service, and if so construct an apiServiceRequest representing it. |
| 22 |
* |
| 23 |
* @author Chris Chabot <chabotc@google.com> |
| 24 |
* |
| 25 |
*/ |
| 26 |
class apiServiceResource { |
| 27 |
// Valid query parameters that work, but don't appear in discovery. |
| 28 |
private $stackParameters = array( |
| 29 |
'alt' => array('type' => 'string', 'location' => 'query'), |
| 30 |
'fields' => array('type' => 'string', 'location' => 'query'), |
| 31 |
'trace' => array('type' => 'string', 'location' => 'query'), |
| 32 |
'userIp' => array('type' => 'string', 'location' => 'query'), |
| 33 |
'userip' => array('type' => 'string', 'location' => 'query') |
| 34 |
); |
| 35 |
|
| 36 |
/** @var apiService $service */ |
| 37 |
private $service; |
| 38 |
private $serviceName; |
| 39 |
private $resourceName; |
| 40 |
|
| 41 |
/** @var array $methods */ |
| 42 |
private $methods; |
| 43 |
|
| 44 |
public function __construct($service, $serviceName, $resourceName, $resource) { |
| 45 |
$this->service = $service; |
| 46 |
$this->serviceName = $serviceName; |
| 47 |
$this->resourceName = $resourceName; |
| 48 |
$this->methods = $resource['methods']; |
| 49 |
} |
| 50 |
|
| 51 |
public function __call($name, $arguments) { |
| 52 |
if (count($arguments) != 1 && count($arguments) != 2) { |
| 53 |
throw new apiException("apiClient method calls expect 1 or 2 parameter (for example: \$client->plus->activities->list(array('userId' => 'me'))"); |
| 54 |
} |
| 55 |
if (! is_array($arguments[0])) { |
| 56 |
throw new apiException("apiClient method parameter should be an array (for example: \$client->plus->activities->list(array('userId' => 'me'))"); |
| 57 |
} |
| 58 |
$batchKey = false; |
| 59 |
if (isset($arguments[1])) { |
| 60 |
if (! is_string($arguments[1])) { |
| 61 |
throw new apiException("The batch key parameter should be a string, for example: \$client->buzz->activities->list( array('userId' => '@me'), 'batchKey')"); |
| 62 |
} |
| 63 |
$batchKey = $arguments[1]; |
| 64 |
} |
| 65 |
if (! isset($this->methods[$name])) { |
| 66 |
throw new apiException("Unknown function: {$this->serviceName}->{$this->resourceName}->{$name}()"); |
| 67 |
} |
| 68 |
$method = $this->methods[$name]; |
| 69 |
$parameters = $arguments[0]; |
| 70 |
// postBody is a special case since it's not defined in the discovery document as parameter, but we abuse the param entry for storing it |
| 71 |
$postBody = null; |
| 72 |
if (isset($parameters['postBody'])) { |
| 73 |
if (is_object($parameters['postBody'])) { |
| 74 |
$this->stripNull($parameters['postBody']); |
| 75 |
} |
| 76 |
|
| 77 |
// Some APIs require the postBody to be set under the data key. |
| 78 |
if (is_array($parameters['postBody']) && 'buzz' == $this->serviceName) { |
| 79 |
if (!isset($parameters['postBody']['data'])) { |
| 80 |
$rawBody = $parameters['postBody']; |
| 81 |
unset($parameters['postBody']); |
| 82 |
$parameters['postBody']['data'] = $rawBody; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
$postBody = is_array($parameters['postBody']) || is_object($parameters['postBody']) ? json_encode($parameters['postBody']) : $parameters['postBody']; |
| 87 |
// remove from the parameter list so not to trip up the param entry checking & make sure it doesn't end up on the query |
| 88 |
unset($parameters['postBody']); |
| 89 |
|
| 90 |
if (isset($parameters['optParams'])) { |
| 91 |
$optParams = $parameters['optParams']; |
| 92 |
unset($parameters['optParams']); |
| 93 |
|
| 94 |
$parameters = array_merge($parameters, $optParams); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
if (!isset($method['parameters'])) { |
| 99 |
$method['parameters'] = array(); |
| 100 |
} |
| 101 |
|
| 102 |
$method['parameters'] = array_merge($method['parameters'], $this->stackParameters); |
| 103 |
foreach ($parameters as $key => $val) { |
| 104 |
if ($key != 'postBody' && ! isset($method['parameters'][$key])) { |
| 105 |
throw new apiException("($name) unknown parameter: '$key'"); |
| 106 |
} |
| 107 |
} |
| 108 |
if (isset($method['parameters'])) { |
| 109 |
foreach ($method['parameters'] as $paramName => $paramSpec) { |
| 110 |
if (isset($paramSpec['required']) && $paramSpec['required'] && ! isset($parameters[$paramName])) { |
| 111 |
throw new apiException("($name) missing required param: '$paramName'"); |
| 112 |
} |
| 113 |
if (isset($parameters[$paramName])) { |
| 114 |
$value = $parameters[$paramName]; |
| 115 |
// check to see if the param value matches the required pattern |
| 116 |
if (isset($parameters[$paramName]['pattern']) && ! empty($parameters[$paramName]['pattern'])) { |
| 117 |
if (preg_match('|' . $parameters[$paramName]['pattern'] . '|', $value) == 0) { |
| 118 |
throw new apiException("($name) invalid parameter format for $paramName: $value doesn't match \"{$parameters[$paramName]['pattern']}\""); |
| 119 |
} |
| 120 |
} |
| 121 |
$parameters[$paramName] = $paramSpec; |
| 122 |
$parameters[$paramName]['value'] = $value; |
| 123 |
// remove all the bits that were already validated in this function & are no longer relevant within the execution chain |
| 124 |
unset($parameters[$paramName]['pattern']); |
| 125 |
unset($parameters[$paramName]['required']); |
| 126 |
} else { |
| 127 |
unset($parameters[$paramName]); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
// Discovery v1.0 puts the canonical method id under the 'id' field. |
| 133 |
if (! isset($method['id'])) { |
| 134 |
$method['id'] = $method['rpcMethod']; |
| 135 |
} |
| 136 |
|
| 137 |
// Discovery v1.0 puts the canonical path under the 'path' field. |
| 138 |
if (! isset($method['path'])) { |
| 139 |
$method['path'] = $method['restPath']; |
| 140 |
} |
| 141 |
|
| 142 |
$request = new apiServiceRequest($this->service->getRestBasePath(), $this->service->getRpcPath(), |
| 143 |
$method['path'], $method['id'], $method['httpMethod'], $parameters, $postBody); |
| 144 |
if ($batchKey) { |
| 145 |
$request->setBatchKey($batchKey); |
| 146 |
return $request; |
| 147 |
} else { |
| 148 |
return apiREST::execute($request); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
protected function useObjects() { |
| 153 |
global $apiConfig; |
| 154 |
return (isset($apiConfig['use_objects']) && $apiConfig['use_objects']); |
| 155 |
} |
| 156 |
|
| 157 |
protected function stripNull(&$o) { |
| 158 |
$o = (array) $o; |
| 159 |
foreach ($o as $k => $v) { |
| 160 |
if ($v === null) { |
| 161 |
unset($o[$k]); |
| 162 |
} |
| 163 |
elseif (is_object($v) || is_array($v)) { |
| 164 |
$this->stripNull($o[$k]); |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|