Google_Model

This class defines attributes, valid values, and usage which is generated from a given json schema.

Defined (1)

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

/vendor/google/apiclient/src/Google/Model.php  
  1. class Google_Model implements ArrayAccess 
  2. /** 
  3. * If you need to specify a NULL JSON value, use Google_Model::NULL_VALUE 
  4. * instead - it will be replaced when converting to JSON with a real null. 
  5. */ 
  6. const NULL_VALUE = "{}gapi-php-null"; 
  7. protected $internal_gapi_mappings = array(); 
  8. protected $modelData = array(); 
  9. protected $processed = array(); 
  10.  
  11. /** 
  12. * Polymorphic - accepts a variable number of arguments dependent 
  13. * on the type of the model subclass. 
  14. */ 
  15. final public function __construct() 
  16. if (func_num_args() == 1 && is_array(func_get_arg(0))) { 
  17. // Initialize the model with the array's contents. 
  18. $array = func_get_arg(0); 
  19. $this->mapTypes($array); 
  20. $this->gapiInit(); 
  21.  
  22. /** 
  23. * Getter that handles passthrough access to the data array, and lazy object creation. 
  24. * @param string $key Property name. 
  25. * @return mixed The value if any, or null. 
  26. */ 
  27. public function __get($key) 
  28. $keyTypeName = $this->keyType($key); 
  29. $keyDataType = $this->dataType($key); 
  30. if (isset($this->$keyTypeName) && !isset($this->processed[$key])) { 
  31. if (isset($this->modelData[$key])) { 
  32. $val = $this->modelData[$key]; 
  33. } else if (isset($this->$keyDataType) && 
  34. ($this->$keyDataType == 'array' || $this->$keyDataType == 'map')) { 
  35. $val = array(); 
  36. } else { 
  37. $val = null; 
  38.  
  39. if ($this->isAssociativeArray($val)) { 
  40. if (isset($this->$keyDataType) && 'map' == $this->$keyDataType) { 
  41. foreach ($val as $arrayKey => $arrayItem) { 
  42. $this->modelData[$key][$arrayKey] = 
  43. $this->createObjectFromName($keyTypeName, $arrayItem); 
  44. } else { 
  45. $this->modelData[$key] = $this->createObjectFromName($keyTypeName, $val); 
  46. } else if (is_array($val)) { 
  47. $arrayObject = array(); 
  48. foreach ($val as $arrayIndex => $arrayItem) { 
  49. $arrayObject[$arrayIndex] = 
  50. $this->createObjectFromName($keyTypeName, $arrayItem); 
  51. $this->modelData[$key] = $arrayObject; 
  52. $this->processed[$key] = true; 
  53.  
  54. return isset($this->modelData[$key]) ? $this->modelData[$key] : null; 
  55.  
  56. /** 
  57. * Initialize this object's properties from an array. 
  58. * @param array $array Used to seed this object's properties. 
  59. * @return void 
  60. */ 
  61. protected function mapTypes($array) 
  62. // Hard initialise simple types, lazy load more complex ones. 
  63. foreach ($array as $key => $val) { 
  64. if ( !property_exists($this, $this->keyType($key)) && 
  65. property_exists($this, $key)) { 
  66. $this->$key = $val; 
  67. unset($array[$key]); 
  68. } elseif (property_exists($this, $camelKey = Google_Utils::camelCase($key))) { 
  69. // This checks if property exists as camelCase, leaving it in array as snake_case 
  70. // in case of backwards compatibility issues. 
  71. $this->$camelKey = $val; 
  72. $this->modelData = $array; 
  73.  
  74. /** 
  75. * Blank initialiser to be used in subclasses to do post-construction initialisation - this 
  76. * avoids the need for subclasses to have to implement the variadics handling in their 
  77. * constructors. 
  78. */ 
  79. protected function gapiInit() 
  80. return; 
  81.  
  82. /** 
  83. * Create a simplified object suitable for straightforward 
  84. * conversion to JSON. This is relatively expensive 
  85. * due to the usage of reflection, but shouldn't be called 
  86. * a whole lot, and is the most straightforward way to filter. 
  87. */ 
  88. public function toSimpleObject() 
  89. $object = new stdClass(); 
  90.  
  91. // Process all other data. 
  92. foreach ($this->modelData as $key => $val) { 
  93. $result = $this->getSimpleValue($val); 
  94. if ($result !== null) { 
  95. $object->$key = $this->nullPlaceholderCheck($result); 
  96.  
  97. // Process all public properties. 
  98. $reflect = new ReflectionObject($this); 
  99. $props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC); 
  100. foreach ($props as $member) { 
  101. $name = $member->getName(); 
  102. $result = $this->getSimpleValue($this->$name); 
  103. if ($result !== null) { 
  104. $name = $this->getMappedName($name); 
  105. $object->$name = $this->nullPlaceholderCheck($result); 
  106.  
  107. return $object; 
  108.  
  109. /** 
  110. * Handle different types of values, primarily 
  111. * other objects and map and array data types. 
  112. */ 
  113. private function getSimpleValue($value) 
  114. if ($value instanceof Google_Model) { 
  115. return $value->toSimpleObject(); 
  116. } else if (is_array($value)) { 
  117. $return = array(); 
  118. foreach ($value as $key => $a_value) { 
  119. $a_value = $this->getSimpleValue($a_value); 
  120. if ($a_value !== null) { 
  121. $key = $this->getMappedName($key); 
  122. $return[$key] = $this->nullPlaceholderCheck($a_value); 
  123. return $return; 
  124. return $value; 
  125.  
  126. /** 
  127. * Check whether the value is the null placeholder and return true null. 
  128. */ 
  129. private function nullPlaceholderCheck($value) 
  130. if ($value === self::NULL_VALUE) { 
  131. return null; 
  132. return $value; 
  133.  
  134. /** 
  135. * If there is an internal name mapping, use that. 
  136. */ 
  137. private function getMappedName($key) 
  138. if (isset($this->internal_gapi_mappings) && 
  139. isset($this->internal_gapi_mappings[$key])) { 
  140. $key = $this->internal_gapi_mappings[$key]; 
  141. return $key; 
  142.  
  143. /** 
  144. * Returns true only if the array is associative. 
  145. * @param array $array 
  146. * @return bool True if the array is associative. 
  147. */ 
  148. protected function isAssociativeArray($array) 
  149. if (!is_array($array)) { 
  150. return false; 
  151. $keys = array_keys($array); 
  152. foreach ($keys as $key) { 
  153. if (is_string($key)) { 
  154. return true; 
  155. return false; 
  156.  
  157. /** 
  158. * Given a variable name, discover its type. 
  159. * @param $name 
  160. * @param $item 
  161. * @return object The object from the item. 
  162. */ 
  163. private function createObjectFromName($name, $item) 
  164. $type = $this->$name; 
  165. return new $type($item); 
  166.  
  167. /** 
  168. * Verify if $obj is an array. 
  169. * @throws Google_Exception Thrown if $obj isn't an array. 
  170. * @param array $obj Items that should be validated. 
  171. * @param string $method Method expecting an array as an argument. 
  172. */ 
  173. public function assertIsArray($obj, $method) 
  174. if ($obj && !is_array($obj)) { 
  175. throw new Google_Exception( 
  176. "Incorrect parameter type passed to $method(). Expected an array." 
  177. ); 
  178.  
  179. public function offsetExists($offset) 
  180. return isset($this->$offset) || isset($this->modelData[$offset]); 
  181.  
  182. public function offsetGet($offset) 
  183. return isset($this->$offset) ? 
  184. $this->$offset : 
  185. $this->__get($offset); 
  186.  
  187. public function offsetSet($offset, $value) 
  188. if (property_exists($this, $offset)) { 
  189. $this->$offset = $value; 
  190. } else { 
  191. $this->modelData[$offset] = $value; 
  192. $this->processed[$offset] = true; 
  193.  
  194. public function offsetUnset($offset) 
  195. unset($this->modelData[$offset]); 
  196.  
  197. protected function keyType($key) 
  198. return $key . "Type"; 
  199.  
  200. protected function dataType($key) 
  201. return $key . "DataType"; 
  202.  
  203. public function __isset($key) 
  204. return isset($this->modelData[$key]); 
  205.  
  206. public function __unset($key) 
  207. unset($this->modelData[$key]);