/google_api/Collection.php

  1. <?php 
  2.  
  3. if (!class_exists('Google_Client')) { 
  4. require_once dirname(__FILE__) . '/../autoload.php'; 
  5.  
  6. /** 
  7. * Extension to the regular Google_Model that automatically 
  8. * exposes the items array for iteration, so you can just 
  9. * iterate over the object rather than a reference inside. 
  10. */ 
  11. class Google_Collection extends Google_Model implements Iterator, Countable 
  12. protected $collection_key = 'items'; 
  13.  
  14. public function rewind() 
  15. if (isset($this->modelData[$this->collection_key]) 
  16. && is_array($this->modelData[$this->collection_key])) { 
  17. reset($this->modelData[$this->collection_key]); 
  18.  
  19. public function current() 
  20. $this->coerceType($this->key()); 
  21. if (is_array($this->modelData[$this->collection_key])) { 
  22. return current($this->modelData[$this->collection_key]); 
  23.  
  24. public function key() 
  25. if (isset($this->modelData[$this->collection_key]) 
  26. && is_array($this->modelData[$this->collection_key])) { 
  27. return key($this->modelData[$this->collection_key]); 
  28.  
  29. public function next() 
  30. return next($this->modelData[$this->collection_key]); 
  31.  
  32. public function valid() 
  33. $key = $this->key(); 
  34. return $key !== null && $key !== false; 
  35.  
  36. public function count() 
  37. if (!isset($this->modelData[$this->collection_key])) { 
  38. return 0; 
  39. return count($this->modelData[$this->collection_key]); 
  40.  
  41. public function offsetExists ($offset) 
  42. if (!is_numeric($offset)) { 
  43. return parent::offsetExists($offset); 
  44. return isset($this->modelData[$this->collection_key][$offset]); 
  45.  
  46. public function offsetGet($offset) 
  47. if (!is_numeric($offset)) { 
  48. return parent::offsetGet($offset); 
  49. $this->coerceType($offset); 
  50. return $this->modelData[$this->collection_key][$offset]; 
  51.  
  52. public function offsetSet($offset, $value) 
  53. if (!is_numeric($offset)) { 
  54. return parent::offsetSet($offset, $value); 
  55. $this->modelData[$this->collection_key][$offset] = $value; 
  56.  
  57. public function offsetUnset($offset) 
  58. if (!is_numeric($offset)) { 
  59. return parent::offsetUnset($offset); 
  60. unset($this->modelData[$this->collection_key][$offset]); 
  61.  
  62. private function coerceType($offset) 
  63. $typeKey = $this->keyType($this->collection_key); 
  64. if (isset($this->$typeKey) && !is_object($this->modelData[$this->collection_key][$offset])) { 
  65. $type = $this->$typeKey; 
  66. $this->modelData[$this->collection_key][$offset] = 
  67. new $type($this->modelData[$this->collection_key][$offset]); 
.