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

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