Google_ServiceResource

Implements the actual methods/resources of the discovered Google API using magic function calling overloading (__call()), which on call will see if the method name (plus.activities.list) is available in this service, and if so construct an apiHttpRequest representing it.

Defined (1)

The class is defined in the following location(s).

/src/Google/Service/Resource.php  
  1. class Google_Service_Resource 
  2. // Valid query parameters that work, but don't appear in discovery. 
  3. private $stackParameters = array( 
  4. 'alt' => array('type' => 'string', 'location' => 'query'),  
  5. 'fields' => array('type' => 'string', 'location' => 'query'),  
  6. 'trace' => array('type' => 'string', 'location' => 'query'),  
  7. 'userIp' => array('type' => 'string', 'location' => 'query'),  
  8. 'userip' => array('type' => 'string', 'location' => 'query'),  
  9. 'quotaUser' => array('type' => 'string', 'location' => 'query'),  
  10. 'data' => array('type' => 'string', 'location' => 'body'),  
  11. 'mimeType' => array('type' => 'string', 'location' => 'header'),  
  12. 'uploadType' => array('type' => 'string', 'location' => 'query'),  
  13. 'mediaUpload' => array('type' => 'complex', 'location' => 'query'),  
  14. ); 
  15.  
  16. /** @var Google_Service $service */ 
  17. private $service; 
  18.  
  19. /** @var Google_Client $client */ 
  20. private $client; 
  21.  
  22. /** @var string $serviceName */ 
  23. private $serviceName; 
  24.  
  25. /** @var string $resourceName */ 
  26. private $resourceName; 
  27.  
  28. /** @var array $methods */ 
  29. private $methods; 
  30.  
  31. public function __construct($service, $serviceName, $resourceName, $resource) 
  32. $this->service = $service; 
  33. $this->client = $service->getClient(); 
  34. $this->serviceName = $serviceName; 
  35. $this->resourceName = $resourceName; 
  36. $this->methods = isset($resource['methods']) ? 
  37. $resource['methods'] : 
  38. array($resourceName => $resource); 
  39.  
  40. /** 
  41. * TODO(ianbarber): This function needs simplifying. 
  42. * @param $name 
  43. * @param $arguments 
  44. * @param $expected_class - optional, the expected class name 
  45. * @return Google_Http_Request|expected_class 
  46. * @throws Google_Exception 
  47. */ 
  48. public function call($name, $arguments, $expected_class = null) 
  49. if (! isset($this->methods[$name])) { 
  50. $this->client->getLogger()->error( 
  51. 'Service method unknown',  
  52. array( 
  53. 'service' => $this->serviceName,  
  54. 'resource' => $this->resourceName,  
  55. 'method' => $name 
  56. ); 
  57.  
  58. throw new Google_Exception( 
  59. "Unknown function: " . 
  60. "{$this->serviceName}->{$this->resourceName}->{$name}()" 
  61. ); 
  62. $method = $this->methods[$name]; 
  63. $parameters = $arguments[0]; 
  64.  
  65. // postBody is a special case since it's not defined in the discovery 
  66. // document as parameter, but we abuse the param entry for storing it. 
  67. $postBody = null; 
  68. if (isset($parameters['postBody'])) { 
  69. if ($parameters['postBody'] instanceof Google_Model) { 
  70. // In the cases the post body is an existing object, we want 
  71. // to use the smart method to create a simple object for 
  72. // for JSONification. 
  73. $parameters['postBody'] = $parameters['postBody']->toSimpleObject(); 
  74. } else if (is_object($parameters['postBody'])) { 
  75. // If the post body is another kind of object, we will try and 
  76. // wrangle it into a sensible format. 
  77. $parameters['postBody'] = 
  78. $this->convertToArrayAndStripNulls($parameters['postBody']); 
  79. $postBody = json_encode($parameters['postBody']); 
  80. unset($parameters['postBody']); 
  81.  
  82. // TODO(ianbarber): optParams here probably should have been 
  83. // handled already - this may well be redundant code. 
  84. if (isset($parameters['optParams'])) { 
  85. $optParams = $parameters['optParams']; 
  86. unset($parameters['optParams']); 
  87. $parameters = array_merge($parameters, $optParams); 
  88.  
  89. if (!isset($method['parameters'])) { 
  90. $method['parameters'] = array(); 
  91.  
  92. $method['parameters'] = array_merge( 
  93. $method['parameters'],  
  94. $this->stackParameters 
  95. ); 
  96. foreach ($parameters as $key => $val) { 
  97. if ($key != 'postBody' && ! isset($method['parameters'][$key])) { 
  98. $this->client->getLogger()->error( 
  99. 'Service parameter unknown',  
  100. array( 
  101. 'service' => $this->serviceName,  
  102. 'resource' => $this->resourceName,  
  103. 'method' => $name,  
  104. 'parameter' => $key 
  105. ); 
  106. throw new Google_Exception("($name) unknown parameter: '$key'"); 
  107.  
  108. foreach ($method['parameters'] as $paramName => $paramSpec) { 
  109. if (isset($paramSpec['required']) && 
  110. $paramSpec['required'] && 
  111. ! isset($parameters[$paramName]) 
  112. ) { 
  113. $this->client->getLogger()->error( 
  114. 'Service parameter missing',  
  115. array( 
  116. 'service' => $this->serviceName,  
  117. 'resource' => $this->resourceName,  
  118. 'method' => $name,  
  119. 'parameter' => $paramName 
  120. ); 
  121. throw new Google_Exception("($name) missing required param: '$paramName'"); 
  122. if (isset($parameters[$paramName])) { 
  123. $value = $parameters[$paramName]; 
  124. $parameters[$paramName] = $paramSpec; 
  125. $parameters[$paramName]['value'] = $value; 
  126. unset($parameters[$paramName]['required']); 
  127. } else { 
  128. // Ensure we don't pass nulls. 
  129. unset($parameters[$paramName]); 
  130.  
  131. $servicePath = $this->service->servicePath; 
  132.  
  133. $this->client->getLogger()->info( 
  134. 'Service Call',  
  135. array( 
  136. 'service' => $this->serviceName,  
  137. 'resource' => $this->resourceName,  
  138. 'method' => $name,  
  139. 'arguments' => $parameters,  
  140. ); 
  141.  
  142. $url = Google_Http_REST::createRequestUri( 
  143. $servicePath,  
  144. $method['path'],  
  145. $parameters 
  146. ); 
  147. $httpRequest = new Google_Http_Request( 
  148. $url,  
  149. $method['httpMethod'],  
  150. null,  
  151. $postBody 
  152. ); 
  153. $httpRequest->setBaseComponent($this->client->getBasePath()); 
  154.  
  155. if ($postBody) { 
  156. $contentTypeHeader = array(); 
  157. $contentTypeHeader['content-type'] = 'application/json; charset=UTF-8'; 
  158. $httpRequest->setRequestHeaders($contentTypeHeader); 
  159. $httpRequest->setPostBody($postBody); 
  160.  
  161. $httpRequest = $this->client->getAuth()->sign($httpRequest); 
  162. $httpRequest->setExpectedClass($expected_class); 
  163.  
  164. if (isset($parameters['data']) && 
  165. ($parameters['uploadType']['value'] == 'media' || $parameters['uploadType']['value'] == 'multipart')) { 
  166. // If we are doing a simple media upload, trigger that as a convenience. 
  167. $mfu = new Google_Http_MediaFileUpload( 
  168. $this->client,  
  169. $httpRequest,  
  170. isset($parameters['mimeType']) ? $parameters['mimeType']['value'] : 'application/octet-stream',  
  171. $parameters['data']['value'] 
  172. ); 
  173.  
  174. if ($this->client->shouldDefer()) { 
  175. // If we are in batch or upload mode, return the raw request. 
  176. return $httpRequest; 
  177.  
  178. return $this->client->execute($httpRequest); 
  179.  
  180. protected function convertToArrayAndStripNulls($o) 
  181. $o = (array) $o; 
  182. foreach ($o as $k => $v) { 
  183. if ($v === null) { 
  184. unset($o[$k]); 
  185. } elseif (is_object($v) || is_array($v)) { 
  186. $o[$k] = $this->convertToArrayAndStripNulls($o[$k]); 
  187. return $o;