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