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).

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