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

/gdwpm-api/service/Google_Model.php  
  1. class Google_Model { 
  2. public function __construct( /** polymorphic */ ) { 
  3. if (func_num_args() == 1 && is_array(func_get_arg(0))) { 
  4. // Initialize the model with the array's contents. 
  5. $array = func_get_arg(0); 
  6. $this->mapTypes($array); 
  7.  
  8. /** 
  9. * Initialize this object's properties from an array. 
  10. * @param array $array Used to seed this object's properties. 
  11. * @return void 
  12. */ 
  13. protected function mapTypes($array) { 
  14. foreach ($array as $key => $val) { 
  15. $this->$key = $val; 
  16.  
  17. $keyTypeName = "__$key" . 'Type'; 
  18. $keyDataType = "__$key" . 'DataType'; 
  19. if ($this->useObjects() && property_exists($this, $keyTypeName)) { 
  20. if ($this->isAssociativeArray($val)) { 
  21. if (isset($this->$keyDataType) && 'map' == $this->$keyDataType) { 
  22. foreach($val as $arrayKey => $arrayItem) { 
  23. $val[$arrayKey] = $this->createObjectFromName($keyTypeName, $arrayItem); 
  24. $this->$key = $val; 
  25. } else { 
  26. $this->$key = $this->createObjectFromName($keyTypeName, $val); 
  27. } else if (is_array($val)) { 
  28. $arrayObject = array(); 
  29. foreach ($val as $arrayIndex => $arrayItem) { 
  30. $arrayObject[$arrayIndex] = $this->createObjectFromName($keyTypeName, $arrayItem); 
  31. $this->$key = $arrayObject; 
  32.  
  33. /** 
  34. * Returns true only if the array is associative. 
  35. * @param array $array 
  36. * @return bool True if the array is associative. 
  37. */ 
  38. protected function isAssociativeArray($array) { 
  39. if (!is_array($array)) { 
  40. return false; 
  41. $keys = array_keys($array); 
  42. foreach($keys as $key) { 
  43. if (is_string($key)) { 
  44. return true; 
  45. return false; 
  46.  
  47. /** 
  48. * Given a variable name, discover its type. 
  49. * @param $name 
  50. * @param $item 
  51. * @return object The object from the item. 
  52. */ 
  53. private function createObjectFromName($name, $item) { 
  54. $type = $this->$name; 
  55. return new $type($item); 
  56.  
  57. protected function useObjects() { 
  58. global $gdwpm_apiConfig; 
  59. return (isset($gdwpm_apiConfig['use_objects']) && $gdwpm_apiConfig['use_objects']); 
  60.  
  61. /** 
  62. * Verify if $obj is an array. 
  63. * @throws Google_Exception Thrown if $obj isn't an array. 
  64. * @param array $obj Items that should be validated. 
  65. * @param string $type Array items should be of this type. 
  66. * @param string $method Method expecting an array as an argument. 
  67. */ 
  68. public function assertIsArray($obj, $type, $method) { 
  69. if ($obj && !is_array($obj)) { 
  70. throw new Google_Exception("Incorrect parameter type passed to $method(), expected an" 
  71. . " array containing items of type $type.");